@solana/keys 2.0.0-experimental.6b66b7d → 2.0.0-experimental.6bfb261
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.
- package/LICENSE +1 -1
- package/README.md +85 -19
- package/dist/index.browser.cjs +130 -96
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +123 -92
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +123 -81
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +130 -85
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +123 -81
- package/dist/index.node.js.map +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/key-pair.d.ts +1 -0
- package/dist/types/key-pair.d.ts.map +1 -0
- package/dist/types/private-key.d.ts +2 -0
- package/dist/types/private-key.d.ts.map +1 -0
- package/dist/types/signatures.d.ts +12 -0
- package/dist/types/signatures.d.ts.map +1 -0
- package/package.json +17 -38
- package/dist/index.development.js +0 -391
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -15
- package/dist/types/base58.d.ts +0 -10
- package/dist/types/guard.d.ts +0 -5
- package/dist/types/pubkey.d.ts +0 -3
package/dist/index.browser.js
CHANGED
|
@@ -1,89 +1,63 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
|
|
2
|
+
import { SolanaError, SOLANA_ERROR__KEYS_INVALID_PRIVATE_KEY_BYTE_LENGTH, SOLANA_ERROR__INVALID_KEYPAIR_BYTES, 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
|
-
//
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
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
|
+
]);
|
|
50
51
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"Ed25519",
|
|
57
|
-
/* extractable */
|
|
58
|
-
false,
|
|
59
|
-
["sign", "verify"]
|
|
60
|
-
).catch(() => {
|
|
61
|
-
resolve(cachedEd25519Decision = false);
|
|
62
|
-
}).then(() => {
|
|
63
|
-
resolve(cachedEd25519Decision = true);
|
|
64
|
-
});
|
|
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
|
|
65
57
|
});
|
|
66
58
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
} else {
|
|
70
|
-
return await cachedEd25519Decision;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
async function assertKeyGenerationIsAvailable() {
|
|
74
|
-
assertIsSecureContext();
|
|
75
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.generateKey !== "function") {
|
|
76
|
-
throw new Error("No key generation implementation could be found");
|
|
77
|
-
}
|
|
78
|
-
if (!await isEd25519CurveSupported(globalThis.crypto.subtle)) {
|
|
79
|
-
throw new Error("This runtime does not support the generation of Ed25519 keypairs");
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
async function assertKeyExporterIsAvailable() {
|
|
83
|
-
assertIsSecureContext();
|
|
84
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.exportKey !== "function") {
|
|
85
|
-
throw new Error("No key export implementation could be found");
|
|
86
|
-
}
|
|
59
|
+
const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
|
|
60
|
+
return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
|
|
87
61
|
}
|
|
88
62
|
|
|
89
63
|
// src/key-pair.ts
|
|
@@ -101,18 +75,75 @@ async function generateKeyPair() {
|
|
|
101
75
|
);
|
|
102
76
|
return keyPair;
|
|
103
77
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
78
|
+
async function createKeyPairFromBytes(bytes, extractable) {
|
|
79
|
+
if (bytes.byteLength !== 64) {
|
|
80
|
+
throw new SolanaError(SOLANA_ERROR__INVALID_KEYPAIR_BYTES, { 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
|
|
106
|
+
});
|
|
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
|
+
});
|
|
110
114
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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);
|
|
114
145
|
}
|
|
115
146
|
|
|
116
|
-
export {
|
|
147
|
+
export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
|
|
117
148
|
//# sourceMappingURL=out.js.map
|
|
118
149
|
//# sourceMappingURL=index.browser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
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,qCAAqC,eAAAA,oBAAmB;;;ACDjE,SAAS,oDAAoD,mBAAmB;AAEhF,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,oDAAoD;AAAA,MACtE;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;A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;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__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","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"]}
|
package/dist/index.native.js
CHANGED
|
@@ -1,78 +1,63 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
|
|
2
|
+
import { SolanaError, SOLANA_ERROR__KEYS_INVALID_PRIVATE_KEY_BYTE_LENGTH, SOLANA_ERROR__INVALID_KEYPAIR_BYTES, 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
|
-
//
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
+
]);
|
|
41
51
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"Ed25519",
|
|
48
|
-
/* extractable */
|
|
49
|
-
false,
|
|
50
|
-
["sign", "verify"]
|
|
51
|
-
).catch(() => {
|
|
52
|
-
resolve(cachedEd25519Decision = false);
|
|
53
|
-
}).then(() => {
|
|
54
|
-
resolve(cachedEd25519Decision = true);
|
|
55
|
-
});
|
|
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
|
|
56
57
|
});
|
|
57
58
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
} else {
|
|
61
|
-
return await cachedEd25519Decision;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
async function assertKeyGenerationIsAvailable() {
|
|
65
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.generateKey !== "function") {
|
|
66
|
-
throw new Error("No key generation implementation could be found");
|
|
67
|
-
}
|
|
68
|
-
if (!await isEd25519CurveSupported(globalThis.crypto.subtle)) {
|
|
69
|
-
throw new Error("This runtime does not support the generation of Ed25519 keypairs");
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
async function assertKeyExporterIsAvailable() {
|
|
73
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.exportKey !== "function") {
|
|
74
|
-
throw new Error("No key export implementation could be found");
|
|
75
|
-
}
|
|
59
|
+
const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
|
|
60
|
+
return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
|
|
76
61
|
}
|
|
77
62
|
|
|
78
63
|
// src/key-pair.ts
|
|
@@ -90,18 +75,75 @@ async function generateKeyPair() {
|
|
|
90
75
|
);
|
|
91
76
|
return keyPair;
|
|
92
77
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
await assertKeyExporterIsAvailable();
|
|
97
|
-
if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
|
|
98
|
-
throw new Error("The `CryptoKey` must be an `Ed25519` public key");
|
|
78
|
+
async function createKeyPairFromBytes(bytes, extractable) {
|
|
79
|
+
if (bytes.byteLength !== 64) {
|
|
80
|
+
throw new SolanaError(SOLANA_ERROR__INVALID_KEYPAIR_BYTES, { byteLength: bytes.byteLength });
|
|
99
81
|
}
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
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
|
|
106
|
+
});
|
|
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);
|
|
103
145
|
}
|
|
104
146
|
|
|
105
|
-
export {
|
|
147
|
+
export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
|
|
106
148
|
//# sourceMappingURL=out.js.map
|
|
107
149
|
//# sourceMappingURL=index.native.js.map
|
package/dist/index.native.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
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,qCAAqC,eAAAA,oBAAmB;;;ACDjE,SAAS,oDAAoD,mBAAmB;AAEhF,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;AA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A,MAC3E,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,kDAAkD;AAAA,MACpE,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__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","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"]}
|