@solana/keys 2.0.0-experimental.3e9cf67 → 2.0.0-experimental.3fc662d
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/README.md +16 -0
- package/dist/index.browser.cjs +56 -34
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +54 -35
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +54 -35
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +56 -34
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +54 -35
- package/dist/index.node.js.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.map +1 -1
- package/dist/types/signatures.d.ts.map +1 -1
- package/package.json +14 -34
- package/dist/index.development.js +0 -262
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -21
package/dist/index.node.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["signature"],"mappings":";AAAA,SAAS,sCAAsC;
|
|
1
|
+
{"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["SolanaError","signature"],"mappings":"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","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"]}
|
package/dist/index.node.js
CHANGED
|
@@ -1,23 +1,8 @@
|
|
|
1
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';
|
|
2
3
|
import { getBase58Encoder } from '@solana/codecs-strings';
|
|
3
4
|
|
|
4
5
|
// src/key-pair.ts
|
|
5
|
-
async function generateKeyPair() {
|
|
6
|
-
await assertKeyGenerationIsAvailable();
|
|
7
|
-
const keyPair = await crypto.subtle.generateKey(
|
|
8
|
-
/* algorithm */
|
|
9
|
-
"Ed25519",
|
|
10
|
-
// Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
|
|
11
|
-
/* extractable */
|
|
12
|
-
false,
|
|
13
|
-
// Prevents the bytes of the private key from being visible to JS.
|
|
14
|
-
/* allowed uses */
|
|
15
|
-
["sign", "verify"]
|
|
16
|
-
);
|
|
17
|
-
return keyPair;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// src/private-key.ts
|
|
21
6
|
function addPkcs8Header(bytes) {
|
|
22
7
|
return new Uint8Array([
|
|
23
8
|
/**
|
|
@@ -65,32 +50,66 @@ function addPkcs8Header(bytes) {
|
|
|
65
50
|
]);
|
|
66
51
|
}
|
|
67
52
|
async function createPrivateKeyFromBytes(bytes, extractable) {
|
|
68
|
-
|
|
69
|
-
|
|
53
|
+
const actualLength = bytes.byteLength;
|
|
54
|
+
if (actualLength !== 32) {
|
|
55
|
+
throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
|
|
56
|
+
actualLength
|
|
57
|
+
});
|
|
70
58
|
}
|
|
71
59
|
const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
|
|
72
60
|
return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
|
|
73
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
|
+
}
|
|
74
95
|
var base58Encoder;
|
|
75
96
|
function assertIsSignature(putativeSignature) {
|
|
76
97
|
if (!base58Encoder)
|
|
77
98
|
base58Encoder = getBase58Encoder();
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
throw new Error(`\`${putativeSignature}\` is not a signature`, {
|
|
93
|
-
cause: e
|
|
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
|
|
94
113
|
});
|
|
95
114
|
}
|
|
96
115
|
}
|
|
@@ -125,6 +144,6 @@ async function verifySignature(key, signature2, data) {
|
|
|
125
144
|
return await crypto.subtle.verify("Ed25519", key, signature2, data);
|
|
126
145
|
}
|
|
127
146
|
|
|
128
|
-
export { assertIsSignature, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
|
|
147
|
+
export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
|
|
129
148
|
//# sourceMappingURL=out.js.map
|
|
130
149
|
//# sourceMappingURL=index.node.js.map
|
package/dist/index.node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/key-pair.ts","../src/private-key.ts","../src/signatures.ts"],"names":["signature"],"mappings":";AAAA,SAAS,sCAAsC;
|
|
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,YAAY,MAAM,WAAW,CAAC;AAAA,EAC5G;AACA,QAAM,CAAC,WAAW,UAAU,IAAI,MAAM,QAAQ,IAAI;AAAA,IAC9C,OAAO,OAAO;AAAA,MAAU;AAAA,MAAO,MAAM,MAAM,EAAE;AAAA,MAAG;AAAA;AAAA,MAA6B;AAAA,MAAM,CAAC,QAAQ;AAAA,IAAC;AAAA,IAC7F,0BAA0B,MAAM,MAAM,GAAG,EAAE,GAAG,WAAW;AAAA,EAC7D,CAAC;AACD,SAAO,EAAE,YAAY,UAAU;AACnC;;;AExBA,SAAS,oCAAoC,+CAA+C;AAE5F,SAAS,wBAAwB;AACjC;AAAA,EACI;AAAA,EACA;AAAA,EACA,eAAAA;AAAA,OACG;AAKP,IAAI;AAEG,SAAS,kBAAkB,mBAAmE;AACjG,MAAI,CAAC;AAAe,oBAAgB,iBAAiB;AAErD;AAAA;AAAA,IAEI,kBAAkB,SAAS;AAAA,IAE3B,kBAAkB,SAAS;AAAA,IAC7B;AACE,UAAM,IAAIA,aAAY,0DAA0D;AAAA,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"]}
|
package/dist/types/key-pair.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-pair.d.ts","sourceRoot":"","sources":["../../src/key-pair.ts"],"names":[],"mappings":"
|
|
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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"private-key.d.ts","sourceRoot":"","sources":["../../src/private-key.ts"],"names":[],"mappings":"
|
|
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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signatures.d.ts","sourceRoot":"","sources":["../../src/signatures.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"signatures.d.ts","sourceRoot":"","sources":["../../src/signatures.ts"],"names":[],"mappings":"AASA,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,MAAM,CAAA;CAAE,CAAC;AACrE,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,MAAM,CAAA;CAAE,CAAC;AAI9E,wBAAgB,iBAAiB,CAAC,iBAAiB,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,IAAI,SAAS,CAqBnG;AAED,wBAAgB,WAAW,CAAC,iBAAiB,EAAE,MAAM,GAAG,iBAAiB,IAAI,SAAS,CAmBrF;AAED,wBAAsB,SAAS,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,cAAc,CAAC,CAIzF;AAED,wBAAgB,SAAS,CAAC,iBAAiB,EAAE,MAAM,GAAG,SAAS,CAG9D;AAED,wBAAsB,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,cAAc,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAGnH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana/keys",
|
|
3
|
-
"version": "2.0.0-experimental.
|
|
3
|
+
"version": "2.0.0-experimental.3fc662d",
|
|
4
4
|
"description": "Helpers for generating and transforming key material",
|
|
5
5
|
"exports": {
|
|
6
6
|
"browser": {
|
|
@@ -49,31 +49,10 @@
|
|
|
49
49
|
"node": ">=17.4"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@solana/
|
|
53
|
-
"@solana/codecs-
|
|
54
|
-
"@solana/
|
|
55
|
-
|
|
56
|
-
"devDependencies": {
|
|
57
|
-
"@solana/eslint-config-solana": "^1.0.2",
|
|
58
|
-
"@swc/jest": "^0.2.29",
|
|
59
|
-
"@types/jest": "^29.5.11",
|
|
60
|
-
"@typescript-eslint/eslint-plugin": "^6.13.2",
|
|
61
|
-
"@typescript-eslint/parser": "^6.3.0",
|
|
62
|
-
"agadoo": "^3.0.0",
|
|
63
|
-
"eslint": "^8.45.0",
|
|
64
|
-
"eslint-plugin-jest": "^27.4.2",
|
|
65
|
-
"eslint-plugin-sort-keys-fix": "^1.1.2",
|
|
66
|
-
"jest": "^29.7.0",
|
|
67
|
-
"jest-environment-jsdom": "^29.7.0",
|
|
68
|
-
"jest-runner-eslint": "^2.1.2",
|
|
69
|
-
"jest-runner-prettier": "^1.0.0",
|
|
70
|
-
"prettier": "^3.1",
|
|
71
|
-
"tsup": "^8.0.1",
|
|
72
|
-
"typescript": "^5.2.2",
|
|
73
|
-
"version-from-git": "^1.1.1",
|
|
74
|
-
"build-scripts": "0.0.0",
|
|
75
|
-
"test-config": "0.0.0",
|
|
76
|
-
"tsconfig": "0.0.0"
|
|
52
|
+
"@solana/codecs-core": "2.0.0-experimental.3fc662d",
|
|
53
|
+
"@solana/codecs-strings": "2.0.0-experimental.3fc662d",
|
|
54
|
+
"@solana/errors": "2.0.0-experimental.3fc662d",
|
|
55
|
+
"@solana/assertions": "2.0.0-experimental.3fc662d"
|
|
77
56
|
},
|
|
78
57
|
"bundlewatch": {
|
|
79
58
|
"defaultCompression": "gzip",
|
|
@@ -84,18 +63,19 @@
|
|
|
84
63
|
]
|
|
85
64
|
},
|
|
86
65
|
"scripts": {
|
|
87
|
-
"compile:js": "tsup --config build-scripts/tsup.config.
|
|
88
|
-
"compile:typedefs": "tsc -p ./tsconfig.declarations.json && node node_modules/build-scripts/add-js-extension-to-types.mjs",
|
|
89
|
-
"dev": "jest -c node_modules/test-config/jest-dev.config.ts --rootDir . --watch",
|
|
90
|
-
"publish-
|
|
66
|
+
"compile:js": "tsup --config build-scripts/tsup.config.package.ts",
|
|
67
|
+
"compile:typedefs": "tsc -p ./tsconfig.declarations.json && node node_modules/@solana/build-scripts/add-js-extension-to-types.mjs",
|
|
68
|
+
"dev": "jest -c node_modules/@solana/test-config/jest-dev.config.ts --rootDir . --watch",
|
|
69
|
+
"publish-impl": "npm view $npm_package_name@$npm_package_version > /dev/null 2>&1 || pnpm publish --tag experimental --access public --no-git-checks",
|
|
70
|
+
"publish-packages": "pnpm prepublishOnly && pnpm publish-impl",
|
|
91
71
|
"style:fix": "pnpm eslint --fix src/* && pnpm prettier -w src/* package.json",
|
|
92
|
-
"test:lint": "jest -c node_modules/test-config/jest-lint.config.ts --rootDir . --silent",
|
|
93
|
-
"test:prettier": "jest -c node_modules/test-config/jest-prettier.config.ts --rootDir . --silent",
|
|
72
|
+
"test:lint": "jest -c node_modules/@solana/test-config/jest-lint.config.ts --rootDir . --silent",
|
|
73
|
+
"test:prettier": "jest -c node_modules/@solana/test-config/jest-prettier.config.ts --rootDir . --silent",
|
|
94
74
|
"test:treeshakability:browser": "agadoo dist/index.browser.js",
|
|
95
75
|
"test:treeshakability:native": "agadoo dist/index.native.js",
|
|
96
76
|
"test:treeshakability:node": "agadoo dist/index.node.js",
|
|
97
77
|
"test:typecheck": "tsc --noEmit",
|
|
98
|
-
"test:unit:browser": "jest -c node_modules/test-config/jest-unit.config.browser.ts --rootDir . --silent",
|
|
99
|
-
"test:unit:node": "jest -c node_modules/test-config/jest-unit.config.node.ts --rootDir . --silent"
|
|
78
|
+
"test:unit:browser": "jest -c node_modules/@solana/test-config/jest-unit.config.browser.ts --rootDir . --silent",
|
|
79
|
+
"test:unit:node": "jest -c node_modules/@solana/test-config/jest-unit.config.node.ts --rootDir . --silent"
|
|
100
80
|
}
|
|
101
81
|
}
|
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
this.globalThis = this.globalThis || {};
|
|
2
|
-
this.globalThis.solanaWeb3 = (function (exports) {
|
|
3
|
-
'use strict';
|
|
4
|
-
|
|
5
|
-
// ../assertions/dist/index.browser.js
|
|
6
|
-
function assertIsSecureContext() {
|
|
7
|
-
if (!globalThis.isSecureContext) {
|
|
8
|
-
throw new Error(
|
|
9
|
-
"Cryptographic operations are only allowed in secure browser contexts. Read more here: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts"
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
var cachedEd25519Decision;
|
|
14
|
-
async function isEd25519CurveSupported(subtle) {
|
|
15
|
-
if (cachedEd25519Decision === void 0) {
|
|
16
|
-
cachedEd25519Decision = new Promise((resolve) => {
|
|
17
|
-
subtle.generateKey(
|
|
18
|
-
"Ed25519",
|
|
19
|
-
/* extractable */
|
|
20
|
-
false,
|
|
21
|
-
["sign", "verify"]
|
|
22
|
-
).catch(() => {
|
|
23
|
-
resolve(cachedEd25519Decision = false);
|
|
24
|
-
}).then(() => {
|
|
25
|
-
resolve(cachedEd25519Decision = true);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
if (typeof cachedEd25519Decision === "boolean") {
|
|
30
|
-
return cachedEd25519Decision;
|
|
31
|
-
} else {
|
|
32
|
-
return await cachedEd25519Decision;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
async function assertKeyGenerationIsAvailable() {
|
|
36
|
-
var _a;
|
|
37
|
-
assertIsSecureContext();
|
|
38
|
-
if (typeof globalThis.crypto === "undefined" || typeof ((_a = globalThis.crypto.subtle) == null ? void 0 : _a.generateKey) !== "function") {
|
|
39
|
-
throw new Error("No key generation implementation could be found");
|
|
40
|
-
}
|
|
41
|
-
if (!await isEd25519CurveSupported(globalThis.crypto.subtle)) {
|
|
42
|
-
throw new Error(
|
|
43
|
-
"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"
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
async function assertSigningCapabilityIsAvailable() {
|
|
48
|
-
var _a;
|
|
49
|
-
assertIsSecureContext();
|
|
50
|
-
if (typeof globalThis.crypto === "undefined" || typeof ((_a = globalThis.crypto.subtle) == null ? void 0 : _a.sign) !== "function") {
|
|
51
|
-
throw new Error("No signing implementation could be found");
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async function assertVerificationCapabilityIsAvailable() {
|
|
55
|
-
var _a;
|
|
56
|
-
assertIsSecureContext();
|
|
57
|
-
if (typeof globalThis.crypto === "undefined" || typeof ((_a = globalThis.crypto.subtle) == null ? void 0 : _a.verify) !== "function") {
|
|
58
|
-
throw new Error("No signature verification implementation could be found");
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// src/key-pair.ts
|
|
63
|
-
async function generateKeyPair() {
|
|
64
|
-
await assertKeyGenerationIsAvailable();
|
|
65
|
-
const keyPair = await crypto.subtle.generateKey(
|
|
66
|
-
/* algorithm */
|
|
67
|
-
"Ed25519",
|
|
68
|
-
// Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
|
|
69
|
-
/* extractable */
|
|
70
|
-
false,
|
|
71
|
-
// Prevents the bytes of the private key from being visible to JS.
|
|
72
|
-
/* allowed uses */
|
|
73
|
-
["sign", "verify"]
|
|
74
|
-
);
|
|
75
|
-
return keyPair;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// src/private-key.ts
|
|
79
|
-
function addPkcs8Header(bytes) {
|
|
80
|
-
return new Uint8Array([
|
|
81
|
-
/**
|
|
82
|
-
* PKCS#8 header
|
|
83
|
-
*/
|
|
84
|
-
48,
|
|
85
|
-
// ASN.1 sequence tag
|
|
86
|
-
46,
|
|
87
|
-
// Length of sequence (46 more bytes)
|
|
88
|
-
2,
|
|
89
|
-
// ASN.1 integer tag
|
|
90
|
-
1,
|
|
91
|
-
// Length of integer
|
|
92
|
-
0,
|
|
93
|
-
// Version number
|
|
94
|
-
48,
|
|
95
|
-
// ASN.1 sequence tag
|
|
96
|
-
5,
|
|
97
|
-
// Length of sequence
|
|
98
|
-
6,
|
|
99
|
-
// ASN.1 object identifier tag
|
|
100
|
-
3,
|
|
101
|
-
// Length of object identifier
|
|
102
|
-
// Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
|
|
103
|
-
43,
|
|
104
|
-
// 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)
|
|
105
|
-
101,
|
|
106
|
-
// thawte(101)
|
|
107
|
-
// Ed25519 identifier
|
|
108
|
-
112,
|
|
109
|
-
// id-Ed25519(112)
|
|
110
|
-
/**
|
|
111
|
-
* Private key payload
|
|
112
|
-
*/
|
|
113
|
-
4,
|
|
114
|
-
// ASN.1 octet string tag
|
|
115
|
-
34,
|
|
116
|
-
// String length (34 more bytes)
|
|
117
|
-
// Private key bytes as octet string
|
|
118
|
-
4,
|
|
119
|
-
// ASN.1 octet string tag
|
|
120
|
-
32,
|
|
121
|
-
// String length (32 bytes)
|
|
122
|
-
...bytes
|
|
123
|
-
]);
|
|
124
|
-
}
|
|
125
|
-
async function createPrivateKeyFromBytes(bytes, extractable) {
|
|
126
|
-
if (bytes.byteLength !== 32) {
|
|
127
|
-
throw new Error("Private key bytes must be of length 32");
|
|
128
|
-
}
|
|
129
|
-
const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
|
|
130
|
-
return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable != null ? extractable : false, ["sign"]);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// ../codecs-core/dist/index.browser.js
|
|
134
|
-
function getEncodedSize(value, encoder) {
|
|
135
|
-
return "fixedSize" in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value);
|
|
136
|
-
}
|
|
137
|
-
function createEncoder(encoder) {
|
|
138
|
-
return Object.freeze({
|
|
139
|
-
...encoder,
|
|
140
|
-
encode: (value) => {
|
|
141
|
-
const bytes = new Uint8Array(getEncodedSize(value, encoder));
|
|
142
|
-
encoder.write(value, bytes, 0);
|
|
143
|
-
return bytes;
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// ../codecs-strings/dist/index.browser.js
|
|
149
|
-
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
|
|
150
|
-
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
|
|
151
|
-
throw new Error(`Expected a string of base ${alphabet4.length}, got [${givenValue}].`);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
var getBaseXEncoder = (alphabet4) => {
|
|
155
|
-
return createEncoder({
|
|
156
|
-
getSizeFromValue: (value) => {
|
|
157
|
-
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
|
|
158
|
-
if (tailChars === "")
|
|
159
|
-
return value.length;
|
|
160
|
-
const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
|
|
161
|
-
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
|
|
162
|
-
},
|
|
163
|
-
write(value, bytes, offset) {
|
|
164
|
-
assertValidBaseString(alphabet4, value);
|
|
165
|
-
if (value === "")
|
|
166
|
-
return offset;
|
|
167
|
-
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
|
|
168
|
-
if (tailChars === "") {
|
|
169
|
-
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
|
|
170
|
-
return offset + leadingZeroes.length;
|
|
171
|
-
}
|
|
172
|
-
let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
|
|
173
|
-
const tailBytes = [];
|
|
174
|
-
while (base10Number > 0n) {
|
|
175
|
-
tailBytes.unshift(Number(base10Number % 256n));
|
|
176
|
-
base10Number /= 256n;
|
|
177
|
-
}
|
|
178
|
-
const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
|
|
179
|
-
bytes.set(bytesToAdd, offset);
|
|
180
|
-
return offset + bytesToAdd.length;
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
};
|
|
184
|
-
function partitionLeadingZeroes(value, zeroCharacter) {
|
|
185
|
-
const leadingZeroIndex = [...value].findIndex((c) => c !== zeroCharacter);
|
|
186
|
-
return leadingZeroIndex === -1 ? [value, ""] : [value.slice(0, leadingZeroIndex), value.slice(leadingZeroIndex)];
|
|
187
|
-
}
|
|
188
|
-
function getBigIntFromBaseX(value, alphabet4) {
|
|
189
|
-
const base = BigInt(alphabet4.length);
|
|
190
|
-
return [...value].reduce((sum, char) => sum * base + BigInt(alphabet4.indexOf(char)), 0n);
|
|
191
|
-
}
|
|
192
|
-
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
193
|
-
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
|
|
194
|
-
|
|
195
|
-
// src/signatures.ts
|
|
196
|
-
var base58Encoder;
|
|
197
|
-
function assertIsSignature(putativeSignature) {
|
|
198
|
-
if (!base58Encoder)
|
|
199
|
-
base58Encoder = getBase58Encoder();
|
|
200
|
-
try {
|
|
201
|
-
if (
|
|
202
|
-
// Lowest value (64 bytes of zeroes)
|
|
203
|
-
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
204
|
-
putativeSignature.length > 88
|
|
205
|
-
) {
|
|
206
|
-
throw new Error("Expected input string to decode to a byte array of length 64.");
|
|
207
|
-
}
|
|
208
|
-
const bytes = base58Encoder.encode(putativeSignature);
|
|
209
|
-
const numBytes = bytes.byteLength;
|
|
210
|
-
if (numBytes !== 64) {
|
|
211
|
-
throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
|
|
212
|
-
}
|
|
213
|
-
} catch (e2) {
|
|
214
|
-
throw new Error(`\`${putativeSignature}\` is not a signature`, {
|
|
215
|
-
cause: e2
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
function isSignature(putativeSignature) {
|
|
220
|
-
if (!base58Encoder)
|
|
221
|
-
base58Encoder = getBase58Encoder();
|
|
222
|
-
if (
|
|
223
|
-
// Lowest value (64 bytes of zeroes)
|
|
224
|
-
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
225
|
-
putativeSignature.length > 88
|
|
226
|
-
) {
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
const bytes = base58Encoder.encode(putativeSignature);
|
|
230
|
-
const numBytes = bytes.byteLength;
|
|
231
|
-
if (numBytes !== 64) {
|
|
232
|
-
return false;
|
|
233
|
-
}
|
|
234
|
-
return true;
|
|
235
|
-
}
|
|
236
|
-
async function signBytes(key, data) {
|
|
237
|
-
await assertSigningCapabilityIsAvailable();
|
|
238
|
-
const signedData = await crypto.subtle.sign("Ed25519", key, data);
|
|
239
|
-
return new Uint8Array(signedData);
|
|
240
|
-
}
|
|
241
|
-
function signature(putativeSignature) {
|
|
242
|
-
assertIsSignature(putativeSignature);
|
|
243
|
-
return putativeSignature;
|
|
244
|
-
}
|
|
245
|
-
async function verifySignature(key, signature2, data) {
|
|
246
|
-
await assertVerificationCapabilityIsAvailable();
|
|
247
|
-
return await crypto.subtle.verify("Ed25519", key, signature2, data);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
exports.assertIsSignature = assertIsSignature;
|
|
251
|
-
exports.createPrivateKeyFromBytes = createPrivateKeyFromBytes;
|
|
252
|
-
exports.generateKeyPair = generateKeyPair;
|
|
253
|
-
exports.isSignature = isSignature;
|
|
254
|
-
exports.signBytes = signBytes;
|
|
255
|
-
exports.signature = signature;
|
|
256
|
-
exports.verifySignature = verifySignature;
|
|
257
|
-
|
|
258
|
-
return exports;
|
|
259
|
-
|
|
260
|
-
})({});
|
|
261
|
-
//# sourceMappingURL=out.js.map
|
|
262
|
-
//# sourceMappingURL=index.development.js.map
|