@solana/keys 2.0.0-experimental.ffa81ab → 2.0.0-preview.1

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,27 +1,149 @@
1
- import bs58 from 'bs58';
1
+ import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
2
+ import { SolanaError, SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH } from '@solana/errors';
3
+ import { getBase58Encoder } from '@solana/codecs-strings';
2
4
 
3
- // src/base58.ts
4
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
5
- try {
6
- if (
7
- // Lowest address (32 bytes of zeroes)
8
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
9
- putativeBase58EncodedAddress.length > 44
10
- ) {
11
- throw new Error("Expected input string to decode to a byte array of length 32.");
12
- }
13
- const bytes = bs58.decode(putativeBase58EncodedAddress);
14
- 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}`);
17
- }
18
- } catch (e) {
19
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
20
- cause: e
5
+ // src/key-pair.ts
6
+ function addPkcs8Header(bytes) {
7
+ return new Uint8Array([
8
+ /**
9
+ * PKCS#8 header
10
+ */
11
+ 48,
12
+ // ASN.1 sequence tag
13
+ 46,
14
+ // Length of sequence (46 more bytes)
15
+ 2,
16
+ // ASN.1 integer tag
17
+ 1,
18
+ // Length of integer
19
+ 0,
20
+ // Version number
21
+ 48,
22
+ // ASN.1 sequence tag
23
+ 5,
24
+ // Length of sequence
25
+ 6,
26
+ // ASN.1 object identifier tag
27
+ 3,
28
+ // Length of object identifier
29
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
30
+ 43,
31
+ // 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)
32
+ 101,
33
+ // thawte(101)
34
+ // Ed25519 identifier
35
+ 112,
36
+ // id-Ed25519(112)
37
+ /**
38
+ * Private key payload
39
+ */
40
+ 4,
41
+ // ASN.1 octet string tag
42
+ 34,
43
+ // String length (34 more bytes)
44
+ // Private key bytes as octet string
45
+ 4,
46
+ // ASN.1 octet string tag
47
+ 32,
48
+ // String length (32 bytes)
49
+ ...bytes
50
+ ]);
51
+ }
52
+ async function createPrivateKeyFromBytes(bytes, extractable) {
53
+ const actualLength = bytes.byteLength;
54
+ if (actualLength !== 32) {
55
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
56
+ actualLength
57
+ });
58
+ }
59
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
60
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
61
+ }
62
+
63
+ // src/key-pair.ts
64
+ async function generateKeyPair() {
65
+ await assertKeyGenerationIsAvailable();
66
+ const keyPair = await crypto.subtle.generateKey(
67
+ /* algorithm */
68
+ "Ed25519",
69
+ // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
70
+ /* extractable */
71
+ false,
72
+ // Prevents the bytes of the private key from being visible to JS.
73
+ /* allowed uses */
74
+ ["sign", "verify"]
75
+ );
76
+ return keyPair;
77
+ }
78
+ async function createKeyPairFromBytes(bytes, extractable) {
79
+ if (bytes.byteLength !== 64) {
80
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { byteLength: bytes.byteLength });
81
+ }
82
+ const [publicKey, privateKey] = await Promise.all([
83
+ crypto.subtle.importKey(
84
+ "raw",
85
+ bytes.slice(32),
86
+ "Ed25519",
87
+ /* extractable */
88
+ true,
89
+ ["verify"]
90
+ ),
91
+ createPrivateKeyFromBytes(bytes.slice(0, 32), extractable)
92
+ ]);
93
+ return { privateKey, publicKey };
94
+ }
95
+ var base58Encoder;
96
+ function assertIsSignature(putativeSignature) {
97
+ if (!base58Encoder)
98
+ base58Encoder = getBase58Encoder();
99
+ if (
100
+ // Lowest value (64 bytes of zeroes)
101
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
102
+ putativeSignature.length > 88
103
+ ) {
104
+ throw new SolanaError(SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {
105
+ actualLength: putativeSignature.length
21
106
  });
22
107
  }
108
+ const bytes = base58Encoder.encode(putativeSignature);
109
+ const numBytes = bytes.byteLength;
110
+ if (numBytes !== 64) {
111
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {
112
+ actualLength: numBytes
113
+ });
114
+ }
115
+ }
116
+ function isSignature(putativeSignature) {
117
+ if (!base58Encoder)
118
+ base58Encoder = getBase58Encoder();
119
+ if (
120
+ // Lowest value (64 bytes of zeroes)
121
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
122
+ putativeSignature.length > 88
123
+ ) {
124
+ return false;
125
+ }
126
+ const bytes = base58Encoder.encode(putativeSignature);
127
+ const numBytes = bytes.byteLength;
128
+ if (numBytes !== 64) {
129
+ return false;
130
+ }
131
+ return true;
132
+ }
133
+ async function signBytes(key, data) {
134
+ await assertSigningCapabilityIsAvailable();
135
+ const signedData = await crypto.subtle.sign("Ed25519", key, data);
136
+ return new Uint8Array(signedData);
137
+ }
138
+ function signature(putativeSignature) {
139
+ assertIsSignature(putativeSignature);
140
+ return putativeSignature;
141
+ }
142
+ async function verifySignature(key, signature2, data) {
143
+ await assertVerificationCapabilityIsAvailable();
144
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
23
145
  }
24
146
 
25
- export { assertIsBase58EncodedAddress };
147
+ export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
26
148
  //# sourceMappingURL=out.js.map
27
149
  //# 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","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"]}
1
+ {"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["SolanaError","signature"],"mappings":";AAAA,SAAS,sCAAsC;AAC/C,SAAS,kDAAkD,eAAAA,oBAAmB;;;ACD9E,SAAS,qDAAqD,mBAAmB;AAEjF,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,QAAM,eAAe,MAAM;AAC3B,MAAI,iBAAiB,IAAI;AACrB,UAAM,IAAI,YAAY,qDAAqD;AAAA,MACvE;AAAA,IACJ,CAAC;AAAA,EACL;AACA,QAAM,uBAAuB,eAAe,KAAK;AACjD,SAAO,MAAM,OAAO,OAAO,UAAU,SAAS,sBAAsB,WAAW,eAAe,OAAO,CAAC,MAAM,CAAC;AACjH;;;AD3CA,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;AAEA,eAAsB,uBAAuB,OAAmB,aAA+C;AAC3G,MAAI,MAAM,eAAe,IAAI;AACzB,UAAM,IAAIA,aAAY,kDAAkD,EAAE,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AM,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,KAAgBC,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__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, 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__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { 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","import { SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, SolanaError } from '@solana/errors';\n\nfunction 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 const actualLength = bytes.byteLength;\n if (actualLength !== 32) {\n throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {\n actualLength,\n });\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';\nimport {\n SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH,\n SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE,\n SolanaError,\n} from '@solana/errors';\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 // 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 SolanaError(SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {\n actualLength: putativeSignature.length,\n });\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 SolanaError(SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {\n actualLength: numBytes,\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,33 +1,158 @@
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 }; }
6
-
7
- var bs58__default = /*#__PURE__*/_interopDefault(bs58);
7
+ // src/key-pair.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
+ const actualLength = bytes.byteLength;
56
+ if (actualLength !== 32) {
57
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
58
+ actualLength
59
+ });
60
+ }
61
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
62
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
63
+ }
8
64
 
9
- // src/base58.ts
10
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
11
- try {
12
- if (
13
- // Lowest address (32 bytes of zeroes)
14
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
15
- putativeBase58EncodedAddress.length > 44
16
- ) {
17
- throw new Error("Expected input string to decode to a byte array of length 32.");
18
- }
19
- const bytes = bs58__default.default.decode(putativeBase58EncodedAddress);
20
- 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}`);
23
- }
24
- } catch (e) {
25
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
26
- cause: e
65
+ // src/key-pair.ts
66
+ async function generateKeyPair() {
67
+ await assertions.assertKeyGenerationIsAvailable();
68
+ const keyPair = await crypto.subtle.generateKey(
69
+ /* algorithm */
70
+ "Ed25519",
71
+ // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
72
+ /* extractable */
73
+ false,
74
+ // Prevents the bytes of the private key from being visible to JS.
75
+ /* allowed uses */
76
+ ["sign", "verify"]
77
+ );
78
+ return keyPair;
79
+ }
80
+ async function createKeyPairFromBytes(bytes, extractable) {
81
+ if (bytes.byteLength !== 64) {
82
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { byteLength: bytes.byteLength });
83
+ }
84
+ const [publicKey, privateKey] = await Promise.all([
85
+ crypto.subtle.importKey(
86
+ "raw",
87
+ bytes.slice(32),
88
+ "Ed25519",
89
+ /* extractable */
90
+ true,
91
+ ["verify"]
92
+ ),
93
+ createPrivateKeyFromBytes(bytes.slice(0, 32), extractable)
94
+ ]);
95
+ return { privateKey, publicKey };
96
+ }
97
+ var base58Encoder;
98
+ function assertIsSignature(putativeSignature) {
99
+ if (!base58Encoder)
100
+ base58Encoder = codecsStrings.getBase58Encoder();
101
+ if (
102
+ // Lowest value (64 bytes of zeroes)
103
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
104
+ putativeSignature.length > 88
105
+ ) {
106
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {
107
+ actualLength: putativeSignature.length
27
108
  });
28
109
  }
