@solana/keys 2.0.0-experimental.0de6c3e → 2.0.0-experimental.0eb69ae
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 +43 -23
- package/dist/index.browser.cjs +52 -121
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +47 -115
- package/dist/index.browser.js.map +1 -1
- package/dist/index.development.js +95 -327
- package/dist/index.development.js.map +1 -1
- package/dist/index.native.js +47 -102
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +52 -108
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +47 -102
- package/dist/index.node.js.map +1 -1
- package/dist/index.production.min.js +8 -9
- package/dist/types/index.d.ts +0 -2
- package/dist/types/signatures.d.ts +10 -4
- package/package.json +18 -19
- package/dist/types/base58.d.ts +0 -10
- package/dist/types/guard.d.ts +0 -5
- package/dist/types/pubkey.d.ts +0 -3
package/dist/index.native.js
CHANGED
|
@@ -1,91 +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(
|
|
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
|
-
}
|
|
88
|
-
}
|
|
1
|
+
import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
|
|
2
|
+
import { getBase58Encoder } from '@solana/codecs-strings';
|
|
89
3
|
|
|
90
4
|
// src/key-pair.ts
|
|
91
5
|
async function generateKeyPair() {
|
|
@@ -102,29 +16,60 @@ async function generateKeyPair() {
|
|
|
102
16
|
);
|
|
103
17
|
return keyPair;
|
|
104
18
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
19
|
+
var base58Encoder;
|
|
20
|
+
function assertIsSignature(putativeSignature) {
|
|
21
|
+
if (!base58Encoder)
|
|
22
|
+
base58Encoder = getBase58Encoder();
|
|
23
|
+
try {
|
|
24
|
+
if (
|
|
25
|
+
// Lowest value (64 bytes of zeroes)
|
|
26
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
27
|
+
putativeSignature.length > 88
|
|
28
|
+
) {
|
|
29
|
+
throw new Error("Expected input string to decode to a byte array of length 64.");
|
|
30
|
+
}
|
|
31
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
32
|
+
const numBytes = bytes.byteLength;
|
|
33
|
+
if (numBytes !== 64) {
|
|
34
|
+
throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
|
|
35
|
+
}
|
|
36
|
+
} catch (e) {
|
|
37
|
+
throw new Error(`\`${putativeSignature}\` is not a signature`, {
|
|
38
|
+
cause: e
|
|
39
|
+
});
|
|
111
40
|
}
|
|
112
|
-
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
113
|
-
const [base58EncodedAddress] = getBase58EncodedAddressCodec().deserialize(new Uint8Array(publicKeyBytes));
|
|
114
|
-
return base58EncodedAddress;
|
|
115
41
|
}
|
|
116
|
-
|
|
117
|
-
|
|
42
|
+
function isSignature(putativeSignature) {
|
|
43
|
+
if (!base58Encoder)
|
|
44
|
+
base58Encoder = getBase58Encoder();
|
|
45
|
+
if (
|
|
46
|
+
// Lowest value (64 bytes of zeroes)
|
|
47
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
48
|
+
putativeSignature.length > 88
|
|
49
|
+
) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
53
|
+
const numBytes = bytes.byteLength;
|
|
54
|
+
if (numBytes !== 64) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
118
59
|
async function signBytes(key, data) {
|
|
119
60
|
await assertSigningCapabilityIsAvailable();
|
|
120
61
|
const signedData = await crypto.subtle.sign("Ed25519", key, data);
|
|
121
62
|
return new Uint8Array(signedData);
|
|
122
63
|
}
|
|
123
|
-
|
|
64
|
+
function signature(putativeSignature) {
|
|
65
|
+
assertIsSignature(putativeSignature);
|
|
66
|
+
return putativeSignature;
|
|
67
|
+
}
|
|
68
|
+
async function verifySignature(key, signature2, data) {
|
|
124
69
|
await assertVerificationCapabilityIsAvailable();
|
|
125
|
-
return await crypto.subtle.verify("Ed25519", key,
|
|
70
|
+
return await crypto.subtle.verify("Ed25519", key, signature2, data);
|
|
126
71
|
}
|
|
127
72
|
|
|
128
|
-
export {
|
|
73
|
+
export { assertIsSignature, generateKeyPair, isSignature, signBytes, signature, verifySignature };
|
|
129
74
|
//# sourceMappingURL=out.js.map
|
|
130
75
|
//# 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/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,oCAAoC,+CAA+C;AAE5F,SAAS,wBAAwB;AAKjC,IAAI;AAEG,SAAS,kBAAkB,mBAAmE;AACjG,MAAI,CAAC;AAAe,oBAAgB,iBAAiB;AAErD,MAAI;AAEA;AAAA;AAAA,MAEI,kBAAkB,SAAS;AAAA,MAE3B,kBAAkB,SAAS;AAAA,MAC7B;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,QAAQ,EAAE;AAAA,IAC9G;AAAA,EACJ,SAAS,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","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,97 +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(
|
|
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
|
-
}
|
|
90
|
-
}
|
|
3
|
+
var assertions = require('@solana/assertions');
|
|
4
|
+
var codecsStrings = require('@solana/codecs-strings');
|
|
91
5
|
|
|
92
6
|
// src/key-pair.ts
|
|
93
7
|
async function generateKeyPair() {
|
|
94
|
-
await assertKeyGenerationIsAvailable();
|
|
8
|
+
await assertions.assertKeyGenerationIsAvailable();
|
|
95
9
|
const keyPair = await crypto.subtle.generateKey(
|
|
96
10
|
/* algorithm */
|
|
97
11
|
"Ed25519",
|
|
@@ -104,35 +18,65 @@ async function generateKeyPair() {
|
|
|
104
18
|
);
|
|
105
19
|
return keyPair;
|
|
106
20
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
21
|
+
var base58Encoder;
|
|
22
|
+
function assertIsSignature(putativeSignature) {
|
|
23
|
+
if (!base58Encoder)
|
|
24
|
+
base58Encoder = codecsStrings.getBase58Encoder();
|
|
25
|
+
try {
|
|
26
|
+
if (
|
|
27
|
+
// Lowest value (64 bytes of zeroes)
|
|
28
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
29
|
+
putativeSignature.length > 88
|
|
30
|
+
) {
|
|
31
|
+
throw new Error("Expected input string to decode to a byte array of length 64.");
|
|
32
|
+
}
|
|
33
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
34
|
+
const numBytes = bytes.byteLength;
|
|
35
|
+
if (numBytes !== 64) {
|
|
36
|
+
throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
|
|
37
|
+
}
|
|
38
|
+
} catch (e) {
|
|
39
|
+
throw new Error(`\`${putativeSignature}\` is not a signature`, {
|
|
40
|
+
cause: e
|
|
41
|
+
});
|
|
113
42
|
}
|
|
114
|
-
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
115
|
-
const [base58EncodedAddress] = getBase58EncodedAddressCodec().deserialize(new Uint8Array(publicKeyBytes));
|
|
116
|
-
return base58EncodedAddress;
|
|
117
43
|
}
|
|
118
|
-
|
|
119
|
-
|
|
44
|
+
function isSignature(putativeSignature) {
|
|
45
|
+
if (!base58Encoder)
|
|
46
|
+
base58Encoder = codecsStrings.getBase58Encoder();
|
|
47
|
+
if (
|
|
48
|
+
// Lowest value (64 bytes of zeroes)
|
|
49
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
50
|
+
putativeSignature.length > 88
|
|
51
|
+
) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
55
|
+
const numBytes = bytes.byteLength;
|
|
56
|
+
if (numBytes !== 64) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
120
61
|
async function signBytes(key, data) {
|
|
121
|
-
await assertSigningCapabilityIsAvailable();
|
|
62
|
+
await assertions.assertSigningCapabilityIsAvailable();
|
|
122
63
|
const signedData = await crypto.subtle.sign("Ed25519", key, data);
|
|
123
64
|
return new Uint8Array(signedData);
|
|
124
65
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return
|
|
66
|
+
function signature(putativeSignature) {
|
|
67
|
+
assertIsSignature(putativeSignature);
|
|
68
|
+
return putativeSignature;
|
|
69
|
+
}
|
|
70
|
+
async function verifySignature(key, signature2, data) {
|
|
71
|
+
await assertions.assertVerificationCapabilityIsAvailable();
|
|
72
|
+
return await crypto.subtle.verify("Ed25519", key, signature2, data);
|
|
128
73
|
}
|
|
129
74
|
|
|
130
|
-
exports.
|
|
75
|
+
exports.assertIsSignature = assertIsSignature;
|
|
131
76
|
exports.generateKeyPair = generateKeyPair;
|
|
132
|
-
exports.
|
|
133
|
-
exports.getBase58EncodedAddressComparator = getBase58EncodedAddressComparator;
|
|
134
|
-
exports.getBase58EncodedAddressFromPublicKey = getBase58EncodedAddressFromPublicKey;
|
|
77
|
+
exports.isSignature = isSignature;
|
|
135
78
|
exports.signBytes = signBytes;
|
|
79
|
+
exports.signature = signature;
|
|
136
80
|
exports.verifySignature = verifySignature;
|
|
137
81
|
//# sourceMappingURL=out.js.map
|
|
138
82
|
//# 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/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,oCAAoC,+CAA+C;AAE5F,SAAS,wBAAwB;AAKjC,IAAI;AAEG,SAAS,kBAAkB,mBAAmE;AACjG,MAAI,CAAC;AAAe,oBAAgB,iBAAiB;AAErD,MAAI;AAEA;AAAA;AAAA,MAEI,kBAAkB,SAAS;AAAA,MAE3B,kBAAkB,SAAS;AAAA,MAC7B;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,QAAQ,EAAE;AAAA,IAC9G;AAAA,EACJ,SAAS,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","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.js
CHANGED
|
@@ -1,91 +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(
|
|
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
|
-
}
|
|
88
|
-
}
|
|
1
|
+
import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
|
|
2
|
+
import { getBase58Encoder } from '@solana/codecs-strings';
|
|
89
3
|
|
|
90
4
|
// src/key-pair.ts
|
|
91
5
|
async function generateKeyPair() {
|
|
@@ -102,29 +16,60 @@ async function generateKeyPair() {
|
|
|
102
16
|
);
|
|
103
17
|
return keyPair;
|
|
104
18
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
19
|
+
var base58Encoder;
|
|
20
|
+
function assertIsSignature(putativeSignature) {
|
|
21
|
+
if (!base58Encoder)
|
|
22
|
+
base58Encoder = getBase58Encoder();
|
|
23
|
+
try {
|
|
24
|
+
if (
|
|
25
|
+
// Lowest value (64 bytes of zeroes)
|
|
26
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
27
|
+
putativeSignature.length > 88
|
|
28
|
+
) {
|
|
29
|
+
throw new Error("Expected input string to decode to a byte array of length 64.");
|
|
30
|
+
}
|
|
31
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
32
|
+
const numBytes = bytes.byteLength;
|
|
33
|
+
if (numBytes !== 64) {
|
|
34
|
+
throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
|
|
35
|
+
}
|
|
36
|
+
} catch (e) {
|
|
37
|
+
throw new Error(`\`${putativeSignature}\` is not a signature`, {
|
|
38
|
+
cause: e
|
|
39
|
+
});
|
|
111
40
|
}
|
|
112
|
-
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
113
|
-
const [base58EncodedAddress] = getBase58EncodedAddressCodec().deserialize(new Uint8Array(publicKeyBytes));
|
|
114
|
-
return base58EncodedAddress;
|
|
115
41
|
}
|
|
116
|
-
|
|
117
|
-
|
|
42
|
+
function isSignature(putativeSignature) {
|
|
43
|
+
if (!base58Encoder)
|
|
44
|
+
base58Encoder = getBase58Encoder();
|
|
45
|
+
if (
|
|
46
|
+
// Lowest value (64 bytes of zeroes)
|
|
47
|
+
putativeSignature.length < 64 || // Highest value (64 bytes of 255)
|
|
48
|
+
putativeSignature.length > 88
|
|
49
|
+
) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
const bytes = base58Encoder.encode(putativeSignature);
|
|
53
|
+
const numBytes = bytes.byteLength;
|
|
54
|
+
if (numBytes !== 64) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
118
59
|
async function signBytes(key, data) {
|
|
119
60
|
await assertSigningCapabilityIsAvailable();
|
|
120
61
|
const signedData = await crypto.subtle.sign("Ed25519", key, data);
|
|
121
62
|
return new Uint8Array(signedData);
|
|
122
63
|
}
|
|
123
|
-
|
|
64
|
+
function signature(putativeSignature) {
|
|
65
|
+
assertIsSignature(putativeSignature);
|
|
66
|
+
return putativeSignature;
|
|
67
|
+
}
|
|
68
|
+
async function verifySignature(key, signature2, data) {
|
|
124
69
|
await assertVerificationCapabilityIsAvailable();
|
|
125
|
-
return await crypto.subtle.verify("Ed25519", key,
|
|
70
|
+
return await crypto.subtle.verify("Ed25519", key, signature2, data);
|
|
126
71
|
}
|
|
127
72
|
|
|
128
|
-
export {
|
|
73
|
+
export { assertIsSignature, generateKeyPair, isSignature, signBytes, signature, verifySignature };
|
|
129
74
|
//# sourceMappingURL=out.js.map
|
|
130
75
|
//# sourceMappingURL=index.node.js.map
|