@solana/keys 2.0.0-experimental.aeec044 → 2.0.0-experimental.aef0571

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.
@@ -1,27 +1,130 @@
1
- import bs58 from 'bs58';
1
+ import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
2
+ import { getBase58Encoder } from '@solana/codecs-strings';
2
3
 
3
- // src/base58.ts
4
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
4
+ // 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
+ 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();
5
78
  try {
6
79
  if (
7
- // Lowest address (32 bytes of zeroes)
8
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
9
- putativeBase58EncodedAddress.length > 44
80
+ // Lowest value (64 bytes of zeroes)
81
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
82
+ putativeSignature.length > 88
10
83
  ) {
11
- throw new Error("Expected input string to decode to a byte array of length 32.");
84
+ throw new Error("Expected input string to decode to a byte array of length 64.");
12
85
  }
13
- const bytes = bs58.decode(putativeBase58EncodedAddress);
86
+ const bytes = base58Encoder.encode(putativeSignature);
14
87
  const numBytes = bytes.byteLength;
15
- if (numBytes !== 32) {
16
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
88
+ if (numBytes !== 64) {
89
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
17
90
  }
18
91
  } catch (e) {
19
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
92
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
20
93
  cause: e
21
94
  });
22
95
  }
23
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;
111
+ }
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);
126
+ }
24
127
 
25
- export { assertIsBase58EncodedAddress };
128
+ export { assertIsSignature, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
26
129
  //# sourceMappingURL=out.js.map
27
130
  //# sourceMappingURL=index.native.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/base58.ts"],"names":[],"mappings":";AAAA,OAAO,UAAU;AAMV,SAAS,6BACZ,8BACiG;AACjG,MAAI;AAEA;AAAA;AAAA,MAEI,6BAA6B,SAAS;AAAA,MAEtC,6BAA6B,SAAS;AAAA,MACxC;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,KAAK,OAAO,4BAA4B;AACtD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,UAAU;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAP;AACE,UAAM,IAAI,MAAM,KAAK,mEAAmE;AAAA,MACpF,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ","sourcesContent":["import bs58 from 'bs58';\n\nexport type Base58EncodedAddress<TAddress extends string = string> = TAddress & {\n readonly __base58EncodedAddress: unique symbol;\n};\n\nexport function assertIsBase58EncodedAddress(\n putativeBase58EncodedAddress: string\n): asserts putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n throw new Error('Expected input string to decode to a byte array of length 32.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = bs58.decode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeBase58EncodedAddress}\\` is not a base-58 encoded address`, {\n cause: e,\n });\n }\n}\n"]}
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;AAEA,eAAsB,0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AAsC;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"]}
@@ -1,33 +1,138 @@
1
1
  'use strict';
2
2
 
3
- var bs58 = require('bs58');
3
+ var assertions = require('@solana/assertions');
4
+ var codecsStrings = require('@solana/codecs-strings');
4
5
 
5
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
-
7
- var bs58__default = /*#__PURE__*/_interopDefault(bs58);
6
+ // src/key-pair.ts
7
+ async function generateKeyPair() {
8
+ await assertions.assertKeyGenerationIsAvailable();
9
+ const keyPair = await crypto.subtle.generateKey(
10
+ /* algorithm */
11
+ "Ed25519",
12
+ // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
13
+ /* extractable */
14
+ false,
15
+ // Prevents the bytes of the private key from being visible to JS.
16
+ /* allowed uses */
17
+ ["sign", "verify"]
18
+ );
19
+ return keyPair;
20
+ }
8
21
 
9
- // src/base58.ts
10
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
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();
11
80
  try {
12
81
  if (
13
- // Lowest address (32 bytes of zeroes)
14
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
15
- putativeBase58EncodedAddress.length > 44
82
+ // Lowest value (64 bytes of zeroes)
83
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
84
+ putativeSignature.length > 88
16
85
  ) {
17
- throw new Error("Expected input string to decode to a byte array of length 32.");
86
+ throw new Error("Expected input string to decode to a byte array of length 64.");
18
87
  }
19
- const bytes = bs58__default.default.decode(putativeBase58EncodedAddress);
88
+ const bytes = base58Encoder.encode(putativeSignature);
20
89
  const numBytes = bytes.byteLength;
21
- if (numBytes !== 32) {
22
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
90
+ if (numBytes !== 64) {
91
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
23
92
  }
24
93
  } catch (e) {
25
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
94
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
26
95
  cause: e
27
96
  });
28
97
  }
29
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;
113
+ }
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);
128
+ }
30
129
 