110
+ const bytes = base58Encoder.encode(putativeSignature);
111
+ const numBytes = bytes.byteLength;
112
+ if (numBytes !== 64) {
113
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {
114
+ actualLength: numBytes
115
+ });
116
+ }
117
+ }
118
+ function isSignature(putativeSignature) {
119
+ if (!base58Encoder)
120
+ base58Encoder = codecsStrings.getBase58Encoder();
121
+ if (
122
+ // Lowest value (64 bytes of zeroes)
123
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
124
+ putativeSignature.length > 88
125
+ ) {
126
+ return false;
127
+ }
128
+ const bytes = base58Encoder.encode(putativeSignature);
129
+ const numBytes = bytes.byteLength;
130
+ if (numBytes !== 64) {
131
+ return false;
132
+ }
133
+ return true;
134
+ }
135
+ async function signBytes(key, data) {
136
+ await assertions.assertSigningCapabilityIsAvailable();
137
+ const signedData = await crypto.subtle.sign("Ed25519", key, data);
138
+ return new Uint8Array(signedData);
139
+ }
140
+ function signature(putativeSignature) {
141
+ assertIsSignature(putativeSignature);
142
+ return putativeSignature;
143
+ }
144
+ async function verifySignature(key, signature2, data) {
145
+ await assertions.assertVerificationCapabilityIsAvailable();
146
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
29
147
  }
