@solana/webcrypto-ed25519-polyfill 2.0.0-experimental.6cedd3a
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 +20 -0
- package/README.md +35 -0
- package/dist/index.browser.cjs +110 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.js +108 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.development.js +1633 -0
- package/dist/index.development.js.map +1 -0
- package/dist/index.native.js +108 -0
- package/dist/index.native.js.map +1 -0
- package/dist/index.node.cjs +110 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.js +108 -0
- package/dist/index.node.js.map +1 -0
- package/dist/index.production.min.js +29 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/secrets.d.ts +8 -0
- package/package.json +98 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2018 Solana Labs, Inc
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[![npm][npm-image]][npm-url]
|
|
2
|
+
[![npm-downloads][npm-downloads-image]][npm-url]
|
|
3
|
+
[![semantic-release][semantic-release-image]][semantic-release-url]
|
|
4
|
+
<br />
|
|
5
|
+
[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]
|
|
6
|
+
|
|
7
|
+
[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
|
|
8
|
+
[code-style-prettier-url]: https://github.com/prettier/prettier
|
|
9
|
+
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/webcrypto-ed25519-polyfill/experimental.svg?style=flat
|
|
10
|
+
[npm-image]: https://img.shields.io/npm/v/@solana/webcrypto-ed25519-polyfill/experimental.svg?style=flat
|
|
11
|
+
[npm-url]: https://www.npmjs.com/package/@solana/webcrypto-ed25519-polyfill/v/experimental
|
|
12
|
+
[semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
|
13
|
+
[semantic-release-url]: https://github.com/semantic-release/semantic-release
|
|
14
|
+
|
|
15
|
+
# @solana/webcrypto-ed25519-polyfill
|
|
16
|
+
|
|
17
|
+
This package contains a polyfill that enables Ed25519 key manipulation in environments where it is not yet implemented. It does so by proxying calls to `SubtleCrypto` instance methods to an Ed25519 implementation in userspace.
|
|
18
|
+
|
|
19
|
+
## Security warning
|
|
20
|
+
|
|
21
|
+
Because this package's implementation of Ed25519 key generation exists in userspace, it can't guarantee that the keys you generate with it are non-exportable. Untrusted code running in your JavaScript context may still be able to gain access to and/or exfiltrate secret key material.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
Environments that support Ed25519 (see https://github.com/WICG/webcrypto-secure-curves/issues/20) do not require this polyfill.
|
|
26
|
+
|
|
27
|
+
For all others, simply import this polyfill before use.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// Importing this will shim methods on `SubtleCrypto`, adding Ed25519 support.
|
|
31
|
+
import '@solana/webcrypto-ed25519-polyfill';
|
|
32
|
+
|
|
33
|
+
// Now you can do this, in environments that do not otherwise support Ed25519.
|
|
34
|
+
const keyPair = await crypto.subtle.generateKey('Ed25519', false, ['sign']);
|
|
35
|
+
```
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ed25519 = require('@noble/curves/ed25519');
|
|
4
|
+
|
|
5
|
+
// ../build-scripts/env-shim.ts
|
|
6
|
+
var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
|
|
7
|
+
var PROHIBITED_KEY_USAGES = /* @__PURE__ */ new Set([
|
|
8
|
+
"decrypt",
|
|
9
|
+
"deriveBits",
|
|
10
|
+
"deriveKey",
|
|
11
|
+
"encrypt",
|
|
12
|
+
"unwrapKey",
|
|
13
|
+
"wrapKey"
|
|
14
|
+
]);
|
|
15
|
+
var storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT;
|
|
16
|
+
function createKeyPairFromBytes(bytes, extractable, keyUsages) {
|
|
17
|
+
const keyPair = createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages);
|
|
18
|
+
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT || (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT = /* @__PURE__ */ new WeakMap());
|
|
19
|
+
cache.set(keyPair.privateKey, bytes);
|
|
20
|
+
cache.set(keyPair.publicKey, bytes);
|
|
21
|
+
return keyPair;
|
|
22
|
+
}
|
|
23
|
+
function createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages) {
|
|
24
|
+
if (keyUsages.length === 0) {
|
|
25
|
+
throw new DOMException("Usages cannot be empty when creating a key.", "SyntaxError");
|
|
26
|
+
}
|
|
27
|
+
if (keyUsages.some((usage) => PROHIBITED_KEY_USAGES.has(usage))) {
|
|
28
|
+
throw new DOMException("Unsupported key usage for an Ed25519 key.", "SyntaxError");
|
|
29
|
+
}
|
|
30
|
+
const base = {
|
|
31
|
+
[Symbol.toStringTag]: "CryptoKey",
|
|
32
|
+
algorithm: Object.freeze({ name: "Ed25519" })
|
|
33
|
+
};
|
|
34
|
+
const privateKey = {
|
|
35
|
+
...base,
|
|
36
|
+
extractable,
|
|
37
|
+
type: "private",
|
|
38
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "sign"))
|
|
39
|
+
};
|
|
40
|
+
const publicKey = {
|
|
41
|
+
...base,
|
|
42
|
+
extractable: true,
|
|
43
|
+
type: "public",
|
|
44
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "verify"))
|
|
45
|
+
};
|
|
46
|
+
return Object.freeze({
|
|
47
|
+
privateKey: Object.freeze(privateKey),
|
|
48
|
+
publicKey: Object.freeze(publicKey)
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function generateKeyPolyfill(extractable, keyUsages) {
|
|
52
|
+
const privateKeyBytes = ed25519.ed25519.utils.randomPrivateKey();
|
|
53
|
+
const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);
|
|
54
|
+
return keyPair;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/index.ts
|
|
58
|
+
if (globalThis.isSecureContext) {
|
|
59
|
+
const originalCryptoObject = globalThis.crypto || (globalThis.crypto = {});
|
|
60
|
+
const originalSubtleCrypto = originalCryptoObject.subtle || (originalCryptoObject.subtle = {});
|
|
61
|
+
const originalGenerateKey = originalSubtleCrypto.generateKey;
|
|
62
|
+
let originalGenerateKeySupportsEd25519;
|
|
63
|
+
originalSubtleCrypto.generateKey = async (...args) => {
|
|
64
|
+
const [algorithm] = args;
|
|
65
|
+
if (algorithm !== "Ed25519") {
|
|
66
|
+
if (originalGenerateKey) {
|
|
67
|
+
return await originalGenerateKey.apply(originalSubtleCrypto, args);
|
|
68
|
+
} else {
|
|
69
|
+
throw new TypeError("No native `generateKey` function exists to handle this call");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
let optimisticallyGeneratedKeyPair;
|
|
73
|
+
if (originalGenerateKeySupportsEd25519 === void 0) {
|
|
74
|
+
originalGenerateKeySupportsEd25519 = new Promise((resolve) => {
|
|
75
|
+
if (!originalGenerateKey) {
|
|
76
|
+
resolve(originalGenerateKeySupportsEd25519 = false);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
originalGenerateKey.apply(originalSubtleCrypto, args).then((keyPair) => {
|
|
80
|
+
if (__DEV__) {
|
|
81
|
+
console.warn(
|
|
82
|
+
"`@solana/webcrypto-ed25519-polyfill` was included in an environment that supports Ed25519 key manipulation natively. Falling back to the native implementation. Consider including this polyfill only in environments where Ed25519 is not supported."
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (originalSubtleCrypto.generateKey !== originalGenerateKey) {
|
|
86
|
+
originalSubtleCrypto.generateKey = originalGenerateKey;
|
|
87
|
+
}
|
|
88
|
+
optimisticallyGeneratedKeyPair = keyPair;
|
|
89
|
+
resolve(originalGenerateKeySupportsEd25519 = true);
|
|
90
|
+
}).catch(() => {
|
|
91
|
+
resolve(originalGenerateKeySupportsEd25519 = false);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (typeof originalGenerateKeySupportsEd25519 === "boolean" ? originalGenerateKeySupportsEd25519 : await originalGenerateKeySupportsEd25519) {
|
|
96
|
+
if (optimisticallyGeneratedKeyPair) {
|
|
97
|
+
return optimisticallyGeneratedKeyPair;
|
|
98
|
+
} else if (originalGenerateKey) {
|
|
99
|
+
return await originalGenerateKey.apply(originalSubtleCrypto, args);
|
|
100
|
+
} else {
|
|
101
|
+
throw new TypeError("No native `generateKey` function exists to handle this call");
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
const [_, extractable, keyUsages] = args;
|
|
105
|
+
return generateKeyPolyfill(extractable, keyUsages);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=out.js.map
|
|
110
|
+
//# sourceMappingURL=index.browser.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../build-scripts/env-shim.ts","../src/secrets.ts","../src/index.ts"],"names":[],"mappings":";AACO,IAAM,UAA2B,uBAAO,QAAgB,KAAU,EAAE,aAAa,eAAe;;;ACcvG,SAAS,eAAe;AAExB,IAAM,wBAAwB,oBAAI,IAAc;AAAA,EAC5C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ,CAAC;AAED,IAAI;AACJ,SAAS,uBACL,OACA,aACA,WACa;AACb,QAAM,UAAU,0CAA0C,aAAa,SAAS;AAChF,QAAM,QAAS,0GAAsD,oBAAI,QAAQ;AACjF,QAAM,IAAI,QAAQ,YAAY,KAAK;AACnC,QAAM,IAAI,QAAQ,WAAW,KAAK;AAClC,SAAO;AACX;AAEA,SAAS,0CACL,aACA,WACa;AACb,MAAI,UAAU,WAAW,GAAG;AACxB,UAAM,IAAI,aAAa,+CAA+C,aAAa;AAAA,EACvF;AACA,MAAI,UAAU,KAAK,WAAS,sBAAsB,IAAI,KAAK,CAAC,GAAG;AAC3D,UAAM,IAAI,aAAa,6CAA6C,aAAa;AAAA,EACrF;AACA,QAAM,OAAO;AAAA,IACT,CAAC,OAAO,WAAW,GAAG;AAAA,IACtB,WAAW,OAAO,OAAO,EAAE,MAAM,UAAU,CAAC;AAAA,EAChD;AACA,QAAM,aAAa;AAAA,IACf,GAAG;AAAA,IACH;AAAA,IACA,MAAM;AAAA,IACN,QAAQ,OAAO,OAAO,UA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;AAAA,QACvD,CAAC,EACA,MAAM,MAAM;AACT,kBAAS,qCAAqC,KAAM;AAAA,QACxD,CAAC;AAAA,MACT,CAAC;AAAA,IACL;AACA,QACI,OAAO,uCAAuC,YACxC,qCACA,MAAM,oCACd;AACE,UAAI,gCAAgC;AAChC,eAAO;AAAA,MACX,WAAW,qBAAqB;AAC5B,eAAO,MAAM,oBAAoB,MAAM,sBAAsB,IAAI;AAAA,MACrE,OAAO;AACH,cAAM,IAAI,UAAU,6DAA6D;AAAA,MACrF;AAAA,IACJ,OAAO;AACH,YAAM,CAAC,GAAG,aAAa,SAAS,IAAI;AACpC,aAAO,oBAAoB,aAAa,SAAS;AAAA,IACrD;AAAA,EACJ;AACJ","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","/**\n * HEY! <== SECRET KEY KOALA\n * |/ <== WOULD LIKE YOUR\n * ʕ·͡ᴥ·ʔ <== ATTENTION PLEASE\n *\n * Key material generated in this module must stay in this module. So long as the secrets cache and\n * the methods that interact with it are not exported from `@solana/webcrypto-ed25519-polyfill`,\n * accidental logging of the actual bytes of a secret key (eg. to the console, or to a remote\n * server) should not be possible.\n *\n * WARNING: This does not imply that the secrets cache is secure against supply-chain attacks.\n * Untrusted code in your JavaScript context can easily override `WeakMap.prototype.set` to steal\n * private keys as they are written to the cache, without alerting you to its presence or affecting\n * the regular operation of the cache.\n */\nimport { ed25519 } from '@noble/curves/ed25519';\n\nconst PROHIBITED_KEY_USAGES = new Set<KeyUsage>([\n 'decrypt',\n 'deriveBits',\n 'deriveKey',\n 'encrypt',\n 'unwrapKey',\n 'wrapKey',\n]);\n\nlet storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT: WeakMap<CryptoKey, Uint8Array> | undefined;\nfunction createKeyPairFromBytes(\n bytes: Uint8Array,\n extractable: boolean,\n keyUsages: readonly KeyUsage[]\n): CryptoKeyPair {\n const keyPair = createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages);\n const cache = (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT ||= new WeakMap());\n cache.set(keyPair.privateKey, bytes);\n cache.set(keyPair.publicKey, bytes);\n return keyPair;\n}\n\nfunction createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(\n extractable: boolean,\n keyUsages: readonly KeyUsage[]\n): CryptoKeyPair {\n if (keyUsages.length === 0) {\n throw new DOMException('Usages cannot be empty when creating a key.', 'SyntaxError');\n }\n if (keyUsages.some(usage => PROHIBITED_KEY_USAGES.has(usage))) {\n throw new DOMException('Unsupported key usage for an Ed25519 key.', 'SyntaxError');\n }\n const base = {\n [Symbol.toStringTag]: 'CryptoKey',\n algorithm: Object.freeze({ name: 'Ed25519' }),\n };\n const privateKey = {\n ...base,\n extractable,\n type: 'private',\n usages: Object.freeze(keyUsages.filter(usage => usage === 'sign')) as KeyUsage[],\n } as CryptoKey;\n const publicKey = {\n ...base,\n extractable: true,\n type: 'public',\n usages: Object.freeze(keyUsages.filter(usage => usage === 'verify')) as KeyUsage[],\n } as CryptoKey;\n return Object.freeze({\n privateKey: Object.freeze(privateKey),\n publicKey: Object.freeze(publicKey),\n });\n}\n\n/**\n * This function generates a key pair and stores the secret bytes associated with it in a\n * module-private cache. Instead of vending the actual secret bytes, it returns a `CryptoKeyPair`\n * that you can use with other methods in this package to produce signatures and derive public keys\n * associated with the secret.\n */\nexport function generateKeyPolyfill(extractable: boolean, keyUsages: readonly KeyUsage[]): CryptoKeyPair {\n const privateKeyBytes = ed25519.utils.randomPrivateKey();\n const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);\n return keyPair;\n}\n","import { generateKeyPolyfill } from './secrets';\n\nif (!__BROWSER__ || globalThis.isSecureContext) {\n /**\n * Create `crypto.subtle` if it doesn't exist.\n */\n const originalCryptoObject = (globalThis.crypto ||= {} as Crypto);\n const originalSubtleCrypto = ((originalCryptoObject as Crypto & { subtle: SubtleCrypto }).subtle ||=\n {} as SubtleCrypto);\n\n /**\n * Override `SubtleCrypto#generateKey`\n */\n const originalGenerateKey = originalSubtleCrypto.generateKey as SubtleCrypto['generateKey'] | undefined;\n let originalGenerateKeySupportsEd25519: Promise<boolean> | boolean | undefined;\n originalSubtleCrypto.generateKey = (async (...args: Parameters<SubtleCrypto['generateKey']>) => {\n const [algorithm] = args;\n if (algorithm !== 'Ed25519') {\n if (originalGenerateKey) {\n return await originalGenerateKey.apply(originalSubtleCrypto, args);\n } else {\n throw new TypeError('No native `generateKey` function exists to handle this call');\n }\n }\n let optimisticallyGeneratedKeyPair;\n if (originalGenerateKeySupportsEd25519 === undefined) {\n originalGenerateKeySupportsEd25519 = new Promise(resolve => {\n if (!originalGenerateKey) {\n resolve((originalGenerateKeySupportsEd25519 = false));\n return;\n }\n originalGenerateKey\n .apply(originalSubtleCrypto, args)\n .then(keyPair => {\n if (__DEV__) {\n console.warn(\n '`@solana/webcrypto-ed25519-polyfill` was included in an ' +\n 'environment that supports Ed25519 key manipulation ' +\n 'natively. Falling back to the native implementation. ' +\n 'Consider including this polyfill only in environments where ' +\n 'Ed25519 is not supported.'\n );\n }\n if (originalSubtleCrypto.generateKey !== originalGenerateKey) {\n originalSubtleCrypto.generateKey = originalGenerateKey;\n }\n optimisticallyGeneratedKeyPair = keyPair;\n resolve((originalGenerateKeySupportsEd25519 = true));\n })\n .catch(() => {\n resolve((originalGenerateKeySupportsEd25519 = false));\n });\n });\n }\n if (\n typeof originalGenerateKeySupportsEd25519 === 'boolean'\n ? originalGenerateKeySupportsEd25519\n : await originalGenerateKeySupportsEd25519\n ) {\n if (optimisticallyGeneratedKeyPair) {\n return optimisticallyGeneratedKeyPair;\n } else if (originalGenerateKey) {\n return await originalGenerateKey.apply(originalSubtleCrypto, args);\n } else {\n throw new TypeError('No native `generateKey` function exists to handle this call');\n }\n } else {\n const [_, extractable, keyUsages] = args;\n return generateKeyPolyfill(extractable, keyUsages);\n }\n }) as SubtleCrypto['generateKey'];\n}\n"]}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
2
|
+
|
|
3
|
+
// ../build-scripts/env-shim.ts
|
|
4
|
+
var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
|
|
5
|
+
var PROHIBITED_KEY_USAGES = /* @__PURE__ */ new Set([
|
|
6
|
+
"decrypt",
|
|
7
|
+
"deriveBits",
|
|
8
|
+
"deriveKey",
|
|
9
|
+
"encrypt",
|
|
10
|
+
"unwrapKey",
|
|
11
|
+
"wrapKey"
|
|
12
|
+
]);
|
|
13
|
+
var storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT;
|
|
14
|
+
function createKeyPairFromBytes(bytes, extractable, keyUsages) {
|
|
15
|
+
const keyPair = createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages);
|
|
16
|
+
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT || (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT = /* @__PURE__ */ new WeakMap());
|
|
17
|
+
cache.set(keyPair.privateKey, bytes);
|
|
18
|
+
cache.set(keyPair.publicKey, bytes);
|
|
19
|
+
return keyPair;
|
|
20
|
+
}
|
|
21
|
+
function createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages) {
|
|
22
|
+
if (keyUsages.length === 0) {
|
|
23
|
+
throw new DOMException("Usages cannot be empty when creating a key.", "SyntaxError");
|
|
24
|
+
}
|
|
25
|
+
if (keyUsages.some((usage) => PROHIBITED_KEY_USAGES.has(usage))) {
|
|
26
|
+
throw new DOMException("Unsupported key usage for an Ed25519 key.", "SyntaxError");
|
|
27
|
+
}
|
|
28
|
+
const base = {
|
|
29
|
+
[Symbol.toStringTag]: "CryptoKey",
|
|
30
|
+
algorithm: Object.freeze({ name: "Ed25519" })
|
|
31
|
+
};
|
|
32
|
+
const privateKey = {
|
|
33
|
+
...base,
|
|
34
|
+
extractable,
|
|
35
|
+
type: "private",
|
|
36
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "sign"))
|
|
37
|
+
};
|
|
38
|
+
const publicKey = {
|
|
39
|
+
...base,
|
|
40
|
+
extractable: true,
|
|
41
|
+
type: "public",
|
|
42
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "verify"))
|
|
43
|
+
};
|
|
44
|
+
return Object.freeze({
|
|
45
|
+
privateKey: Object.freeze(privateKey),
|
|
46
|
+
publicKey: Object.freeze(publicKey)
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function generateKeyPolyfill(extractable, keyUsages) {
|
|
50
|
+
const privateKeyBytes = ed25519.utils.randomPrivateKey();
|
|
51
|
+
const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);
|
|
52
|
+
return keyPair;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/index.ts
|
|
56
|
+
if (globalThis.isSecureContext) {
|
|
57
|
+
const originalCryptoObject = globalThis.crypto || (globalThis.crypto = {});
|
|
58
|
+
const originalSubtleCrypto = originalCryptoObject.subtle || (originalCryptoObject.subtle = {});
|
|
59
|
+
const originalGenerateKey = originalSubtleCrypto.generateKey;
|
|
60
|
+
let originalGenerateKeySupportsEd25519;
|
|
61
|
+
originalSubtleCrypto.generateKey = async (...args) => {
|
|
62
|
+
const [algorithm] = args;
|
|
63
|
+
if (algorithm !== "Ed25519") {
|
|
64
|
+
if (originalGenerateKey) {
|
|
65
|
+
return await originalGenerateKey.apply(originalSubtleCrypto, args);
|
|
66
|
+
} else {
|
|
67
|
+
throw new TypeError("No native `generateKey` function exists to handle this call");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
let optimisticallyGeneratedKeyPair;
|
|
71
|
+
if (originalGenerateKeySupportsEd25519 === void 0) {
|
|
72
|
+
originalGenerateKeySupportsEd25519 = new Promise((resolve) => {
|
|
73
|
+
if (!originalGenerateKey) {
|
|
74
|
+
resolve(originalGenerateKeySupportsEd25519 = false);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
originalGenerateKey.apply(originalSubtleCrypto, args).then((keyPair) => {
|
|
78
|
+
if (__DEV__) {
|
|
79
|
+
console.warn(
|
|
80
|
+
"`@solana/webcrypto-ed25519-polyfill` was included in an environment that supports Ed25519 key manipulation natively. Falling back to the native implementation. Consider including this polyfill only in environments where Ed25519 is not supported."
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (originalSubtleCrypto.generateKey !== originalGenerateKey) {
|
|
84
|
+
originalSubtleCrypto.generateKey = originalGenerateKey;
|
|
85
|
+
}
|
|
86
|
+
optimisticallyGeneratedKeyPair = keyPair;
|
|
87
|
+
resolve(originalGenerateKeySupportsEd25519 = true);
|
|
88
|
+
}).catch(() => {
|
|
89
|
+
resolve(originalGenerateKeySupportsEd25519 = false);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (typeof originalGenerateKeySupportsEd25519 === "boolean" ? originalGenerateKeySupportsEd25519 : await originalGenerateKeySupportsEd25519) {
|
|
94
|
+
if (optimisticallyGeneratedKeyPair) {
|
|
95
|
+
return optimisticallyGeneratedKeyPair;
|
|
96
|
+
} else if (originalGenerateKey) {
|
|
97
|
+
return await originalGenerateKey.apply(originalSubtleCrypto, args);
|
|
98
|
+
} else {
|
|
99
|
+
throw new TypeError("No native `generateKey` function exists to handle this call");
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
const [_, extractable, keyUsages] = args;
|
|
103
|
+
return generateKeyPolyfill(extractable, keyUsages);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=out.js.map
|
|
108
|
+
//# sourceMappingURL=index.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../build-scripts/env-shim.ts","../src/secrets.ts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AAS,IAAI;AACpC,aAAO,oBAAoB,aAAa,SAAS;AAAA,IACrD;AAAA,EACJ;AACJ","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","/**\n * HEY! <== SECRET KEY KOALA\n * |/ <== WOULD LIKE YOUR\n * ʕ·͡ᴥ·ʔ <== ATTENTION PLEASE\n *\n * Key material generated in this module must stay in this module. So long as the secrets cache and\n * the methods that interact with it are not exported from `@solana/webcrypto-ed25519-polyfill`,\n * accidental logging of the actual bytes of a secret key (eg. to the console, or to a remote\n * server) should not be possible.\n *\n * WARNING: This does not imply that the secrets cache is secure against supply-chain attacks.\n * Untrusted code in your JavaScript context can easily override `WeakMap.prototype.set` to steal\n * private keys as they are written to the cache, without alerting you to its presence or affecting\n * the regular operation of the cache.\n */\nimport { ed25519 } from '@noble/curves/ed25519';\n\nconst PROHIBITED_KEY_USAGES = new Set<KeyUsage>([\n 'decrypt',\n 'deriveBits',\n 'deriveKey',\n 'encrypt',\n 'unwrapKey',\n 'wrapKey',\n]);\n\nlet storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT: WeakMap<CryptoKey, Uint8Array> | undefined;\nfunction createKeyPairFromBytes(\n bytes: Uint8Array,\n extractable: boolean,\n keyUsages: readonly KeyUsage[]\n): CryptoKeyPair {\n const keyPair = createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages);\n const cache = (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT ||= new WeakMap());\n cache.set(keyPair.privateKey, bytes);\n cache.set(keyPair.publicKey, bytes);\n return keyPair;\n}\n\nfunction createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(\n extractable: boolean,\n keyUsages: readonly KeyUsage[]\n): CryptoKeyPair {\n if (keyUsages.length === 0) {\n throw new DOMException('Usages cannot be empty when creating a key.', 'SyntaxError');\n }\n if (keyUsages.some(usage => PROHIBITED_KEY_USAGES.has(usage))) {\n throw new DOMException('Unsupported key usage for an Ed25519 key.', 'SyntaxError');\n }\n const base = {\n [Symbol.toStringTag]: 'CryptoKey',\n algorithm: Object.freeze({ name: 'Ed25519' }),\n };\n const privateKey = {\n ...base,\n extractable,\n type: 'private',\n usages: Object.freeze(keyUsages.filter(usage => usage === 'sign')) as KeyUsage[],\n } as CryptoKey;\n const publicKey = {\n ...base,\n extractable: true,\n type: 'public',\n usages: Object.freeze(keyUsages.filter(usage => usage === 'verify')) as KeyUsage[],\n } as CryptoKey;\n return Object.freeze({\n privateKey: Object.freeze(privateKey),\n publicKey: Object.freeze(publicKey),\n });\n}\n\n/**\n * This function generates a key pair and stores the secret bytes associated with it in a\n * module-private cache. Instead of vending the actual secret bytes, it returns a `CryptoKeyPair`\n * that you can use with other methods in this package to produce signatures and derive public keys\n * associated with the secret.\n */\nexport function generateKeyPolyfill(extractable: boolean, keyUsages: readonly KeyUsage[]): CryptoKeyPair {\n const privateKeyBytes = ed25519.utils.randomPrivateKey();\n const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);\n return keyPair;\n}\n","import { generateKeyPolyfill } from './secrets';\n\nif (!__BROWSER__ || globalThis.isSecureContext) {\n /**\n * Create `crypto.subtle` if it doesn't exist.\n */\n const originalCryptoObject = (globalThis.crypto ||= {} as Crypto);\n const originalSubtleCrypto = ((originalCryptoObject as Crypto & { subtle: SubtleCrypto }).subtle ||=\n {} as SubtleCrypto);\n\n /**\n * Override `SubtleCrypto#generateKey`\n */\n const originalGenerateKey = originalSubtleCrypto.generateKey as SubtleCrypto['generateKey'] | undefined;\n let originalGenerateKeySupportsEd25519: Promise<boolean> | boolean | undefined;\n originalSubtleCrypto.generateKey = (async (...args: Parameters<SubtleCrypto['generateKey']>) => {\n const [algorithm] = args;\n if (algorithm !== 'Ed25519') {\n if (originalGenerateKey) {\n return await originalGenerateKey.apply(originalSubtleCrypto, args);\n } else {\n throw new TypeError('No native `generateKey` function exists to handle this call');\n }\n }\n let optimisticallyGeneratedKeyPair;\n if (originalGenerateKeySupportsEd25519 === undefined) {\n originalGenerateKeySupportsEd25519 = new Promise(resolve => {\n if (!originalGenerateKey) {\n resolve((originalGenerateKeySupportsEd25519 = false));\n return;\n }\n originalGenerateKey\n .apply(originalSubtleCrypto, args)\n .then(keyPair => {\n if (__DEV__) {\n console.warn(\n '`@solana/webcrypto-ed25519-polyfill` was included in an ' +\n 'environment that supports Ed25519 key manipulation ' +\n 'natively. Falling back to the native implementation. ' +\n 'Consider including this polyfill only in environments where ' +\n 'Ed25519 is not supported.'\n );\n }\n if (originalSubtleCrypto.generateKey !== originalGenerateKey) {\n originalSubtleCrypto.generateKey = originalGenerateKey;\n }\n optimisticallyGeneratedKeyPair = keyPair;\n resolve((originalGenerateKeySupportsEd25519 = true));\n })\n .catch(() => {\n resolve((originalGenerateKeySupportsEd25519 = false));\n });\n });\n }\n if (\n typeof originalGenerateKeySupportsEd25519 === 'boolean'\n ? originalGenerateKeySupportsEd25519\n : await originalGenerateKeySupportsEd25519\n ) {\n if (optimisticallyGeneratedKeyPair) {\n return optimisticallyGeneratedKeyPair;\n } else if (originalGenerateKey) {\n return await originalGenerateKey.apply(originalSubtleCrypto, args);\n } else {\n throw new TypeError('No native `generateKey` function exists to handle this call');\n }\n } else {\n const [_, extractable, keyUsages] = args;\n return generateKeyPolyfill(extractable, keyUsages);\n }\n }) as SubtleCrypto['generateKey'];\n}\n"]}
|