@solana/keys 2.0.0-experimental.ffeddf6 → 2.0.0-preview.1

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 CHANGED
@@ -18,7 +18,11 @@ This package contains utilities for validating, generating, and manipulating add
18
18
 
19
19
  ## Types
20
20
 
21
- ### `Ed25519Signature`
21
+ ### `Signature`
22
+
23
+ This type represents a 64-byte Ed25519 signature of some data with a private key, as a base58-encoded string.
24
+
25
+ ### `SignatureBytes`
22
26
 
23
27
  This type represents a 64-byte Ed25519 signature of some data with a private key.
24
28
 
@@ -26,6 +30,31 @@ Whenever you need to verify that a particular signature is, in fact, the one tha
26
30
 
27
31
  ## Functions
28
32
 
33
+ ### `assertIsSignature()`
34
+
35
+ From time to time you might acquire a string that you expect to be a base58-encoded signature (eg. of a transaction) from an untrusted network API or user input. To assert that such an arbitrary string is in fact an Ed25519 signature, use the `assertIsSignature` function.
36
+
37
+ ```ts
38
+ import { assertIsSignature } from '@solana/keys';
39
+
40
+ // Imagine a function that asserts whether a user-supplied signature is valid or not.
41
+ function handleSubmit() {
42
+ // We know only that what the user typed conforms to the `string` type.
43
+ const signature: string = signatureInput.value;
44
+ try {
45
+ // If this type assertion function doesn't throw, then
46
+ // Typescript will upcast `signature` to `Signature`.
47
+ assertIsSignature(signature);
48
+ // At this point, `signature` is a `Signature` that can be used with the RPC.
49
+ const {
50
+ value: [status],
51
+ } = await rpc.getSignatureStatuses([signature]).send();
52
+ } catch (e) {
53
+ // `signature` turned out not to be a base58-encoded signature
54
+ }
55
+ }
56
+ ```
57
+
29
58
  ### `generateKeyPair()`
30
59
 
31
60
  Generates an Ed25519 public/private key pair for use with other methods in this package that accept `CryptoKey` objects.
@@ -36,6 +65,41 @@ import { generateKeyPair } from '@solana/keys';
36
65
  const { privateKey, publicKey } = await generateKeyPair();
37
66
  ```
38
67
 
68
+ ### `createKeyPairFromBytes()`
69
+
70
+ Given a 64-bytes `Uint8Array` secret key, creates an Ed25519 public/private key pair for use with other methods in this package that accept `CryptoKey` objects.
71
+
72
+ ```ts
73
+ import fs from 'fs';
74
+ import { createKeyPairFromBytes } from '@solana/keys';
75
+
76
+ // Get bytes from local keypair file.
77
+ const keypairFile = fs.readFileSync('~/.config/solana/id.json');
78
+ const keypairBytes = new Uint8Array(JSON.parse(keypairFile.toString()));
79
+
80
+ // Create a CryptoKeyPair from the bytes.
81
+ const { privateKey, publicKey } = await createKeyPairFromBytes(keypairBytes);
82
+ ```
83
+
84
+ ### `isSignature()`
85
+
86
+ This is a type guard that accepts a string as input. It will both return `true` if the string conforms to the `Signature` type and will refine the type for use in your program.
87
+
88
+ ```ts
89
+ import { isSignature } from '@solana/keys';
90
+
91
+ if (isSignature(signature)) {
92
+ // At this point, `signature` has been refined to a
93
+ // `Signature` that can be used with the RPC.
94
+ const {
95
+ value: [status],
96
+ } = await rpc.getSignatureStatuses([signature]).send();
97
+ setSignatureStatus(status);
98
+ } else {
99
+ setError(`${signature} is not a transaction signature`);
100
+ }
101
+ ```
102
+
39
103
  ### `signBytes()`
40
104
 
41
105
  Given a private `CryptoKey` and a `Uint8Array` of bytes, this method will return the 64-byte Ed25519 signature of that data as a `Uint8Array`.
@@ -47,9 +111,22 @@ const data = new Uint8Array([1, 2, 3]);
47
111
  const signature = await signBytes(privateKey, data);
48
112
  ```
49
113
 
114
+ ### `signature()`
115
+
116
+ This helper combines _asserting_ that a string is an Ed25519 signature with _coercing_ it to the `Signature` type. It's best used with untrusted input.
117
+
118
+ ```ts
119
+ import { signature } from '@solana/keys';
120
+
121
+ const signature = signature(userSuppliedSignature);
122
+ const {
123
+ value: [status],
124
+ } = await rpc.getSignatureStatuses([signature]).send();
125
+ ```
126
+
50
127
  ### `verifySignature()`
51
128
 
52
- Given a public `CryptoKey`, an `Ed25519Signature`, and a `Uint8Array` of bytes, this method will return `true` if the signature was produced by signing the bytes using the private key associated with the public key, and `false` otherwise.
129
+ Given a public `CryptoKey`, some `SignatureBytes`, and a `Uint8Array` of data, this method will return `true` if the signature was produced by signing the data using the private key associated with the public key, and `false` otherwise.
53
130
 