30
148
 
31
- exports.assertIsBase58EncodedAddress = assertIsBase58EncodedAddress;
149
+ exports.assertIsSignature = assertIsSignature;
150
+ exports.createKeyPairFromBytes = createKeyPairFromBytes;
151
+ exports.createPrivateKeyFromBytes = createPrivateKeyFromBytes;
152
+ exports.generateKeyPair = generateKeyPair;
153
+ exports.isSignature = isSignature;
154
+ exports.signBytes = signBytes;
155
+ exports.signature = signature;
156
+ exports.verifySignature = verifySignature;
32
157
  //# sourceMappingURL=out.js.map
33
158
  //# 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","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"]}
1
+ {"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["SolanaError","signature"],"mappings":";AAAA,SAAS,sCAAsC;AAC/C,SAAS,kDAAkD,eAAAA,oBAAmB;;;ACD9E,SAAS,qDAAqD,mBAAmB;AAEjF,SAAS,eAAe,OAA+B;AAEnD,SAAO,IAAI,WAAW;AAAA;AAAA;AAAA;AAAA,I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cesContent":["import { assertKeyGenerationIsAvailable } from '@solana/assertions';\nimport { SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, 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__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { 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","import { SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, SolanaError } from '@solana/errors';\n\nfunction 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 const actualLength = bytes.byteLength;\n if (actualLength !== 32) {\n throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {\n actualLength,\n });\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';\nimport {\n SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH,\n SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE,\n SolanaError,\n} from '@solana/errors';\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 // 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 SolanaError(SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {\n actualLength: putativeSignature.length,\n });\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 SolanaError(SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {\n actualLength: numBytes,\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,27 +1,149 @@
1
- import bs58 from 'bs58';
1
+ import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
2
+ import { SolanaError, SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH } from '@solana/errors';
3
+ import { getBase58Encoder } from '@solana/codecs-strings';
2
4
 
3
- // src/base58.ts
4
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
5
- try {
6
- if (
7
- // Lowest address (32 bytes of zeroes)
8
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
9
- putativeBase58EncodedAddress.length > 44
10
- ) {
11
- throw new Error("Expected input string to decode to a byte array of length 32.");
12
- }
13
- const bytes = bs58.decode(putativeBase58EncodedAddress);
14
- 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}`);
17
- }
18
- } catch (e) {
19
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
20
- cause: e
5
+ // src/key-pair.ts
6
+ function addPkcs8Header(bytes) {
7
+ return new Uint8Array([
8
+ /**
9
+ * PKCS#8 header
10
+ */
11
+ 48,
12
+ // ASN.1 sequence tag
13
+ 46,
14
+ // Length of sequence (46 more bytes)
15
+ 2,
16
+ // ASN.1 integer tag
17
+ 1,
18
+ // Length of integer
19
+ 0,
20
+ // Version number
21
+ 48,
22
+ // ASN.1 sequence tag
23
+ 5,
24
+ // Length of sequence
25
+ 6,
26
+ // ASN.1 object identifier tag
27
+ 3,
28
+ // Length of object identifier
29
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
30
+ 43,
31
+ // 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)
32
+ 101,
33
+ // thawte(101)
34
+ // Ed25519 identifier
35
+ 112,
36
+ // id-Ed25519(112)
37
+ /**
38
+ * Private key payload
39
+ */
40
+ 4,
41
+ // ASN.1 octet string tag
42
+ 34,
43
+ // String length (34 more bytes)
44
+ // Private key bytes as octet string
45
+ 4,
46
+ // ASN.1 octet string tag
47
+ 32,
48
+ // String length (32 bytes)
49
+ ...bytes
50
+ ]);
51
+ }
52
+ async function createPrivateKeyFromBytes(bytes, extractable) {
53
+ const actualLength = bytes.byteLength;
54
+ if (actualLength !== 32) {
55
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
56
+ actualLength
57
+ });
58
+ }
59
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
60
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
61
+ }
62
+
63
+ // src/key-pair.ts
64
+ async function generateKeyPair() {
65
+ await assertKeyGenerationIsAvailable();
66
+ const keyPair = await crypto.subtle.generateKey(
67
+ /* algorithm */
68
+ "Ed25519",
69
+ // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
70
+ /* extractable */
71
+ false,
72
+ // Prevents the bytes of the private key from being visible to JS.
73
+ /* allowed uses */
74
+ ["sign", "verify"]
75
+ );
76
+ return keyPair;
77
+ }
78
+ async function createKeyPairFromBytes(bytes, extractable) {
79
+ if (bytes.byteLength !== 64) {
80
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { byteLength: bytes.byteLength });
81
+ }
82
+ const [publicKey, privateKey] = await Promise.all([
83
+ crypto.subtle.importKey(
84
+ "raw",
85
+ bytes.slice(32),
86
+ "Ed25519",
87
+ /* extractable */
88
+ true,
89
+ ["verify"]
90
+ ),
91
+ createPrivateKeyFromBytes(bytes.slice(0, 32), extractable)
92
+ ]);
93
+ return { privateKey, publicKey };
94
+ }
95
+ var base58Encoder;
96
+ function assertIsSignature(putativeSignature) {
97
+ if (!base58Encoder)
98
+ base58Encoder = getBase58Encoder();
99
+ if (
100
+ // Lowest value (64 bytes of zeroes)
101
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
102
+ putativeSignature.length > 88
103
+ ) {
104
+ throw new SolanaError(SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {
105
+ actualLength: putativeSignature.length
21
106
  });
22
107
  }
108
+ const bytes = base58Encoder.encode(putativeSignature);
109
+ const numBytes = bytes.byteLength;
110
+ if (numBytes !== 64) {
111
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {
112
+ actualLength: numBytes
113
+ });
114
+ }
115
+ }
116
+ function isSignature(putativeSignature) {
117
+ if (!base58Encoder)
118
+ base58Encoder = getBase58Encoder();
119
+ if (
120
+ // Lowest value (64 bytes of zeroes)
121
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
122
+ putativeSignature.length > 88
123
+ ) {
124
+ return false;
125
+ }
126
+ const bytes = base58Encoder.encode(putativeSignature);
127
+ const numBytes = bytes.byteLength;
128
+ if (numBytes !== 64) {
129
+ return false;
130
+ }
131
+ return true;
132
+ }
133
+ async function signBytes(key, data) {
134
+ await assertSigningCapabilityIsAvailable();
135
+ const signedData = await crypto.subtle.sign("Ed25519", key, data);
136
+ return new Uint8Array(signedData);
137
+ }
138
+ function signature(putativeSignature) {
139
+ assertIsSignature(putativeSignature);
140
+ return putativeSignature;
141
+ }
142
+ async function verifySignature(key, signature2, data) {
143
+ await assertVerificationCapabilityIsAvailable();
144
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
23
145
  }
24
146
 
25
- export { assertIsBase58EncodedAddress };
147
+ export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
26
148
  //# sourceMappingURL=out.js.map
27
149
  //# 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","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"]}
1
+ {"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["SolanaError","signature"],"mappings":";AAAA,SAAS,sCAAsC;AAC/C,SAAS,kDAAkD,eAAAA,oBAAmB;;;ACD9E,SAAS,qDAAqD,mBAAmB;AAEjF,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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MAC5E,cAAc,kBAAkB;AAAA,IACpC,CAAC;AAAA,EACL;AAEA,QAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,QAAM,WAAW,MAAM;AACvB,MAAI,aAAa,IAAI;AACjB,UAAM,IAAIA,aAAY,mDAAmD;AAAA,MACrE,cAAc;AAAA,IAClB,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,KAAgBC,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__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, 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__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { 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","import { SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, SolanaError } from '@solana/errors';\n\nfunction 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 const actualLength = bytes.byteLength;\n if (actualLength !== 32) {\n throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {\n actualLength,\n });\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';\nimport {\n SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH,\n SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE,\n SolanaError,\n} from '@solana/errors';\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 // 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 SolanaError(SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {\n actualLength: putativeSignature.length,\n });\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 SolanaError(SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {\n actualLength: numBytes,\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":"AAuCA,wBAAsB,yBAAyB,CAAC,KAAK,EAAE,UAAU,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAS5G"}