@solana/keys 2.0.0-experimental.ee9f3d8 → 2.0.0-experimental.eeb355e

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,39 @@ 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/keys';
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
  ```
@@ -61,12 +65,58 @@ import { generateKeyPair } from '@solana/keys';
61
65
  const { privateKey, publicKey } = await generateKeyPair();
62
66
  ```
63
67
 
64
- ### `getBase58EncodedAddressFromPublicKey()`
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()`
65
99
 
66
- Given a public `CryptoKey`, this method will return its associated `Base58EncodedAddress`.
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.
67
101
 
68
102
  ```ts
69
- import { getBase58EncodedAddressFromPublicKey } from '@solana/keys';
103
+ import { signature } from '@solana/keys';
104
+
105
+ const signature = signature(userSuppliedSignature);
106
+ const {
107
+ value: [status],
108
+ } = await rpc.getSignatureStatuses([signature]).send();
109
+ ```
70
110
 
71
- const address = await getBase58EncodedAddressFromPublicKey(publicKey);
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
+ }
72
122
  ```
@@ -1,124 +1,82 @@
1
1
  'use strict';
2
2
 
3
- var umiSerializers = require('@metaplex-foundation/umi-serializers');
3
+ var assertions = require('@solana/assertions');
4
+ var codecsStrings = require('@solana/codecs-strings');
4
5
 
5
- // ../build-scripts/env-shim.ts
6
- var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
7
- 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();
8
25
  try {
9
26
  if (
10
- // Lowest address (32 bytes of zeroes)
11
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
12
- putativeBase58EncodedAddress.length > 44
27
+ // Lowest value (64 bytes of zeroes)
28
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
29
+ putativeSignature.length > 88
13
30
  ) {
14
- 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.");
15
32
  }
16
- const bytes = umiSerializers.base58.serialize(putativeBase58EncodedAddress);
33
+ const bytes = base58Encoder.encode(putativeSignature);
17
34
  const numBytes = bytes.byteLength;
18
- if (numBytes !== 32) {
19
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
35
+ if (numBytes !== 64) {
36
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
20
37
  }
21
38
  } catch (e) {
22
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
39
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
23
40
  cause: e
24
41
  });
25
42
  }
26
43
  }
27
- function getBase58EncodedAddressCodec(config) {
28
- return umiSerializers.string({
29
- description: config?.description ?? (__DEV__ ? "A 32-byte account address" : ""),
30
- encoding: umiSerializers.base58,
31
- size: 32
32
- });
33
- }
34
- function getBase58EncodedAddressComparator() {
35
- return new Intl.Collator("en", {
36
- caseFirst: "lower",
37
- ignorePunctuation: false,
38
- localeMatcher: "best fit",
39
- numeric: false,
40
- sensitivity: "variant",
41
- usage: "sort"
42
- }).compare;
43
- }
44
-
45
- // src/guard.ts
46
- function assertIsSecureContext() {
47
- if (!globalThis.isSecureContext) {
48
- throw new Error(
49
- "Cryptographic operations are only allowed in secure browser contexts. Read more here: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts"
50
- );
51
- }
52
- }
53
- var cachedEd25519Decision;
54
- async function isEd25519CurveSupported(subtle) {
55
- if (cachedEd25519Decision === void 0) {
56
- cachedEd25519Decision = new Promise((resolve) => {
57
- subtle.generateKey(
58
- "Ed25519",
59
- /* extractable */
60
- false,
61
- ["sign", "verify"]
62
- ).catch(() => {
63
- resolve(cachedEd25519Decision = false);
64
- }).then(() => {
65
- resolve(cachedEd25519Decision = true);
66
- });
67
- });
68
- }
69
- if (typeof cachedEd25519Decision === "boolean") {
70
- return cachedEd25519Decision;
71
- } else {
72
- return await cachedEd25519Decision;
73
- }
74
- }
75
- async function assertKeyGenerationIsAvailable() {
76
- assertIsSecureContext();
77
- if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.generateKey !== "function") {
78
- throw new Error("No key generation implementation could be found");
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;
79
53
  }
80
- if (!await isEd25519CurveSupported(globalThis.crypto.subtle)) {
81
- throw new Error("This runtime does not support the generation of Ed25519 keypairs");
54
+ const bytes = base58Encoder.encode(putativeSignature);
55
+ const numBytes = bytes.byteLength;
56
+ if (numBytes !== 64) {
57
+ return false;
82
58
  }
59
+ return true;
83
60
  }
84
- async function assertKeyExporterIsAvailable() {
85
- assertIsSecureContext();
86
- if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.exportKey !== "function") {
87
- throw new Error("No key export implementation could be found");
88
- }
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);
89
65
  }