54
131
  ```ts
55
132
  import { verifySignature } from '@solana/keys';
@@ -1,6 +1,66 @@
1
1
  'use strict';
2
2
 
3
3
  var assertions = require('@solana/assertions');
4
+ var errors = require('@solana/errors');
5
+ var codecsStrings = require('@solana/codecs-strings');
6
+
7
+ // src/key-pair.ts
8
+ function addPkcs8Header(bytes) {
9
+ return new Uint8Array([
10
+ /**
11
+ * PKCS#8 header
12
+ */
13
+ 48,
14
+ // ASN.1 sequence tag
15
+ 46,
16
+ // Length of sequence (46 more bytes)
17
+ 2,
18
+ // ASN.1 integer tag
19
+ 1,
20
+ // Length of integer
21
+ 0,
22
+ // Version number
23
+ 48,
24
+ // ASN.1 sequence tag
25
+ 5,
26
+ // Length of sequence
27
+ 6,
28
+ // ASN.1 object identifier tag
29
+ 3,
30
+ // Length of object identifier
31
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
32
+ 43,
33
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
34
+ 101,
35
+ // thawte(101)
36
+ // Ed25519 identifier
37
+ 112,
38
+ // id-Ed25519(112)
39
+ /**
40
+ * Private key payload
41
+ */
42
+ 4,
43
+ // ASN.1 octet string tag
44
+ 34,
45
+ // String length (34 more bytes)
46
+ // Private key bytes as octet string
47
+ 4,
48
+ // ASN.1 octet string tag
49
+ 32,
50
+ // String length (32 bytes)
51
+ ...bytes
52
+ ]);
53
+ }
54
+ async function createPrivateKeyFromBytes(bytes, extractable) {
55
+ const actualLength = bytes.byteLength;
56
+ if (actualLength !== 32) {
57
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
58
+ actualLength
59
+ });
60
+ }
61
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
62
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
63
+ }
4
64
 
5
65
  // src/key-pair.ts
6
66
  async function generateKeyPair() {
@@ -17,18 +77,82 @@ async function generateKeyPair() {
17
77
  );
18
78
  return keyPair;
19
79
  }
80
+ async function createKeyPairFromBytes(bytes, extractable) {
81
+ if (bytes.byteLength !== 64) {
82
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { byteLength: bytes.byteLength });
83
+ }
84
+ const [publicKey, privateKey] = await Promise.all([
85
+ crypto.subtle.importKey(
86
+ "raw",
87
+ bytes.slice(32),
88
+ "Ed25519",
89
+ /* extractable */
90
+ true,
91
+ ["verify"]
92
+ ),
93
+ createPrivateKeyFromBytes(bytes.slice(0, 32), extractable)
94
+ ]);
95
+ return { privateKey, publicKey };
96
+ }
97
+ var base58Encoder;
98
+ function assertIsSignature(putativeSignature) {
99
+ if (!base58Encoder)
100
+ base58Encoder = codecsStrings.getBase58Encoder();
101
+ if (
102
+ // Lowest value (64 bytes of zeroes)
103
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
104
+ putativeSignature.length > 88
105
+ ) {
106
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {
107
+ actualLength: putativeSignature.length
108
+ });
109
+ }
110
+ const bytes = base58Encoder.encode(putativeSignature);
111
+ const numBytes = bytes.byteLength;
112
+ if (numBytes !== 64) {
113
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {
114
+ actualLength: numBytes
115
+ });
116
+ }
117
+ }
118
+ function isSignature(putativeSignature) {
119
+ if (!base58Encoder)
120
+ base58Encoder = codecsStrings.getBase58Encoder();
121
+ if (
122
+ // Lowest value (64 bytes of zeroes)
123
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
124
+ putativeSignature.length > 88
125
+ ) {
126
+ return false;
127
+ }
128
+ const bytes = base58Encoder.encode(putativeSignature);
129
+ const numBytes = bytes.byteLength;
130
+ if (numBytes !== 64) {
131
+ return false;
132
+ }
133
+ return true;
134
+ }
20
135
  async function signBytes(key, data) {
21
136
  await assertions.assertSigningCapabilityIsAvailable();
22
137
  const signedData = await crypto.subtle.sign("Ed25519", key, data);
23
138
  return new Uint8Array(signedData);
24
139
  }
25
- async function verifySignature(key, signature, data) {
140
+ function signature(putativeSignature) {
141
+ assertIsSignature(putativeSignature);
142
+ return putativeSignature;
143
+ }
144
+ async function verifySignature(key, signature2, data) {
26
145
  await assertions.assertVerificationCapabilityIsAvailable();
27
- return await crypto.subtle.verify("Ed25519", key, signature, data);
146
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
28
147
  }
29
148
 
149
+ exports.assertIsSignature = assertIsSignature;
150
+ exports.createKeyPairFromBytes = createKeyPairFromBytes;
151
+ exports.createPrivateKeyFromBytes = createPrivateKeyFromBytes;
30
152
  exports.generateKeyPair = generateKeyPair;
153
+ exports.isSignature = isSignature;
31
154
  exports.signBytes = signBytes;
155
+ exports.signature = signature;
32
156
  exports.verifySignature = verifySignature;
33
157
  //# sourceMappingURL=out.js.map
34
158
  //# sourceMappingURL=index.browser.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/key-pair.ts","../src/signatures.ts"],"names":[],"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;AAI5F,eAAsB,UAAU,KAAgB,MAA6C;AACzF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEA,eAAsB,gBAAgB,KAAgB,WAA6B,MAAoC;AACnH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAK,WAAW,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';\n\nexport type Ed25519Signature = Uint8Array & { readonly __brand: unique symbol };\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<Ed25519Signature> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as Ed25519Signature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: Ed25519Signature, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
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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;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"]}
@@ -1,4 +1,64 @@
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';
3
+ import { getBase58Encoder } from '@solana/codecs-strings';
4
+
5
+ // src/key-pair.ts
6
+ function addPkcs8Header(bytes) {
7
+ return new Uint8Array([
8
+ /**
9
+ * PKCS#8 header
10
+ */
11
+ 48,
12
+ // ASN.1 sequence tag
13
+ 46,
14
+ // Length of sequence (46 more bytes)
15
+ 2,
16
+ // ASN.1 integer tag
17
+ 1,
18
+ // Length of integer
19
+ 0,
20
+ // Version number
21
+ 48,
22
+ // ASN.1 sequence tag
23
+ 5,
24
+ // Length of sequence
25
+ 6,
26
+ // ASN.1 object identifier tag
27
+ 3,
28
+ // Length of object identifier
29
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
30
+ 43,
31
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
32
+ 101,
33
+ // thawte(101)
34
+ // Ed25519 identifier
35
+ 112,
36
+ // id-Ed25519(112)
37
+ /**
38
+ * Private key payload
39
+ */
40
+ 4,
41
+ // ASN.1 octet string tag
42
+ 34,
43
+ // String length (34 more bytes)
44
+ // Private key bytes as octet string
45
+ 4,
46
+ // ASN.1 octet string tag
47
+ 32,
48
+ // String length (32 bytes)
49
+ ...bytes
50
+ ]);
51
+ }
52
+ async function createPrivateKeyFromBytes(bytes, extractable) {
53
+ const actualLength = bytes.byteLength;
54
+ if (actualLength !== 32) {
55
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
56
+ actualLength
57
+ });
58
+ }
59
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
60
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
61
+ }
2
62
 
3
63
  // src/key-pair.ts
4
64
  async function generateKeyPair() {
@@ -15,16 +75,75 @@ async function generateKeyPair() {
15
75
  );
16
76
  return keyPair;
17
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
+ }
95
+ var base58Encoder;
96
+ function assertIsSignature(putativeSignature) {
97
+ if (!base58Encoder)
98
+ base58Encoder = getBase58Encoder();
99
+ if (
100
+ // Lowest value (64 bytes of zeroes)
101
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
102
+ putativeSignature.length > 88
103
+ ) {
104
+ throw new SolanaError(SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {
105
+ actualLength: putativeSignature.length
106
+ });
107
+ }
108
+ const bytes = base58Encoder.encode(putativeSignature);
109
+ const numBytes = bytes.byteLength;
110
+ if (numBytes !== 64) {
111
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {
112
+ actualLength: numBytes
113
+ });
114
+ }
115
+ }
116
+ function isSignature(putativeSignature) {
117
+ if (!base58Encoder)
118
+ base58Encoder = getBase58Encoder();
119
+ if (
120
+ // Lowest value (64 bytes of zeroes)
121
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
122
+ putativeSignature.length > 88
123
+ ) {
124
+ return false;
125
+ }
126
+ const bytes = base58Encoder.encode(putativeSignature);
127
+ const numBytes = bytes.byteLength;
128
+ if (numBytes !== 64) {
129
+ return false;
130
+ }
131
+ return true;
132
+ }
18
133
  async function signBytes(key, data) {
19
134
  await assertSigningCapabilityIsAvailable();
20
135
  const signedData = await crypto.subtle.sign("Ed25519", key, data);
21
136
  return new Uint8Array(signedData);
22
137
  }
23
- async function verifySignature(key, signature, data) {
138
+ function signature(putativeSignature) {
139
+ assertIsSignature(putativeSignature);
140
+ return putativeSignature;
141
+ }
142
+ async function verifySignature(key, signature2, data) {
24
143
  await assertVerificationCapabilityIsAvailable();
25
- return await crypto.subtle.verify("Ed25519", key, signature, data);
144
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
26
145
  }
27
146
 
28
- export { generateKeyPair, signBytes, verifySignature };
147
+ export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
29
148
  //# sourceMappingURL=out.js.map
30
149
  //# sourceMappingURL=index.browser.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/key-pair.ts","../src/signatures.ts"],"names":[],"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;AAI5F,eAAsB,UAAU,KAAgB,MAA6C;AACzF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEA,eAAsB,gBAAgB,KAAgB,WAA6B,MAAoC;AACnH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAK,WAAW,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';\n\nexport type Ed25519Signature = Uint8Array & { readonly __brand: unique symbol };\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<Ed25519Signature> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as Ed25519Signature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: Ed25519Signature, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
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,M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a,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"]}
@@ -1,4 +1,64 @@
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';
3
+ import { getBase58Encoder } from '@solana/codecs-strings';
4
+
5
+ // src/key-pair.ts
6
+ function addPkcs8Header(bytes) {
7
+ return new Uint8Array([
8
+ /**
9
+ * PKCS#8 header
10
+ */
11
+ 48,
12
+ // ASN.1 sequence tag
13
+ 46,
14
+ // Length of sequence (46 more bytes)
15
+ 2,
16
+ // ASN.1 integer tag
17
+ 1,
18
+ // Length of integer
19
+ 0,
20
+ // Version number
21
+ 48,
22
+ // ASN.1 sequence tag
23
+ 5,
24
+ // Length of sequence
25
+ 6,
26
+ // ASN.1 object identifier tag
27
+ 3,
28
+ // Length of object identifier
29
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
30
+ 43,
31
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
32
+ 101,
33
+ // thawte(101)
34
+ // Ed25519 identifier
35
+ 112,
36
+ // id-Ed25519(112)
37
+ /**
38
+ * Private key payload
39
+ */
40
+ 4,
41
+ // ASN.1 octet string tag
42
+ 34,
43
+ // String length (34 more bytes)
44
+ // Private key bytes as octet string
45
+ 4,
46
+ // ASN.1 octet string tag
47
+ 32,
48
+ // String length (32 bytes)
49
+ ...bytes
50
+ ]);
51
+ }
52
+ async function createPrivateKeyFromBytes(bytes, extractable) {
53
+ const actualLength = bytes.byteLength;
54
+ if (actualLength !== 32) {
55
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
56
+ actualLength
57
+ });
58
+ }
59
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
60
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
61
+ }
2
62
 
3
63
  // src/key-pair.ts
4
64
  async function generateKeyPair() {
@@ -15,16 +75,75 @@ async function generateKeyPair() {
15
75
  );
16
76
  return keyPair;
17
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
+ }
95
+ var base58Encoder;
96
+ function assertIsSignature(putativeSignature) {
97
+ if (!base58Encoder)
98
+ base58Encoder = getBase58Encoder();
99
+ if (
100
+ // Lowest value (64 bytes of zeroes)
101
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
102
+ putativeSignature.length > 88
103
+ ) {
104
+ throw new SolanaError(SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {
105
+ actualLength: putativeSignature.length
106
+ });
107
+ }
108
+ const bytes = base58Encoder.encode(putativeSignature);
109
+ const numBytes = bytes.byteLength;
110
+ if (numBytes !== 64) {
111
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {
112
+ actualLength: numBytes
113
+ });
114
+ }
115
+ }
116
+ function isSignature(putativeSignature) {
117
+ if (!base58Encoder)
118
+ base58Encoder = getBase58Encoder();
119
+ if (
120
+ // Lowest value (64 bytes of zeroes)
121
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
122
+ putativeSignature.length > 88
123
+ ) {
124
+ return false;
125
+ }
126
+ const bytes = base58Encoder.encode(putativeSignature);
127
+ const numBytes = bytes.byteLength;
128
+ if (numBytes !== 64) {
129
+ return false;
130
+ }
131
+ return true;
132
+ }
18
133
  async function signBytes(key, data) {
19
134
  await assertSigningCapabilityIsAvailable();
20
135
  const signedData = await crypto.subtle.sign("Ed25519", key, data);
21
136
  return new Uint8Array(signedData);
22
137
  }
23
- async function verifySignature(key, signature, data) {
138
+ function signature(putativeSignature) {
139
+ assertIsSignature(putativeSignature);
140
+ return putativeSignature;
141
+ }
142
+ async function verifySignature(key, signature2, data) {
24
143
  await assertVerificationCapabilityIsAvailable();
25
- return await crypto.subtle.verify("Ed25519", key, signature, data);
144
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
26
145
  }
27
146
 
28
- export { generateKeyPair, signBytes, verifySignature };
147
+ export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
29
148
  //# sourceMappingURL=out.js.map
30
149
  //# sourceMappingURL=index.native.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/key-pair.ts","../src/signatures.ts"],"names":[],"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;AAI5F,eAAsB,UAAU,KAAgB,MAA6C;AACzF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEA,eAAsB,gBAAgB,KAAgB,WAA6B,MAAoC;AACnH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAK,WAAW,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';\n\nexport type Ed25519Signature = Uint8Array & { readonly __brand: unique symbol };\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<Ed25519Signature> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as Ed25519Signature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: Ed25519Signature, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
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,qD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AM;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"]}
@@ -1,6 +1,66 @@
1
1
  'use strict';
2
2
 
3
3
  var assertions = require('@solana/assertions');
4
+ var errors = require('@solana/errors');
5
+ var codecsStrings = require('@solana/codecs-strings');
6
+
7
+ // src/key-pair.ts
8
+ function addPkcs8Header(bytes) {
9
+ return new Uint8Array([
10
+ /**
11
+ * PKCS#8 header
12
+ */
13
+ 48,
14
+ // ASN.1 sequence tag
15
+ 46,
16
+ // Length of sequence (46 more bytes)
17
+ 2,
18
+ // ASN.1 integer tag
19
+ 1,
20
+ // Length of integer
21
+ 0,
22
+ // Version number
23
+ 48,
24
+ // ASN.1 sequence tag
25
+ 5,
26
+ // Length of sequence
27
+ 6,
28
+ // ASN.1 object identifier tag
29
+ 3,
30
+ // Length of object identifier
31
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
32
+ 43,
33
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
34
+ 101,
35
+ // thawte(101)
36
+ // Ed25519 identifier
37
+ 112,
38
+ // id-Ed25519(112)
39
+ /**
40
+ * Private key payload
41
+ */
42
+ 4,
43
+ // ASN.1 octet string tag
44
+ 34,
45
+ // String length (34 more bytes)
46
+ // Private key bytes as octet string
47
+ 4,
48
+ // ASN.1 octet string tag
49
+ 32,
50
+ // String length (32 bytes)
51
+ ...bytes
52
+ ]);
53
+ }
54
+ async function createPrivateKeyFromBytes(bytes, extractable) {
55
+ const actualLength = bytes.byteLength;
56
+ if (actualLength !== 32) {
57
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
58
+ actualLength
59
+ });
60
+ }
61
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
62
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
63
+ }
4
64
 
5
65
  // src/key-pair.ts
6
66
  async function generateKeyPair() {
@@ -17,18 +77,82 @@ async function generateKeyPair() {
17
77
  );
18
78
  return keyPair;
19
79
  }
80
+ async function createKeyPairFromBytes(bytes, extractable) {
81
+ if (bytes.byteLength !== 64) {
82
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { byteLength: bytes.byteLength });
83
+ }
84
+ const [publicKey, privateKey] = await Promise.all([
85
+ crypto.subtle.importKey(
86
+ "raw",
87
+ bytes.slice(32),
88
+ "Ed25519",
89
+ /* extractable */
90
+ true,
91
+ ["verify"]
92
+ ),
93
+ createPrivateKeyFromBytes(bytes.slice(0, 32), extractable)
94
+ ]);
95
+ return { privateKey, publicKey };
96
+ }
97
+ var base58Encoder;
98
+ function assertIsSignature(putativeSignature) {
99
+ if (!base58Encoder)
100
+ base58Encoder = codecsStrings.getBase58Encoder();
101
+ if (
102
+ // Lowest value (64 bytes of zeroes)
103
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
104
+ putativeSignature.length > 88
105
+ ) {
106
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {
107
+ actualLength: putativeSignature.length
108
+ });
109
+ }
110
+ const bytes = base58Encoder.encode(putativeSignature);
111
+ const numBytes = bytes.byteLength;
112
+ if (numBytes !== 64) {
113
+ throw new errors.SolanaError(errors.SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {
114
+ actualLength: numBytes
115
+ });
116
+ }
117
+ }
118
+ function isSignature(putativeSignature) {
119
+ if (!base58Encoder)
120
+ base58Encoder = codecsStrings.getBase58Encoder();
121
+ if (
122
+ // Lowest value (64 bytes of zeroes)
123
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
124
+ putativeSignature.length > 88
125
+ ) {
126
+ return false;
127
+ }
128
+ const bytes = base58Encoder.encode(putativeSignature);
129
+ const numBytes = bytes.byteLength;
130
+ if (numBytes !== 64) {
131
+ return false;
132
+ }
133
+ return true;
134
+ }
20
135
  async function signBytes(key, data) {
21
136
  await assertions.assertSigningCapabilityIsAvailable();
22
137
  const signedData = await crypto.subtle.sign("Ed25519", key, data);
23
138
  return new Uint8Array(signedData);
24
139
  }
25
- async function verifySignature(key, signature, data) {
140
+ function signature(putativeSignature) {
141
+ assertIsSignature(putativeSignature);
142
+ return putativeSignature;
143
+ }
144
+ async function verifySignature(key, signature2, data) {
26
145
  await assertions.assertVerificationCapabilityIsAvailable();
27
- return await crypto.subtle.verify("Ed25519", key, signature, data);
146
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
28
147
  }
29
148
 
149
+ exports.assertIsSignature = assertIsSignature;
150
+ exports.createKeyPairFromBytes = createKeyPairFromBytes;
151
+ exports.createPrivateKeyFromBytes = createPrivateKeyFromBytes;
30
152
  exports.generateKeyPair = generateKeyPair;
153
+ exports.isSignature = isSignature;
31
154
  exports.signBytes = signBytes;
155
+ exports.signature = signature;
32
156
  exports.verifySignature = verifySignature;
33
157
  //# sourceMappingURL=out.js.map
34
158
  //# sourceMappingURL=index.node.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/key-pair.ts","../src/signatures.ts"],"names":[],"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;AAI5F,eAAsB,UAAU,KAAgB,MAA6C;AACzF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEA,eAAsB,gBAAgB,KAAgB,WAA6B,MAAoC;AACnH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAK,WAAW,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';\n\nexport type Ed25519Signature = Uint8Array & { readonly __brand: unique symbol };\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<Ed25519Signature> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as Ed25519Signature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: Ed25519Signature, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
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;A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__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"]}
@@ -1,4 +1,64 @@
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';
3
+ import { getBase58Encoder } from '@solana/codecs-strings';
4
+
5
+ // src/key-pair.ts
6
+ function addPkcs8Header(bytes) {
7
+ return new Uint8Array([
8
+ /**
9
+ * PKCS#8 header
10
+ */
11
+ 48,
12
+ // ASN.1 sequence tag
13
+ 46,
14
+ // Length of sequence (46 more bytes)
15
+ 2,
16
+ // ASN.1 integer tag
17
+ 1,
18
+ // Length of integer
19
+ 0,
20
+ // Version number
21
+ 48,
22
+ // ASN.1 sequence tag
23
+ 5,
24
+ // Length of sequence
25
+ 6,
26
+ // ASN.1 object identifier tag
27
+ 3,
28
+ // Length of object identifier
29
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
30
+ 43,
31
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
32
+ 101,
33
+ // thawte(101)
34
+ // Ed25519 identifier
35
+ 112,
36
+ // id-Ed25519(112)
37
+ /**
38
+ * Private key payload
39
+ */
40
+ 4,
41
+ // ASN.1 octet string tag
42
+ 34,
43
+ // String length (34 more bytes)
44
+ // Private key bytes as octet string
45
+ 4,
46
+ // ASN.1 octet string tag
47
+ 32,
48
+ // String length (32 bytes)
49
+ ...bytes
50
+ ]);
51
+ }
52
+ async function createPrivateKeyFromBytes(bytes, extractable) {
53
+ const actualLength = bytes.byteLength;
54
+ if (actualLength !== 32) {
55
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
56
+ actualLength
57
+ });
58
+ }
59
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
60
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable ?? false, ["sign"]);
61
+ }
2
62
 
3
63
  // src/key-pair.ts
4
64
  async function generateKeyPair() {
@@ -15,16 +75,75 @@ async function generateKeyPair() {
15
75
  );
16
76
  return keyPair;
17
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
+ }
95
+ var base58Encoder;
96
+ function assertIsSignature(putativeSignature) {
97
+ if (!base58Encoder)
98
+ base58Encoder = getBase58Encoder();
99
+ if (
100
+ // Lowest value (64 bytes of zeroes)
101
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
102
+ putativeSignature.length > 88
103
+ ) {
104
+ throw new SolanaError(SOLANA_ERROR__KEYS__SIGNATURE_STRING_LENGTH_OUT_OF_RANGE, {
105
+ actualLength: putativeSignature.length
106
+ });
107
+ }
108
+ const bytes = base58Encoder.encode(putativeSignature);
109
+ const numBytes = bytes.byteLength;
110
+ if (numBytes !== 64) {
111
+ throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_SIGNATURE_BYTE_LENGTH, {
112
+ actualLength: numBytes
113
+ });
114
+ }
115
+ }
116
+ function isSignature(putativeSignature) {
117
+ if (!base58Encoder)
118
+ base58Encoder = getBase58Encoder();
119
+ if (
120
+ // Lowest value (64 bytes of zeroes)
121
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
122
+ putativeSignature.length > 88
123
+ ) {
124
+ return false;
125
+ }
126
+ const bytes = base58Encoder.encode(putativeSignature);
127
+ const numBytes = bytes.byteLength;
128
+ if (numBytes !== 64) {
129
+ return false;
130
+ }
131
+ return true;
132
+ }
18
133
  async function signBytes(key, data) {
19
134
  await assertSigningCapabilityIsAvailable();
20
135
  const signedData = await crypto.subtle.sign("Ed25519", key, data);
21
136
  return new Uint8Array(signedData);
22
137
  }
23
- async function verifySignature(key, signature, data) {
138
+ function signature(putativeSignature) {
139
+ assertIsSignature(putativeSignature);
140
+ return putativeSignature;
141
+ }
142
+ async function verifySignature(key, signature2, data) {
24
143
  await assertVerificationCapabilityIsAvailable();
25
- return await crypto.subtle.verify("Ed25519", key, signature, data);
144
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
26
145
  }
27
146
 
28
- export { generateKeyPair, signBytes, verifySignature };
147
+ export { assertIsSignature, createKeyPairFromBytes, createPrivateKeyFromBytes, generateKeyPair, isSignature, signBytes, signature, verifySignature };
29
148
  //# sourceMappingURL=out.js.map
30
149
  //# sourceMappingURL=index.node.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/key-pair.ts","../src/signatures.ts"],"names":[],"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;AAI5F,eAAsB,UAAU,KAAgB,MAA6C;AACzF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEA,eAAsB,gBAAgB,KAAgB,WAA6B,MAAoC;AACnH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAK,WAAW,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';\n\nexport type Ed25519Signature = Uint8Array & { readonly __brand: unique symbol };\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<Ed25519Signature> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as Ed25519Signature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: Ed25519Signature, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
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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ACvB,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"]}
@@ -1,3 +1,4 @@
1
- export * from './key-pair';
2
- export * from './signatures';
1
+ export * from './key-pair.js';
2
+ export * from './private-key.js';
3
+ export * from './signatures.js';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -1,2 +1,3 @@
1
1
  export declare function generateKeyPair(): Promise<CryptoKeyPair>;
2
+ export declare function createKeyPairFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKeyPair>;
2
3
  //# 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":"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"}
@@ -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":"AAuCA,wBAAsB,yBAAyB,CAAC,KAAK,EAAE,UAAU,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAS5G"}
@@ -1,6 +1,12 @@
1
- export type Ed25519Signature = Uint8Array & {
1
+ export type Signature = string & {
2
2
  readonly __brand: unique symbol;
3
3
  };
4
- export declare function signBytes(key: CryptoKey, data: Uint8Array): Promise<Ed25519Signature>;
5
- export declare function verifySignature(key: CryptoKey, signature: Ed25519Signature, data: Uint8Array): Promise<boolean>;
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>;
6
12
  //# sourceMappingURL=signatures.d.ts.map
@@ -0,0 +1 @@
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.ffeddf6",
3
+ "version": "2.0.0-preview.1",
4
4
  "description": "Helpers for generating and transforming key material",
5
5
  "exports": {
6
6
  "browser": {
@@ -49,29 +49,10 @@
49
49
  "node": ">=17.4"
50
50
  },
51
51
  "dependencies": {
52
- "@solana/assertions": "2.0.0-experimental.ffeddf6"
53
- },
54
- "devDependencies": {
55
- "@solana/eslint-config-solana": "^1.0.2",
56
- "@swc/jest": "^0.2.29",
57
- "@types/jest": "^29.5.6",
58
- "@typescript-eslint/eslint-plugin": "^6.7.0",
59
- "@typescript-eslint/parser": "^6.3.0",
60
- "agadoo": "^3.0.0",
61
- "eslint": "^8.45.0",
62
- "eslint-plugin-jest": "^27.4.2",
63
- "eslint-plugin-sort-keys-fix": "^1.1.2",
64
- "jest": "^29.7.0",
65
- "jest-environment-jsdom": "^29.7.0",
66
- "jest-runner-eslint": "^2.1.2",
67
- "jest-runner-prettier": "^1.0.0",
68
- "prettier": "^2.8",
69
- "tsup": "7.2.0",
70
- "typescript": "^5.2.2",
71
- "version-from-git": "^1.1.1",
72
- "build-scripts": "0.0.0",
73
- "test-config": "0.0.0",
74
- "tsconfig": "0.0.0"
52
+ "@solana/assertions": "2.0.0-preview.1",
53
+ "@solana/codecs-core": "2.0.0-preview.1",
54
+ "@solana/codecs-strings": "2.0.0-preview.1",
55
+ "@solana/errors": "2.0.0-preview.1"
75
56
  },
76
57
  "bundlewatch": {
77
58
  "defaultCompression": "gzip",
@@ -82,18 +63,19 @@
82
63
  ]
83
64
  },
84
65
  "scripts": {
85
- "compile:js": "tsup --config build-scripts/tsup.config.library.ts",
86
- "compile:typedefs": "tsc -p ./tsconfig.declarations.json",
87
- "dev": "jest -c node_modules/test-config/jest-dev.config.ts --rootDir . --watch",
88
- "publish-packages": "pnpm publish --tag experimental --access public --no-git-checks",
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 preview --access public --no-git-checks",
70
+ "publish-packages": "pnpm prepublishOnly && pnpm publish-impl",
89
71
  "style:fix": "pnpm eslint --fix src/* && pnpm prettier -w src/* package.json",
90
- "test:lint": "jest -c node_modules/test-config/jest-lint.config.ts --rootDir . --silent",
91
- "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",
92
74
  "test:treeshakability:browser": "agadoo dist/index.browser.js",
93
75
  "test:treeshakability:native": "agadoo dist/index.native.js",
94
76
  "test:treeshakability:node": "agadoo dist/index.node.js",
95
77
  "test:typecheck": "tsc --noEmit",
96
- "test:unit:browser": "jest -c node_modules/test-config/jest-unit.config.browser.ts --rootDir . --silent",
97
- "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"
98
80
  }
99
81
  }
@@ -1,94 +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
- assertIsSecureContext();
37
- if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.generateKey !== "function") {
38
- throw new Error("No key generation implementation could be found");
39
- }
40
- if (!await isEd25519CurveSupported(globalThis.crypto.subtle)) {
41
- throw new Error(
42
- "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"
43
- );
44
- }
45
- }
46
- async function assertSigningCapabilityIsAvailable() {
47
- assertIsSecureContext();
48
- if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.sign !== "function") {
49
- throw new Error("No signing implementation could be found");
50
- }
51
- }
52
- async function assertVerificationCapabilityIsAvailable() {
53
- assertIsSecureContext();
54
- if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.verify !== "function") {
55
- throw new Error("No signature verification implementation could be found");
56
- }
57
- }
58
-
59
- // src/key-pair.ts
60
- async function generateKeyPair() {
61
- await assertKeyGenerationIsAvailable();
62
- const keyPair = await crypto.subtle.generateKey(
63
- /* algorithm */
64
- "Ed25519",
65
- // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
66
- /* extractable */
67
- false,
68
- // Prevents the bytes of the private key from being visible to JS.
69
- /* allowed uses */
70
- ["sign", "verify"]
71
- );
72
- return keyPair;
73
- }
74
-
75
- // src/signatures.ts
76
- async function signBytes(key, data) {
77
- await assertSigningCapabilityIsAvailable();
78
- const signedData = await crypto.subtle.sign("Ed25519", key, data);
79
- return new Uint8Array(signedData);
80
- }
81
- async function verifySignature(key, signature, data) {
82
- await assertVerificationCapabilityIsAvailable();
83
- return await crypto.subtle.verify("Ed25519", key, signature, data);
84
- }
85
-
86
- exports.generateKeyPair = generateKeyPair;
87
- exports.signBytes = signBytes;
88
- exports.verifySignature = verifySignature;
89
-
90
- return exports;
91
-
92
- })({});
93
- //# sourceMappingURL=out.js.map
94
- //# sourceMappingURL=index.development.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../assertions/src/subtle-crypto.ts","../src/key-pair.ts","../src/signatures.ts"],"names":[],"mappings":";AAAA,SAAS,wBAAwB;AAC7B,MAAmB,CAAC,WAAW,iBAAiB;AAE5C,UAAM,IAAI;MACN;IAEJ;EACJ;AACJ;AAEA,IAAI;AACJ,eAAe,wBAAwB,QAAwC;AAC3E,MAAI,0BAA0B,QAAW;AACrC,4BAAwB,IAAI,QAAQ,CAAA,YAAW;AAC3C,aACK;QAAY;;QAA6B;QAAO,CAAC,QAAQ,QAAQ;MAAC,EAClE,MAAM,MAAM;AACT,gBAAS,wBAAwB,KAAM;MAC3C,CAAC,EACA,KAAK,MAAM;AACR,gBAAS,wBAAwB,IAAK;MAC1C,CAAC;IACT,CAAC;EACL;AACA,MAAI,OAAO,0BAA0B,WAAW;AAC5C,WAAO;EACX,OAAO;AACH,WAAO,MAAM;EACjB;AACJ;AAUA,eAAsB,iCAAiC;AACnD,wBAAsB;AACtB,MAAI,OAAO,WAAW,WAAW,eAAe,OAAO,WAAW,OAAO,QAAQ,gBAAgB,YAAY;AAEzG,UAAM,IAAI,MAAM,iDAAiD;EACrE;AACA,MAAI,CAAE,MAAM,wBAAwB,WAAW,OAAO,MAAM,GAAI;AAE5D,UAAM,IAAI;MACN;IAKJ;EACJ;AACJ;AAUA,eAAsB,qCAAqC;AACvD,wBAAsB;AACtB,MAAI,OAAO,WAAW,WAAW,eAAe,OAAO,WAAW,OAAO,QAAQ,SAAS,YAAY;AAElG,UAAM,IAAI,MAAM,0CAA0C;EAC9D;AACJ;AAEA,eAAsB,0CAA0C;AAC5D,wBAAsB;AACtB,MAAI,OAAO,WAAW,WAAW,eAAe,OAAO,WAAW,OAAO,QAAQ,WAAW,YAAY;AAEpG,UAAM,IAAI,MAAM,yDAAyD;EAC7E;AACJ;;;AC7EA,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;;;ACNA,eAAsB,UAAU,KAAgB,MAA6C;AACzF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEA,eAAsB,gBAAgB,KAAgB,WAA6B,MAAoC;AACnH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAK,WAAW,IAAI;AACrE","sourcesContent":["function assertIsSecureContext() {\n if (__BROWSER__ && !globalThis.isSecureContext) {\n // TODO: Coded error.\n throw new Error(\n 'Cryptographic operations are only allowed in secure browser contexts. Read more ' +\n 'here: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts'\n );\n }\n}\n\nlet cachedEd25519Decision: PromiseLike<boolean> | boolean | undefined;\nasync function isEd25519CurveSupported(subtle: SubtleCrypto): Promise<boolean> {\n if (cachedEd25519Decision === undefined) {\n cachedEd25519Decision = new Promise(resolve => {\n subtle\n .generateKey('Ed25519', /* extractable */ false, ['sign', 'verify'])\n .catch(() => {\n resolve((cachedEd25519Decision = false));\n })\n .then(() => {\n resolve((cachedEd25519Decision = true));\n });\n });\n }\n if (typeof cachedEd25519Decision === 'boolean') {\n return cachedEd25519Decision;\n } else {\n return await cachedEd25519Decision;\n }\n}\n\nexport async function assertDigestCapabilityIsAvailable() {\n assertIsSecureContext();\n if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.digest !== 'function') {\n // TODO: Coded error.\n throw new Error('No digest implementation could be found');\n }\n}\n\nexport async function assertKeyGenerationIsAvailable() {\n assertIsSecureContext();\n if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.generateKey !== 'function') {\n // TODO: Coded error.\n throw new Error('No key generation implementation could be found');\n }\n if (!(await isEd25519CurveSupported(globalThis.crypto.subtle))) {\n // TODO: Coded error.\n throw new Error(\n 'This runtime does not support the generation of Ed25519 key pairs.\\n\\nInstall and ' +\n 'import `@solana/webcrypto-ed25519-polyfill` before generating keys in ' +\n 'environments that do not support Ed25519.\\n\\nFor a list of runtimes that ' +\n 'currently support Ed25519 operations, visit ' +\n 'https://github.com/WICG/webcrypto-secure-curves/issues/20'\n );\n }\n}\n\nexport async function assertKeyExporterIsAvailable() {\n assertIsSecureContext();\n if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.exportKey !== 'function') {\n // TODO: Coded error.\n throw new Error('No key export implementation could be found');\n }\n}\n\nexport async function assertSigningCapabilityIsAvailable() {\n assertIsSecureContext();\n if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.sign !== 'function') {\n // TODO: Coded error.\n throw new Error('No signing implementation could be found');\n }\n}\n\nexport async function assertVerificationCapabilityIsAvailable() {\n assertIsSecureContext();\n if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.verify !== 'function') {\n // TODO: Coded error.\n throw new Error('No signature verification implementation could be found');\n }\n}\n","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';\n\nexport type Ed25519Signature = Uint8Array & { readonly __brand: unique symbol };\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<Ed25519Signature> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as Ed25519Signature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: Ed25519Signature, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
@@ -1,17 +0,0 @@
1
- this.globalThis = this.globalThis || {};
2
- this.globalThis.solanaWeb3 = (function (exports) {
3
- 'use strict';
4
-
5
- function n(){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 e;async function y(t){return e===void 0&&(e=new Promise(o=>{t.generateKey("Ed25519",!1,["sign","verify"]).catch(()=>{o(e=!1);}).then(()=>{o(e=!0);});})),typeof e=="boolean"?e:await e}async function a(){if(n(),typeof globalThis.crypto>"u"||typeof globalThis.crypto.subtle?.generateKey!="function")throw new Error("No key generation implementation could be found");if(!await y(globalThis.crypto.subtle))throw new Error(`This runtime does not support the generation of Ed25519 key pairs.
6
-
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 s(){if(n(),typeof globalThis.crypto>"u"||typeof globalThis.crypto.subtle?.sign!="function")throw new Error("No signing implementation could be found")}async function l(){if(n(),typeof globalThis.crypto>"u"||typeof globalThis.crypto.subtle?.verify!="function")throw new Error("No signature verification implementation could be found")}async function d(){return await a(),await crypto.subtle.generateKey("Ed25519",!1,["sign","verify"])}async function m(t,o){await s();let r=await crypto.subtle.sign("Ed25519",t,o);return new Uint8Array(r)}async function w(t,o,r){return await l(),await crypto.subtle.verify("Ed25519",t,o,r)}
10
-
11
- exports.generateKeyPair = d;
12
- exports.signBytes = m;
13
- exports.verifySignature = w;
14
-
15
- return exports;
16
-
17
- })({});