@solana/keys 2.0.0-experimental.2a97655 → 2.0.0-experimental.2ab5f3f
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 +58 -22
- package/dist/index.browser.cjs +123 -116
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +116 -110
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +116 -97
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +123 -103
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +116 -97
- package/dist/index.node.js.map +1 -1
- package/dist/types/index.d.ts +3 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/key-pair.d.ts +1 -0
- package/dist/types/key-pair.d.ts.map +1 -1
- 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 +10 -4
- package/dist/types/signatures.d.ts.map +1 -1
- package/package.json +17 -38
- package/dist/index.development.js +0 -418
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -21
- package/dist/types/base58.d.ts +0 -10
- package/dist/types/base58.d.ts.map +0 -1
- package/dist/types/guard.d.ts +0 -5
- package/dist/types/guard.d.ts.map +0 -1
- package/dist/types/pubkey.d.ts +0 -3
- package/dist/types/pubkey.d.ts.map +0 -1
package/dist/index.node.cjs
CHANGED
|
@@ -1,97 +1,70 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var assertions = require('@solana/assertions');
|
|
4
|
+
var errors = require('@solana/errors');
|
|
5
|
+
var codecsStrings = require('@solana/codecs-strings');
|
|
4
6
|
|
|
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
|
-
|
|
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
|
+
]);
|
|
43
53
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"Ed25519",
|
|
50
|
-
/* extractable */
|
|
51
|
-
false,
|
|
52
|
-
["sign", "verify"]
|
|
53
|
-
).catch(() => {
|
|
54
|
-
resolve(cachedEd25519Decision = false);
|
|
55
|
-
}).then(() => {
|
|
56
|
-
resolve(cachedEd25519Decision = true);
|
|
57
|
-
});
|
|
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
|
|
58
59
|
});
|
|
59
60
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
} else {
|
|
63
|
-
return await cachedEd25519Decision;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
async function assertKeyGenerationIsAvailable() {
|
|
67
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.generateKey !== "function") {
|
|
68
|
-
throw new Error("No key generation implementation could be found");
|
|
69
|
-
}
|
|
70
|
-
if (!await isEd25519CurveSupported(globalThis.crypto.subtle)) {
|
|
71
|
-
throw new Error(
|
|
72
|
-
"This runtime does not support the generation of Ed25519 key pairs.\n\nInstall and import `@solana/webcrypto-ed25519-polyfill` before generating keys in environments that do not support Ed25519.\n\nFor a list of runtimes that currently support Ed25519 operations, visit https://github.com/WICG/webcrypto-secure-curves/issues/20"
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async function assertKeyExporterIsAvailable() {
|
|
77
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.exportKey !== "function") {
|
|
78
|
-
throw new Error("No key export implementation could be found");
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
async function assertSigningCapabilityIsAvailable() {
|
|
82
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.sign !== "function") {
|
|
83
|
-
throw new Error("No signing implementation could be found");
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
async function assertVerificationCapabilityIsAvailable() {
|
|
87
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.verify !== "function") {
|
|
88
|
-
throw new Error("No signature verification implementation could be found");
|
|
89
|
-
}
|
|
61
|
+
const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
|
|
62
|
+
return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
|
|
90
63
|
}
|
|
91
64
|
|
|
92
65
|
// src/key-pair.ts
|
|
93
66
|
async function generateKeyPair() {
|
|
94
|
-
await assertKeyGenerationIsAvailable();
|
|
67
|
+
await assertions.assertKeyGenerationIsAvailable();
|
|
95
68
|
const keyPair = await crypto.subtle.generateKey(
|
|
96
69
|
/* algorithm */
|
|
97
70
|
"Ed25519",
|
|
@@ -104,35 +77,82 @@ async function generateKeyPair() {
|
|
|
104
77
|
);
|
|
105
78
|
return keyPair;
|
|
106
79
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
await assertKeyExporterIsAvailable();
|
|
111
|
-
if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
|
|
112
|
-
throw new Error("The `CryptoKey` must be an `Ed25519` public key");
|
|
80
|
+
async function createKeyPairFromBytes(bytes, extractable) {
|
|
81
|
+
if (bytes.byteLength !== 64) {
|
|
82
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__INVALID_KEYPAIR_BYTES, { byteLength: bytes.byteLength });
|
|
113
83
|
}
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
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
|
|
108
|
+
});
|
|
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;
|
|
117
134
|
}
|
|
118
|
-
|
|
119
|
-
// src/signatures.ts
|
|
120
135
|
async function signBytes(key, data) {
|
|
121
|
-
await assertSigningCapabilityIsAvailable();
|
|
136
|
+
await assertions.assertSigningCapabilityIsAvailable();
|
|
122
137
|
const signedData = await crypto.subtle.sign("Ed25519", key, data);
|
|
123
138
|
return new Uint8Array(signedData);
|
|
124
139
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return
|
|
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);
|
|
128
147
|
}
|
|
129
148
|
|
|
130
|
-
exports.
|
|
149
|
+
exports.assertIsSignature = assertIsSignature;
|
|
150
|
+
exports.createKeyPairFromBytes = createKeyPairFromBytes;
|
|
151
|
+
exports.createPrivateKeyFromBytes = createPrivateKeyFromBytes;
|
|
131
152
|
exports.generateKeyPair = generateKeyPair;
|
|
132
|
-
exports.
|
|
133
|
-
exports.getBase58EncodedAddressComparator = getBase58EncodedAddressComparator;
|
|
134
|
-
exports.getBase58EncodedAddressFromPublicKey = getBase58EncodedAddressFromPublicKey;
|
|
153
|
+
exports.isSignature = isSignature;
|
|
135
154
|
exports.signBytes = signBytes;
|
|
155
|
+
exports.signature = signature;
|
|
136
156
|
exports.verifySignature = verifySignature;
|
|
137
157
|
//# sourceMappingURL=out.js.map
|
|
138
158
|
//# sourceMappingURL=index.node.cjs.map
|
package/dist/index.node.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":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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.node.js
CHANGED
|
@@ -1,90 +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(
|
|
70
|
-
"This runtime does not support the generation of Ed25519 key pairs.\n\nInstall and import `@solana/webcrypto-ed25519-polyfill` before generating keys in environments that do not support Ed25519.\n\nFor a list of runtimes that currently support Ed25519 operations, visit https://github.com/WICG/webcrypto-secure-curves/issues/20"
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
async function assertKeyExporterIsAvailable() {
|
|
75
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.exportKey !== "function") {
|
|
76
|
-
throw new Error("No key export implementation could be found");
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
async function assertSigningCapabilityIsAvailable() {
|
|
80
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.sign !== "function") {
|
|
81
|
-
throw new Error("No signing implementation could be found");
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
async function assertVerificationCapabilityIsAvailable() {
|
|
85
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.verify !== "function") {
|
|
86
|
-
throw new Error("No signature verification implementation could be found");
|
|
87
|
-
}
|
|
59
|
+
const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
|
|
60
|
+
return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
|
|
88
61
|
}
|
|
89
62
|
|
|
90
63
|
// src/key-pair.ts
|
|
@@ -102,29 +75,75 @@ async function generateKeyPair() {
|
|
|
102
75
|
);
|
|
103
76
|
return keyPair;
|
|
104
77
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
await assertKeyExporterIsAvailable();
|
|
109
|
-
if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
|
|
110
|
-
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 });
|
|
111
81
|
}
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
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;
|
|
115
132
|
}
|
|
116
|
-
|
|
117
|
-
// src/signatures.ts
|
|
118
133
|
async function signBytes(key, data) {
|
|
119
134
|
await assertSigningCapabilityIsAvailable();
|
|
120
135
|
const signedData = await crypto.subtle.sign("Ed25519", key, data);
|
|
121
136
|
return new Uint8Array(signedData);
|
|
122
137
|
}
|
|
123
|
-
|
|
138
|
+
function signature(putativeSignature) {
|
|
139
|
+
assertIsSignature(putativeSignature);
|
|
140
|
+
return putativeSignature;
|
|
141
|
+
}
|
|
142
|
+
async function verifySignature(key, signature2, data) {
|
|
124
143
|
await assertVerificationCapabilityIsAvailable();
|
|
125
|
-
return await crypto.subtle.verify("Ed25519", key,
|
|
144
|
+
return await crypto.subtle.verify("Ed25519", key, signature2, data);
|
|
126
145
|
}
|
|
127
146
|
|
|
128
|
-
export {
|
|
147
|
+
export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
|
|
129
148
|
//# sourceMappingURL=out.js.map
|
|
130
149
|
//# sourceMappingURL=index.node.js.map
|