@solana/webcrypto-ed25519-polyfill 2.0.0-experimental.7b33fcb → 2.0.0-experimental.7c1df0a
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 +1 -1
- package/dist/index.browser.cjs +161 -6
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +161 -6
- package/dist/index.browser.js.map +1 -1
- package/dist/index.development.js +161 -6
- package/dist/index.development.js.map +1 -1
- package/dist/index.native.js +161 -6
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +161 -6
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +161 -6
- package/dist/index.node.js.map +1 -1
- package/dist/index.production.min.js +1 -1
- package/dist/types/secrets.d.ts +1 -0
- package/package.json +7 -7
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/secrets.d.ts.map +0 -1
|
@@ -1392,10 +1392,57 @@
|
|
|
1392
1392
|
"unwrapKey",
|
|
1393
1393
|
"wrapKey"
|
|
1394
1394
|
]);
|
|
1395
|
+
var ED25519_PKCS8_HEADER = (
|
|
1396
|
+
// prettier-ignore
|
|
1397
|
+
[
|
|
1398
|
+
/**
|
|
1399
|
+
* PKCS#8 header
|
|
1400
|
+
*/
|
|
1401
|
+
48,
|
|
1402
|
+
// ASN.1 sequence tag
|
|
1403
|
+
46,
|
|
1404
|
+
// Length of sequence (46 more bytes)
|
|
1405
|
+
2,
|
|
1406
|
+
// ASN.1 integer tag
|
|
1407
|
+
1,
|
|
1408
|
+
// Length of integer
|
|
1409
|
+
0,
|
|
1410
|
+
// Version number
|
|
1411
|
+
48,
|
|
1412
|
+
// ASN.1 sequence tag
|
|
1413
|
+
5,
|
|
1414
|
+
// Length of sequence
|
|
1415
|
+
6,
|
|
1416
|
+
// ASN.1 object identifier tag
|
|
1417
|
+
3,
|
|
1418
|
+
// Length of object identifier
|
|
1419
|
+
// Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
|
|
1420
|
+
43,
|
|
1421
|
+
// iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
|
|
1422
|
+
101,
|
|
1423
|
+
// thawte(101)
|
|
1424
|
+
// Ed25519 identifier
|
|
1425
|
+
112,
|
|
1426
|
+
// id-Ed25519(112)
|
|
1427
|
+
/**
|
|
1428
|
+
* Private key payload
|
|
1429
|
+
*/
|
|
1430
|
+
4,
|
|
1431
|
+
// ASN.1 octet string tag
|
|
1432
|
+
34,
|
|
1433
|
+
// String length (34 more bytes)
|
|
1434
|
+
// Private key bytes as octet string
|
|
1435
|
+
4,
|
|
1436
|
+
// ASN.1 octet string tag
|
|
1437
|
+
32
|
|
1438
|
+
// String length (32 bytes)
|
|
1439
|
+
]
|
|
1440
|
+
);
|
|
1395
1441
|
function bufferSourceToUint8Array(data) {
|
|
1396
1442
|
return data instanceof Uint8Array ? data : new Uint8Array(ArrayBuffer.isView(data) ? data.buffer : data);
|
|
1397
1443
|
}
|
|
1398
1444
|
var storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT;
|
|
1445
|
+
var publicKeyBytesStore;
|
|
1399
1446
|
function createKeyPairFromBytes(bytes2, extractable, keyUsages) {
|
|
1400
1447
|
const keyPair = createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages);
|
|
1401
1448
|
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT || (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT = /* @__PURE__ */ new WeakMap());
|
|
@@ -1438,18 +1485,34 @@
|
|
|
1438
1485
|
}
|
|
1439
1486
|
return secretKeyBytes;
|
|
1440
1487
|
}
|
|
1488
|
+
function getPublicKeyBytes(key) {
|
|
1489
|
+
const publicKeyStore = publicKeyBytesStore || (publicKeyBytesStore = /* @__PURE__ */ new WeakMap());
|
|
1490
|
+
const fromPublicStore = publicKeyStore.get(key);
|
|
1491
|
+
if (fromPublicStore)
|
|
1492
|
+
return fromPublicStore;
|
|
1493
|
+
const publicKeyBytes = ed25519.getPublicKey(getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key));
|
|
1494
|
+
publicKeyStore.set(key, publicKeyBytes);
|
|
1495
|
+
return publicKeyBytes;
|
|
1496
|
+
}
|
|
1441
1497
|
function exportKeyPolyfill(format, key) {
|
|
1498
|
+
if (key.extractable === false) {
|
|
1499
|
+
throw new DOMException("key is not extractable", "InvalidAccessException");
|
|
1500
|
+
}
|
|
1442
1501
|
switch (format) {
|
|
1443
1502
|
case "raw": {
|
|
1444
|
-
if (key.extractable === false) {
|
|
1445
|
-
throw new DOMException("key is not extractable", "InvalidAccessException");
|
|
1446
|
-
}
|
|
1447
1503
|
if (key.type !== "public") {
|
|
1448
1504
|
throw new DOMException(`Unable to export a raw Ed25519 ${key.type} key`, "InvalidAccessError");
|
|
1449
1505
|
}
|
|
1450
|
-
const publicKeyBytes =
|
|
1506
|
+
const publicKeyBytes = getPublicKeyBytes(key);
|
|
1451
1507
|
return publicKeyBytes;
|
|
1452
1508
|
}
|
|
1509
|
+
case "pkcs8": {
|
|
1510
|
+
if (key.type !== "private") {
|
|
1511
|
+
throw new DOMException(`Unable to export a pkcs8 Ed25519 ${key.type} key`, "InvalidAccessError");
|
|
1512
|
+
}
|
|
1513
|
+
const secretKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
|
|
1514
|
+
return new Uint8Array([...ED25519_PKCS8_HEADER, ...secretKeyBytes]);
|
|
1515
|
+
}
|
|
1453
1516
|
default:
|
|
1454
1517
|
throw new Error(`Exporting polyfilled Ed25519 keys in the "${format}" format is unimplemented`);
|
|
1455
1518
|
}
|
|
@@ -1460,7 +1523,7 @@
|
|
|
1460
1523
|
return keyPair;
|
|
1461
1524
|
}
|
|
1462
1525
|
function isPolyfilledKey(key) {
|
|
1463
|
-
return !!storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT?.has(key);
|
|
1526
|
+
return !!storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT?.has(key) || !!publicKeyBytesStore?.has(key);
|
|
1464
1527
|
}
|
|
1465
1528
|
function signPolyfill(key, data) {
|
|
1466
1529
|
if (key.type !== "private" || !key.usages.includes("sign")) {
|
|
@@ -1475,13 +1538,58 @@
|
|
|
1475
1538
|
if (key.type !== "public" || !key.usages.includes("verify")) {
|
|
1476
1539
|
throw new DOMException("Unable to use this key to verify", "InvalidAccessError");
|
|
1477
1540
|
}
|
|
1478
|
-
const publicKeyBytes =
|
|
1541
|
+
const publicKeyBytes = getPublicKeyBytes(key);
|
|
1479
1542
|
try {
|
|
1480
1543
|
return ed25519.verify(bufferSourceToUint8Array(signature), bufferSourceToUint8Array(data), publicKeyBytes);
|
|
1481
1544
|
} catch {
|
|
1482
1545
|
return false;
|
|
1483
1546
|
}
|
|
1484
1547
|
}
|
|
1548
|
+
function importKeyPolyfill(format, keyData, extractable, keyUsages) {
|
|
1549
|
+
const bytes2 = bufferSourceToUint8Array(keyData);
|
|
1550
|
+
if (format === "raw") {
|
|
1551
|
+
if (keyUsages.some((usage) => usage === "sign" || PROHIBITED_KEY_USAGES.has(usage))) {
|
|
1552
|
+
throw new DOMException("Unsupported key usage for a Ed25519 key", "SyntaxError");
|
|
1553
|
+
}
|
|
1554
|
+
if (bytes2.length !== 32) {
|
|
1555
|
+
throw new DOMException("Ed25519 raw keys must be exactly 32-bytes", "DataError");
|
|
1556
|
+
}
|
|
1557
|
+
const publicKey = {
|
|
1558
|
+
[Symbol.toStringTag]: "CryptoKey",
|
|
1559
|
+
algorithm: Object.freeze({ name: "Ed25519" }),
|
|
1560
|
+
extractable,
|
|
1561
|
+
type: "public",
|
|
1562
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "verify"))
|
|
1563
|
+
};
|
|
1564
|
+
const cache = publicKeyBytesStore || (publicKeyBytesStore = /* @__PURE__ */ new WeakMap());
|
|
1565
|
+
cache.set(publicKey, bytes2);
|
|
1566
|
+
return publicKey;
|
|
1567
|
+
}
|
|
1568
|
+
if (format === "pkcs8") {
|
|
1569
|
+
if (keyUsages.some((usage) => usage === "verify" || PROHIBITED_KEY_USAGES.has(usage))) {
|
|
1570
|
+
throw new DOMException("Unsupported key usage for a Ed25519 key", "SyntaxError");
|
|
1571
|
+
}
|
|
1572
|
+
if (bytes2.length !== 48) {
|
|
1573
|
+
throw new DOMException("Invalid keyData", "DataError");
|
|
1574
|
+
}
|
|
1575
|
+
const header = bytes2.slice(0, 16);
|
|
1576
|
+
if (!header.every((val, i) => val === ED25519_PKCS8_HEADER[i])) {
|
|
1577
|
+
throw new DOMException("Invalid keyData", "DataError");
|
|
1578
|
+
}
|
|
1579
|
+
const secretKeyBytes = bytes2.slice(16);
|
|
1580
|
+
const privateKey = {
|
|
1581
|
+
[Symbol.toStringTag]: "CryptoKey",
|
|
1582
|
+
algorithm: Object.freeze({ name: "Ed25519" }),
|
|
1583
|
+
extractable,
|
|
1584
|
+
type: "private",
|
|
1585
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "sign"))
|
|
1586
|
+
};
|
|
1587
|
+
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT || (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT = /* @__PURE__ */ new WeakMap());
|
|
1588
|
+
cache.set(privateKey, secretKeyBytes);
|
|
1589
|
+
return privateKey;
|
|
1590
|
+
}
|
|
1591
|
+
throw new Error(`Importing Ed25519 keys in the "${format}" format is unimplemented`);
|
|
1592
|
+
}
|
|
1485
1593
|
|
|
1486
1594
|
// src/index.ts
|
|
1487
1595
|
if (globalThis.isSecureContext) {
|
|
@@ -1569,6 +1677,53 @@
|
|
|
1569
1677
|
throw new TypeError("No native `verify` function exists to handle this call");
|
|
1570
1678
|
}
|
|
1571
1679
|
};
|
|
1680
|
+
const originalImportKey = originalSubtleCrypto.importKey;
|
|
1681
|
+
let originalImportKeySupportsEd25519;
|
|
1682
|
+
originalSubtleCrypto.importKey = async (...args) => {
|
|
1683
|
+
const [format, keyData, algorithm] = args;
|
|
1684
|
+
if (algorithm !== "Ed25519") {
|
|
1685
|
+
if (originalImportKey) {
|
|
1686
|
+
return await originalImportKey.apply(originalSubtleCrypto, args);
|
|
1687
|
+
} else {
|
|
1688
|
+
throw new TypeError("No native `importKey` function exists to handle this call");
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
let optimisticallyImportedKey;
|
|
1692
|
+
if (originalImportKeySupportsEd25519 === void 0) {
|
|
1693
|
+
originalImportKeySupportsEd25519 = new Promise((resolve) => {
|
|
1694
|
+
if (!originalImportKey) {
|
|
1695
|
+
resolve(originalImportKeySupportsEd25519 = false);
|
|
1696
|
+
return;
|
|
1697
|
+
}
|
|
1698
|
+
originalImportKey.apply(originalSubtleCrypto, args).then((key) => {
|
|
1699
|
+
{
|
|
1700
|
+
console.warn(
|
|
1701
|
+
"`@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."
|
|
1702
|
+
);
|
|
1703
|
+
}
|
|
1704
|
+
if (originalSubtleCrypto.importKey !== originalImportKey) {
|
|
1705
|
+
originalSubtleCrypto.importKey = originalImportKey;
|
|
1706
|
+
}
|
|
1707
|
+
optimisticallyImportedKey = key;
|
|
1708
|
+
resolve(originalImportKeySupportsEd25519 = true);
|
|
1709
|
+
}).catch(() => {
|
|
1710
|
+
resolve(originalImportKeySupportsEd25519 = false);
|
|
1711
|
+
});
|
|
1712
|
+
});
|
|
1713
|
+
}
|
|
1714
|
+
if (typeof originalImportKey === "boolean" ? originalImportKeySupportsEd25519 : await originalImportKeySupportsEd25519) {
|
|
1715
|
+
if (optimisticallyImportedKey) {
|
|
1716
|
+
return optimisticallyImportedKey;
|
|
1717
|
+
} else if (originalImportKey) {
|
|
1718
|
+
return await originalImportKey.apply(originalSubtleCrypto, args);
|
|
1719
|
+
} else {
|
|
1720
|
+
throw new TypeError("No native `importKey` function exists to handle this call");
|
|
1721
|
+
}
|
|
1722
|
+
} else {
|
|
1723
|
+
const [_format, _keyData, _algorithm, extractable, keyUsages] = args;
|
|
1724
|
+
return importKeyPolyfill(format, keyData, extractable, keyUsages);
|
|
1725
|
+
}
|
|
1726
|
+
};
|
|
1572
1727
|
}
|
|
1573
1728
|
/*! Bundled license information:
|
|
1574
1729
|
|