@solana/keys 2.0.0-experimental.de111d2 → 2.0.0-experimental.dfb72ea

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,37 +1,150 @@
1
- import bs58 from 'bs58';
1
+ import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
2
+ import { SolanaError, SOLANA_ERROR__INVALID_KEYPAIR_BYTES } from '@solana/errors';
3
+ import { getBase58Encoder } from '@solana/codecs-strings';
2
4
 
3
- // src/base58.ts
4
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
5
+ // src/key-pair.ts
6
+
7
+ // src/private-key.ts
8
+ function addPkcs8Header(bytes) {
9
+ return new Uint8Array([
10
+ /**
11
+ * PKCS#8 header
12
+ */
13
+ 48,
14
+ // ASN.1 sequence tag
15
+ 46,
16
+ // Length of sequence (46 more bytes)
17
+ 2,
18
+ // ASN.1 integer tag
19
+ 1,
20
+ // Length of integer
21
+ 0,
22
+ // Version number
23
+ 48,
24
+ // ASN.1 sequence tag
25
+ 5,
26
+ // Length of sequence
27
+ 6,
28
+ // ASN.1 object identifier tag
29
+ 3,
30
+ // Length of object identifier
31
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
32
+ 43,
33
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
34
+ 101,
35
+ // thawte(101)
36
+ // Ed25519 identifier
37
+ 112,
38
+ // id-Ed25519(112)
39
+ /**
40
+ * Private key payload
41
+ */
42
+ 4,
43
+ // ASN.1 octet string tag
44
+ 34,
45
+ // String length (34 more bytes)
46
+ // Private key bytes as octet string
47
+ 4,
48
+ // ASN.1 octet string tag
49
+ 32,
50
+ // String length (32 bytes)
51
+ ...bytes
52
+ ]);
53
+ }
54
+ async function createPrivateKeyFromBytes(bytes, extractable) {
55
+ if (bytes.byteLength !== 32) {
56
+ throw new Error("Private key bytes must be of length 32");
57
+ }
58
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
59
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
60
+ }
61
+
62
+ // src/key-pair.ts
63
+ async function generateKeyPair() {
64
+ await assertKeyGenerationIsAvailable();
65
+ const keyPair = await crypto.subtle.generateKey(
66
+ /* algorithm */
67
+ "Ed25519",
68
+ // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
69
+ /* extractable */
70
+ false,
71
+ // Prevents the bytes of the private key from being visible to JS.
72
+ /* allowed uses */
73
+ ["sign", "verify"]
74
+ );
75
+ return keyPair;
76
+ }
77
+ async function createKeyPairFromBytes(bytes, extractable) {
78
+ if (bytes.byteLength !== 64) {
79
+ throw new SolanaError(SOLANA_ERROR__INVALID_KEYPAIR_BYTES, { byteLength: bytes.byteLength });
80
+ }
81
+ const [publicKey, privateKey] = await Promise.all([
82
+ crypto.subtle.importKey(
83
+ "raw",
84
+ bytes.slice(32),
85
+ "Ed25519",
86
+ /* extractable */
87
+ true,
88
+ ["verify"]
89
+ ),
90
+ createPrivateKeyFromBytes(bytes.slice(0, 32), extractable)
91
+ ]);
92
+ return { privateKey, publicKey };
93
+ }
94
+ var base58Encoder;
95
+ function assertIsSignature(putativeSignature) {
96
+ if (!base58Encoder)
97
+ base58Encoder = getBase58Encoder();
5
98
  try {
6
99
  if (
7
- // Lowest address (32 bytes of zeroes)
8
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
9
- putativeBase58EncodedAddress.length > 44
100
+ // Lowest value (64 bytes of zeroes)
101
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
102
+ putativeSignature.length > 88
10
103
  ) {
11
- throw new Error("Expected input string to decode to a byte array of length 32.");
104
+ throw new Error("Expected input string to decode to a byte array of length 64.");
12
105
  }
13
- const bytes = bs58.decode(putativeBase58EncodedAddress);
106
+ const bytes = base58Encoder.encode(putativeSignature);
14
107
  const numBytes = bytes.byteLength;
15
- if (numBytes !== 32) {
16
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
108
+ if (numBytes !== 64) {
109
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
17
110
  }
18
111
  } catch (e) {
19
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
112
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
20
113
  cause: e
21
114
  });
22
115
  }
23
116
  }
