@solana/keys 2.0.0-experimental.eefafdf → 2.0.0-experimental.ef09aec

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2018 Solana Labs, Inc
1
+ Copyright (c) 2023 Solana Labs, Inc
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
package/README.md CHANGED
@@ -18,35 +18,105 @@ This package contains utilities for validating, generating, and manipulating add
18
18
 
19
19
  ## Types
20
20
 
21
- ### `Base58EncodedAddress`
21
+ ### `Signature`
22
22
 
23
- This type represents a string that validates as a Solana address or public key. Functions that require well-formed addresses should specify their inputs in terms of this type.
23
+ This type represents a 64-byte Ed25519 signature of some data with a private key, as a base58-encoded string.
24
24
 
25
- Whenever you need to validate an arbitrary string as a base58-encoded address, use the `assertIsBase58EncodedAddress()` function in this package.
25
+ ### `SignatureBytes`
26
26
 
27
- ## Functions
27
+ This type represents a 64-byte Ed25519 signature of some data with a private key.
28
+
29
+ Whenever you need to verify that a particular signature is, in fact, the one that would have been produced by signing some known bytes using the private key associated with some known public key, use the `verifySignature()` function in this package.
28
30
 
29
- ### `assertIsBase58EncodedAddress()`
31
+ ## Functions
30
32
 
31
- Client applications primarily deal with addresses and public keys in the form of base58-encoded strings. Addresses and public keys returned from the RPC API conform to the type `Base58EncodedAddress`. You can use a value of that type wherever a base58-encoded address or key is expected.
33
+ ### `assertIsSignature()`
32
34
 
33
- From time to time you might acquire a string, that you expect to validate as an address, from an untrusted network API or user input. To assert that such an arbitrary string is a base58-encoded address, use the `assertIsBase58EncodedAddress` function.
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.
34
36
 
35
37
  ```ts
36
- import { assertIsBase58EncodedAddress } from '@solana/web3.js`;
38
+ import { assertIsSignature } from '@solana/keys';
37
39
 
38
- // Imagine a function that fetches an account's balance when a user submits a form.
40
+ // Imagine a function that asserts whether a user-supplied signature is valid or not.
39
41
  function handleSubmit() {
40
42
  // We know only that what the user typed conforms to the `string` type.
41
- const address: string = accountAddressInput.value;
43
+ const signature: string = signatureInput.value;
42
44
  try {
43
45
  // If this type assertion function doesn't throw, then
44
- // Typescript will upcast `address` to `Base58EncodedAddress`.
45
- assertIsBase58EncodedAddress(address);
46
- // At this point, `address` is a `Base58EncodedAddress` that can be used with the RPC.
47
- const balanceInLamports = await rpc.getBalance(address).send();
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();
48
52
  } catch (e) {
49
- // `address` turned out not to be a base58-encoded address
53
+ // `signature` turned out not to be a base58-encoded signature
50
54
  }
51
55
  }