90
-
91
- // src/key-pair.ts
92
- async function generateKeyPair() {
93
- await assertKeyGenerationIsAvailable();
94
- const keyPair = await crypto.subtle.generateKey(
95
- /* algorithm */
96
- "Ed25519",
97
- // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
98
- /* extractable */
99
- false,
100
- // Prevents the bytes of the private key from being visible to JS.
101
- /* allowed uses */
102
- ["sign", "verify"]
103
- );
104
- return keyPair;
66
+ function signature(putativeSignature) {
67
+ assertIsSignature(putativeSignature);
68
+ return putativeSignature;
105
69
  }
106
-
107
- // src/pubkey.ts
108
- async function getBase58EncodedAddressFromPublicKey(publicKey) {
109
- await assertKeyExporterIsAvailable();
110
- if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
111
- throw new Error("The `CryptoKey` must be an `Ed25519` public key");
112
- }
113
- const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
114
- const [base58EncodedAddress] = getBase58EncodedAddressCodec().deserialize(new Uint8Array(publicKeyBytes));
115
- return base58EncodedAddress;
70
+ async function verifySignature(key, signature2, data) {
71
+ await assertions.assertVerificationCapabilityIsAvailable();
72
+ return await crypto.subtle.verify("Ed25519", key, signature2, data);
116
73
  }
117
74
 
118
- exports.assertIsBase58EncodedAddress = assertIsBase58EncodedAddress;
75
+ exports.assertIsSignature = assertIsSignature;
119
76
  exports.generateKeyPair = generateKeyPair;
120
- exports.getBase58EncodedAddressCodec = getBase58EncodedAddressCodec;
121
- exports.getBase58EncodedAddressComparator = getBase58EncodedAddressComparator;
122
- exports.getBase58EncodedAddressFromPublicKey = getBase58EncodedAddressFromPublicKey;
77
+ exports.isSignature = isSignature;
78
+ exports.signBytes = signBytes;
79
+ exports.signature = signature;
80
+ exports.verifySignature = verifySignature;
123
81
  //# sourceMappingURL=out.js.map
124
82
  //# sourceMappingURL=index.browser.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../build-scripts/env-shim.ts","../src/base58.ts","../src/guard.ts","../src/key-pair.ts","../src/pubkey.ts"],"names":[],"mappings":";AACO,IAAM,UAA2B,uBAAO,QAAgB,KAAU,EAAE,aAAa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,IACT,CAAC;AAAA,EACL;AACA,MAAI,OAAO,0BAA0B,WAAW;AAC5C,WAAO;AAAA,EACX,OAAO;AACH,WAAO,MAAM;AAAA,EACjB;AACJ;AAEA,eAAsB,iCAAiC;AACnD,wBAAsB;AACtB,MAAI,OAAO,WAAW,WAAW,eAAe,OAAO,WAAW,OAAO,QAAQ,gBAAgB,YAAY;AAEzG,UAAM,IAAI,MAAM,iDAAiD;AAAA,EACrE;AACA,MAAI,CAAE,MAAM,wBAAwB,WAAW,OAAO,MAAM,GAAI;AAE5D,UAAM,IAAI,MAAM,kEAAkE;AAAA,EACtF;AACJ;AAEA,eAAsB,+BAA+B;AACjD,wBAAsB;AACtB,MAAI,OAAO,WAAW,WAAW,eAAe,OAAO,WAAW,OAAO,QAAQ,cAAc,YAAY;AAEvG,UAAM,IAAI,MAAM,6CAA6C;AAAA,EACjE;AACJ;;;AC/CA,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;;;ACPA,eAAsB,qCAAqC,WAAqD;AAC5G,QAAM,6BAA6B;AACnC,MAAI,UAAU,SAAS,YAAY,UAAU,UAAU,SAAS,WAAW;AAEvE,UAAM,IAAI,MAAM,iDAAiD;AAAA,EACrE;AACA,QAAM,iBAAiB,MAAM,OAAO,OAAO,UAAU,OAAO,SAAS;AACrE,QAAM,CAAC,oBAAoB,IAAI,6BAA6B,EAAE,YAAY,IAAI,WAAW,cAAc,CAAC;AACxG,SAAO;AACX","sourcesContent":["// Clever obfuscation to prevent the build system from inlining the value of `NODE_ENV`\nexport const __DEV__ = /* @__PURE__ */ (() => (process as any)['en' + 'v'].NODE_ENV === 'development')();\n","import { base58, Serializer, string } from '@metaplex-foundation/umi-serializers';\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 = base58.serialize(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 getBase58EncodedAddressCodec(\n config?: Readonly<{\n description: string;\n }>\n): Serializer<Base58EncodedAddress> {\n return string({\n description: config?.description ?? (__DEV__ ? 'A 32-byte account address' : ''),\n encoding: base58,\n size: 32,\n }) as unknown as Serializer<Base58EncodedAddress>;\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","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 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('This runtime does not support the generation of Ed25519 keypairs');\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 './guard';\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 { Base58EncodedAddress, getBase58EncodedAddressCodec } from './base58';\nimport { assertKeyExporterIsAvailable } from './guard';\n\nexport async function getBase58EncodedAddressFromPublicKey(publicKey: CryptoKey): Promise<Base58EncodedAddress> {\n await assertKeyExporterIsAvailable();\n if (publicKey.type !== 'public' || publicKey.algorithm.name !== 'Ed25519') {\n // TODO: Coded error.\n throw new Error('The `CryptoKey` must be an `Ed25519` public key');\n }\n const publicKeyBytes = await crypto.subtle.exportKey('raw', publicKey);\n const [base58EncodedAddress] = getBase58EncodedAddressCodec().deserialize(new Uint8Array(publicKeyBytes));\n return base58EncodedAddress;\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,90 +1,5 @@
1
- import { base58, string } from '@metaplex-foundation/umi-serializers';
2
-
3
- // ../build-scripts/env-shim.ts
4
- var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
5
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
6
- try {
7
- if (
8
- // Lowest address (32 bytes of zeroes)
9
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
10
- putativeBase58EncodedAddress.length > 44
11
- ) {
12
- throw new Error("Expected input string to decode to a byte array of length 32.");
13
- }
14
- const bytes = base58.serialize(putativeBase58EncodedAddress);
15
- const numBytes = bytes.byteLength;
16
- if (numBytes !== 32) {
17
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
18
- }
19
- } catch (e) {
20
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
21
- cause: e
22
- });
23
- }
24
- }
25
- function getBase58EncodedAddressCodec(config) {
26
- return string({
27
- description: config?.description ?? (__DEV__ ? "A 32-byte account address" : ""),
28
- encoding: base58,
29
- size: 32
30
- });
31
- }
32
- function getBase58EncodedAddressComparator() {
33
- return new Intl.Collator("en", {
34
- caseFirst: "lower",
35
- ignorePunctuation: false,
36
- localeMatcher: "best fit",
37
- numeric: false,
38
- sensitivity: "variant",
39
- usage: "sort"
40
- }).compare;
41
- }
42
-
43
- // src/guard.ts
44
- function assertIsSecureContext() {
45
- if (!globalThis.isSecureContext) {
46
- throw new Error(
47
- "Cryptographic operations are only allowed in secure browser contexts. Read more here: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts"
48
- );
49
- }
50
- }
51
- var cachedEd25519Decision;
52
- async function isEd25519CurveSupported(subtle) {
53
- if (cachedEd25519Decision === void 0) {
54
- cachedEd25519Decision = new Promise((resolve) => {
55
- subtle.generateKey(
56
- "Ed25519",
57
- /* extractable */
58
- false,
59
- ["sign", "verify"]
60
- ).catch(() => {
61
- resolve(cachedEd25519Decision = false);
62
- }).then(() => {
63
- resolve(cachedEd25519Decision = true);
64
- });
65
- });
66
- }
67
- if (typeof cachedEd25519Decision === "boolean") {
68
- return cachedEd25519Decision;
69
- } else {
70
- return await cachedEd25519Decision;
71
- }
72
- }
73
- async function assertKeyGenerationIsAvailable() {
74
- assertIsSecureContext();
75
- if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.generateKey !== "function") {
76
- throw new Error("No key generation implementation could be found");
77
- }
78
- if (!await isEd25519CurveSupported(globalThis.crypto.subtle)) {
79
- throw new Error("This runtime does not support the generation of Ed25519 keypairs");
80
- }
81
- }
82
- async function assertKeyExporterIsAvailable() {
83
- assertIsSecureContext();
84
- if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.exportKey !== "function") {
85
- throw new Error("No key export implementation could be found");
86
- }
87
- }
1
+ import { assertKeyGenerationIsAvailable, assertSigningCapabilityIsAvailable, assertVerificationCapabilityIsAvailable } from '@solana/assertions';
2
+ import { getBase58Encoder } from '@solana/codecs-strings';
88
3
 