24
- function getBase58EncodedAddressComparator() {
25
- return new Intl.Collator("en", {
26
- caseFirst: "lower",
27
- ignorePunctuation: false,
28
- localeMatcher: "best fit",
29
- numeric: false,
30
- sensitivity: "variant",
31
- usage: "sort"
32
- }).compare;
117
+ function isSignature(putativeSignature) {
118
+ if (!base58Encoder)
119
+ base58Encoder = getBase58Encoder();
120
+ if (
121
+ // Lowest value (64 bytes of zeroes)
122
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
123
+ putativeSignature.length > 88
124
+ ) {
125
+ return false;
126
+ }
127
+ const bytes = base58Encoder.encode(putativeSignature);
128
+ const numBytes = bytes.byteLength;
129
+ if (numBytes !== 64) {
130
+ return false;
131
+ }
132
+ return true;
133
+ }
134
+ async function signBytes(key, data) {
135
+ await assertSigningCapabilityIsAvailable();
136
+ const signedData = await crypto.subtle.sign("Ed25519", key, data);
137
+ return new Uint8Array(signedData);
138
+ }
139
+ function signature(putativeSignature) {
140
+ assertIsSignature(putativeSignature);
141
+ return putativeSignature;
142
+ }
143
+ async function verifySignature(key, signature2, data) {
144
+ await assertVerificationCapabilityIsAvailable();
145
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
33
146
  }
34
147
 
35
- export { assertIsBase58EncodedAddress, getBase58EncodedAddressComparator };
148
+ export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
36
149
  //# sourceMappingURL=out.js.map
37
150
  //# sourceMappingURL=index.native.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/base58.ts"],"names":[],"mappings":";AAAA,OAAO,UAAU;AAMV,SAAS,6BACZ,8BACiG;AACjG,MAAI;AAEA;AAAA;AAAA,MAEI,6BAA6B,SAAS;AAAA,MAEtC,6BAA6B,SAAS;AAAA,MACxC;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,KAAK,OAAO,4BAA4B;AACtD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,UAAU;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAP;AACE,UAAM,IAAI,MAAM,KAAK,mEAAmE;AAAA,MACpF,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ;AAEO,SAAS,oCAAsE;AAClF,SAAO,IAAI,KAAK,SAAS,MAAM;AAAA,IAC3B,WAAW;AAAA,IACX,mBAAmB;AAAA,IACnB,eAAe;AAAA,IACf,SAAS;AAAA,IACT,aAAa;AAAA,IACb,OAAO;AAAA,EACX,CAAC,EAAE;AACP","sourcesContent":["import bs58 from 'bs58';\n\nexport type Base58EncodedAddress<TAddress extends string = string> = TAddress & {\n readonly __base58EncodedAddress: unique symbol;\n};\n\nexport function assertIsBase58EncodedAddress(\n putativeBase58EncodedAddress: string\n): asserts putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n throw new Error('Expected input string to decode to a byte array of length 32.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = bs58.decode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeBase58EncodedAddress}\\` is not a base-58 encoded address`, {\n cause: e,\n });\n }\n}\n\nexport function getBase58EncodedAddressComparator(): (x: string, y: string) => number {\n return new Intl.Collator('en', {\n caseFirst: 'lower',\n ignorePunctuation: false,\n localeMatcher: 'best fit',\n numeric: false,\n sensitivity: 'variant',\n usage: 'sort',\n }).compare;\n}\n"]}
1
+ {"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["signature"],"mappings":";AAAA,SAAS,sCAAsC;AAC/C,SAAS,qCAAqC,mBAAmB;;;ACDjE,SAAS,eAAe,OAA+B;AAEnD,SAAO,IAAI,WAAW;AAAA;AAAA;AAAA;AAAA,IAIlB;AAAA;AAAA,IACA;AAAA;AAAA,IAEI;AAAA;AAAA,IACA;AAAA;AAAA,IACI;AAAA;AAAA,IAEJ;AAAA;AAAA,IACA;AAAA;AAAA,IACI;AAAA;AAAA,IACA;AAAA;AAAA;AAAA,IAEQ;AAAA;AAAA,IACA;AAAA;AAAA;AAAA,IAEA;AAAA;AAAA;AAAA;AAAA;AAAA,IAKhB;AAAA;AAAA,IACA;AAAA;AAAA;AAAA,IAGI;AAAA;AAAA,IACA;AAAA;AAAA,IAEJ,GAAG;AAAA,EACP,CAAC;AACL;AAEA,eAAsB,0BAA0B,OAAmB,aAA2C;AAC1G,MAAI,MAAM,eAAe,IAAI;AAEzB,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC5D;AACA,QAAM,uBAAuB,eAAe,KAAK;AACjD,SAAO,MAAM,OAAO,OAAO,UAAU,SAAS,sBAAsB,WAAW,eAAe,OAAO,CAAC,MAAM,CAAC;AACjH;;;ADvCA,eAAsB,kBAA0C;AAC5D,QAAM,+BAA+B;AACrC,QAAM,UAAU,MAAM,OAAO,OAAO;AAAA;AAAA,IAChB;AAAA;AAAA;AAAA,IACE;AAAA;AAAA;AAAA,IACC,CAAC,QAAQ,QAAQ;AAAA,EACxC;AACA,SAAO;AACX;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AAa,IAAI;AACjB,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEA,eAAsB,UAAU,KAAgB,MAA2C;AACvF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEO,SAAS,UAAU,mBAAsC;AAC5D,oBAAkB,iBAAiB;AACnC,SAAO;AACX;AAEA,eAAsB,gBAAgB,KAAgBA,YAA2B,MAAoC;AACjH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAKA,YAAW,IAAI;AACrE","sourcesContent":["import { assertKeyGenerationIsAvailable } from '@solana/assertions';\nimport { SOLANA_ERROR__INVALID_KEYPAIR_BYTES, SolanaError } from '@solana/errors';\n\nimport { createPrivateKeyFromBytes } from './private-key';\n\nexport async function generateKeyPair(): Promise<CryptoKeyPair> {\n await assertKeyGenerationIsAvailable();\n const keyPair = await crypto.subtle.generateKey(\n /* algorithm */ 'Ed25519', // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20\n /* extractable */ false, // Prevents the bytes of the private key from being visible to JS.\n /* allowed uses */ ['sign', 'verify'],\n );\n return keyPair as CryptoKeyPair;\n}\n\nexport async function createKeyPairFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKeyPair> {\n if (bytes.byteLength !== 64) {\n throw new SolanaError(SOLANA_ERROR__INVALID_KEYPAIR_BYTES, { byteLength: bytes.byteLength });\n }\n const [publicKey, privateKey] = await Promise.all([\n crypto.subtle.importKey('raw', bytes.slice(32), 'Ed25519', /* extractable */ true, ['verify']),\n createPrivateKeyFromBytes(bytes.slice(0, 32), extractable),\n ]);\n return { privateKey, publicKey } as CryptoKeyPair;\n}\n","function addPkcs8Header(bytes: Uint8Array): Uint8Array {\n // prettier-ignore\n return new Uint8Array([\n /**\n * PKCS#8 header\n */\n 0x30, // ASN.1 sequence tag\n 0x2e, // Length of sequence (46 more bytes)\n\n 0x02, // ASN.1 integer tag\n 0x01, // Length of integer\n 0x00, // Version number\n\n 0x30, // ASN.1 sequence tag\n 0x05, // Length of sequence\n 0x06, // ASN.1 object identifier tag\n 0x03, // Length of object identifier\n // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112\n 0x2b, // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)\n 0x65, // thawte(101)\n // Ed25519 identifier\n 0x70, // id-Ed25519(112)\n\n /**\n * Private key payload\n */\n 0x04, // ASN.1 octet string tag\n 0x22, // String length (34 more bytes)\n\n // Private key bytes as octet string\n 0x04, // ASN.1 octet string tag\n 0x20, // String length (32 bytes)\n\n ...bytes\n ]);\n}\n\nexport async function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKey> {\n if (bytes.byteLength !== 32) {\n // TODO: Coded error.\n throw new Error('Private key bytes must be of length 32');\n }\n const privateKeyBytesPkcs8 = addPkcs8Header(bytes);\n return await crypto.subtle.importKey('pkcs8', privateKeyBytesPkcs8, 'Ed25519', extractable ?? false, ['sign']);\n}\n","import { assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';\nimport { Encoder } from '@solana/codecs-core';\nimport { getBase58Encoder } from '@solana/codecs-strings';\n\nexport type Signature = string & { readonly __brand: unique symbol };\nexport type SignatureBytes = Uint8Array & { readonly __brand: unique symbol };\n\nlet base58Encoder: Encoder<string> | undefined;\n\nexport function assertIsSignature(putativeSignature: string): asserts putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n throw new Error('Expected input string to decode to a byte array of length 64.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeSignature}\\` is not a signature`, {\n cause: e,\n });\n }\n}\n\nexport function isSignature(putativeSignature: string): putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n return false;\n }\n return true;\n}\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<SignatureBytes> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as SignatureBytes;\n}\n\nexport function signature(putativeSignature: string): Signature {\n assertIsSignature(putativeSignature);\n return putativeSignature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: SignatureBytes, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
@@ -1,44 +1,159 @@
1
1
  'use strict';
2
2
 
3
- var bs58 = require('bs58');
3
+ var assertions = require('@solana/assertions');
4
+ var errors = require('@solana/errors');
5
+ var codecsStrings = require('@solana/codecs-strings');
4
6
 
5
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+ // src/key-pair.ts
6
8
 
7
- var bs58__default = /*#__PURE__*/_interopDefault(bs58);
9
+ // src/private-key.ts
10
+ function addPkcs8Header(bytes) {
11
+ return new Uint8Array([
12
+ /**
13
+ * PKCS#8 header
14
+ */
15
+ 48,
16
+ // ASN.1 sequence tag
17
+ 46,
18
+ // Length of sequence (46 more bytes)
19
+ 2,
20
+ // ASN.1 integer tag
21
+ 1,
22
+ // Length of integer
23
+ 0,
24
+ // Version number
25
+ 48,
26
+ // ASN.1 sequence tag
27
+ 5,
28
+ // Length of sequence
29
+ 6,
30
+ // ASN.1 object identifier tag
31
+ 3,
32
+ // Length of object identifier
33
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
34
+ 43,
35
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
36
+ 101,
37
+ // thawte(101)
38
+ // Ed25519 identifier
39
+ 112,
40
+ // id-Ed25519(112)
41
+ /**
42
+ * Private key payload
43
+ */
44
+ 4,
45
+ // ASN.1 octet string tag
46
+ 34,
47
+ // String length (34 more bytes)
48
+ // Private key bytes as octet string
49
+ 4,
50
+ // ASN.1 octet string tag
51
+ 32,
52
+ // String length (32 bytes)
53
+ ...bytes
54
+ ]);
55
+ }
56
+ async function createPrivateKeyFromBytes(bytes, extractable) {
57
+ if (bytes.byteLength !== 32) {
58
+ throw new Error("Private key bytes must be of length 32");
59
+ }
60
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
61
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
62
+ }
8
63
 
9
- // src/base58.ts
10
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
64
+ // src/key-pair.ts
65
+ async function generateKeyPair() {
66
+ await assertions.assertKeyGenerationIsAvailable();
67
+ const keyPair = await crypto.subtle.generateKey(
68
+ /* algorithm */
69
+ "Ed25519",
70
+ // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
71
+ /* extractable */
72
+ false,
73
+ // Prevents the bytes of the private key from being visible to JS.
74
+ /* allowed uses */
75
+ ["sign", "verify"]
76
+ );
77
+ return keyPair;
78
+ }
79
+ async function createKeyPairFromBytes(bytes, extractable) {
80
+ if (bytes.byteLength !== 64) {
81
+ throw new errors.SolanaError(errors.SOLANA_ERROR__INVALID_KEYPAIR_BYTES, { byteLength: bytes.byteLength });
82
+ }
83
+ const [publicKey, privateKey] = await Promise.all([
84
+ crypto.subtle.importKey(
85
+ "raw",
86
+ bytes.slice(32),
87
+ "Ed25519",
88
+ /* extractable */
89
+ true,
90
+ ["verify"]
91
+ ),
92
+ createPrivateKeyFromBytes(bytes.slice(0, 32), extractable)
93
+ ]);
94
+ return { privateKey, publicKey };
95
+ }
96
+ var base58Encoder;
97
+ function assertIsSignature(putativeSignature) {
98
+ if (!base58Encoder)
99
+ base58Encoder = codecsStrings.getBase58Encoder();
11
100
  try {
12
101
  if (
13
- // Lowest address (32 bytes of zeroes)
14
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
15
- putativeBase58EncodedAddress.length > 44
102
+ // Lowest value (64 bytes of zeroes)
103
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
104
+ putativeSignature.length > 88
16
105
  ) {
17
- throw new Error("Expected input string to decode to a byte array of length 32.");
106
+ throw new Error("Expected input string to decode to a byte array of length 64.");
18
107
  }
19
- const bytes = bs58__default.default.decode(putativeBase58EncodedAddress);
108
+ const bytes = base58Encoder.encode(putativeSignature);
20
109
  const numBytes = bytes.byteLength;
21
- if (numBytes !== 32) {
22
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
110
+ if (numBytes !== 64) {
111
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
23
112
  }
24
113
  } catch (e) {
25
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
114
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
26
115
  cause: e
27
116
  });
28
117
  }
29
118
  }
30
- function getBase58EncodedAddressComparator() {
31
- return new Intl.Collator("en", {
32
- caseFirst: "lower",
33
- ignorePunctuation: false,
34
- localeMatcher: "best fit",
35
- numeric: false,
36
- sensitivity: "variant",
37
- usage: "sort"
38
- }).compare;
119
+ function isSignature(putativeSignature) {
120
+ if (!base58Encoder)
121
+ base58Encoder = codecsStrings.getBase58Encoder();
122
+ if (
123
+ // Lowest value (64 bytes of zeroes)
124
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
125
+ putativeSignature.length > 88
126
+ ) {
127
+ return false;
128
+ }
129
+ const bytes = base58Encoder.encode(putativeSignature);
130
+ const numBytes = bytes.byteLength;
131
+ if (numBytes !== 64) {
132
+ return false;
133
+ }
134
+ return true;
135
+ }
136
+ async function signBytes(key, data) {
137
+ await assertions.assertSigningCapabilityIsAvailable();
138
+ const signedData = await crypto.subtle.sign("Ed25519", key, data);
139
+ return new Uint8Array(signedData);
140
+ }
141
+ function signature(putativeSignature) {
142
+ assertIsSignature(putativeSignature);
143
+ return putativeSignature;
144
+ }
145
+ async function verifySignature(key, signature2, data) {
146
+ await assertions.assertVerificationCapabilityIsAvailable();
147
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
39
148
  }
40
149
 
41
- exports.assertIsBase58EncodedAddress = assertIsBase58EncodedAddress;
42
- exports.getBase58EncodedAddressComparator = getBase58EncodedAddressComparator;
150
+ exports.assertIsSignature = assertIsSignature;
151
+ exports.createKeyPairFromBytes = createKeyPairFromBytes;
152
+ exports.createPrivateKeyFromBytes = createPrivateKeyFromBytes;
153
+ exports.generateKeyPair = generateKeyPair;
154
+ exports.isSignature = isSignature;
155
+ exports.signBytes = signBytes;
156
+ exports.signature = signature;
157
+ exports.verifySignature = verifySignature;
43
158
  //# sourceMappingURL=out.js.map
44
159
  //# sourceMappingURL=index.node.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/base58.ts"],"names":[],"mappings":";AAAA,OAAO,UAAU;AAMV,SAAS,6BACZ,8BACiG;AACjG,MAAI;AAEA;AAAA;AAAA,MAEI,6BAA6B,SAAS;AAAA,MAEtC,6BAA6B,SAAS;AAAA,MACxC;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,KAAK,OAAO,4BAA4B;AACtD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,UAAU;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAP;AACE,UAAM,IAAI,MAAM,KAAK,mEAAmE;AAAA,MACpF,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ;AAEO,SAAS,oCAAsE;AAClF,SAAO,IAAI,KAAK,SAAS,MAAM;AAAA,IAC3B,WAAW;AAAA,IACX,mBAAmB;AAAA,IACnB,eAAe;AAAA,IACf,SAAS;AAAA,IACT,aAAa;AAAA,IACb,OAAO;AAAA,EACX,CAAC,EAAE;AACP","sourcesContent":["import bs58 from 'bs58';\n\nexport type Base58EncodedAddress<TAddress extends string = string> = TAddress & {\n readonly __base58EncodedAddress: unique symbol;\n};\n\nexport function assertIsBase58EncodedAddress(\n putativeBase58EncodedAddress: string\n): asserts putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n throw new Error('Expected input string to decode to a byte array of length 32.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = bs58.decode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeBase58EncodedAddress}\\` is not a base-58 encoded address`, {\n cause: e,\n });\n }\n}\n\nexport function getBase58EncodedAddressComparator(): (x: string, y: string) => number {\n return new Intl.Collator('en', {\n caseFirst: 'lower',\n ignorePunctuation: false,\n localeMatcher: 'best fit',\n numeric: false,\n sensitivity: 'variant',\n usage: 'sort',\n }).compare;\n}\n"]}
1
+ {"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["signature"],"mappings":";AAAA,SAAS,sCAAs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C9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAKA,YAAW,IAAI;AACrE","sourcesContent":["import { assertKeyGenerationIsAvailable } from '@solana/assertions';\nimport { SOLANA_ERROR__INVALID_KEYPAIR_BYTES, SolanaError } from '@solana/errors';\n\nimport { createPrivateKeyFromBytes } from './private-key';\n\nexport async function generateKeyPair(): Promise<CryptoKeyPair> {\n await assertKeyGenerationIsAvailable();\n const keyPair = await crypto.subtle.generateKey(\n /* algorithm */ 'Ed25519', // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20\n /* extractable */ false, // Prevents the bytes of the private key from being visible to JS.\n /* allowed uses */ ['sign', 'verify'],\n );\n return keyPair as CryptoKeyPair;\n}\n\nexport async function createKeyPairFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKeyPair> {\n if (bytes.byteLength !== 64) {\n throw new SolanaError(SOLANA_ERROR__INVALID_KEYPAIR_BYTES, { byteLength: bytes.byteLength });\n }\n const [publicKey, privateKey] = await Promise.all([\n crypto.subtle.importKey('raw', bytes.slice(32), 'Ed25519', /* extractable */ true, ['verify']),\n createPrivateKeyFromBytes(bytes.slice(0, 32), extractable),\n ]);\n return { privateKey, publicKey } as CryptoKeyPair;\n}\n","function addPkcs8Header(bytes: Uint8Array): Uint8Array {\n // prettier-ignore\n return new Uint8Array([\n /**\n * PKCS#8 header\n */\n 0x30, // ASN.1 sequence tag\n 0x2e, // Length of sequence (46 more bytes)\n\n 0x02, // ASN.1 integer tag\n 0x01, // Length of integer\n 0x00, // Version number\n\n 0x30, // ASN.1 sequence tag\n 0x05, // Length of sequence\n 0x06, // ASN.1 object identifier tag\n 0x03, // Length of object identifier\n // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112\n 0x2b, // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)\n 0x65, // thawte(101)\n // Ed25519 identifier\n 0x70, // id-Ed25519(112)\n\n /**\n * Private key payload\n */\n 0x04, // ASN.1 octet string tag\n 0x22, // String length (34 more bytes)\n\n // Private key bytes as octet string\n 0x04, // ASN.1 octet string tag\n 0x20, // String length (32 bytes)\n\n ...bytes\n ]);\n}\n\nexport async function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKey> {\n if (bytes.byteLength !== 32) {\n // TODO: Coded error.\n throw new Error('Private key bytes must be of length 32');\n }\n const privateKeyBytesPkcs8 = addPkcs8Header(bytes);\n return await crypto.subtle.importKey('pkcs8', privateKeyBytesPkcs8, 'Ed25519', extractable ?? false, ['sign']);\n}\n","import { assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';\nimport { Encoder } from '@solana/codecs-core';\nimport { getBase58Encoder } from '@solana/codecs-strings';\n\nexport type Signature = string & { readonly __brand: unique symbol };\nexport type SignatureBytes = Uint8Array & { readonly __brand: unique symbol };\n\nlet base58Encoder: Encoder<string> | undefined;\n\nexport function assertIsSignature(putativeSignature: string): asserts putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n throw new Error('Expected input string to decode to a byte array of length 64.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeSignature}\\` is not a signature`, {\n cause: e,\n });\n }\n}\n\nexport function isSignature(putativeSignature: string): putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n return false;\n }\n return true;\n}\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<SignatureBytes> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as SignatureBytes;\n}\n\nexport function signature(putativeSignature: string): Signature {\n assertIsSignature(putativeSignature);\n return putativeSignature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: SignatureBytes, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
@@ -1,37 +1,150 @@
1
- import bs58 from 'bs58';
1
+ import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
2
+ import { SolanaError, SOLANA_ERROR__INVALID_KEYPAIR_BYTES } from '@solana/errors';
3
+ import { getBase58Encoder } from '@solana/codecs-strings';
2
4
 
3
- // src/base58.ts
4
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
5
+ // src/key-pair.ts
6
+
7
+ // src/private-key.ts
8
+ function addPkcs8Header(bytes) {
9
+ return new Uint8Array([
10
+ /**
11
+ * PKCS#8 header
12
+ */
13
+ 48,
14
+ // ASN.1 sequence tag
15
+ 46,
16
+ // Length of sequence (46 more bytes)
17
+ 2,
18
+ // ASN.1 integer tag
19
+ 1,
20
+ // Length of integer
21
+ 0,
22
+ // Version number
23
+ 48,
24
+ // ASN.1 sequence tag
25
+ 5,
26
+ // Length of sequence
27
+ 6,
28
+ // ASN.1 object identifier tag
29
+ 3,
30
+ // Length of object identifier
31
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
32
+ 43,
33
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
34
+ 101,
35
+ // thawte(101)
36
+ // Ed25519 identifier
37
+ 112,
38
+ // id-Ed25519(112)
39
+ /**
40
+ * Private key payload
41
+ */
42
+ 4,
43
+ // ASN.1 octet string tag
44
+ 34,
45
+ // String length (34 more bytes)
46
+ // Private key bytes as octet string
47
+ 4,
48
+ // ASN.1 octet string tag
49
+ 32,
50
+ // String length (32 bytes)
51
+ ...bytes
52
+ ]);
53
+ }
54
+ async function createPrivateKeyFromBytes(bytes, extractable) {
55
+ if (bytes.byteLength !== 32) {
56
+ throw new Error("Private key bytes must be of length 32");
57
+ }
58
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
59
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
60
+ }
61
+
62
+ // src/key-pair.ts
63
+ async function generateKeyPair() {
64
+ await assertKeyGenerationIsAvailable();
65
+ const keyPair = await crypto.subtle.generateKey(
66
+ /* algorithm */
67
+ "Ed25519",
68
+ // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
69
+ /* extractable */
70
+ false,
71
+ // Prevents the bytes of the private key from being visible to JS.
72
+ /* allowed uses */
73
+ ["sign", "verify"]
74
+ );
75
+ return keyPair;
76
+ }
77
+ async function createKeyPairFromBytes(bytes, extractable) {
78
+ if (bytes.byteLength !== 64) {
79
+ throw new SolanaError(SOLANA_ERROR__INVALID_KEYPAIR_BYTES, { byteLength: bytes.byteLength });
80
+ }
81
+ const [publicKey, privateKey] = await Promise.all([
82
+ crypto.subtle.importKey(
83
+ "raw",
84
+ bytes.slice(32),
85
+ "Ed25519",
86
+ /* extractable */
87
+ true,
88
+ ["verify"]
89
+ ),
90
+ createPrivateKeyFromBytes(bytes.slice(0, 32), extractable)
91
+ ]);
92
+ return { privateKey, publicKey };
93
+ }
94
+ var base58Encoder;
95
+ function assertIsSignature(putativeSignature) {
96
+ if (!base58Encoder)
97
+ base58Encoder = getBase58Encoder();
5
98
  try {
6
99
  if (
7
- // Lowest address (32 bytes of zeroes)
8
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
9
- putativeBase58EncodedAddress.length > 44
100
+ // Lowest value (64 bytes of zeroes)
101
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
102
+ putativeSignature.length > 88
10
103
  ) {
11
- throw new Error("Expected input string to decode to a byte array of length 32.");
104
+ throw new Error("Expected input string to decode to a byte array of length 64.");
12
105
  }
13
- const bytes = bs58.decode(putativeBase58EncodedAddress);
106
+ const bytes = base58Encoder.encode(putativeSignature);
14
107
  const numBytes = bytes.byteLength;
15
- if (numBytes !== 32) {
16
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
108
+ if (numBytes !== 64) {
109
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
17
110
  }
18
111
  } catch (e) {
19
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
112
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
20
113
  cause: e
21
114
  });
22
115
  }
23
116
  }
24
- function getBase58EncodedAddressComparator() {
25
- return new Intl.Collator("en", {
26
- caseFirst: "lower",
27
- ignorePunctuation: false,
28
- localeMatcher: "best fit",
29
- numeric: false,
30
- sensitivity: "variant",
31
- usage: "sort"
32
- }).compare;
117
+ function isSignature(putativeSignature) {
118
+ if (!base58Encoder)
119
+ base58Encoder = getBase58Encoder();
120
+ if (
121
+ // Lowest value (64 bytes of zeroes)
122
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
123
+ putativeSignature.length > 88
124
+ ) {
125
+ return false;
126
+ }
127
+ const bytes = base58Encoder.encode(putativeSignature);
128
+ const numBytes = bytes.byteLength;
129
+ if (numBytes !== 64) {
130
+ return false;
131
+ }
132
+ return true;
133
+ }
134
+ async function signBytes(key, data) {
135
+ await assertSigningCapabilityIsAvailable();
136
+ const signedData = await crypto.subtle.sign("Ed25519", key, data);
137
+ return new Uint8Array(signedData);
138
+ }
139
+ function signature(putativeSignature) {
140
+ assertIsSignature(putativeSignature);
141
+ return putativeSignature;
142
+ }
143
+ async function verifySignature(key, signature2, data) {
144
+ await assertVerificationCapabilityIsAvailable();
145
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
33
146
  }
34
147
 
35
- export { assertIsBase58EncodedAddress, getBase58EncodedAddressComparator };
148
+ export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
36
149
  //# sourceMappingURL=out.js.map
37
150
  //# sourceMappingURL=index.node.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/base58.ts"],"names":[],"mappings":";AAAA,OAAO,UAAU;AAMV,SAAS,6BACZ,8BACiG;AACjG,MAAI;AAEA;AAAA;AAAA,MAEI,6BAA6B,SAAS;AAAA,MAEtC,6BAA6B,SAAS;AAAA,MACxC;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,KAAK,OAAO,4BAA4B;AACtD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,UAAU;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAP;AACE,UAAM,IAAI,MAAM,KAAK,mEAAmE;AAAA,MACpF,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ;AAEO,SAAS,oCAAsE;AAClF,SAAO,IAAI,KAAK,SAAS,MAAM;AAAA,IAC3B,WAAW;AAAA,IACX,mBAAmB;AAAA,IACnB,eAAe;AAAA,IACf,SAAS;AAAA,IACT,aAAa;AAAA,IACb,OAAO;AAAA,EACX,CAAC,EAAE;AACP","sourcesContent":["import bs58 from 'bs58';\n\nexport type Base58EncodedAddress<TAddress extends string = string> = TAddress & {\n readonly __base58EncodedAddress: unique symbol;\n};\n\nexport function assertIsBase58EncodedAddress(\n putativeBase58EncodedAddress: string\n): asserts putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n throw new Error('Expected input string to decode to a byte array of length 32.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = bs58.decode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeBase58EncodedAddress}\\` is not a base-58 encoded address`, {\n cause: e,\n });\n }\n}\n\nexport function getBase58EncodedAddressComparator(): (x: string, y: string) => number {\n return new Intl.Collator('en', {\n caseFirst: 'lower',\n ignorePunctuation: false,\n localeMatcher: 'best fit',\n numeric: false,\n sensitivity: 'variant',\n usage: 'sort',\n }).compare;\n}\n"]}
1
+ {"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["signature"],"mappings":";AAAA,SAAS,sCAAsC;AAC/C,SAAS,qCAAqC,mBAAmB;;;ACDjE,SAAS,eAAe,OAA+B;AAEnD,SAAO,IAAI,WAAW;AAAA;AAAA;AAAA;AAAA,IAIlB;AAAA;AAAA,IACA;AAAA;AAAA,IAEI;AAAA;AAAA,IACA;AAAA;AAAA,IACI;AAAA;AAAA,IAEJ;AAAA;AAAA,IACA;AAAA;AAAA,IACI;AAAA;AAAA,IACA;AAAA;AAAA;AAAA,IAEQ;AAAA;AAAA,IACA;AAAA;AAAA;AAAA,IAEA;AAAA;AAAA;AAAA;AAAA;AAAA,IAKhB;AAAA;AAAA,IACA;AAAA;AAAA;AAAA,IAGI;AAAA;AAAA,IACA;AAAA;AAAA,IAEJ,GAAG;AAAA,EACP,CAAC;AACL;AAEA,eAAsB,0BAA0B,OAAmB,aAA2C;AAC1G,MAAI,MAAM,eAAe,IAAI;AAEzB,UAAM,IAAI,MAAM,wCAAwC;AAAA,E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M,KAAK,iBAAiB,yBAAyB;AAAA,MAC3D,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ;AAEO,SAAS,YAAY,mBAA2D;AACnF,MAAI,CAAC;AAAe,oBAAgB,iBAAiB;AAGrD;AAAA;AAAA,IAEI,kBAAkB,SAAS;AAAA,IAE3B,kBAAkB,SAAS;AAAA,IAC7B;AACE,WAAO;AAAA,EACX;AAEA,QAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,QAAM,WAAW,MAAM;AACvB,MAAI,aAAa,IAAI;AACjB,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEA,eAAsB,UAAU,KAAgB,MAA2C;AACvF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEO,SAAS,UAAU,mBAAsC;AAC5D,oBAAkB,iBAAiB;AACnC,SAAO;AACX;AAEA,eAAsB,gBAAgB,KAAgBA,YAA2B,MAAoC;AACjH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAKA,YAAW,IAAI;AACrE","sourcesContent":["import { assertKeyGenerationIsAvailable } from '@solana/assertions';\nimport { SOLANA_ERROR__INVALID_KEYPAIR_BYTES, SolanaError } from '@solana/errors';\n\nimport { createPrivateKeyFromBytes } from './private-key';\n\nexport async function generateKeyPair(): Promise<CryptoKeyPair> {\n await assertKeyGenerationIsAvailable();\n const keyPair = await crypto.subtle.generateKey(\n /* algorithm */ 'Ed25519', // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20\n /* extractable */ false, // Prevents the bytes of the private key from being visible to JS.\n /* allowed uses */ ['sign', 'verify'],\n );\n return keyPair as CryptoKeyPair;\n}\n\nexport async function createKeyPairFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKeyPair> {\n if (bytes.byteLength !== 64) {\n throw new SolanaError(SOLANA_ERROR__INVALID_KEYPAIR_BYTES, { byteLength: bytes.byteLength });\n }\n const [publicKey, privateKey] = await Promise.all([\n crypto.subtle.importKey('raw', bytes.slice(32), 'Ed25519', /* extractable */ true, ['verify']),\n createPrivateKeyFromBytes(bytes.slice(0, 32), extractable),\n ]);\n return { privateKey, publicKey } as CryptoKeyPair;\n}\n","function addPkcs8Header(bytes: Uint8Array): Uint8Array {\n // prettier-ignore\n return new Uint8Array([\n /**\n * PKCS#8 header\n */\n 0x30, // ASN.1 sequence tag\n 0x2e, // Length of sequence (46 more bytes)\n\n 0x02, // ASN.1 integer tag\n 0x01, // Length of integer\n 0x00, // Version number\n\n 0x30, // ASN.1 sequence tag\n 0x05, // Length of sequence\n 0x06, // ASN.1 object identifier tag\n 0x03, // Length of object identifier\n // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112\n 0x2b, // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)\n 0x65, // thawte(101)\n // Ed25519 identifier\n 0x70, // id-Ed25519(112)\n\n /**\n * Private key payload\n */\n 0x04, // ASN.1 octet string tag\n 0x22, // String length (34 more bytes)\n\n // Private key bytes as octet string\n 0x04, // ASN.1 octet string tag\n 0x20, // String length (32 bytes)\n\n ...bytes\n ]);\n}\n\nexport async function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKey> {\n if (bytes.byteLength !== 32) {\n // TODO: Coded error.\n throw new Error('Private key bytes must be of length 32');\n }\n const privateKeyBytesPkcs8 = addPkcs8Header(bytes);\n return await crypto.subtle.importKey('pkcs8', privateKeyBytesPkcs8, 'Ed25519', extractable ?? false, ['sign']);\n}\n","import { assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';\nimport { Encoder } from '@solana/codecs-core';\nimport { getBase58Encoder } from '@solana/codecs-strings';\n\nexport type Signature = string & { readonly __brand: unique symbol };\nexport type SignatureBytes = Uint8Array & { readonly __brand: unique symbol };\n\nlet base58Encoder: Encoder<string> | undefined;\n\nexport function assertIsSignature(putativeSignature: string): asserts putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n throw new Error('Expected input string to decode to a byte array of length 64.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeSignature}\\` is not a signature`, {\n cause: e,\n });\n }\n}\n\nexport function isSignature(putativeSignature: string): putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n return false;\n }\n return true;\n}\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<SignatureBytes> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as SignatureBytes;\n}\n\nexport function signature(putativeSignature: string): Signature {\n assertIsSignature(putativeSignature);\n return putativeSignature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: SignatureBytes, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
@@ -1,2 +1,4 @@
1
- export * from './base58';
1
+ export * from './key-pair.js';
2
+ export * from './private-key.js';
3
+ export * from './signatures.js';
2
4
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC"}
@@ -0,0 +1,3 @@
1
+ export declare function generateKeyPair(): Promise<CryptoKeyPair>;
2
+ export declare function createKeyPairFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKeyPair>;
3
+ //# sourceMappingURL=key-pair.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-pair.d.ts","sourceRoot":"","sources":["../../src/key-pair.ts"],"names":[],"mappings":"AAKA,wBAAsB,eAAe,IAAI,OAAO,CAAC,aAAa,CAAC,CAQ9D;AAED,wBAAsB,sBAAsB,CAAC,KAAK,EAAE,UAAU,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,aAAa,CAAC,CAS7G"}
@@ -0,0 +1,2 @@
1
+ export declare function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKey>;
2
+ //# sourceMappingURL=private-key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"private-key.d.ts","sourceRoot":"","sources":["../../src/private-key.ts"],"names":[],"mappings":"AAqCA,wBAAsB,yBAAyB,CAAC,KAAK,EAAE,UAAU,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAO5G"}