31
- exports.assertIsBase58EncodedAddress = assertIsBase58EncodedAddress;
130
+ exports.assertIsSignature = assertIsSignature;
131
+ exports.createPrivateKeyFromBytes = createPrivateKeyFromBytes;
132
+ exports.generateKeyPair = generateKeyPair;
133
+ exports.isSignature = isSignature;
134
+ exports.signBytes = signBytes;
135
+ exports.signature = signature;
136
+ exports.verifySignature = verifySignature;
32
137
  //# sourceMappingURL=out.js.map
33
138
  //# sourceMappingURL=index.node.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/base58.ts"],"names":[],"mappings":";AAAA,OAAO,UAAU;AAMV,SAAS,6BACZ,8BACiG;AACjG,MAAI;AAEA;AAAA;AAAA,MAEI,6BAA6B,SAAS;AAAA,MAEtC,6BAA6B,SAAS;AAAA,MACxC;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,KAAK,OAAO,4BAA4B;AACtD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,UAAU;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAP;AACE,UAAM,IAAI,MAAM,KAAK,mEAAmE;AAAA,MACpF,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ","sourcesContent":["import bs58 from 'bs58';\n\nexport type Base58EncodedAddress<TAddress extends string = string> = TAddress & {\n readonly __base58EncodedAddress: unique symbol;\n};\n\nexport function assertIsBase58EncodedAddress(\n putativeBase58EncodedAddress: string\n): asserts putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n throw new Error('Expected input string to decode to a byte array of length 32.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = bs58.decode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeBase58EncodedAddress}\\` is not a base-58 encoded address`, {\n cause: e,\n });\n }\n}\n"]}
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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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"]}
@@ -1,27 +1,130 @@
1
- import bs58 from 'bs58';
1
+ import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
2
+ import { getBase58Encoder } from '@solana/codecs-strings';
2
3
 
3
- // src/base58.ts
4
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
4
+ // 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
+ 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();
5
78
  try {
6
79
  if (
7
- // Lowest address (32 bytes of zeroes)
8
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
9
- putativeBase58EncodedAddress.length > 44
80
+ // Lowest value (64 bytes of zeroes)
81
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
82
+ putativeSignature.length > 88
10
83
  ) {
11
- throw new Error("Expected input string to decode to a byte array of length 32.");
84
+ throw new Error("Expected input string to decode to a byte array of length 64.");
12
85
  }
13
- const bytes = bs58.decode(putativeBase58EncodedAddress);
86
+ const bytes = base58Encoder.encode(putativeSignature);
14
87
  const numBytes = bytes.byteLength;
15
- if (numBytes !== 32) {
16
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
88
+ if (numBytes !== 64) {
89
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
17
90
  }
18
91
  } catch (e) {
19
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
92
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
20
93
  cause: e
21
94
  });
22
95
  }
23
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;
111
+ }
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);
126
+ }
24
127
 