89
4
  // src/key-pair.ts
90
5
  async function generateKeyPair() {
@@ -101,18 +16,60 @@ async function generateKeyPair() {
101
16
  );
102
17
  return keyPair;
103
18
  }
104
-
105
- // src/pubkey.ts
106
- async function getBase58EncodedAddressFromPublicKey(publicKey) {
107
- await assertKeyExporterIsAvailable();
108
- if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
109
- throw new Error("The `CryptoKey` must be an `Ed25519` public key");
19
+ var base58Encoder;
20
+ function assertIsSignature(putativeSignature) {
21
+ if (!base58Encoder)
22
+ base58Encoder = getBase58Encoder();
23
+ try {
24
+ if (
25
+ // Lowest value (64 bytes of zeroes)
26
+ putativeSignature.length < 64 || // Highest value (64 bytes of 255)
27
+ putativeSignature.length > 88
28
+ ) {
29
+ throw new Error("Expected input string to decode to a byte array of length 64.");
30
+ }
31
+ const bytes = base58Encoder.encode(putativeSignature);
32
+ const numBytes = bytes.byteLength;
33
+ if (numBytes !== 64) {
34
+ throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
35
+ }
36
+ } catch (e) {
37
+ throw new Error(`\`${putativeSignature}\` is not a signature`, {
38
+ cause: e
39
+ });
40
+ }
41
+ }
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;
110
56
  }
