@solana/keys 2.0.0-experimental.c29513e → 2.0.0-experimental.c42ccfd
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 +69 -19
- package/dist/index.browser.cjs +114 -100
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +108 -96
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +108 -85
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +114 -89
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +108 -85
- 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.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 +20 -21
- 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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["signature"],"mappings":";AAAA,SAAS,sCAAsC;AAE/C,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;;;ACVA,SAAS,eAAe,OAA+B;AAEnD,SAAO,IAAI,WAAW;AAAA;AAAA;AAAA;AAAA,IAIlB;AAAA;AAAA,IACA;AAAA;AAAA,IAEI;AAAA;AAAA,IACA;AAAA;AAAA,I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AEA,QAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,QAAM,WAAW,MAAM;AACvB,MAAI,aAAa,IAAI;AACjB,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEA,eAAsB,UAAU,KAAgB,MAA2C;AACvF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEO,SAAS,UAAU,mBAAsC;AAC5D,oBAAkB,iBAAiB;AACnC,SAAO;AACX;AAEA,eAAsB,gBAAgB,KAAgBA,YAA2B,MAAoC;AACjH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAKA,YAAW,IAAI;AACrE","sourcesContent":["import { assertKeyGenerationIsAvailable } from '@solana/assertions';\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","function addPkcs8Header(bytes: Uint8Array): Uint8Array {\n // prettier-ignore\n return new Uint8Array([\n /**\n * PKCS#8 header\n */\n 0x30, // ASN.1 sequence tag\n 0x2e, // Length of sequence (46 more bytes)\n\n 0x02, // ASN.1 integer tag\n 0x01, // Length of integer\n 0x00, // Version number\n\n 0x30, // ASN.1 sequence tag\n 0x05, // Length of sequence\n 0x06, // ASN.1 object identifier tag\n 0x03, // Length of object identifier\n // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112\n 0x2b, // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)\n 0x65, // thawte(101)\n // Ed25519 identifier\n 0x70, // id-Ed25519(112)\n\n /**\n * Private key payload\n */\n 0x04, // ASN.1 octet string tag\n 0x22, // String length (34 more bytes)\n\n // Private key bytes as octet string\n 0x04, // ASN.1 octet string tag\n 0x20, // String length (32 bytes)\n\n ...bytes\n ]);\n}\n\nexport async function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKey> {\n if (bytes.byteLength !== 32) {\n // TODO: Coded error.\n throw new Error('Private key bytes must be of length 32');\n }\n const privateKeyBytesPkcs8 = addPkcs8Header(bytes);\n return await crypto.subtle.importKey('pkcs8', privateKeyBytesPkcs8, 'Ed25519', extractable ?? false, ['sign']);\n}\n","import { assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';\nimport { Encoder } from '@solana/codecs-core';\nimport { getBase58Encoder } from '@solana/codecs-strings';\n\nexport type Signature = string & { readonly __brand: unique symbol };\nexport type SignatureBytes = Uint8Array & { readonly __brand: unique symbol };\n\nlet base58Encoder: Encoder<string> | undefined;\n\nexport function assertIsSignature(putativeSignature: string): asserts putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n throw new Error('Expected input string to decode to a byte array of length 64.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeSignature}\\` is not a signature`, {\n cause: e,\n });\n }\n}\n\nexport function isSignature(putativeSignature: string): putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n return false;\n }\n return true;\n}\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<SignatureBytes> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as SignatureBytes;\n}\n\nexport function signature(putativeSignature: string): Signature {\n assertIsSignature(putativeSignature);\n return putativeSignature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: SignatureBytes, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
|
package/dist/index.native.js
CHANGED
|
@@ -1,79 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
// ../build-scripts/env-shim.ts
|
|
4
|
-
var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
|
|
5
|
-
function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
|
|
6
|
-
try {
|
|
7
|
-
if (
|
|
8
|
-
// Lowest address (32 bytes of zeroes)
|
|
9
|
-
putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
10
|
-
putativeBase58EncodedAddress.length > 44
|
|
11
|
-
) {
|
|
12
|
-
throw new Error("Expected input string to decode to a byte array of length 32.");
|
|
13
|
-
}
|
|
14
|
-
const bytes = base58.serialize(putativeBase58EncodedAddress);
|
|
15
|
-
const numBytes = bytes.byteLength;
|
|
16
|
-
if (numBytes !== 32) {
|
|
17
|
-
throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
|
|
18
|
-
}
|
|
19
|
-
} catch (e) {
|
|
20
|
-
throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
|
|
21
|
-
cause: e
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function getBase58EncodedAddressCodec(config) {
|
|
26
|
-
return string({
|
|
27
|
-
description: config?.description ?? (__DEV__ ? "A 32-byte account address" : ""),
|
|
28
|
-
encoding: base58,
|
|
29
|
-
size: 32
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
function getBase58EncodedAddressComparator() {
|
|
33
|
-
return new Intl.Collator("en", {
|
|
34
|
-
caseFirst: "lower",
|
|
35
|
-
ignorePunctuation: false,
|
|
36
|
-
localeMatcher: "best fit",
|
|
37
|
-
numeric: false,
|
|
38
|
-
sensitivity: "variant",
|
|
39
|
-
usage: "sort"
|
|
40
|
-
}).compare;
|
|
41
|
-
}
|
|
42
|
-
var cachedEd25519Decision;
|
|
43
|
-
async function isEd25519CurveSupported(subtle) {
|
|
44
|
-
if (cachedEd25519Decision === void 0) {
|
|
45
|
-
cachedEd25519Decision = new Promise((resolve) => {
|
|
46
|
-
subtle.generateKey(
|
|
47
|
-
"Ed25519",
|
|
48
|
-
/* extractable */
|
|
49
|
-
false,
|
|
50
|
-
["sign", "verify"]
|
|
51
|
-
).catch(() => {
|
|
52
|
-
resolve(cachedEd25519Decision = false);
|
|
53
|
-
}).then(() => {
|
|
54
|
-
resolve(cachedEd25519Decision = true);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
if (typeof cachedEd25519Decision === "boolean") {
|
|
59
|
-
return cachedEd25519Decision;
|
|
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
|
-
}
|
|
76
|
-
}
|
|
1
|
+
import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
|
|
2
|
+
import { getBase58Encoder } from '@solana/codecs-strings';
|
|
77
3
|
|
|
78
4
|
// src/key-pair.ts
|
|
79
5
|
async function generateKeyPair() {
|
|
@@ -91,17 +17,114 @@ async function generateKeyPair() {
|
|
|
91
17
|
return keyPair;
|
|
92
18
|
}
|
|
93
19
|
|
|
94
|
-
// src/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
20
|
+
// src/private-key.ts
|
|
21
|
+
function addPkcs8Header(bytes) {
|
|
22
|
+
return new Uint8Array([
|
|
23
|
+
/**
|
|
24
|
+
* PKCS#8 header
|
|
25
|
+
*/
|
|
26
|
+
48,
|
|
27
|
+
// ASN.1 sequence tag
|
|
28
|
+
46,
|
|
29
|
+
// Length of sequence (46 more bytes)
|
|
30
|
+
2,
|
|
31
|
+
// ASN.1 integer tag
|
|
32
|
+
1,
|
|
33
|
+
// Length of integer
|
|
34
|
+
0,
|
|
35
|
+
// Version number
|
|
36
|
+
48,
|
|
37
|
+
// ASN.1 sequence tag
|
|
38
|
+
5,
|
|
39
|
+
// Length of sequence
|
|
40
|
+
6,
|
|
41
|
+
// ASN.1 object identifier tag
|
|
42
|
+
3,
|
|
43
|
+
// Length of object identifier
|
|
44
|
+
// Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
|
|
45
|
+
43,
|
|
46
|
+
// 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)
|
|
47
|
+
101,
|
|
48
|
+
// thawte(101)
|
|
49
|
+
// Ed25519 identifier
|
|
50
|
+
112,
|
|
51
|
+
// id-Ed25519(112)
|
|
52
|
+
/**
|
|
53
|
+
* Private key payload
|
|
54
|
+
*/
|
|
55
|
+
4,
|
|
56
|
+
// ASN.1 octet string tag
|
|
57
|
+
34,
|
|
58
|
+
// String length (34 more bytes)
|
|
59
|
+
// Private key bytes as octet string
|
|
60
|
+
4,
|
|
61
|
+
// ASN.1 octet string tag
|
|
62
|
+
32,
|
|
63
|
+
// String length (32 bytes)
|
|
64
|
+
...bytes
|
|
65
|
+
]);
|
|
66
|
+
}
|
|
67
|
+
async function createPrivateKeyFromBytes(bytes, extractable) {
|
|
68
|
+
if (bytes.byteLength !== 32) {
|
|
69
|
+
throw new Error("Private key bytes must be of length 32");
|
|
70
|
+
}
|
|
71
|
+
const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
|
|
72
|
+
return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
|
|
73
|
+
}
|
|
74
|
+
var base58Encoder;
|
|
75
|
+
function assertIsSignature(putativeSignature) {
|
|
76
|
+
if (!base58Encoder)
|
|
77
|
+
base58Encoder = getBase58Encoder();
|
|
78
|
+
try {
|
|
79
|
+
if (
|
|
80
|
+
// Lowest value (64 bytes of zeroes)
|
|
81
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
82
|
+
putativeSignature.length > 88
|
|
83
|
+
) {
|
|
84
|
+
throw new Error("Expected input string to decode to a byte array of length 64.");
|
|
85
|
+
}
|
|
86
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
87
|
+
const numBytes = bytes.byteLength;
|
|
88
|
+
if (numBytes !== 64) {
|
|
89
|
+
throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
|
|
90
|
+
}
|
|
91
|
+
} catch (e) {
|
|
92
|
+
throw new Error(`\`${putativeSignature}\` is not a signature`, {
|
|
93
|
+
cause: e
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function isSignature(putativeSignature) {
|
|
98
|
+
if (!base58Encoder)
|
|
99
|
+
base58Encoder = getBase58Encoder();
|
|
100
|
+
if (
|
|
101
|
+
// Lowest value (64 bytes of zeroes)
|
|
102
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
103
|
+
putativeSignature.length > 88
|
|
104
|
+
) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
108
|
+
const numBytes = bytes.byteLength;
|
|
109
|
+
if (numBytes !== 64) {
|
|
110
|
+
return false;
|
|
99
111
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
async function signBytes(key, data) {
|
|
115
|
+
await assertSigningCapabilityIsAvailable();
|
|
116
|
+
const signedData = await crypto.subtle.sign("Ed25519", key, data);
|
|
117
|
+
return new Uint8Array(signedData);
|
|
118
|
+
}
|
|
119
|
+
function signature(putativeSignature) {
|
|
120
|
+
assertIsSignature(putativeSignature);
|
|
121
|
+
return putativeSignature;
|
|
122
|
+
}
|
|
123
|
+
async function verifySignature(key, signature2, data) {
|
|
124
|
+
await assertVerificationCapabilityIsAvailable();
|
|
125
|
+
return await crypto.subtle.verify("Ed25519", key, signature2, data);
|
|
103
126
|
}
|
|
104
127
|
|
|
105
|
-
export {
|
|
128
|
+
export { assertIsSignature, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
|
|
106
129
|
//# sourceMappingURL=out.js.map
|
|
107
130
|
//# 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":["signature"],"mappings":";AAAA,SAAS,sCAAsC;AAE/C,eAAsB,kBAA0C;AAC5D,QAAM,+BAA+B;AACrC,QAAM,UAAU,MA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,GAAG;AACR,UAAM,IAAI,MAAM,KAAK,iBAAiB,yBAAyB;AAAA,MAC3D,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ;AAEO,SAAS,YAAY,mBAA2D;AACnF,MAAI,CAAC;AAAe,oBAAgB,iBAAiB;AAGrD;AAAA;AAAA,IAEI,kBAAkB,SAAS;AAAA,IAE3B,kBAAkB,SAAS;AAAA,IAC7B;AACE,WAAO;AAAA,EACX;AAEA,QAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,QAAM,WAAW,MAAM;AACvB,MAAI,aAAa,IAAI;AACjB,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEA,eAAsB,UAAU,KAAgB,MAA2C;AACvF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEO,SAAS,UAAU,mBAAsC;AAC5D,oBAAkB,iBAAiB;AACnC,SAAO;AACX;AAEA,eAAsB,gBAAgB,KAAgBA,YAA2B,MAAoC;AACjH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAKA,YAAW,IAAI;AACrE","sourcesContent":["import { assertKeyGenerationIsAvailable } from '@solana/assertions';\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","function addPkcs8Header(bytes: Uint8Array): Uint8Array {\n // prettier-ignore\n return new Uint8Array([\n /**\n * PKCS#8 header\n */\n 0x30, // ASN.1 sequence tag\n 0x2e, // Length of sequence (46 more bytes)\n\n 0x02, // ASN.1 integer tag\n 0x01, // Length of integer\n 0x00, // Version number\n\n 0x30, // ASN.1 sequence tag\n 0x05, // Length of sequence\n 0x06, // ASN.1 object identifier tag\n 0x03, // Length of object identifier\n // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112\n 0x2b, // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)\n 0x65, // thawte(101)\n // Ed25519 identifier\n 0x70, // id-Ed25519(112)\n\n /**\n * Private key payload\n */\n 0x04, // ASN.1 octet string tag\n 0x22, // String length (34 more bytes)\n\n // Private key bytes as octet string\n 0x04, // ASN.1 octet string tag\n 0x20, // String length (32 bytes)\n\n ...bytes\n ]);\n}\n\nexport async function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKey> {\n if (bytes.byteLength !== 32) {\n // TODO: Coded error.\n throw new Error('Private key bytes must be of length 32');\n }\n const privateKeyBytesPkcs8 = addPkcs8Header(bytes);\n return await crypto.subtle.importKey('pkcs8', privateKeyBytesPkcs8, 'Ed25519', extractable ?? false, ['sign']);\n}\n","import { assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';\nimport { Encoder } from '@solana/codecs-core';\nimport { getBase58Encoder } from '@solana/codecs-strings';\n\nexport type Signature = string & { readonly __brand: unique symbol };\nexport type SignatureBytes = Uint8Array & { readonly __brand: unique symbol };\n\nlet base58Encoder: Encoder<string> | undefined;\n\nexport function assertIsSignature(putativeSignature: string): asserts putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n throw new Error('Expected input string to decode to a byte array of length 64.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeSignature}\\` is not a signature`, {\n cause: e,\n });\n }\n}\n\nexport function isSignature(putativeSignature: string): putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n return false;\n }\n return true;\n}\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<SignatureBytes> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as SignatureBytes;\n}\n\nexport function signature(putativeSignature: string): Signature {\n assertIsSignature(putativeSignature);\n return putativeSignature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: SignatureBytes, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
|
package/dist/index.node.cjs
CHANGED
|
@@ -1,85 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
// ../build-scripts/env-shim.ts
|
|
6
|
-
var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
|
|
7
|
-
function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
|
|
8
|
-
try {
|
|
9
|
-
if (
|
|
10
|
-
// Lowest address (32 bytes of zeroes)
|
|
11
|
-
putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
12
|
-
putativeBase58EncodedAddress.length > 44
|
|
13
|
-
) {
|
|
14
|
-
throw new Error("Expected input string to decode to a byte array of length 32.");
|
|
15
|
-
}
|
|
16
|
-
const bytes = umiSerializers.base58.serialize(putativeBase58EncodedAddress);
|
|
17
|
-
const numBytes = bytes.byteLength;
|
|
18
|
-
if (numBytes !== 32) {
|
|
19
|
-
throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
|
|
20
|
-
}
|
|
21
|
-
} catch (e) {
|
|
22
|
-
throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
|
|
23
|
-
cause: e
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function getBase58EncodedAddressCodec(config) {
|
|
28
|
-
return umiSerializers.string({
|
|
29
|
-
description: config?.description ?? (__DEV__ ? "A 32-byte account address" : ""),
|
|
30
|
-
encoding: umiSerializers.base58,
|
|
31
|
-
size: 32
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
function getBase58EncodedAddressComparator() {
|
|
35
|
-
return new Intl.Collator("en", {
|
|
36
|
-
caseFirst: "lower",
|
|
37
|
-
ignorePunctuation: false,
|
|
38
|
-
localeMatcher: "best fit",
|
|
39
|
-
numeric: false,
|
|
40
|
-
sensitivity: "variant",
|
|
41
|
-
usage: "sort"
|
|
42
|
-
}).compare;
|
|
43
|
-
}
|
|
44
|
-
var cachedEd25519Decision;
|
|
45
|
-
async function isEd25519CurveSupported(subtle) {
|
|
46
|
-
if (cachedEd25519Decision === void 0) {
|
|
47
|
-
cachedEd25519Decision = new Promise((resolve) => {
|
|
48
|
-
subtle.generateKey(
|
|
49
|
-
"Ed25519",
|
|
50
|
-
/* extractable */
|
|
51
|
-
false,
|
|
52
|
-
["sign", "verify"]
|
|
53
|
-
).catch(() => {
|
|
54
|
-
resolve(cachedEd25519Decision = false);
|
|
55
|
-
}).then(() => {
|
|
56
|
-
resolve(cachedEd25519Decision = true);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
if (typeof cachedEd25519Decision === "boolean") {
|
|
61
|
-
return cachedEd25519Decision;
|
|
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("This runtime does not support the generation of Ed25519 keypairs");
|
|
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
|
-
}
|
|
3
|
+
var assertions = require('@solana/assertions');
|
|
4
|
+
var codecsStrings = require('@solana/codecs-strings');
|
|
79
5
|
|
|
80
6
|
// src/key-pair.ts
|
|
81
7
|
async function generateKeyPair() {
|
|
82
|
-
await assertKeyGenerationIsAvailable();
|
|
8
|
+
await assertions.assertKeyGenerationIsAvailable();
|
|
83
9
|
const keyPair = await crypto.subtle.generateKey(
|
|
84
10
|
/* algorithm */
|
|
85
11
|
"Ed25519",
|
|
@@ -93,21 +19,120 @@ async function generateKeyPair() {
|
|
|
93
19
|
return keyPair;
|
|
94
20
|
}
|
|
95
21
|
|
|
96
|
-
// src/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
22
|
+
// src/private-key.ts
|
|
23
|
+
function addPkcs8Header(bytes) {
|
|
24
|
+
return new Uint8Array([
|
|
25
|
+
/**
|
|
26
|
+
* PKCS#8 header
|
|
27
|
+
*/
|
|
28
|
+
48,
|
|
29
|
+
// ASN.1 sequence tag
|
|
30
|
+
46,
|
|
31
|
+
// Length of sequence (46 more bytes)
|
|
32
|
+
2,
|
|
33
|
+
// ASN.1 integer tag
|
|
34
|
+
1,
|
|
35
|
+
// Length of integer
|
|
36
|
+
0,
|
|
37
|
+
// Version number
|
|
38
|
+
48,
|
|
39
|
+
// ASN.1 sequence tag
|
|
40
|
+
5,
|
|
41
|
+
// Length of sequence
|
|
42
|
+
6,
|
|
43
|
+
// ASN.1 object identifier tag
|
|
44
|
+
3,
|
|
45
|
+
// Length of object identifier
|
|
46
|
+
// Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
|
|
47
|
+
43,
|
|
48
|
+
// 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)
|
|
49
|
+
101,
|
|
50
|
+
// thawte(101)
|
|
51
|
+
// Ed25519 identifier
|
|
52
|
+
112,
|
|
53
|
+
// id-Ed25519(112)
|
|
54
|
+
/**
|
|
55
|
+
* Private key payload
|
|
56
|
+
*/
|
|
57
|
+
4,
|
|
58
|
+
// ASN.1 octet string tag
|
|
59
|
+
34,
|
|
60
|
+
// String length (34 more bytes)
|
|
61
|
+
// Private key bytes as octet string
|
|
62
|
+
4,
|
|
63
|
+
// ASN.1 octet string tag
|
|
64
|
+
32,
|
|
65
|
+
// String length (32 bytes)
|
|
66
|
+
...bytes
|
|
67
|
+
]);
|
|
68
|
+
}
|
|
69
|
+
async function createPrivateKeyFromBytes(bytes, extractable) {
|
|
70
|
+
if (bytes.byteLength !== 32) {
|
|
71
|
+
throw new Error("Private key bytes must be of length 32");
|
|
72
|
+
}
|
|
73
|
+
const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
|
|
74
|
+
return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
|
|
75
|
+
}
|
|
76
|
+
var base58Encoder;
|
|
77
|
+
function assertIsSignature(putativeSignature) {
|
|
78
|
+
if (!base58Encoder)
|
|
79
|
+
base58Encoder = codecsStrings.getBase58Encoder();
|
|
80
|
+
try {
|
|
81
|
+
if (
|
|
82
|
+
// Lowest value (64 bytes of zeroes)
|
|
83
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
84
|
+
putativeSignature.length > 88
|
|
85
|
+
) {
|
|
86
|
+
throw new Error("Expected input string to decode to a byte array of length 64.");
|
|
87
|
+
}
|
|
88
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
89
|
+
const numBytes = bytes.byteLength;
|
|
90
|
+
if (numBytes !== 64) {
|
|
91
|
+
throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
|
|
92
|
+
}
|
|
93
|
+
} catch (e) {
|
|
94
|
+
throw new Error(`\`${putativeSignature}\` is not a signature`, {
|
|
95
|
+
cause: e
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function isSignature(putativeSignature) {
|
|
100
|
+
if (!base58Encoder)
|
|
101
|
+
base58Encoder = codecsStrings.getBase58Encoder();
|
|
102
|
+
if (
|
|
103
|
+
// Lowest value (64 bytes of zeroes)
|
|
104
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
105
|
+
putativeSignature.length > 88
|
|
106
|
+
) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
110
|
+
const numBytes = bytes.byteLength;
|
|
111
|
+
if (numBytes !== 64) {
|
|
112
|
+
return false;
|
|
101
113
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
async function signBytes(key, data) {
|
|
117
|
+
await assertions.assertSigningCapabilityIsAvailable();
|
|
118
|
+
const signedData = await crypto.subtle.sign("Ed25519", key, data);
|
|
119
|
+
return new Uint8Array(signedData);
|
|
120
|
+
}
|
|
121
|
+
function signature(putativeSignature) {
|
|
122
|
+
assertIsSignature(putativeSignature);
|
|
123
|
+
return putativeSignature;
|
|
124
|
+
}
|
|
125
|
+
async function verifySignature(key, signature2, data) {
|
|
126
|
+
await assertions.assertVerificationCapabilityIsAvailable();
|
|
127
|
+
return await crypto.subtle.verify("Ed25519", key, signature2, data);
|
|
105
128
|
}
|
|
106
129
|
|
|
107
|
-
exports.
|
|
130
|
+
exports.assertIsSignature = assertIsSignature;
|
|
131
|
+
exports.createPrivateKeyFromBytes = createPrivateKeyFromBytes;
|
|
108
132
|
exports.generateKeyPair = generateKeyPair;
|
|
109
|
-
exports.
|
|
110
|
-
exports.
|
|
111
|
-
exports.
|
|
133
|
+
exports.isSignature = isSignature;
|
|
134
|
+
exports.signBytes = signBytes;
|
|
135
|
+
exports.signature = signature;
|
|
136
|
+
exports.verifySignature = verifySignature;
|
|
112
137
|
//# sourceMappingURL=out.js.map
|
|
113
138
|
//# sourceMappingURL=index.node.cjs.map
|
package/dist/index.node.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["signature"],"mappings":";AAAA,SAAS,sCAAsC;AAE/C,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;;;ACVA,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;AAE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S,UAAU,mBAAsC;AAC5D,oBAAkB,iBAAiB;AACnC,SAAO;AACX;AAEA,eAAsB,gBAAgB,KAAgBA,YAA2B,MAAoC;AACjH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAKA,YAAW,IAAI;AACrE","sourcesContent":["import { assertKeyGenerationIsAvailable } from '@solana/assertions';\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","function addPkcs8Header(bytes: Uint8Array): Uint8Array {\n // prettier-ignore\n return new Uint8Array([\n /**\n * PKCS#8 header\n */\n 0x30, // ASN.1 sequence tag\n 0x2e, // Length of sequence (46 more bytes)\n\n 0x02, // ASN.1 integer tag\n 0x01, // Length of integer\n 0x00, // Version number\n\n 0x30, // ASN.1 sequence tag\n 0x05, // Length of sequence\n 0x06, // ASN.1 object identifier tag\n 0x03, // Length of object identifier\n // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112\n 0x2b, // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)\n 0x65, // thawte(101)\n // Ed25519 identifier\n 0x70, // id-Ed25519(112)\n\n /**\n * Private key payload\n */\n 0x04, // ASN.1 octet string tag\n 0x22, // String length (34 more bytes)\n\n // Private key bytes as octet string\n 0x04, // ASN.1 octet string tag\n 0x20, // String length (32 bytes)\n\n ...bytes\n ]);\n}\n\nexport async function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKey> {\n if (bytes.byteLength !== 32) {\n // TODO: Coded error.\n throw new Error('Private key bytes must be of length 32');\n }\n const privateKeyBytesPkcs8 = addPkcs8Header(bytes);\n return await crypto.subtle.importKey('pkcs8', privateKeyBytesPkcs8, 'Ed25519', extractable ?? false, ['sign']);\n}\n","import { assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';\nimport { Encoder } from '@solana/codecs-core';\nimport { getBase58Encoder } from '@solana/codecs-strings';\n\nexport type Signature = string & { readonly __brand: unique symbol };\nexport type SignatureBytes = Uint8Array & { readonly __brand: unique symbol };\n\nlet base58Encoder: Encoder<string> | undefined;\n\nexport function assertIsSignature(putativeSignature: string): asserts putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n throw new Error('Expected input string to decode to a byte array of length 64.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeSignature}\\` is not a signature`, {\n cause: e,\n });\n }\n}\n\nexport function isSignature(putativeSignature: string): putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n return false;\n }\n return true;\n}\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<SignatureBytes> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as SignatureBytes;\n}\n\nexport function signature(putativeSignature: string): Signature {\n assertIsSignature(putativeSignature);\n return putativeSignature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: SignatureBytes, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
|