52
56
  ```
57
+
58
+ ### `generateKeyPair()`
59
+
60
+ Generates an Ed25519 public/private key pair for use with other methods in this package that accept `CryptoKey` objects.
61
+
62
+ ```ts
63
+ import { generateKeyPair } from '@solana/keys';
64
+
65
+ const { privateKey, publicKey } = await generateKeyPair();
66
+ ```
67
+
68
+ ### `isSignature()`
69
+
70
+ 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.
71
+
72
+ ```ts
73
+ import { isSignature } from '@solana/keys';
74
+
75
+ if (isSignature(signature)) {
76
+ // At this point, `signature` has been refined to a
77
+ // `Signature` that can be used with the RPC.
78
+ const {
79
+ value: [status],
80
+ } = await rpc.getSignatureStatuses([signature]).send();
81
+ setSignatureStatus(status);
82
+ } else {
83
+ setError(`${signature} is not a transaction signature`);
84
+ }
85
+ ```
86
+
87
+ ### `signBytes()`
88
+
89
+ Given a private `CryptoKey` and a `Uint8Array` of bytes, this method will return the 64-byte Ed25519 signature of that data as a `Uint8Array`.
90
+
91
+ ```ts
92
+ import { signBytes } from '@solana/keys';
93
+
94
+ const data = new Uint8Array([1, 2, 3]);
95
+ const signature = await signBytes(privateKey, data);
96
+ ```
97
+
98
+ ### `signature()`
99
+
100
+ 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.
101
+
102
+ ```ts
103
+ import { signature } from '@solana/keys';
104
+
105
+ const signature = signature(userSuppliedSignature);
106
+ const {
107
+ value: [status],
108
+ } = await rpc.getSignatureStatuses([signature]).send();
109
+ ```
110
+
111
+ ### `verifySignature()`
112
+
113
+ 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.
114
+
115
+ ```ts
116
+ import { verifySignature } from '@solana/keys';
117
+
118
+ const data = new Uint8Array([1, 2, 3]);
119
+ if (!(await verifySignature(publicKey, signature, data))) {
120
+ throw new Error('The data were *not* signed by the private key associated with `publicKey`');
121
+ }
122
+ ```
@@ -1,44 +1,82 @@
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);
8
-
9
- // src/base58.ts
10
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
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
+ }
21
+ var base58Encoder;
22
+ function assertIsSignature(putativeSignature) {
23
+ if (!base58Encoder)
24
+ base58Encoder = codecsStrings.getBase58Encoder();
11
25
  try {
12
26
  if (
13
- // Lowest address (32 bytes of zeroes)
14
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
15
- putativeBase58EncodedAddress.length > 44
27
+ // Lowest value (64 bytes of zeroes)
28
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
29
+ putativeSignature.length > 88
16
30
  ) {
17
- throw new Error("Expected input string to decode to a byte array of length 32.");
31
+ throw new Error("Expected input string to decode to a byte array of length 64.");
18
32
  }
19
- const bytes = bs58__default.default.decode(putativeBase58EncodedAddress);
33
+ const bytes = base58Encoder.encode(putativeSignature);
20
34
  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}`);
35
+ if (numBytes !== 64) {
36
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
23
37
  }
24
38
  } catch (e) {
25
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
39
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
26
40
  cause: e
27
41
  });
28
42
  }
29
43
  }