25
- export { assertIsBase58EncodedAddress };
128
+ export { assertIsSignature, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
26
129
  //# sourceMappingURL=out.js.map
27
130
  //# sourceMappingURL=index.node.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/base58.ts"],"names":[],"mappings":";AAAA,OAAO,UAAU;AAMV,SAAS,6BACZ,8BACiG;AACjG,MAAI;AAEA;AAAA;AAAA,MAEI,6BAA6B,SAAS;AAAA,MAEtC,6BAA6B,SAAS;AAAA,MACxC;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,KAAK,OAAO,4BAA4B;AACtD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,UAAU;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAP;AACE,UAAM,IAAI,MAAM,KAAK,mEAAmE;AAAA,MACpF,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ","sourcesContent":["import bs58 from 'bs58';\n\nexport type Base58EncodedAddress<TAddress extends string = string> = TAddress & {\n readonly __base58EncodedAddress: unique symbol;\n};\n\nexport function assertIsBase58EncodedAddress(\n putativeBase58EncodedAddress: string\n): asserts putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n throw new Error('Expected input string to decode to a byte array of length 32.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = bs58.decode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeBase58EncodedAddress}\\` is not a base-58 encoded address`, {\n cause: e,\n });\n }\n}\n"]}
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,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,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"]}
@@ -2,9 +2,19 @@ this.globalThis = this.globalThis || {};
2
2
  this.globalThis.solanaWeb3 = (function (exports) {
3
3
  'use strict';
4
4
 
5
- var S=Object.create;var m=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var B=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var $=(e,r)=>()=>(e&&(r=e(e=0)),r);var u=(e,r)=>()=>(r||e((r={exports:{}}).exports,r),r.exports);var k=(e,r,n,p)=>{if(r&&typeof r=="object"||typeof r=="function")for(let l of q(r))!F.call(e,l)&&l!==n&&m(e,l,{get:()=>r[l],enumerable:!(p=V(r,l))||p.enumerable});return e};var G=(e,r,n)=>(n=e!=null?S(B(e)):{},k(r||!e||!e.__esModule?m(n,"default",{value:e,enumerable:!0}):n,e));var w=$(()=>{});var _=u((X,z)=>{w();function I(e){if(e.length>=255)throw new TypeError("Alphabet too long");for(var r=new Uint8Array(256),n=0;n<r.length;n++)r[n]=255;for(var p=0;p<e.length;p++){var l=e.charAt(p),c=l.charCodeAt(0);if(r[c]!==255)throw new TypeError(l+" is ambiguous");r[c]=p;}var A=e.length,U=e.charAt(0),T=Math.log(A)/Math.log(256),D=Math.log(256)/Math.log(A);function O(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(t.length===0)return "";for(var a=0,y=0,i=0,h=t.length;i!==h&&t[i]===0;)i++,a++;for(var d=(h-i)*D+1>>>0,o=new Uint8Array(d);i!==h;){for(var f=t[i],g=0,s=d-1;(f!==0||g<y)&&s!==-1;s--,g++)f+=256*o[s]>>>0,o[s]=f%A>>>0,f=f/A>>>0;if(f!==0)throw new Error("Non-zero carry");y=g,i++;}for(var v=d-y;v!==d&&o[v]===0;)v++;for(var b=U.repeat(a);v<d;++v)b+=e.charAt(o[v]);return b}function x(t){if(typeof t!="string")throw new TypeError("Expected String");if(t.length===0)return new Uint8Array;for(var a=0,y=0,i=0;t[a]===U;)y++,a++;for(var h=(t.length-a)*T+1>>>0,d=new Uint8Array(h);t[a];){var o=r[t.charCodeAt(a)];if(o===255)return;for(var f=0,g=h-1;(o!==0||f<i)&&g!==-1;g--,f++)o+=A*d[g]>>>0,d[g]=o%256>>>0,o=o/256>>>0;if(o!==0)throw new Error("Non-zero carry");i=f,a++;}for(var s=h-i;s!==h&&d[s]===0;)s++;for(var v=new Uint8Array(y+(h-s)),b=y;s!==h;)v[b++]=d[s++];return v}function R(t){var a=x(t);if(a)return a;throw new Error("Non-base"+A+" character")}return {encode:O,decodeUnsafe:x,decode:R}}z.exports=I;});var N=u((Z,M)=>{w();var J=_(),K="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";M.exports=J(K);});w();w();var C=G(N(),1);function L(e){try{if(e.length<32||e.length>44)throw new Error("Expected input string to decode to a byte array of length 32.");let n=C.default.decode(e).byteLength;if(n!==32)throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${n}`)}catch(r){throw new Error(`\`${e}\` is not a base-58 encoded address`,{cause:r})}}
5
+ function u(){if(!globalThis.isSecureContext)throw new Error("Cryptographic operations are only allowed in secure browser contexts. Read more here: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts")}var o;async function b(e){return o===void 0&&(o=new Promise(r=>{e.generateKey("Ed25519",!1,["sign","verify"]).catch(()=>{r(o=!1);}).then(()=>{r(o=!0);});})),typeof o=="boolean"?o:await o}async function h(){var e;if(u(),typeof globalThis.crypto>"u"||typeof((e=globalThis.crypto.subtle)==null?void 0:e.generateKey)!="function")throw new Error("No key generation implementation could be found");if(!await b(globalThis.crypto.subtle))throw new Error(`This runtime does not support the generation of Ed25519 key pairs.
6
6
 
7
- exports.assertIsBase58EncodedAddress = L;
7
+ Install and import \`@solana/webcrypto-ed25519-polyfill\` before generating keys in environments that do not support Ed25519.
8
+
9
+ For a list of runtimes that currently support Ed25519 operations, visit https://github.com/WICG/webcrypto-secure-curves/issues/20`)}async function y(){var e;if(u(),typeof globalThis.crypto>"u"||typeof((e=globalThis.crypto.subtle)==null?void 0:e.sign)!="function")throw new Error("No signing implementation could be found")}async function p(){var e;if(u(),typeof globalThis.crypto>"u"||typeof((e=globalThis.crypto.subtle)==null?void 0:e.verify)!="function")throw new Error("No signature verification implementation could be found")}async function U(){return await h(),await crypto.subtle.generateKey("Ed25519",!1,["sign","verify"])}function v(e){return new Uint8Array([48,46,2,1,0,48,5,6,3,43,101,112,4,34,4,32,...e])}async function K(e,r){if(e.byteLength!==32)throw new Error("Private key bytes must be of length 32");let t=v(e);return await crypto.subtle.importKey("pkcs8",t,"Ed25519",r!=null?r:!1,["sign"])}function m(e,r){return "fixedSize"in r?r.fixedSize:r.getSizeFromValue(e)}function w(e){return Object.freeze({...e,encode:r=>{let t=new Uint8Array(m(r,e));return e.write(r,t,0),t}})}function S(e,r,t=r){if(!r.match(new RegExp(`^[${e}]*$`)))throw new Error(`Expected a string of base ${e.length}, got [${t}].`)}var z=e=>w({getSizeFromValue:r=>{let[t,n]=x(r,e[0]);if(n==="")return r.length;let i=E(n,e);return t.length+Math.ceil(i.toString(16).length/2)},write(r,t,n){if(S(e,r),r==="")return n;let[i,d]=x(r,e[0]);if(d==="")return t.set(new Uint8Array(i.length).fill(0),n),n+i.length;let c=E(d,e),g=[];for(;c>0n;)g.unshift(Number(c%256n)),c/=256n;let f=[...Array(i.length).fill(0),...g];return t.set(f,n),n+f.length}});function x(e,r){let t=[...e].findIndex(n=>n!==r);return t===-1?[e,""]:[e.slice(0,t),e.slice(t)]}function E(e,r){let t=BigInt(r.length);return [...e].reduce((n,i)=>n*t+BigInt(r.indexOf(i)),0n)}var C="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",l=()=>z(C);var s;function B(e){s||(s=l());try{if(e.length<64||e.length>88)throw new Error("Expected input string to decode to a byte array of length 64.");let t=s.encode(e).byteLength;if(t!==64)throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${t}`)}catch(r){throw new Error(`\`${e}\` is not a signature`,{cause:r})}}function Q(e){return s||(s=l()),!(e.length<64||e.length>88||s.encode(e).byteLength!==64)}async function Y(e,r){await y();let t=await crypto.subtle.sign("Ed25519",e,r);return new Uint8Array(t)}function ee(e){return B(e),e}async function re(e,r,t){return await p(),await crypto.subtle.verify("Ed25519",e,r,t)}
10
+
11
+ exports.assertIsSignature = B;
12
+ exports.createPrivateKeyFromBytes = K;
13
+ exports.generateKeyPair = U;
14
+ exports.isSignature = Q;
15
+ exports.signBytes = Y;
16
+ exports.signature = ee;
17
+ exports.verifySignature = re;
8
18
 
9
19
  return exports;
10
20
 
@@ -1,2 +1,4 @@
1
- export * from './base58';
1
+ export * from './key-pair';
2
+ export * from './private-key';
3
+ export * from './signatures';
2
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function generateKeyPair(): Promise<CryptoKeyPair>;
2
+ //# sourceMappingURL=key-pair.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-pair.d.ts","sourceRoot":"","sources":["../../src/key-pair.ts"],"names":[],"mappings":"AAEA,wBAAsB,eAAe,IAAI,OAAO,CAAC,aAAa,CAAC,CAQ9D"}
@@ -0,0 +1,2 @@
1
+ export declare function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKey>;
2
+ //# sourceMappingURL=private-key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"private-key.d.ts","sourceRoot":"","sources":["../../src/private-key.ts"],"names":[],"mappings":"AAqCA,wBAAsB,yBAAyB,CAAC,KAAK,EAAE,UAAU,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAO5G"}
@@ -0,0 +1,12 @@
1
+ export type Signature = string & {
2
+ readonly __brand: unique symbol;
3
+ };
4
+ export type SignatureBytes = Uint8Array & {
5
+ readonly __brand: unique symbol;
6
+ };
7
+ export declare function assertIsSignature(putativeSignature: string): asserts putativeSignature is Signature;
8
+ export declare function isSignature(putativeSignature: string): putativeSignature is Signature;
9
+ export declare function signBytes(key: CryptoKey, data: Uint8Array): Promise<SignatureBytes>;
10
+ export declare function signature(putativeSignature: string): Signature;
11
+ export declare function verifySignature(key: CryptoKey, signature: SignatureBytes, data: Uint8Array): Promise<boolean>;
12
+ //# sourceMappingURL=signatures.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signatures.d.ts","sourceRoot":"","sources":["../../src/signatures.ts"],"names":[],"mappings":"AAIA,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,CAwBnG;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"}