111
- const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
112
- const [base58EncodedAddress] = getBase58EncodedAddressCodec().deserialize(new Uint8Array(publicKeyBytes));
113
- return base58EncodedAddress;
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);
114
71
  }
115
72
 
116
- export { assertIsBase58EncodedAddress, generateKeyPair, getBase58EncodedAddressCodec, getBase58EncodedAddressComparator, getBase58EncodedAddressFromPublicKey };
73
+ export { assertIsSignature, generateKeyPair, isSignature, signBytes, signature, verifySignature };
117
74
  //# sourceMappingURL=out.js.map
118
75
  //# sourceMappingURL=index.browser.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../build-scripts/env-shim.ts","../src/base58.ts","../src/guard.ts","../src/key-pair.ts","../src/pubkey.ts"],"names":[],"mappings":";AACO,IAAM,UAA2B,uBAAO,QAAgB,KAAU,EAAE,aAAa,eAAe;;;ACDvG,SAAS,QAAoB,cAAc;AAMpC,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,OAAO,UAAU,4BAA4B;AAC3D,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,UAAU;A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GAAI;AAE5D,UAAM,IAAI,MAAM,kEAAkE;AAAA,EACtF;AACJ;AAEA,eAAsB,+BAA+B;AACjD,wBAAsB;AACtB,MAAI,OAAO,WAAW,WAAW,eAAe,OAAO,WAAW,OAAO,QAAQ,cAAc,YAAY;AAEvG,UAAM,IAAI,MAAM,6CAA6C;AAAA,EACjE;AACJ;;;AC/CA,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;;;ACPA,eAAsB,qCAAqC,WAAqD;AAC5G,QAAM,6BAA6B;AACnC,MAAI,UAAU,SAAS,YAAY,UAAU,UAAU,SAAS,WAAW;AAEvE,UAAM,IAAI,MAAM,iDAAiD;AAAA,EACrE;AACA,QAAM,iBAAiB,MAAM,OAAO,OAAO,UAAU,OAAO,SAAS;AACrE,QAAM,CAAC,oBAAoB,IAAI,6BAA6B,EAAE,YAAY,IAAI,WAAW,cAAc,CAAC;AACxG,SAAO;AACX","sourcesContent":["// Clever obfuscation to prevent the build system from inlining the value of `NODE_ENV`\nexport const __DEV__ = /* @__PURE__ */ (() => (process as any)['en' + 'v'].NODE_ENV === 'development')();\n","import { base58, Serializer, string } from '@metaplex-foundation/umi-serializers';\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 = base58.serialize(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 getBase58EncodedAddressCodec(\n config?: Readonly<{\n description: string;\n }>\n): Serializer<Base58EncodedAddress> {\n return string({\n description: config?.description ?? (__DEV__ ? 'A 32-byte account address' : ''),\n encoding: base58,\n size: 32,\n }) as unknown as Serializer<Base58EncodedAddress>;\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","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 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('This runtime does not support the generation of Ed25519 keypairs');\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 './guard';\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 { Base58EncodedAddress, getBase58EncodedAddressCodec } from './base58';\nimport { assertKeyExporterIsAvailable } from './guard';\n\nexport async function getBase58EncodedAddressFromPublicKey(publicKey: CryptoKey): Promise<Base58EncodedAddress> {\n await assertKeyExporterIsAvailable();\n if (publicKey.type !== 'public' || publicKey.algorithm.name !== 'Ed25519') {\n // TODO: Coded error.\n throw new Error('The `CryptoKey` must be an `Ed25519` public key');\n }\n const publicKeyBytes = await crypto.subtle.exportKey('raw', publicKey);\n const [base58EncodedAddress] = getBase58EncodedAddressCodec().deserialize(new Uint8Array(publicKeyBytes));\n return base58EncodedAddress;\n}\n"]}
1
+ {"version":3,"sources":["../src/key-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ACpC;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"]}