30
- function getBase58EncodedAddressComparator() {
31
- return new Intl.Collator("en", {
32
- caseFirst: "lower",
33
- ignorePunctuation: false,
34
- localeMatcher: "best fit",
35
- numeric: false,
36
- sensitivity: "variant",
37
- usage: "sort"
38
- }).compare;
44
+ function isSignature(putativeSignature) {
45
+ if (!base58Encoder)
46
+ base58Encoder = codecsStrings.getBase58Encoder();
47
+ if (
48
+ // Lowest value (64 bytes of zeroes)
49
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
50
+ putativeSignature.length > 88
51
+ ) {
52
+ return false;
53
+ }
54
+ const bytes = base58Encoder.encode(putativeSignature);
55
+ const numBytes = bytes.byteLength;
56
+ if (numBytes !== 64) {
57
+ return false;
58
+ }
59
+ return true;
60
+ }
61
+ async function signBytes(key, data) {
62
+ await assertions.assertSigningCapabilityIsAvailable();
63
+ const signedData = await crypto.subtle.sign("Ed25519", key, data);
64
+ return new Uint8Array(signedData);
65
+ }
66
+ function signature(putativeSignature) {
67
+ assertIsSignature(putativeSignature);
68
+ return putativeSignature;
69
+ }
70
+ async function verifySignature(key, signature2, data) {
71
+ await assertions.assertVerificationCapabilityIsAvailable();
72
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
39
73
  }
40
74
 
41
- exports.assertIsBase58EncodedAddress = assertIsBase58EncodedAddress;
42
- exports.getBase58EncodedAddressComparator = getBase58EncodedAddressComparator;
75
+ exports.assertIsSignature = assertIsSignature;
76
+ exports.generateKeyPair = generateKeyPair;
77
+ exports.isSignature = isSignature;
78
+ exports.signBytes = signBytes;
79
+ exports.signature = signature;
80
+ exports.verifySignature = verifySignature;
43
81
  //# sourceMappingURL=out.js.map
44
82
  //# sourceMappingURL=index.browser.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;AAEO,SAAS,oCAAsE;AAClF,SAAO,IAAI,KAAK,SAAS,MAAM;AAAA,IAC3B,WAAW;AAAA,IACX,mBAAmB;AAAA,IACnB,eAAe;AAAA,IACf,SAAS;AAAA,IACT,aAAa;AAAA,IACb,OAAO;AAAA,EACX,CAAC,EAAE;AACP","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\nexport function getBase58EncodedAddressComparator(): (x: string, y: string) => number {\n return new Intl.Collator('en', {\n caseFirst: 'lower',\n ignorePunctuation: false,\n localeMatcher: 'best fit',\n numeric: false,\n sensitivity: 'variant',\n usage: 'sort',\n }).compare;\n}\n"]}
1
+ {"version":3,"sources":["../src/key-pair.ts","../src/signatures.ts"],"names":["signature"],"mappings":";AAAA,SAAS,sCAAsC;AAE/C,eAAsB,kBAA0C;AAC5D,QAAM,+BAA+B;AACrC,QAAM,UAAU,MAAM,OAAO,OAAO;AAAA;AAAA,IAChB;AAAA;AAAA;AAAA,IACE;AAAA;AAAA;AAAA,IACC,CAAC,QAAQ,QAAQ;AAAA,EACxC;AACA,SAAO;AACX;;;ACVA,SAAS,oCAAoC,+CAA+C;AAE5F,SAAS,wBAAwB;AAKjC,IAAI;AAEG,SAAS,kBAAkB,mBAAmE;AACjG,MAAI,CAAC;AAAe,oBAAgB,iBAAiB;AAErD,MAAI;AAEA;AAAA;AAAA,MAEI,kBAAkB,SAAS;AAAA,MAE3B,kBAAkB,SAAS;AAAA,MAC7B;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,QAAQ,EAAE;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAG;AACR,UAAM,IAAI,MAAM,KAAK,iBAAiB,yBAAyB;AAAA,MAC3D,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ;AAEO,SAAS,YAAY,mBAA2D;AACnF,MAAI,CAAC;AAAe,oBAAgB,iBAAiB;AAGrD;AAAA;AAAA,IAEI,kBAAkB,SAAS;AAAA,IAE3B,kBAAkB,SAAS;AAAA,IAC7B;AACE,WAAO;AAAA,EACX;AAEA,QAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,QAAM,WAAW,MAAM;AACvB,MAAI,aAAa,IAAI;AACjB,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEA,eAAsB,UAAU,KAAgB,MAA2C;AACvF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEO,SAAS,UAAU,mBAAsC;AAC5D,oBAAkB,iBAAiB;AACnC,SAAO;AACX;AAEA,eAAsB,gBAAgB,KAAgBA,YAA2B,MAAoC;AACjH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAKA,YAAW,IAAI;AACrE","sourcesContent":["import { assertKeyGenerationIsAvailable } from '@solana/assertions';\n\nexport async function generateKeyPair(): Promise<CryptoKeyPair> {\n await assertKeyGenerationIsAvailable();\n const keyPair = await crypto.subtle.generateKey(\n /* algorithm */ 'Ed25519', // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20\n /* extractable */ false, // Prevents the bytes of the private key from being visible to JS.\n /* allowed uses */ ['sign', 'verify']\n );\n return keyPair as CryptoKeyPair;\n}\n","import { assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';\nimport { Encoder } from '@solana/codecs-core';\nimport { getBase58Encoder } from '@solana/codecs-strings';\n\nexport type Signature = string & { readonly __brand: unique symbol };\nexport type SignatureBytes = Uint8Array & { readonly __brand: unique symbol };\n\nlet base58Encoder: Encoder<string> | undefined;\n\nexport function assertIsSignature(putativeSignature: string): asserts putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n throw new Error('Expected input string to decode to a byte array of length 64.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeSignature}\\` is not a signature`, {\n cause: e,\n });\n }\n}\n\nexport function isSignature(putativeSignature: string): putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n return false;\n }\n return true;\n}\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<SignatureBytes> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as SignatureBytes;\n}\n\nexport function signature(putativeSignature: string): Signature {\n assertIsSignature(putativeSignature);\n return putativeSignature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: SignatureBytes, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}
@@ -1,37 +1,75 @@
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
+ var base58Encoder;
20
+ function assertIsSignature(putativeSignature) {
21
+ if (!base58Encoder)
22
+ base58Encoder = getBase58Encoder();
5
23
  try {
6
24
  if (
7
- // Lowest address (32 bytes of zeroes)
8
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
9
- putativeBase58EncodedAddress.length > 44
25
+ // Lowest value (64 bytes of zeroes)
26
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
27
+ putativeSignature.length > 88
10
28
  ) {
11
- throw new Error("Expected input string to decode to a byte array of length 32.");
29
+ throw new Error("Expected input string to decode to a byte array of length 64.");
12
30
  }
13
- const bytes = bs58.decode(putativeBase58EncodedAddress);
31
+ const bytes = base58Encoder.encode(putativeSignature);
14
32
  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}`);
33
+ if (numBytes !== 64) {
34
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
17
35
  }
18
36
  } catch (e) {
19
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
37
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
20
38
  cause: e
21
39
  });
22
40
  }
23
41
  }
24
- function getBase58EncodedAddressComparator() {
25
- return new Intl.Collator("en", {
26
- caseFirst: "lower",
27
- ignorePunctuation: false,
28
- localeMatcher: "best fit",
29
- numeric: false,
30
- sensitivity: "variant",
31
- usage: "sort"
32
- }).compare;
42
+ function isSignature(putativeSignature) {
43
+ if (!base58Encoder)
44
+ base58Encoder = getBase58Encoder();
45
+ if (
46
+ // Lowest value (64 bytes of zeroes)
47
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
48
+ putativeSignature.length > 88
49
+ ) {
50
+ return false;
51
+ }
52
+ const bytes = base58Encoder.encode(putativeSignature);
53
+ const numBytes = bytes.byteLength;
54
+ if (numBytes !== 64) {
55
+ return false;
56
+ }
57
+ return true;
58
+ }
59
+ async function signBytes(key, data) {
60
+ await assertSigningCapabilityIsAvailable();
61
+ const signedData = await crypto.subtle.sign("Ed25519", key, data);
62
+ return new Uint8Array(signedData);
63
+ }
64
+ function signature(putativeSignature) {
65
+ assertIsSignature(putativeSignature);
66
+ return putativeSignature;
67
+ }
68
+ async function verifySignature(key, signature2, data) {
69
+ await assertVerificationCapabilityIsAvailable();
70
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
33
71
  }
34
72
 
35
- export { assertIsBase58EncodedAddress, getBase58EncodedAddressComparator };
73
+ export { assertIsSignature, generateKeyPair, isSignature, signBytes, signature, verifySignature };
36
74
  //# sourceMappingURL=out.js.map
37
75
  //# sourceMappingURL=index.browser.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;AAEO,SAAS,oCAAsE;AAClF,SAAO,IAAI,KAAK,SAAS,MAAM;AAAA,IAC3B,WAAW;AAAA,IACX,mBAAmB;AAAA,IACnB,eAAe;AAAA,IACf,SAAS;AAAA,IACT,aAAa;AAAA,IACb,OAAO;AAAA,EACX,CAAC,EAAE;AACP","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\nexport function getBase58EncodedAddressComparator(): (x: string, y: string) => number {\n return new Intl.Collator('en', {\n caseFirst: 'lower',\n ignorePunctuation: false,\n localeMatcher: 'best fit',\n numeric: false,\n sensitivity: 'variant',\n usage: 'sort',\n }).compare;\n}\n"]}
1
+ {"version":3,"sources":["../src/key-pair.ts","../src/signatures.ts"],"names":["signature"],"mappings":";AAAA,SAAS,sCAAsC;AAE/C,eAAsB,kBAA0C;AAC5D,QAAM,+BAA+B;AACrC,QAAM,UAAU,MAAM,OAAO,OAAO;AAAA;AAAA,IAChB;AAAA;AAAA;AAAA,IACE;AAAA;AAAA;AAAA,IACC,CAAC,QAAQ,QAAQ;AAAA,EACxC;AACA,SAAO;AACX;;;ACVA,SAAS,oCAAoC,+CAA+C;AAE5F,SAAS,wBAAwB;AAKjC,IAAI;AAEG,SAAS,kBAAkB,mBAAmE;AACjG,MAAI,CAAC;AAAe,oBAAgB,iBAAiB;AAErD,MAAI;AAEA;AAAA;AAAA,MAEI,kBAAkB,SAAS;AAAA,MAE3B,kBAAkB,SAAS;AAAA,MAC7B;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,QAAQ,EAAE;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAG;AACR,UAAM,IAAI,MAAM,KAAK,iBAAiB,yBAAyB;AAAA,MAC3D,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ;AAEO,SAAS,YAAY,mBAA2D;AACnF,MAAI,CAAC;AAAe,oBAAgB,iBAAiB;AAGrD;AAAA;AAAA,IAEI,kBAAkB,SAAS;AAAA,IAE3B,kBAAkB,SAAS;AAAA,IAC7B;AACE,WAAO;AAAA,EACX;AAEA,QAAM,QAAQ,cAAc,OAAO,iBAAiB;AACpD,QAAM,WAAW,MAAM;AACvB,MAAI,aAAa,IAAI;AACjB,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEA,eAAsB,UAAU,KAAgB,MAA2C;AACvF,QAAM,mCAAmC;AACzC,QAAM,aAAa,MAAM,OAAO,OAAO,KAAK,WAAW,KAAK,IAAI;AAChE,SAAO,IAAI,WAAW,UAAU;AACpC;AAEO,SAAS,UAAU,mBAAsC;AAC5D,oBAAkB,iBAAiB;AACnC,SAAO;AACX;AAEA,eAAsB,gBAAgB,KAAgBA,YAA2B,MAAoC;AACjH,QAAM,wCAAwC;AAC9C,SAAO,MAAM,OAAO,OAAO,OAAO,WAAW,KAAKA,YAAW,IAAI;AACrE","sourcesContent":["import { assertKeyGenerationIsAvailable } from '@solana/assertions';\n\nexport async function generateKeyPair(): Promise<CryptoKeyPair> {\n await assertKeyGenerationIsAvailable();\n const keyPair = await crypto.subtle.generateKey(\n /* algorithm */ 'Ed25519', // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20\n /* extractable */ false, // Prevents the bytes of the private key from being visible to JS.\n /* allowed uses */ ['sign', 'verify']\n );\n return keyPair as CryptoKeyPair;\n}\n","import { assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';\nimport { Encoder } from '@solana/codecs-core';\nimport { getBase58Encoder } from '@solana/codecs-strings';\n\nexport type Signature = string & { readonly __brand: unique symbol };\nexport type SignatureBytes = Uint8Array & { readonly __brand: unique symbol };\n\nlet base58Encoder: Encoder<string> | undefined;\n\nexport function assertIsSignature(putativeSignature: string): asserts putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n throw new Error('Expected input string to decode to a byte array of length 64.');\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeSignature}\\` is not a signature`, {\n cause: e,\n });\n }\n}\n\nexport function isSignature(putativeSignature: string): putativeSignature is Signature {\n if (!base58Encoder) base58Encoder = getBase58Encoder();\n\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest value (64 bytes of zeroes)\n putativeSignature.length < 64 ||\n // Highest value (64 bytes of 255)\n putativeSignature.length > 88\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const bytes = base58Encoder.encode(putativeSignature);\n const numBytes = bytes.byteLength;\n if (numBytes !== 64) {\n return false;\n }\n return true;\n}\n\nexport async function signBytes(key: CryptoKey, data: Uint8Array): Promise<SignatureBytes> {\n await assertSigningCapabilityIsAvailable();\n const signedData = await crypto.subtle.sign('Ed25519', key, data);\n return new Uint8Array(signedData) as SignatureBytes;\n}\n\nexport function signature(putativeSignature: string): Signature {\n assertIsSignature(putativeSignature);\n return putativeSignature;\n}\n\nexport async function verifySignature(key: CryptoKey, signature: SignatureBytes, data: Uint8Array): Promise<boolean> {\n await assertVerificationCapabilityIsAvailable();\n return await crypto.subtle.verify('Ed25519', key, signature, data);\n}\n"]}