@waku/enr 0.0.27-535a20f.0 → 0.0.27-98208d5.0
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/bundle/index.js +399 -1210
- package/dist/.tsbuildinfo +1 -1
- package/dist/creator.d.ts +1 -1
- package/dist/creator.js +1 -2
- package/dist/creator.js.map +1 -1
- package/dist/decoder.js +1 -1
- package/dist/decoder.js.map +1 -1
- package/dist/enr.d.ts +1 -1
- package/dist/enr.js +2 -2
- package/dist/enr.js.map +1 -1
- package/dist/peer_id.d.ts +3 -4
- package/dist/peer_id.js +7 -24
- package/dist/peer_id.js.map +1 -1
- package/package.json +1 -1
- package/src/creator.ts +2 -3
- package/src/decoder.ts +1 -1
- package/src/enr.ts +3 -3
- package/src/peer_id.ts +9 -34
package/bundle/index.js
CHANGED
@@ -745,7 +745,7 @@ function encodingLength$1(int) {
|
|
745
745
|
/**
|
746
746
|
* Creates a multihash digest.
|
747
747
|
*/
|
748
|
-
function create
|
748
|
+
function create(code, digest) {
|
749
749
|
const size = digest.byteLength;
|
750
750
|
const sizeOffset = encodingLength$1(code);
|
751
751
|
const digestOffset = sizeOffset + encodingLength$1(size);
|
@@ -804,7 +804,7 @@ const code = 0x0;
|
|
804
804
|
const name = 'identity';
|
805
805
|
const encode$2 = coerce;
|
806
806
|
function digest(input) {
|
807
|
-
return create
|
807
|
+
return create(code, encode$2(input));
|
808
808
|
}
|
809
809
|
const identity = { code, name, encode: encode$2, digest };
|
810
810
|
|
@@ -828,9 +828,9 @@ class Hasher {
|
|
828
828
|
if (input instanceof Uint8Array) {
|
829
829
|
const result = this.encode(input);
|
830
830
|
return result instanceof Uint8Array
|
831
|
-
? create
|
831
|
+
? create(this.code, result)
|
832
832
|
/* c8 ignore next 1 */
|
833
|
-
: result.then(digest => create
|
833
|
+
: result.then(digest => create(this.code, digest));
|
834
834
|
}
|
835
835
|
else {
|
836
836
|
throw Error('Unknown type, must be binary type');
|
@@ -930,7 +930,7 @@ class CID {
|
|
930
930
|
switch (this.version) {
|
931
931
|
case 0: {
|
932
932
|
const { code, digest } = this.multihash;
|
933
|
-
const multihash = create
|
933
|
+
const multihash = create(code, digest);
|
934
934
|
return (CID.createV1(this.code, multihash));
|
935
935
|
}
|
936
936
|
case 1: {
|
@@ -3072,7 +3072,7 @@ async function sign$1(message, privateKey) {
|
|
3072
3072
|
function keccak256(input) {
|
3073
3073
|
return new Uint8Array(sha3.keccak256.arrayBuffer(input));
|
3074
3074
|
}
|
3075
|
-
function compressPublicKey
|
3075
|
+
function compressPublicKey(publicKey) {
|
3076
3076
|
if (publicKey.length === 64) {
|
3077
3077
|
publicKey = concat$2([new Uint8Array([4]), publicKey], 65);
|
3078
3078
|
}
|
@@ -3141,23 +3141,6 @@ const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLen
|
|
3141
3141
|
// The rotate right (circular right shift) operation for uint32
|
3142
3142
|
const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
|
3143
3143
|
new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
|
3144
|
-
// There is no setImmediate in browser and setTimeout is slow.
|
3145
|
-
// call of async fn will return Promise, which will be fullfiled only on
|
3146
|
-
// next scheduler queue processing step and this is exactly what we need.
|
3147
|
-
const nextTick = async () => { };
|
3148
|
-
// Returns control to thread each 'tick' ms to avoid blocking
|
3149
|
-
async function asyncLoop(iters, tick, cb) {
|
3150
|
-
let ts = Date.now();
|
3151
|
-
for (let i = 0; i < iters; i++) {
|
3152
|
-
cb(i);
|
3153
|
-
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
3154
|
-
const diff = Date.now() - ts;
|
3155
|
-
if (diff >= 0 && diff < tick)
|
3156
|
-
continue;
|
3157
|
-
await nextTick();
|
3158
|
-
ts += diff;
|
3159
|
-
}
|
3160
|
-
}
|
3161
3144
|
/**
|
3162
3145
|
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
3163
3146
|
*/
|
@@ -3202,13 +3185,6 @@ class Hash {
|
|
3202
3185
|
return this._cloneInto();
|
3203
3186
|
}
|
3204
3187
|
}
|
3205
|
-
const toStr = {}.toString;
|
3206
|
-
function checkOpts(defaults, opts) {
|
3207
|
-
if (opts !== undefined && toStr.call(opts) !== '[object Object]')
|
3208
|
-
throw new Error('Options should be object or undefined');
|
3209
|
-
const merged = Object.assign(defaults, opts);
|
3210
|
-
return merged;
|
3211
|
-
}
|
3212
3188
|
function wrapConstructor(hashCons) {
|
3213
3189
|
const hashC = (msg) => hashCons().update(toBytes$1(msg)).digest();
|
3214
3190
|
const tmp = hashCons();
|
@@ -3220,7 +3196,7 @@ function wrapConstructor(hashCons) {
|
|
3220
3196
|
/**
|
3221
3197
|
* Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS.
|
3222
3198
|
*/
|
3223
|
-
function randomBytes
|
3199
|
+
function randomBytes(bytesLength = 32) {
|
3224
3200
|
if (crypto$1 && typeof crypto$1.getRandomValues === 'function') {
|
3225
3201
|
return crypto$1.getRandomValues(new Uint8Array(bytesLength));
|
3226
3202
|
}
|
@@ -4340,7 +4316,6 @@ var debug = /*@__PURE__*/getDefaultExportFromCjs(browserExports);
|
|
4340
4316
|
|
4341
4317
|
const APP_NAME = "waku";
|
4342
4318
|
let Logger$1 = class Logger {
|
4343
|
-
_debug;
|
4344
4319
|
_info;
|
4345
4320
|
_warn;
|
4346
4321
|
_error;
|
@@ -4348,14 +4323,10 @@ let Logger$1 = class Logger {
|
|
4348
4323
|
return prefix ? `${APP_NAME}:${level}:${prefix}` : `${APP_NAME}:${level}`;
|
4349
4324
|
}
|
4350
4325
|
constructor(prefix) {
|
4351
|
-
this._debug = debug(Logger.createDebugNamespace("debug", prefix));
|
4352
4326
|
this._info = debug(Logger.createDebugNamespace("info", prefix));
|
4353
4327
|
this._warn = debug(Logger.createDebugNamespace("warn", prefix));
|
4354
4328
|
this._error = debug(Logger.createDebugNamespace("error", prefix));
|
4355
4329
|
}
|
4356
|
-
get debug() {
|
4357
|
-
return this._debug;
|
4358
|
-
}
|
4359
4330
|
get info() {
|
4360
4331
|
return this._info;
|
4361
4332
|
}
|
@@ -5817,31 +5788,30 @@ function locationMultiaddrFromEnrFields(enr, protocol) {
|
|
5817
5788
|
return multiaddrFromFields(isIpv6 ? "ip6" : "ip4", protoName, ipVal, protoVal);
|
5818
5789
|
}
|
5819
5790
|
|
5820
|
-
const peerIdSymbol = Symbol.for('@libp2p/peer-id');
|
5821
|
-
|
5822
5791
|
/**
|
5823
5792
|
* When this error is thrown it means an operation was aborted,
|
5824
5793
|
* usually in response to the `abort` event being emitted by an
|
5825
5794
|
* AbortSignal.
|
5826
5795
|
*/
|
5827
|
-
|
5828
|
-
|
5829
|
-
|
5830
|
-
|
5796
|
+
/**
|
5797
|
+
* Thrown when invalid parameters are passed to a function or method call
|
5798
|
+
*/
|
5799
|
+
class InvalidParametersError extends Error {
|
5800
|
+
static name = 'InvalidParametersError';
|
5801
|
+
constructor(message = 'Invalid parameters') {
|
5831
5802
|
super(message);
|
5832
|
-
this.
|
5833
|
-
this.name = props?.name ?? 'CodeError';
|
5834
|
-
this.props = props ?? {}; // eslint-disable-line @typescript-eslint/consistent-type-assertions
|
5803
|
+
this.name = 'InvalidParametersError';
|
5835
5804
|
}
|
5836
5805
|
}
|
5837
|
-
|
5838
|
-
|
5839
|
-
|
5840
|
-
|
5806
|
+
/**
|
5807
|
+
* Thrown when a public key is invalid
|
5808
|
+
*/
|
5809
|
+
class InvalidPublicKeyError extends Error {
|
5810
|
+
static name = 'InvalidPublicKeyError';
|
5811
|
+
constructor(message = 'Invalid public key') {
|
5812
|
+
super(message);
|
5813
|
+
this.name = 'InvalidPublicKeyError';
|
5841
5814
|
}
|
5842
|
-
return typeof thing.then === 'function' &&
|
5843
|
-
typeof thing.catch === 'function' &&
|
5844
|
-
typeof thing.finally === 'function';
|
5845
5815
|
}
|
5846
5816
|
|
5847
5817
|
const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
@@ -7515,7 +7485,7 @@ const ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
7515
7485
|
Gx: BigInt('15112221349535400772501151409588531511454012693041857206046113283949847762202'),
|
7516
7486
|
Gy: BigInt('46316835694926478169428394003475163141307993866256225615783033603165251855960'),
|
7517
7487
|
hash: sha512,
|
7518
|
-
randomBytes
|
7488
|
+
randomBytes,
|
7519
7489
|
adjustScalarBytes,
|
7520
7490
|
// dom2
|
7521
7491
|
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
@@ -7528,176 +7498,46 @@ const ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
7528
7498
|
const ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
|
7529
7499
|
|
7530
7500
|
const PUBLIC_KEY_BYTE_LENGTH = 32;
|
7531
|
-
const PRIVATE_KEY_BYTE_LENGTH = 64; // private key is actually 32 bytes but for historical reasons we concat private and public keys
|
7532
|
-
const KEYS_BYTE_LENGTH = 32;
|
7533
|
-
function generateKey$2() {
|
7534
|
-
// the actual private key (32 bytes)
|
7535
|
-
const privateKeyRaw = ed25519.utils.randomPrivateKey();
|
7536
|
-
const publicKey = ed25519.getPublicKey(privateKeyRaw);
|
7537
|
-
// concatenated the public key to the private key
|
7538
|
-
const privateKey = concatKeys(privateKeyRaw, publicKey);
|
7539
|
-
return {
|
7540
|
-
privateKey,
|
7541
|
-
publicKey
|
7542
|
-
};
|
7543
|
-
}
|
7544
|
-
/**
|
7545
|
-
* Generate keypair from a 32 byte uint8array
|
7546
|
-
*/
|
7547
|
-
function generateKeyFromSeed(seed) {
|
7548
|
-
if (seed.length !== KEYS_BYTE_LENGTH) {
|
7549
|
-
throw new TypeError('"seed" must be 32 bytes in length.');
|
7550
|
-
}
|
7551
|
-
else if (!(seed instanceof Uint8Array)) {
|
7552
|
-
throw new TypeError('"seed" must be a node.js Buffer, or Uint8Array.');
|
7553
|
-
}
|
7554
|
-
// based on node forges algorithm, the seed is used directly as private key
|
7555
|
-
const privateKeyRaw = seed;
|
7556
|
-
const publicKey = ed25519.getPublicKey(privateKeyRaw);
|
7557
|
-
const privateKey = concatKeys(privateKeyRaw, publicKey);
|
7558
|
-
return {
|
7559
|
-
privateKey,
|
7560
|
-
publicKey
|
7561
|
-
};
|
7562
|
-
}
|
7563
|
-
function hashAndSign$2(privateKey, msg) {
|
7564
|
-
const privateKeyRaw = privateKey.subarray(0, KEYS_BYTE_LENGTH);
|
7565
|
-
return ed25519.sign(msg instanceof Uint8Array ? msg : msg.subarray(), privateKeyRaw);
|
7566
|
-
}
|
7567
7501
|
function hashAndVerify$2(publicKey, sig, msg) {
|
7568
7502
|
return ed25519.verify(sig, msg instanceof Uint8Array ? msg : msg.subarray(), publicKey);
|
7569
7503
|
}
|
7570
|
-
function concatKeys(privateKeyRaw, publicKey) {
|
7571
|
-
const privateKey = new Uint8Array(PRIVATE_KEY_BYTE_LENGTH);
|
7572
|
-
for (let i = 0; i < KEYS_BYTE_LENGTH; i++) {
|
7573
|
-
privateKey[i] = privateKeyRaw[i];
|
7574
|
-
privateKey[KEYS_BYTE_LENGTH + i] = publicKey[i];
|
7575
|
-
}
|
7576
|
-
return privateKey;
|
7577
|
-
}
|
7578
7504
|
|
7579
|
-
|
7580
|
-
|
7581
|
-
|
7582
|
-
|
7583
|
-
|
7584
|
-
const nativeCrypto = win.crypto;
|
7585
|
-
if (nativeCrypto?.subtle == null) {
|
7586
|
-
throw Object.assign(new Error('Missing Web Crypto API. ' +
|
7587
|
-
'The most likely cause of this error is that this page is being accessed ' +
|
7588
|
-
'from an insecure context (i.e. not HTTPS). For more information and ' +
|
7589
|
-
'possible resolutions see ' +
|
7590
|
-
'https://github.com/libp2p/js-libp2p/blob/main/packages/crypto/README.md#web-crypto-api'), { code: 'ERR_MISSING_WEB_CRYPTO' });
|
7591
|
-
}
|
7592
|
-
return nativeCrypto;
|
7505
|
+
class Ed25519PublicKey {
|
7506
|
+
type = 'Ed25519';
|
7507
|
+
raw;
|
7508
|
+
constructor(key) {
|
7509
|
+
this.raw = ensureEd25519Key(key, PUBLIC_KEY_BYTE_LENGTH);
|
7593
7510
|
}
|
7594
|
-
|
7595
|
-
|
7596
|
-
|
7597
|
-
|
7598
|
-
|
7599
|
-
|
7600
|
-
|
7601
|
-
|
7602
|
-
|
7603
|
-
|
7604
|
-
|
7605
|
-
|
7606
|
-
const derivedEmptyPasswordKey = { alg: 'A128GCM', ext: true, k: 'scm9jmO_4BJAgdwWGVulLg', key_ops: ['encrypt', 'decrypt'], kty: 'oct' };
|
7607
|
-
// Based off of code from https://github.com/luke-park/SecureCompatibleEncryptionExamples
|
7608
|
-
function create(opts) {
|
7609
|
-
const algorithm = 'AES-GCM';
|
7610
|
-
let keyLength = 16;
|
7611
|
-
const nonceLength = 12;
|
7612
|
-
const digest = 'SHA-256';
|
7613
|
-
const saltLength = 16;
|
7614
|
-
const iterations = 32767;
|
7615
|
-
const crypto = webcrypto.get();
|
7616
|
-
keyLength *= 8; // Browser crypto uses bits instead of bytes
|
7617
|
-
/**
|
7618
|
-
* Uses the provided password to derive a pbkdf2 key. The key
|
7619
|
-
* will then be used to encrypt the data.
|
7620
|
-
*/
|
7621
|
-
async function encrypt(data, password) {
|
7622
|
-
const salt = crypto.getRandomValues(new Uint8Array(saltLength));
|
7623
|
-
const nonce = crypto.getRandomValues(new Uint8Array(nonceLength));
|
7624
|
-
const aesGcm = { name: algorithm, iv: nonce };
|
7625
|
-
if (typeof password === 'string') {
|
7626
|
-
password = fromString(password);
|
7627
|
-
}
|
7628
|
-
let cryptoKey;
|
7629
|
-
if (password.length === 0) {
|
7630
|
-
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['encrypt']);
|
7631
|
-
try {
|
7632
|
-
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } };
|
7633
|
-
const runtimeDerivedEmptyPassword = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey']);
|
7634
|
-
cryptoKey = await crypto.subtle.deriveKey(deriveParams, runtimeDerivedEmptyPassword, { name: algorithm, length: keyLength }, true, ['encrypt']);
|
7635
|
-
}
|
7636
|
-
catch {
|
7637
|
-
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['encrypt']);
|
7638
|
-
}
|
7639
|
-
}
|
7640
|
-
else {
|
7641
|
-
// Derive a key using PBKDF2.
|
7642
|
-
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } };
|
7643
|
-
const rawKey = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey']);
|
7644
|
-
cryptoKey = await crypto.subtle.deriveKey(deriveParams, rawKey, { name: algorithm, length: keyLength }, true, ['encrypt']);
|
7645
|
-
}
|
7646
|
-
// Encrypt the string.
|
7647
|
-
const ciphertext = await crypto.subtle.encrypt(aesGcm, cryptoKey, data);
|
7648
|
-
return concat$1([salt, aesGcm.iv, new Uint8Array(ciphertext)]);
|
7649
|
-
}
|
7650
|
-
/**
|
7651
|
-
* Uses the provided password to derive a pbkdf2 key. The key
|
7652
|
-
* will then be used to decrypt the data. The options used to create
|
7653
|
-
* this decryption cipher must be the same as those used to create
|
7654
|
-
* the encryption cipher.
|
7655
|
-
*/
|
7656
|
-
async function decrypt(data, password) {
|
7657
|
-
const salt = data.subarray(0, saltLength);
|
7658
|
-
const nonce = data.subarray(saltLength, saltLength + nonceLength);
|
7659
|
-
const ciphertext = data.subarray(saltLength + nonceLength);
|
7660
|
-
const aesGcm = { name: algorithm, iv: nonce };
|
7661
|
-
if (typeof password === 'string') {
|
7662
|
-
password = fromString(password);
|
7663
|
-
}
|
7664
|
-
let cryptoKey;
|
7665
|
-
if (password.length === 0) {
|
7666
|
-
try {
|
7667
|
-
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } };
|
7668
|
-
const runtimeDerivedEmptyPassword = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey']);
|
7669
|
-
cryptoKey = await crypto.subtle.deriveKey(deriveParams, runtimeDerivedEmptyPassword, { name: algorithm, length: keyLength }, true, ['decrypt']);
|
7670
|
-
}
|
7671
|
-
catch {
|
7672
|
-
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['decrypt']);
|
7673
|
-
}
|
7511
|
+
toMultihash() {
|
7512
|
+
return identity.digest(publicKeyToProtobuf(this));
|
7513
|
+
}
|
7514
|
+
toCID() {
|
7515
|
+
return CID.createV1(114, this.toMultihash());
|
7516
|
+
}
|
7517
|
+
toString() {
|
7518
|
+
return base58btc.encode(this.toMultihash().bytes).substring(1);
|
7519
|
+
}
|
7520
|
+
equals(key) {
|
7521
|
+
if (key == null || !(key.raw instanceof Uint8Array)) {
|
7522
|
+
return false;
|
7674
7523
|
}
|
7675
|
-
|
7676
|
-
|
7677
|
-
|
7678
|
-
|
7679
|
-
|
7680
|
-
}
|
7681
|
-
// Decrypt the string.
|
7682
|
-
const plaintext = await crypto.subtle.decrypt(aesGcm, cryptoKey, ciphertext);
|
7683
|
-
return new Uint8Array(plaintext);
|
7684
|
-
}
|
7685
|
-
const cipher = {
|
7686
|
-
encrypt,
|
7687
|
-
decrypt
|
7688
|
-
};
|
7689
|
-
return cipher;
|
7524
|
+
return equals(this.raw, key.raw);
|
7525
|
+
}
|
7526
|
+
verify(data, sig) {
|
7527
|
+
return hashAndVerify$2(this.raw, sig, data);
|
7528
|
+
}
|
7690
7529
|
}
|
7691
7530
|
|
7692
|
-
|
7693
|
-
|
7694
|
-
|
7695
|
-
|
7696
|
-
|
7697
|
-
|
7698
|
-
|
7699
|
-
|
7700
|
-
|
7531
|
+
function unmarshalEd25519PublicKey(bytes) {
|
7532
|
+
bytes = ensureEd25519Key(bytes, PUBLIC_KEY_BYTE_LENGTH);
|
7533
|
+
return new Ed25519PublicKey(bytes);
|
7534
|
+
}
|
7535
|
+
function ensureEd25519Key(key, length) {
|
7536
|
+
key = Uint8Array.from(key ?? []);
|
7537
|
+
if (key.length !== length) {
|
7538
|
+
throw new InvalidParametersError(`Key must be a Uint8Array of length ${length}, got ${key.length}`);
|
7539
|
+
}
|
7540
|
+
return key;
|
7701
7541
|
}
|
7702
7542
|
|
7703
7543
|
const f32 = new Float32Array([-0]);
|
@@ -8906,13 +8746,13 @@ var KeyType;
|
|
8906
8746
|
(function (KeyType) {
|
8907
8747
|
KeyType["RSA"] = "RSA";
|
8908
8748
|
KeyType["Ed25519"] = "Ed25519";
|
8909
|
-
KeyType["
|
8749
|
+
KeyType["secp256k1"] = "secp256k1";
|
8910
8750
|
})(KeyType || (KeyType = {}));
|
8911
8751
|
var __KeyTypeValues;
|
8912
8752
|
(function (__KeyTypeValues) {
|
8913
8753
|
__KeyTypeValues[__KeyTypeValues["RSA"] = 0] = "RSA";
|
8914
8754
|
__KeyTypeValues[__KeyTypeValues["Ed25519"] = 1] = "Ed25519";
|
8915
|
-
__KeyTypeValues[__KeyTypeValues["
|
8755
|
+
__KeyTypeValues[__KeyTypeValues["secp256k1"] = 2] = "secp256k1";
|
8916
8756
|
})(__KeyTypeValues || (__KeyTypeValues = {}));
|
8917
8757
|
(function (KeyType) {
|
8918
8758
|
KeyType.codec = () => {
|
@@ -8939,21 +8779,24 @@ var PublicKey;
|
|
8939
8779
|
if (opts.lengthDelimited !== false) {
|
8940
8780
|
w.ldelim();
|
8941
8781
|
}
|
8942
|
-
}, (reader, length) => {
|
8782
|
+
}, (reader, length, opts = {}) => {
|
8943
8783
|
const obj = {};
|
8944
8784
|
const end = length == null ? reader.len : reader.pos + length;
|
8945
8785
|
while (reader.pos < end) {
|
8946
8786
|
const tag = reader.uint32();
|
8947
8787
|
switch (tag >>> 3) {
|
8948
|
-
case 1:
|
8788
|
+
case 1: {
|
8949
8789
|
obj.Type = KeyType.codec().decode(reader);
|
8950
8790
|
break;
|
8951
|
-
|
8791
|
+
}
|
8792
|
+
case 2: {
|
8952
8793
|
obj.Data = reader.bytes();
|
8953
8794
|
break;
|
8954
|
-
|
8795
|
+
}
|
8796
|
+
default: {
|
8955
8797
|
reader.skipType(tag & 7);
|
8956
8798
|
break;
|
8799
|
+
}
|
8957
8800
|
}
|
8958
8801
|
}
|
8959
8802
|
return obj;
|
@@ -8964,8 +8807,8 @@ var PublicKey;
|
|
8964
8807
|
PublicKey.encode = (obj) => {
|
8965
8808
|
return encodeMessage(obj, PublicKey.codec());
|
8966
8809
|
};
|
8967
|
-
PublicKey.decode = (buf) => {
|
8968
|
-
return decodeMessage(buf, PublicKey.codec());
|
8810
|
+
PublicKey.decode = (buf, opts) => {
|
8811
|
+
return decodeMessage(buf, PublicKey.codec(), opts);
|
8969
8812
|
};
|
8970
8813
|
})(PublicKey || (PublicKey = {}));
|
8971
8814
|
var PrivateKey;
|
@@ -8988,21 +8831,24 @@ var PrivateKey;
|
|
8988
8831
|
if (opts.lengthDelimited !== false) {
|
8989
8832
|
w.ldelim();
|
8990
8833
|
}
|
8991
|
-
}, (reader, length) => {
|
8834
|
+
}, (reader, length, opts = {}) => {
|
8992
8835
|
const obj = {};
|
8993
8836
|
const end = length == null ? reader.len : reader.pos + length;
|
8994
8837
|
while (reader.pos < end) {
|
8995
8838
|
const tag = reader.uint32();
|
8996
8839
|
switch (tag >>> 3) {
|
8997
|
-
case 1:
|
8840
|
+
case 1: {
|
8998
8841
|
obj.Type = KeyType.codec().decode(reader);
|
8999
8842
|
break;
|
9000
|
-
|
8843
|
+
}
|
8844
|
+
case 2: {
|
9001
8845
|
obj.Data = reader.bytes();
|
9002
8846
|
break;
|
9003
|
-
|
8847
|
+
}
|
8848
|
+
default: {
|
9004
8849
|
reader.skipType(tag & 7);
|
9005
8850
|
break;
|
8851
|
+
}
|
9006
8852
|
}
|
9007
8853
|
}
|
9008
8854
|
return obj;
|
@@ -9013,286 +8859,11 @@ var PrivateKey;
|
|
9013
8859
|
PrivateKey.encode = (obj) => {
|
9014
8860
|
return encodeMessage(obj, PrivateKey.codec());
|
9015
8861
|
};
|
9016
|
-
PrivateKey.decode = (buf) => {
|
9017
|
-
return decodeMessage(buf, PrivateKey.codec());
|
8862
|
+
PrivateKey.decode = (buf, opts) => {
|
8863
|
+
return decodeMessage(buf, PrivateKey.codec(), opts);
|
9018
8864
|
};
|
9019
8865
|
})(PrivateKey || (PrivateKey = {}));
|
9020
8866
|
|
9021
|
-
class Ed25519PublicKey {
|
9022
|
-
_key;
|
9023
|
-
constructor(key) {
|
9024
|
-
this._key = ensureKey(key, PUBLIC_KEY_BYTE_LENGTH);
|
9025
|
-
}
|
9026
|
-
verify(data, sig) {
|
9027
|
-
return hashAndVerify$2(this._key, sig, data);
|
9028
|
-
}
|
9029
|
-
marshal() {
|
9030
|
-
return this._key;
|
9031
|
-
}
|
9032
|
-
get bytes() {
|
9033
|
-
return PublicKey.encode({
|
9034
|
-
Type: KeyType.Ed25519,
|
9035
|
-
Data: this.marshal()
|
9036
|
-
}).subarray();
|
9037
|
-
}
|
9038
|
-
equals(key) {
|
9039
|
-
return equals(this.bytes, key.bytes);
|
9040
|
-
}
|
9041
|
-
hash() {
|
9042
|
-
const p = sha256$1.digest(this.bytes);
|
9043
|
-
if (isPromise(p)) {
|
9044
|
-
return p.then(({ bytes }) => bytes);
|
9045
|
-
}
|
9046
|
-
return p.bytes;
|
9047
|
-
}
|
9048
|
-
}
|
9049
|
-
class Ed25519PrivateKey {
|
9050
|
-
_key;
|
9051
|
-
_publicKey;
|
9052
|
-
// key - 64 byte Uint8Array containing private key
|
9053
|
-
// publicKey - 32 byte Uint8Array containing public key
|
9054
|
-
constructor(key, publicKey) {
|
9055
|
-
this._key = ensureKey(key, PRIVATE_KEY_BYTE_LENGTH);
|
9056
|
-
this._publicKey = ensureKey(publicKey, PUBLIC_KEY_BYTE_LENGTH);
|
9057
|
-
}
|
9058
|
-
sign(message) {
|
9059
|
-
return hashAndSign$2(this._key, message);
|
9060
|
-
}
|
9061
|
-
get public() {
|
9062
|
-
return new Ed25519PublicKey(this._publicKey);
|
9063
|
-
}
|
9064
|
-
marshal() {
|
9065
|
-
return this._key;
|
9066
|
-
}
|
9067
|
-
get bytes() {
|
9068
|
-
return PrivateKey.encode({
|
9069
|
-
Type: KeyType.Ed25519,
|
9070
|
-
Data: this.marshal()
|
9071
|
-
}).subarray();
|
9072
|
-
}
|
9073
|
-
equals(key) {
|
9074
|
-
return equals(this.bytes, key.bytes);
|
9075
|
-
}
|
9076
|
-
async hash() {
|
9077
|
-
const p = sha256$1.digest(this.bytes);
|
9078
|
-
let bytes;
|
9079
|
-
if (isPromise(p)) {
|
9080
|
-
({ bytes } = await p);
|
9081
|
-
}
|
9082
|
-
else {
|
9083
|
-
bytes = p.bytes;
|
9084
|
-
}
|
9085
|
-
return bytes;
|
9086
|
-
}
|
9087
|
-
/**
|
9088
|
-
* Gets the ID of the key.
|
9089
|
-
*
|
9090
|
-
* The key id is the base58 encoding of the identity multihash containing its public key.
|
9091
|
-
* The public key is a protobuf encoding containing a type and the DER encoding
|
9092
|
-
* of the PKCS SubjectPublicKeyInfo.
|
9093
|
-
*
|
9094
|
-
* @returns {Promise<string>}
|
9095
|
-
*/
|
9096
|
-
async id() {
|
9097
|
-
const encoding = identity.digest(this.public.bytes);
|
9098
|
-
return base58btc.encode(encoding.bytes).substring(1);
|
9099
|
-
}
|
9100
|
-
/**
|
9101
|
-
* Exports the key into a password protected `format`
|
9102
|
-
*/
|
9103
|
-
async export(password, format = 'libp2p-key') {
|
9104
|
-
if (format === 'libp2p-key') {
|
9105
|
-
return exporter(this.bytes, password);
|
9106
|
-
}
|
9107
|
-
else {
|
9108
|
-
throw new CodeError(`export format '${format}' is not supported`, 'ERR_INVALID_EXPORT_FORMAT');
|
9109
|
-
}
|
9110
|
-
}
|
9111
|
-
}
|
9112
|
-
function unmarshalEd25519PrivateKey(bytes) {
|
9113
|
-
// Try the old, redundant public key version
|
9114
|
-
if (bytes.length > PRIVATE_KEY_BYTE_LENGTH) {
|
9115
|
-
bytes = ensureKey(bytes, PRIVATE_KEY_BYTE_LENGTH + PUBLIC_KEY_BYTE_LENGTH);
|
9116
|
-
const privateKeyBytes = bytes.subarray(0, PRIVATE_KEY_BYTE_LENGTH);
|
9117
|
-
const publicKeyBytes = bytes.subarray(PRIVATE_KEY_BYTE_LENGTH, bytes.length);
|
9118
|
-
return new Ed25519PrivateKey(privateKeyBytes, publicKeyBytes);
|
9119
|
-
}
|
9120
|
-
bytes = ensureKey(bytes, PRIVATE_KEY_BYTE_LENGTH);
|
9121
|
-
const privateKeyBytes = bytes.subarray(0, PRIVATE_KEY_BYTE_LENGTH);
|
9122
|
-
const publicKeyBytes = bytes.subarray(PUBLIC_KEY_BYTE_LENGTH);
|
9123
|
-
return new Ed25519PrivateKey(privateKeyBytes, publicKeyBytes);
|
9124
|
-
}
|
9125
|
-
function unmarshalEd25519PublicKey(bytes) {
|
9126
|
-
bytes = ensureKey(bytes, PUBLIC_KEY_BYTE_LENGTH);
|
9127
|
-
return new Ed25519PublicKey(bytes);
|
9128
|
-
}
|
9129
|
-
async function generateKeyPair$2() {
|
9130
|
-
const { privateKey, publicKey } = generateKey$2();
|
9131
|
-
return new Ed25519PrivateKey(privateKey, publicKey);
|
9132
|
-
}
|
9133
|
-
async function generateKeyPairFromSeed(seed) {
|
9134
|
-
const { privateKey, publicKey } = generateKeyFromSeed(seed);
|
9135
|
-
return new Ed25519PrivateKey(privateKey, publicKey);
|
9136
|
-
}
|
9137
|
-
function ensureKey(key, length) {
|
9138
|
-
key = Uint8Array.from(key ?? []);
|
9139
|
-
if (key.length !== length) {
|
9140
|
-
throw new CodeError(`Key must be a Uint8Array of length ${length}, got ${key.length}`, 'ERR_INVALID_KEY_TYPE');
|
9141
|
-
}
|
9142
|
-
return key;
|
9143
|
-
}
|
9144
|
-
|
9145
|
-
var Ed25519 = /*#__PURE__*/Object.freeze({
|
9146
|
-
__proto__: null,
|
9147
|
-
Ed25519PrivateKey: Ed25519PrivateKey,
|
9148
|
-
Ed25519PublicKey: Ed25519PublicKey,
|
9149
|
-
generateKeyPair: generateKeyPair$2,
|
9150
|
-
generateKeyPairFromSeed: generateKeyPairFromSeed,
|
9151
|
-
unmarshalEd25519PrivateKey: unmarshalEd25519PrivateKey,
|
9152
|
-
unmarshalEd25519PublicKey: unmarshalEd25519PublicKey
|
9153
|
-
});
|
9154
|
-
|
9155
|
-
/**
|
9156
|
-
* Generates a Uint8Array with length `number` populated by random bytes
|
9157
|
-
*/
|
9158
|
-
function randomBytes(length) {
|
9159
|
-
if (isNaN(length) || length <= 0) {
|
9160
|
-
throw new CodeError('random bytes length must be a Number bigger than 0', 'ERR_INVALID_LENGTH');
|
9161
|
-
}
|
9162
|
-
return randomBytes$1(length);
|
9163
|
-
}
|
9164
|
-
|
9165
|
-
// HMAC (RFC 2104)
|
9166
|
-
class HMAC extends Hash {
|
9167
|
-
constructor(hash$1, _key) {
|
9168
|
-
super();
|
9169
|
-
this.finished = false;
|
9170
|
-
this.destroyed = false;
|
9171
|
-
hash(hash$1);
|
9172
|
-
const key = toBytes$1(_key);
|
9173
|
-
this.iHash = hash$1.create();
|
9174
|
-
if (typeof this.iHash.update !== 'function')
|
9175
|
-
throw new Error('Expected instance of class which extends utils.Hash');
|
9176
|
-
this.blockLen = this.iHash.blockLen;
|
9177
|
-
this.outputLen = this.iHash.outputLen;
|
9178
|
-
const blockLen = this.blockLen;
|
9179
|
-
const pad = new Uint8Array(blockLen);
|
9180
|
-
// blockLen can be bigger than outputLen
|
9181
|
-
pad.set(key.length > blockLen ? hash$1.create().update(key).digest() : key);
|
9182
|
-
for (let i = 0; i < pad.length; i++)
|
9183
|
-
pad[i] ^= 0x36;
|
9184
|
-
this.iHash.update(pad);
|
9185
|
-
// By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
|
9186
|
-
this.oHash = hash$1.create();
|
9187
|
-
// Undo internal XOR && apply outer XOR
|
9188
|
-
for (let i = 0; i < pad.length; i++)
|
9189
|
-
pad[i] ^= 0x36 ^ 0x5c;
|
9190
|
-
this.oHash.update(pad);
|
9191
|
-
pad.fill(0);
|
9192
|
-
}
|
9193
|
-
update(buf) {
|
9194
|
-
exists(this);
|
9195
|
-
this.iHash.update(buf);
|
9196
|
-
return this;
|
9197
|
-
}
|
9198
|
-
digestInto(out) {
|
9199
|
-
exists(this);
|
9200
|
-
bytes(out, this.outputLen);
|
9201
|
-
this.finished = true;
|
9202
|
-
this.iHash.digestInto(out);
|
9203
|
-
this.oHash.update(out);
|
9204
|
-
this.oHash.digestInto(out);
|
9205
|
-
this.destroy();
|
9206
|
-
}
|
9207
|
-
digest() {
|
9208
|
-
const out = new Uint8Array(this.oHash.outputLen);
|
9209
|
-
this.digestInto(out);
|
9210
|
-
return out;
|
9211
|
-
}
|
9212
|
-
_cloneInto(to) {
|
9213
|
-
// Create new instance without calling constructor since key already in state and we don't know it.
|
9214
|
-
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
9215
|
-
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
9216
|
-
to = to;
|
9217
|
-
to.finished = finished;
|
9218
|
-
to.destroyed = destroyed;
|
9219
|
-
to.blockLen = blockLen;
|
9220
|
-
to.outputLen = outputLen;
|
9221
|
-
to.oHash = oHash._cloneInto(to.oHash);
|
9222
|
-
to.iHash = iHash._cloneInto(to.iHash);
|
9223
|
-
return to;
|
9224
|
-
}
|
9225
|
-
destroy() {
|
9226
|
-
this.destroyed = true;
|
9227
|
-
this.oHash.destroy();
|
9228
|
-
this.iHash.destroy();
|
9229
|
-
}
|
9230
|
-
}
|
9231
|
-
/**
|
9232
|
-
* HMAC: RFC2104 message authentication code.
|
9233
|
-
* @param hash - function that would be used e.g. sha256
|
9234
|
-
* @param key - message key
|
9235
|
-
* @param message - message data
|
9236
|
-
* @example
|
9237
|
-
* import { hmac } from '@noble/hashes/hmac';
|
9238
|
-
* import { sha256 } from '@noble/hashes/sha2';
|
9239
|
-
* const mac1 = hmac(sha256, 'key', 'message');
|
9240
|
-
*/
|
9241
|
-
const hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
9242
|
-
hmac.create = (hash, key) => new HMAC(hash, key);
|
9243
|
-
|
9244
|
-
// Common prologue and epilogue for sync/async functions
|
9245
|
-
function pbkdf2Init(hash$1, _password, _salt, _opts) {
|
9246
|
-
hash(hash$1);
|
9247
|
-
const opts = checkOpts({ dkLen: 32, asyncTick: 10 }, _opts);
|
9248
|
-
const { c, dkLen, asyncTick } = opts;
|
9249
|
-
number(c);
|
9250
|
-
number(dkLen);
|
9251
|
-
number(asyncTick);
|
9252
|
-
if (c < 1)
|
9253
|
-
throw new Error('PBKDF2: iterations (c) should be >= 1');
|
9254
|
-
const password = toBytes$1(_password);
|
9255
|
-
const salt = toBytes$1(_salt);
|
9256
|
-
// DK = PBKDF2(PRF, Password, Salt, c, dkLen);
|
9257
|
-
const DK = new Uint8Array(dkLen);
|
9258
|
-
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
9259
|
-
const PRF = hmac.create(hash$1, password);
|
9260
|
-
const PRFSalt = PRF._cloneInto().update(salt);
|
9261
|
-
return { c, dkLen, asyncTick, DK, PRF, PRFSalt };
|
9262
|
-
}
|
9263
|
-
function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
|
9264
|
-
PRF.destroy();
|
9265
|
-
PRFSalt.destroy();
|
9266
|
-
if (prfW)
|
9267
|
-
prfW.destroy();
|
9268
|
-
u.fill(0);
|
9269
|
-
return DK;
|
9270
|
-
}
|
9271
|
-
async function pbkdf2Async(hash, password, salt, opts) {
|
9272
|
-
const { c, dkLen, asyncTick, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
|
9273
|
-
let prfW; // Working copy
|
9274
|
-
const arr = new Uint8Array(4);
|
9275
|
-
const view = createView(arr);
|
9276
|
-
const u = new Uint8Array(PRF.outputLen);
|
9277
|
-
// DK = T1 + T2 + ⋯ + Tdklen/hlen
|
9278
|
-
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
|
9279
|
-
// Ti = F(Password, Salt, c, i)
|
9280
|
-
const Ti = DK.subarray(pos, pos + PRF.outputLen);
|
9281
|
-
view.setInt32(0, ti, false);
|
9282
|
-
// F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
|
9283
|
-
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
9284
|
-
(prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
|
9285
|
-
Ti.set(u.subarray(0, Ti.length));
|
9286
|
-
await asyncLoop(c - 1, asyncTick, () => {
|
9287
|
-
// Uc = PRF(Password, Uc−1)
|
9288
|
-
PRF._cloneInto(prfW).update(u).digestInto(u);
|
9289
|
-
for (let i = 0; i < Ti.length; i++)
|
9290
|
-
Ti[i] ^= u[i];
|
9291
|
-
});
|
9292
|
-
}
|
9293
|
-
return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
|
9294
|
-
}
|
9295
|
-
|
9296
8867
|
/*!
|
9297
8868
|
* MIT License
|
9298
8869
|
*
|
@@ -12544,75 +12115,124 @@ _a = TIME;
|
|
12544
12115
|
TIME.NAME = "TIME";
|
12545
12116
|
|
12546
12117
|
/**
|
12547
|
-
*
|
12118
|
+
* Signing a message failed
|
12548
12119
|
*/
|
12549
|
-
function pkcs1ToJwk(bytes) {
|
12550
|
-
const { result } = fromBER(bytes);
|
12551
|
-
// @ts-expect-error this looks fragile but DER is a canonical format so we are
|
12552
|
-
// safe to have deeply property chains like this
|
12553
|
-
const values = result.valueBlock.value;
|
12554
|
-
const key = {
|
12555
|
-
n: toString$1(bnToBuf(values[1].toBigInt()), 'base64url'),
|
12556
|
-
e: toString$1(bnToBuf(values[2].toBigInt()), 'base64url'),
|
12557
|
-
d: toString$1(bnToBuf(values[3].toBigInt()), 'base64url'),
|
12558
|
-
p: toString$1(bnToBuf(values[4].toBigInt()), 'base64url'),
|
12559
|
-
q: toString$1(bnToBuf(values[5].toBigInt()), 'base64url'),
|
12560
|
-
dp: toString$1(bnToBuf(values[6].toBigInt()), 'base64url'),
|
12561
|
-
dq: toString$1(bnToBuf(values[7].toBigInt()), 'base64url'),
|
12562
|
-
qi: toString$1(bnToBuf(values[8].toBigInt()), 'base64url'),
|
12563
|
-
kty: 'RSA',
|
12564
|
-
alg: 'RS256'
|
12565
|
-
};
|
12566
|
-
return key;
|
12567
|
-
}
|
12568
12120
|
/**
|
12569
|
-
*
|
12121
|
+
* Verifying a message signature failed
|
12570
12122
|
*/
|
12571
|
-
|
12572
|
-
|
12573
|
-
|
12123
|
+
class VerificationError extends Error {
|
12124
|
+
constructor(message = 'An error occurred while verifying a message') {
|
12125
|
+
super(message);
|
12126
|
+
this.name = 'VerificationError';
|
12574
12127
|
}
|
12575
|
-
const root = new Sequence({
|
12576
|
-
value: [
|
12577
|
-
new Integer({ value: 0 }),
|
12578
|
-
Integer.fromBigInt(bufToBn(fromString(jwk.n, 'base64url'))),
|
12579
|
-
Integer.fromBigInt(bufToBn(fromString(jwk.e, 'base64url'))),
|
12580
|
-
Integer.fromBigInt(bufToBn(fromString(jwk.d, 'base64url'))),
|
12581
|
-
Integer.fromBigInt(bufToBn(fromString(jwk.p, 'base64url'))),
|
12582
|
-
Integer.fromBigInt(bufToBn(fromString(jwk.q, 'base64url'))),
|
12583
|
-
Integer.fromBigInt(bufToBn(fromString(jwk.dp, 'base64url'))),
|
12584
|
-
Integer.fromBigInt(bufToBn(fromString(jwk.dq, 'base64url'))),
|
12585
|
-
Integer.fromBigInt(bufToBn(fromString(jwk.qi, 'base64url')))
|
12586
|
-
]
|
12587
|
-
});
|
12588
|
-
const der = root.toBER();
|
12589
|
-
return new Uint8Array(der, 0, der.byteLength);
|
12590
|
-
}
|
12591
|
-
/**
|
12592
|
-
* Convert a PKCIX in ASN1 DER format to a JWK key
|
12593
|
-
*/
|
12594
|
-
function pkixToJwk(bytes) {
|
12595
|
-
const { result } = fromBER(bytes);
|
12596
|
-
// @ts-expect-error this looks fragile but DER is a canonical format so we are
|
12597
|
-
// safe to have deeply property chains like this
|
12598
|
-
const values = result.valueBlock.value[1].valueBlock.value[0].valueBlock.value;
|
12599
|
-
return {
|
12600
|
-
kty: 'RSA',
|
12601
|
-
n: toString$1(bnToBuf(values[0].toBigInt()), 'base64url'),
|
12602
|
-
e: toString$1(bnToBuf(values[1].toBigInt()), 'base64url')
|
12603
|
-
};
|
12604
12128
|
}
|
12605
12129
|
/**
|
12606
|
-
*
|
12130
|
+
* WebCrypto was not available in the current context
|
12607
12131
|
*/
|
12608
|
-
|
12609
|
-
|
12610
|
-
|
12132
|
+
class WebCryptoMissingError extends Error {
|
12133
|
+
constructor(message = 'Missing Web Crypto API') {
|
12134
|
+
super(message);
|
12135
|
+
this.name = 'WebCryptoMissingError';
|
12611
12136
|
}
|
12612
|
-
|
12613
|
-
|
12614
|
-
|
12615
|
-
|
12137
|
+
}
|
12138
|
+
|
12139
|
+
/* eslint-env browser */
|
12140
|
+
// Check native crypto exists and is enabled (In insecure context `self.crypto`
|
12141
|
+
// exists but `self.crypto.subtle` does not).
|
12142
|
+
var webcrypto = {
|
12143
|
+
get(win = globalThis) {
|
12144
|
+
const nativeCrypto = win.crypto;
|
12145
|
+
if (nativeCrypto?.subtle == null) {
|
12146
|
+
throw new WebCryptoMissingError('Missing Web Crypto API. ' +
|
12147
|
+
'The most likely cause of this error is that this page is being accessed ' +
|
12148
|
+
'from an insecure context (i.e. not HTTPS). For more information and ' +
|
12149
|
+
'possible resolutions see ' +
|
12150
|
+
'https://github.com/libp2p/js-libp2p/blob/main/packages/crypto/README.md#web-crypto-api');
|
12151
|
+
}
|
12152
|
+
return nativeCrypto;
|
12153
|
+
}
|
12154
|
+
};
|
12155
|
+
|
12156
|
+
async function hashAndVerify$1(key, sig, msg) {
|
12157
|
+
const publicKey = await webcrypto.get().subtle.importKey('jwk', key, {
|
12158
|
+
name: 'RSASSA-PKCS1-v1_5',
|
12159
|
+
hash: { name: 'SHA-256' }
|
12160
|
+
}, false, ['verify']);
|
12161
|
+
return webcrypto.get().subtle.verify({ name: 'RSASSA-PKCS1-v1_5' }, publicKey, sig, msg instanceof Uint8Array ? msg : msg.subarray());
|
12162
|
+
}
|
12163
|
+
function rsaKeySize(jwk) {
|
12164
|
+
if (jwk.kty !== 'RSA') {
|
12165
|
+
throw new InvalidParametersError('invalid key type');
|
12166
|
+
}
|
12167
|
+
else if (jwk.n == null) {
|
12168
|
+
throw new InvalidParametersError('invalid key modulus');
|
12169
|
+
}
|
12170
|
+
const bytes = fromString(jwk.n, 'base64url');
|
12171
|
+
return bytes.length * 8;
|
12172
|
+
}
|
12173
|
+
|
12174
|
+
class RSAPublicKey {
|
12175
|
+
type = 'RSA';
|
12176
|
+
_key;
|
12177
|
+
_raw;
|
12178
|
+
_multihash;
|
12179
|
+
constructor(key, digest) {
|
12180
|
+
this._key = key;
|
12181
|
+
this._multihash = digest;
|
12182
|
+
}
|
12183
|
+
get raw() {
|
12184
|
+
if (this._raw == null) {
|
12185
|
+
this._raw = jwkToPkix(this._key);
|
12186
|
+
}
|
12187
|
+
return this._raw;
|
12188
|
+
}
|
12189
|
+
toMultihash() {
|
12190
|
+
return this._multihash;
|
12191
|
+
}
|
12192
|
+
toCID() {
|
12193
|
+
return CID.createV1(114, this._multihash);
|
12194
|
+
}
|
12195
|
+
toString() {
|
12196
|
+
return base58btc.encode(this.toMultihash().bytes).substring(1);
|
12197
|
+
}
|
12198
|
+
equals(key) {
|
12199
|
+
if (key == null || !(key.raw instanceof Uint8Array)) {
|
12200
|
+
return false;
|
12201
|
+
}
|
12202
|
+
return equals(this.raw, key.raw);
|
12203
|
+
}
|
12204
|
+
verify(data, sig) {
|
12205
|
+
return hashAndVerify$1(this._key, sig, data);
|
12206
|
+
}
|
12207
|
+
}
|
12208
|
+
|
12209
|
+
const MAX_RSA_KEY_SIZE = 8192;
|
12210
|
+
const SHA2_256_CODE = 0x12;
|
12211
|
+
/**
|
12212
|
+
* Convert a PKIX in ASN1 DER format to a JWK key
|
12213
|
+
*/
|
12214
|
+
function pkixToJwk(bytes) {
|
12215
|
+
const { result } = fromBER(bytes);
|
12216
|
+
// @ts-expect-error this looks fragile but DER is a canonical format so we are
|
12217
|
+
// safe to have deeply property chains like this
|
12218
|
+
const values = result.valueBlock.value[1].valueBlock.value[0].valueBlock.value;
|
12219
|
+
return {
|
12220
|
+
kty: 'RSA',
|
12221
|
+
n: toString$1(bnToBuf(values[0].toBigInt()), 'base64url'),
|
12222
|
+
e: toString$1(bnToBuf(values[1].toBigInt()), 'base64url')
|
12223
|
+
};
|
12224
|
+
}
|
12225
|
+
/**
|
12226
|
+
* Convert a JWK key to PKIX in ASN1 DER format
|
12227
|
+
*/
|
12228
|
+
function jwkToPkix(jwk) {
|
12229
|
+
if (jwk.n == null || jwk.e == null) {
|
12230
|
+
throw new InvalidParametersError('JWK was missing components');
|
12231
|
+
}
|
12232
|
+
const root = new Sequence({
|
12233
|
+
value: [
|
12234
|
+
new Sequence({
|
12235
|
+
value: [
|
12616
12236
|
// rsaEncryption
|
12617
12237
|
new ObjectIdentifier({
|
12618
12238
|
value: '1.2.840.113549.1.1.1'
|
@@ -12662,328 +12282,100 @@ function bufToBn(u8) {
|
|
12662
12282
|
});
|
12663
12283
|
return BigInt('0x' + hex.join(''));
|
12664
12284
|
}
|
12665
|
-
|
12666
|
-
|
12667
|
-
|
12668
|
-
|
12669
|
-
const
|
12670
|
-
|
12671
|
-
|
12672
|
-
value: [
|
12673
|
-
// version (0)
|
12674
|
-
new Integer({ value: 0 }),
|
12675
|
-
// privateKeyAlgorithm
|
12676
|
-
new Sequence({
|
12677
|
-
value: [
|
12678
|
-
// rsaEncryption OID
|
12679
|
-
new ObjectIdentifier({
|
12680
|
-
value: '1.2.840.113549.1.1.1'
|
12681
|
-
}),
|
12682
|
-
new Null()
|
12683
|
-
]
|
12684
|
-
}),
|
12685
|
-
// PrivateKey
|
12686
|
-
new OctetString({
|
12687
|
-
valueHex: privateKey.marshal()
|
12688
|
-
})
|
12689
|
-
]
|
12690
|
-
});
|
12691
|
-
const keyBuf = keyWrapper.toBER();
|
12692
|
-
const keyArr = new Uint8Array(keyBuf, 0, keyBuf.byteLength);
|
12693
|
-
const salt = randomBytes(SALT_LENGTH);
|
12694
|
-
const encryptionKey = await pbkdf2Async(sha512, password, salt, {
|
12695
|
-
c: ITERATIONS,
|
12696
|
-
dkLen: KEY_SIZE
|
12697
|
-
});
|
12698
|
-
const iv = randomBytes(16);
|
12699
|
-
const cryptoKey = await crypto.subtle.importKey('raw', encryptionKey, 'AES-CBC', false, ['encrypt']);
|
12700
|
-
const encrypted = await crypto.subtle.encrypt({
|
12701
|
-
name: 'AES-CBC',
|
12702
|
-
iv
|
12703
|
-
}, cryptoKey, keyArr);
|
12704
|
-
const pbkdf2Params = new Sequence({
|
12705
|
-
value: [
|
12706
|
-
// salt
|
12707
|
-
new OctetString({ valueHex: salt }),
|
12708
|
-
// iteration count
|
12709
|
-
new Integer({ value: ITERATIONS }),
|
12710
|
-
// key length
|
12711
|
-
new Integer({ value: KEY_SIZE }),
|
12712
|
-
// AlgorithmIdentifier
|
12713
|
-
new Sequence({
|
12714
|
-
value: [
|
12715
|
-
// hmacWithSHA512
|
12716
|
-
new ObjectIdentifier({ value: '1.2.840.113549.2.11' }),
|
12717
|
-
new Null()
|
12718
|
-
]
|
12719
|
-
})
|
12720
|
-
]
|
12721
|
-
});
|
12722
|
-
const encryptionAlgorithm = new Sequence({
|
12723
|
-
value: [
|
12724
|
-
// pkcs5PBES2
|
12725
|
-
new ObjectIdentifier({
|
12726
|
-
value: '1.2.840.113549.1.5.13'
|
12727
|
-
}),
|
12728
|
-
new Sequence({
|
12729
|
-
value: [
|
12730
|
-
// keyDerivationFunc
|
12731
|
-
new Sequence({
|
12732
|
-
value: [
|
12733
|
-
// pkcs5PBKDF2
|
12734
|
-
new ObjectIdentifier({
|
12735
|
-
value: '1.2.840.113549.1.5.12'
|
12736
|
-
}),
|
12737
|
-
// PBKDF2-params
|
12738
|
-
pbkdf2Params
|
12739
|
-
]
|
12740
|
-
}),
|
12741
|
-
// encryptionScheme
|
12742
|
-
new Sequence({
|
12743
|
-
value: [
|
12744
|
-
// aes256-CBC
|
12745
|
-
new ObjectIdentifier({
|
12746
|
-
value: '2.16.840.1.101.3.4.1.42'
|
12747
|
-
}),
|
12748
|
-
// iv
|
12749
|
-
new OctetString({
|
12750
|
-
valueHex: iv
|
12751
|
-
})
|
12752
|
-
]
|
12753
|
-
})
|
12754
|
-
]
|
12755
|
-
})
|
12756
|
-
]
|
12757
|
-
});
|
12758
|
-
const finalWrapper = new Sequence({
|
12759
|
-
value: [
|
12760
|
-
encryptionAlgorithm,
|
12761
|
-
new OctetString({ valueHex: encrypted })
|
12762
|
-
]
|
12763
|
-
});
|
12764
|
-
const finalWrapperBuf = finalWrapper.toBER();
|
12765
|
-
const finalWrapperArr = new Uint8Array(finalWrapperBuf, 0, finalWrapperBuf.byteLength);
|
12766
|
-
return [
|
12767
|
-
'-----BEGIN ENCRYPTED PRIVATE KEY-----',
|
12768
|
-
...toString$1(finalWrapperArr, 'base64pad').split(/(.{64})/).filter(Boolean),
|
12769
|
-
'-----END ENCRYPTED PRIVATE KEY-----'
|
12770
|
-
].join('\n');
|
12771
|
-
}
|
12772
|
-
|
12773
|
-
async function generateKey$1(bits) {
|
12774
|
-
const pair = await webcrypto.get().subtle.generateKey({
|
12775
|
-
name: 'RSASSA-PKCS1-v1_5',
|
12776
|
-
modulusLength: bits,
|
12777
|
-
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
|
12778
|
-
hash: { name: 'SHA-256' }
|
12779
|
-
}, true, ['sign', 'verify']);
|
12780
|
-
const keys = await exportKey(pair);
|
12781
|
-
return {
|
12782
|
-
privateKey: keys[0],
|
12783
|
-
publicKey: keys[1]
|
12784
|
-
};
|
12785
|
-
}
|
12786
|
-
// Takes a jwk key
|
12787
|
-
async function unmarshalPrivateKey$1(key) {
|
12788
|
-
const privateKey = await webcrypto.get().subtle.importKey('jwk', key, {
|
12789
|
-
name: 'RSASSA-PKCS1-v1_5',
|
12790
|
-
hash: { name: 'SHA-256' }
|
12791
|
-
}, true, ['sign']);
|
12792
|
-
const pair = [
|
12793
|
-
privateKey,
|
12794
|
-
await derivePublicFromPrivate(key)
|
12795
|
-
];
|
12796
|
-
const keys = await exportKey({
|
12797
|
-
privateKey: pair[0],
|
12798
|
-
publicKey: pair[1]
|
12799
|
-
});
|
12800
|
-
return {
|
12801
|
-
privateKey: keys[0],
|
12802
|
-
publicKey: keys[1]
|
12803
|
-
};
|
12804
|
-
}
|
12805
|
-
async function hashAndSign$1(key, msg) {
|
12806
|
-
const privateKey = await webcrypto.get().subtle.importKey('jwk', key, {
|
12807
|
-
name: 'RSASSA-PKCS1-v1_5',
|
12808
|
-
hash: { name: 'SHA-256' }
|
12809
|
-
}, false, ['sign']);
|
12810
|
-
const sig = await webcrypto.get().subtle.sign({ name: 'RSASSA-PKCS1-v1_5' }, privateKey, msg instanceof Uint8Array ? msg : msg.subarray());
|
12811
|
-
return new Uint8Array(sig, 0, sig.byteLength);
|
12812
|
-
}
|
12813
|
-
async function hashAndVerify$1(key, sig, msg) {
|
12814
|
-
const publicKey = await webcrypto.get().subtle.importKey('jwk', key, {
|
12815
|
-
name: 'RSASSA-PKCS1-v1_5',
|
12816
|
-
hash: { name: 'SHA-256' }
|
12817
|
-
}, false, ['verify']);
|
12818
|
-
return webcrypto.get().subtle.verify({ name: 'RSASSA-PKCS1-v1_5' }, publicKey, sig, msg instanceof Uint8Array ? msg : msg.subarray());
|
12819
|
-
}
|
12820
|
-
async function exportKey(pair) {
|
12821
|
-
if (pair.privateKey == null || pair.publicKey == null) {
|
12822
|
-
throw new CodeError('Private and public key are required', 'ERR_INVALID_PARAMETERS');
|
12823
|
-
}
|
12824
|
-
return Promise.all([
|
12825
|
-
webcrypto.get().subtle.exportKey('jwk', pair.privateKey),
|
12826
|
-
webcrypto.get().subtle.exportKey('jwk', pair.publicKey)
|
12827
|
-
]);
|
12828
|
-
}
|
12829
|
-
async function derivePublicFromPrivate(jwKey) {
|
12830
|
-
return webcrypto.get().subtle.importKey('jwk', {
|
12831
|
-
kty: jwKey.kty,
|
12832
|
-
n: jwKey.n,
|
12833
|
-
e: jwKey.e
|
12834
|
-
}, {
|
12835
|
-
name: 'RSASSA-PKCS1-v1_5',
|
12836
|
-
hash: { name: 'SHA-256' }
|
12837
|
-
}, true, ['verify']);
|
12838
|
-
}
|
12839
|
-
function keySize(jwk) {
|
12840
|
-
if (jwk.kty !== 'RSA') {
|
12841
|
-
throw new CodeError('invalid key type', 'ERR_INVALID_KEY_TYPE');
|
12842
|
-
}
|
12843
|
-
else if (jwk.n == null) {
|
12844
|
-
throw new CodeError('invalid key modulus', 'ERR_INVALID_KEY_MODULUS');
|
12285
|
+
/**
|
12286
|
+
* Turn PKIX bytes to a PublicKey
|
12287
|
+
*/
|
12288
|
+
function pkixToRSAPublicKey(bytes) {
|
12289
|
+
const jwk = pkixToJwk(bytes);
|
12290
|
+
if (rsaKeySize(jwk) > MAX_RSA_KEY_SIZE) {
|
12291
|
+
throw new InvalidPublicKeyError('Key size is too large');
|
12845
12292
|
}
|
12846
|
-
const
|
12847
|
-
|
12293
|
+
const hash = sha256(PublicKey.encode({
|
12294
|
+
Type: KeyType.RSA,
|
12295
|
+
Data: bytes
|
12296
|
+
}));
|
12297
|
+
const digest = create(SHA2_256_CODE, hash);
|
12298
|
+
return new RSAPublicKey(jwk, digest);
|
12848
12299
|
}
|
12849
12300
|
|
12850
|
-
|
12851
|
-
class
|
12852
|
-
_key
|
12853
|
-
|
12854
|
-
this.
|
12855
|
-
|
12856
|
-
|
12857
|
-
|
12858
|
-
|
12859
|
-
|
12860
|
-
|
12861
|
-
|
12862
|
-
|
12863
|
-
|
12864
|
-
|
12865
|
-
|
12866
|
-
|
12867
|
-
|
12868
|
-
|
12869
|
-
|
12870
|
-
|
12871
|
-
|
12872
|
-
|
12873
|
-
|
12874
|
-
|
12875
|
-
|
12876
|
-
|
12877
|
-
}
|
12878
|
-
}
|
12879
|
-
class RsaPrivateKey {
|
12880
|
-
_key;
|
12881
|
-
_publicKey;
|
12882
|
-
constructor(key, publicKey) {
|
12883
|
-
this._key = key;
|
12884
|
-
this._publicKey = publicKey;
|
12885
|
-
}
|
12886
|
-
genSecret() {
|
12887
|
-
return randomBytes(16);
|
12888
|
-
}
|
12889
|
-
sign(message) {
|
12890
|
-
return hashAndSign$1(this._key, message);
|
12891
|
-
}
|
12892
|
-
get public() {
|
12893
|
-
if (this._publicKey == null) {
|
12894
|
-
throw new CodeError('public key not provided', 'ERR_PUBKEY_NOT_PROVIDED');
|
12895
|
-
}
|
12896
|
-
return new RsaPublicKey(this._publicKey);
|
12897
|
-
}
|
12898
|
-
marshal() {
|
12899
|
-
return jwkToPkcs1(this._key);
|
12900
|
-
}
|
12901
|
-
get bytes() {
|
12902
|
-
return PrivateKey.encode({
|
12903
|
-
Type: KeyType.RSA,
|
12904
|
-
Data: this.marshal()
|
12905
|
-
}).subarray();
|
12906
|
-
}
|
12907
|
-
equals(key) {
|
12908
|
-
return equals(this.bytes, key.bytes);
|
12909
|
-
}
|
12910
|
-
hash() {
|
12911
|
-
const p = sha256$1.digest(this.bytes);
|
12912
|
-
if (isPromise(p)) {
|
12913
|
-
return p.then(({ bytes }) => bytes);
|
12914
|
-
}
|
12915
|
-
return p.bytes;
|
12916
|
-
}
|
12917
|
-
/**
|
12918
|
-
* Gets the ID of the key.
|
12919
|
-
*
|
12920
|
-
* The key id is the base58 encoding of the SHA-256 multihash of its public key.
|
12921
|
-
* The public key is a protobuf encoding containing a type and the DER encoding
|
12922
|
-
* of the PKCS SubjectPublicKeyInfo.
|
12923
|
-
*/
|
12924
|
-
async id() {
|
12925
|
-
const hash = await this.public.hash();
|
12926
|
-
return toString$1(hash, 'base58btc');
|
12301
|
+
// HMAC (RFC 2104)
|
12302
|
+
class HMAC extends Hash {
|
12303
|
+
constructor(hash$1, _key) {
|
12304
|
+
super();
|
12305
|
+
this.finished = false;
|
12306
|
+
this.destroyed = false;
|
12307
|
+
hash(hash$1);
|
12308
|
+
const key = toBytes$1(_key);
|
12309
|
+
this.iHash = hash$1.create();
|
12310
|
+
if (typeof this.iHash.update !== 'function')
|
12311
|
+
throw new Error('Expected instance of class which extends utils.Hash');
|
12312
|
+
this.blockLen = this.iHash.blockLen;
|
12313
|
+
this.outputLen = this.iHash.outputLen;
|
12314
|
+
const blockLen = this.blockLen;
|
12315
|
+
const pad = new Uint8Array(blockLen);
|
12316
|
+
// blockLen can be bigger than outputLen
|
12317
|
+
pad.set(key.length > blockLen ? hash$1.create().update(key).digest() : key);
|
12318
|
+
for (let i = 0; i < pad.length; i++)
|
12319
|
+
pad[i] ^= 0x36;
|
12320
|
+
this.iHash.update(pad);
|
12321
|
+
// By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
|
12322
|
+
this.oHash = hash$1.create();
|
12323
|
+
// Undo internal XOR && apply outer XOR
|
12324
|
+
for (let i = 0; i < pad.length; i++)
|
12325
|
+
pad[i] ^= 0x36 ^ 0x5c;
|
12326
|
+
this.oHash.update(pad);
|
12327
|
+
pad.fill(0);
|
12927
12328
|
}
|
12928
|
-
|
12929
|
-
|
12930
|
-
|
12931
|
-
|
12932
|
-
* To export it as a password protected PEM file, please use the `exportPEM`
|
12933
|
-
* function from `@libp2p/rsa`.
|
12934
|
-
*/
|
12935
|
-
async export(password, format = 'pkcs-8') {
|
12936
|
-
if (format === 'pkcs-8') {
|
12937
|
-
return exportToPem(this, password);
|
12938
|
-
}
|
12939
|
-
else if (format === 'libp2p-key') {
|
12940
|
-
return exporter(this.bytes, password);
|
12941
|
-
}
|
12942
|
-
else {
|
12943
|
-
throw new CodeError(`export format '${format}' is not supported`, 'ERR_INVALID_EXPORT_FORMAT');
|
12944
|
-
}
|
12329
|
+
update(buf) {
|
12330
|
+
exists(this);
|
12331
|
+
this.iHash.update(buf);
|
12332
|
+
return this;
|
12945
12333
|
}
|
12946
|
-
|
12947
|
-
|
12948
|
-
|
12949
|
-
|
12950
|
-
|
12334
|
+
digestInto(out) {
|
12335
|
+
exists(this);
|
12336
|
+
bytes(out, this.outputLen);
|
12337
|
+
this.finished = true;
|
12338
|
+
this.iHash.digestInto(out);
|
12339
|
+
this.oHash.update(out);
|
12340
|
+
this.oHash.digestInto(out);
|
12341
|
+
this.destroy();
|
12951
12342
|
}
|
12952
|
-
|
12953
|
-
|
12954
|
-
|
12955
|
-
|
12956
|
-
const jwk = pkixToJwk(bytes);
|
12957
|
-
if (keySize(jwk) > MAX_RSA_KEY_SIZE) {
|
12958
|
-
throw new CodeError('key size is too large', 'ERR_KEY_SIZE_TOO_LARGE');
|
12343
|
+
digest() {
|
12344
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
12345
|
+
this.digestInto(out);
|
12346
|
+
return out;
|
12959
12347
|
}
|
12960
|
-
|
12961
|
-
|
12962
|
-
|
12963
|
-
|
12964
|
-
|
12348
|
+
_cloneInto(to) {
|
12349
|
+
// Create new instance without calling constructor since key already in state and we don't know it.
|
12350
|
+
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
12351
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
12352
|
+
to = to;
|
12353
|
+
to.finished = finished;
|
12354
|
+
to.destroyed = destroyed;
|
12355
|
+
to.blockLen = blockLen;
|
12356
|
+
to.outputLen = outputLen;
|
12357
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
12358
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
12359
|
+
return to;
|
12965
12360
|
}
|
12966
|
-
|
12967
|
-
|
12968
|
-
|
12969
|
-
|
12970
|
-
if (bits > MAX_RSA_KEY_SIZE) {
|
12971
|
-
throw new CodeError('key size is too large', 'ERR_KEY_SIZE_TOO_LARGE');
|
12361
|
+
destroy() {
|
12362
|
+
this.destroyed = true;
|
12363
|
+
this.oHash.destroy();
|
12364
|
+
this.iHash.destroy();
|
12972
12365
|
}
|
12973
|
-
const keys = await generateKey$1(bits);
|
12974
|
-
return new RsaPrivateKey(keys.privateKey, keys.publicKey);
|
12975
12366
|
}
|
12976
|
-
|
12977
|
-
|
12978
|
-
|
12979
|
-
|
12980
|
-
|
12981
|
-
|
12982
|
-
|
12983
|
-
|
12984
|
-
|
12985
|
-
|
12986
|
-
|
12367
|
+
/**
|
12368
|
+
* HMAC: RFC2104 message authentication code.
|
12369
|
+
* @param hash - function that would be used e.g. sha256
|
12370
|
+
* @param key - message key
|
12371
|
+
* @param message - message data
|
12372
|
+
* @example
|
12373
|
+
* import { hmac } from '@noble/hashes/hmac';
|
12374
|
+
* import { sha256 } from '@noble/hashes/sha2';
|
12375
|
+
* const mac1 = hmac(sha256, 'key', 'message');
|
12376
|
+
*/
|
12377
|
+
const hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
12378
|
+
hmac.create = (hash, key) => new HMAC(hash, key);
|
12987
12379
|
|
12988
12380
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
12989
12381
|
// Short Weierstrass curve. The formula is: y² = x³ + ax + b
|
@@ -14003,7 +13395,7 @@ function getHash(hash) {
|
|
14003
13395
|
return {
|
14004
13396
|
hash,
|
14005
13397
|
hmac: (key, ...msgs) => hmac(hash, key, concatBytes$1(...msgs)),
|
14006
|
-
randomBytes
|
13398
|
+
randomBytes,
|
14007
13399
|
};
|
14008
13400
|
}
|
14009
13401
|
function createCurve(curveDef, defHash) {
|
@@ -14096,27 +13488,15 @@ const secp256k1 = createCurve({
|
|
14096
13488
|
BigInt(0);
|
14097
13489
|
secp256k1.ProjectivePoint;
|
14098
13490
|
|
14099
|
-
function
|
14100
|
-
|
14101
|
-
|
14102
|
-
/**
|
14103
|
-
* Hash and sign message with private key
|
14104
|
-
*/
|
14105
|
-
function hashAndSign(key, msg) {
|
14106
|
-
const p = sha256$1.digest(msg instanceof Uint8Array ? msg : msg.subarray());
|
14107
|
-
if (isPromise(p)) {
|
14108
|
-
return p.then(({ digest }) => secp256k1.sign(digest, key).toDERRawBytes())
|
14109
|
-
.catch(err => {
|
14110
|
-
throw new CodeError(String(err), 'ERR_INVALID_INPUT');
|
14111
|
-
});
|
14112
|
-
}
|
14113
|
-
try {
|
14114
|
-
return secp256k1.sign(p.digest, key).toDERRawBytes();
|
14115
|
-
}
|
14116
|
-
catch (err) {
|
14117
|
-
throw new CodeError(String(err), 'ERR_INVALID_INPUT');
|
13491
|
+
function isPromise(thing) {
|
13492
|
+
if (thing == null) {
|
13493
|
+
return false;
|
14118
13494
|
}
|
13495
|
+
return typeof thing.then === 'function' &&
|
13496
|
+
typeof thing.catch === 'function' &&
|
13497
|
+
typeof thing.finally === 'function';
|
14119
13498
|
}
|
13499
|
+
|
14120
13500
|
/**
|
14121
13501
|
* Hash message and verify signature with public key
|
14122
13502
|
*/
|
@@ -14125,207 +13505,114 @@ function hashAndVerify(key, sig, msg) {
|
|
14125
13505
|
if (isPromise(p)) {
|
14126
13506
|
return p.then(({ digest }) => secp256k1.verify(sig, digest, key))
|
14127
13507
|
.catch(err => {
|
14128
|
-
throw new
|
13508
|
+
throw new VerificationError(String(err));
|
14129
13509
|
});
|
14130
13510
|
}
|
14131
13511
|
try {
|
14132
13512
|
return secp256k1.verify(sig, p.digest, key);
|
14133
13513
|
}
|
14134
13514
|
catch (err) {
|
14135
|
-
throw new
|
14136
|
-
}
|
14137
|
-
}
|
14138
|
-
function compressPublicKey(key) {
|
14139
|
-
const point = secp256k1.ProjectivePoint.fromHex(key).toRawBytes(true);
|
14140
|
-
return point;
|
14141
|
-
}
|
14142
|
-
function validatePrivateKey(key) {
|
14143
|
-
try {
|
14144
|
-
secp256k1.getPublicKey(key, true);
|
14145
|
-
}
|
14146
|
-
catch (err) {
|
14147
|
-
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY');
|
14148
|
-
}
|
14149
|
-
}
|
14150
|
-
function validatePublicKey(key) {
|
14151
|
-
try {
|
14152
|
-
secp256k1.ProjectivePoint.fromHex(key);
|
14153
|
-
}
|
14154
|
-
catch (err) {
|
14155
|
-
throw new CodeError(String(err), 'ERR_INVALID_PUBLIC_KEY');
|
14156
|
-
}
|
14157
|
-
}
|
14158
|
-
function computePublicKey(privateKey) {
|
14159
|
-
try {
|
14160
|
-
return secp256k1.getPublicKey(privateKey, true);
|
14161
|
-
}
|
14162
|
-
catch (err) {
|
14163
|
-
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY');
|
13515
|
+
throw new VerificationError(String(err));
|
14164
13516
|
}
|
14165
13517
|
}
|
14166
13518
|
|
14167
13519
|
class Secp256k1PublicKey {
|
13520
|
+
type = 'secp256k1';
|
13521
|
+
raw;
|
14168
13522
|
_key;
|
14169
13523
|
constructor(key) {
|
14170
|
-
|
14171
|
-
this.
|
13524
|
+
this._key = validateSecp256k1PublicKey(key);
|
13525
|
+
this.raw = compressSecp256k1PublicKey(this._key);
|
14172
13526
|
}
|
14173
|
-
|
14174
|
-
return
|
14175
|
-
}
|
14176
|
-
marshal() {
|
14177
|
-
return compressPublicKey(this._key);
|
14178
|
-
}
|
14179
|
-
get bytes() {
|
14180
|
-
return PublicKey.encode({
|
14181
|
-
Type: KeyType.Secp256k1,
|
14182
|
-
Data: this.marshal()
|
14183
|
-
}).subarray();
|
14184
|
-
}
|
14185
|
-
equals(key) {
|
14186
|
-
return equals(this.bytes, key.bytes);
|
14187
|
-
}
|
14188
|
-
async hash() {
|
14189
|
-
const p = sha256$1.digest(this.bytes);
|
14190
|
-
let bytes;
|
14191
|
-
if (isPromise(p)) {
|
14192
|
-
({ bytes } = await p);
|
14193
|
-
}
|
14194
|
-
else {
|
14195
|
-
bytes = p.bytes;
|
14196
|
-
}
|
14197
|
-
return bytes;
|
14198
|
-
}
|
14199
|
-
}
|
14200
|
-
class Secp256k1PrivateKey {
|
14201
|
-
_key;
|
14202
|
-
_publicKey;
|
14203
|
-
constructor(key, publicKey) {
|
14204
|
-
this._key = key;
|
14205
|
-
this._publicKey = publicKey ?? computePublicKey(key);
|
14206
|
-
validatePrivateKey(this._key);
|
14207
|
-
validatePublicKey(this._publicKey);
|
13527
|
+
toMultihash() {
|
13528
|
+
return identity.digest(publicKeyToProtobuf(this));
|
14208
13529
|
}
|
14209
|
-
|
14210
|
-
return
|
14211
|
-
}
|
14212
|
-
get public() {
|
14213
|
-
return new Secp256k1PublicKey(this._publicKey);
|
14214
|
-
}
|
14215
|
-
marshal() {
|
14216
|
-
return this._key;
|
13530
|
+
toCID() {
|
13531
|
+
return CID.createV1(114, this.toMultihash());
|
14217
13532
|
}
|
14218
|
-
|
14219
|
-
return
|
14220
|
-
Type: KeyType.Secp256k1,
|
14221
|
-
Data: this.marshal()
|
14222
|
-
}).subarray();
|
13533
|
+
toString() {
|
13534
|
+
return base58btc.encode(this.toMultihash().bytes).substring(1);
|
14223
13535
|
}
|
14224
13536
|
equals(key) {
|
14225
|
-
|
14226
|
-
|
14227
|
-
hash() {
|
14228
|
-
const p = sha256$1.digest(this.bytes);
|
14229
|
-
if (isPromise(p)) {
|
14230
|
-
return p.then(({ bytes }) => bytes);
|
13537
|
+
if (key == null || !(key.raw instanceof Uint8Array)) {
|
13538
|
+
return false;
|
14231
13539
|
}
|
14232
|
-
return
|
13540
|
+
return equals(this.raw, key.raw);
|
14233
13541
|
}
|
14234
|
-
|
14235
|
-
|
14236
|
-
*
|
14237
|
-
* The key id is the base58 encoding of the SHA-256 multihash of its public key.
|
14238
|
-
* The public key is a protobuf encoding containing a type and the DER encoding
|
14239
|
-
* of the PKCS SubjectPublicKeyInfo.
|
14240
|
-
*/
|
14241
|
-
async id() {
|
14242
|
-
const hash = await this.public.hash();
|
14243
|
-
return toString$1(hash, 'base58btc');
|
14244
|
-
}
|
14245
|
-
/**
|
14246
|
-
* Exports the key into a password protected `format`
|
14247
|
-
*/
|
14248
|
-
async export(password, format = 'libp2p-key') {
|
14249
|
-
if (format === 'libp2p-key') {
|
14250
|
-
return exporter(this.bytes, password);
|
14251
|
-
}
|
14252
|
-
else {
|
14253
|
-
throw new CodeError(`export format '${format}' is not supported`, 'ERR_INVALID_EXPORT_FORMAT');
|
14254
|
-
}
|
13542
|
+
verify(data, sig) {
|
13543
|
+
return hashAndVerify(this._key, sig, data);
|
14255
13544
|
}
|
14256
13545
|
}
|
14257
|
-
|
14258
|
-
return new Secp256k1PrivateKey(bytes);
|
14259
|
-
}
|
13546
|
+
|
14260
13547
|
function unmarshalSecp256k1PublicKey(bytes) {
|
14261
13548
|
return new Secp256k1PublicKey(bytes);
|
14262
13549
|
}
|
14263
|
-
|
14264
|
-
const
|
14265
|
-
return
|
13550
|
+
function compressSecp256k1PublicKey(key) {
|
13551
|
+
const point = secp256k1.ProjectivePoint.fromHex(key).toRawBytes(true);
|
13552
|
+
return point;
|
13553
|
+
}
|
13554
|
+
function validateSecp256k1PublicKey(key) {
|
13555
|
+
try {
|
13556
|
+
secp256k1.ProjectivePoint.fromHex(key);
|
13557
|
+
return key;
|
13558
|
+
}
|
13559
|
+
catch (err) {
|
13560
|
+
throw new InvalidPublicKeyError(String(err));
|
13561
|
+
}
|
14266
13562
|
}
|
14267
|
-
|
14268
|
-
var Secp256k1 = /*#__PURE__*/Object.freeze({
|
14269
|
-
__proto__: null,
|
14270
|
-
Secp256k1PrivateKey: Secp256k1PrivateKey,
|
14271
|
-
Secp256k1PublicKey: Secp256k1PublicKey,
|
14272
|
-
generateKeyPair: generateKeyPair,
|
14273
|
-
unmarshalSecp256k1PrivateKey: unmarshalSecp256k1PrivateKey,
|
14274
|
-
unmarshalSecp256k1PublicKey: unmarshalSecp256k1PublicKey
|
14275
|
-
});
|
14276
13563
|
|
14277
13564
|
/**
|
14278
13565
|
* @packageDocumentation
|
14279
13566
|
*
|
14280
|
-
*
|
14281
|
-
*
|
14282
|
-
* The {@link generateKeyPair}, {@link marshalPublicKey}, and {@link marshalPrivateKey} functions accept a string `type` argument.
|
13567
|
+
* ## Supported Key Types
|
14283
13568
|
*
|
14284
13569
|
* Currently the `'RSA'`, `'ed25519'`, and `secp256k1` types are supported, although ed25519 and secp256k1 keys support only signing and verification of messages.
|
14285
13570
|
*
|
14286
13571
|
* For encryption / decryption support, RSA keys should be used.
|
14287
13572
|
*/
|
14288
|
-
const supportedKeys = {
|
14289
|
-
rsa: RSA,
|
14290
|
-
ed25519: Ed25519,
|
14291
|
-
secp256k1: Secp256k1
|
14292
|
-
};
|
14293
|
-
function unsupportedKey(type) {
|
14294
|
-
const supported = Object.keys(supportedKeys).join(' / ');
|
14295
|
-
return new CodeError(`invalid or unsupported key type ${type}. Must be ${supported}`, 'ERR_UNSUPPORTED_KEY_TYPE');
|
14296
|
-
}
|
14297
13573
|
/**
|
14298
|
-
*
|
13574
|
+
* Creates a public key from the raw key bytes
|
14299
13575
|
*/
|
14300
|
-
function
|
14301
|
-
|
14302
|
-
|
14303
|
-
|
14304
|
-
|
14305
|
-
|
14306
|
-
|
14307
|
-
|
14308
|
-
|
14309
|
-
return supportedKeys.secp256k1.unmarshalSecp256k1PublicKey(data);
|
14310
|
-
default:
|
14311
|
-
throw unsupportedKey(decoded.Type ?? 'unknown');
|
13576
|
+
function publicKeyFromRaw(buf) {
|
13577
|
+
if (buf.byteLength === 32) {
|
13578
|
+
return unmarshalEd25519PublicKey(buf);
|
13579
|
+
}
|
13580
|
+
else if (buf.byteLength === 33) {
|
13581
|
+
return unmarshalSecp256k1PublicKey(buf);
|
13582
|
+
}
|
13583
|
+
else {
|
13584
|
+
return pkixToRSAPublicKey(buf);
|
14312
13585
|
}
|
14313
13586
|
}
|
14314
13587
|
/**
|
14315
|
-
* Converts a
|
13588
|
+
* Converts a public key object into a protobuf serialized public key
|
14316
13589
|
*/
|
14317
|
-
|
14318
|
-
|
14319
|
-
|
14320
|
-
|
14321
|
-
|
14322
|
-
|
14323
|
-
|
14324
|
-
|
14325
|
-
|
14326
|
-
|
14327
|
-
|
14328
|
-
|
13590
|
+
function publicKeyToProtobuf(key) {
|
13591
|
+
return PublicKey.encode({
|
13592
|
+
Type: KeyType[key.type],
|
13593
|
+
Data: key.raw
|
13594
|
+
});
|
13595
|
+
}
|
13596
|
+
|
13597
|
+
/**
|
13598
|
+
* All PeerId implementations must use this symbol as the name of a property
|
13599
|
+
* with a boolean `true` value
|
13600
|
+
*/
|
13601
|
+
const peerIdSymbol = Symbol.for('@libp2p/peer-id');
|
13602
|
+
|
13603
|
+
/**
|
13604
|
+
* When this error is thrown it means an operation was aborted,
|
13605
|
+
* usually in response to the `abort` event being emitted by an
|
13606
|
+
* AbortSignal.
|
13607
|
+
*/
|
13608
|
+
/**
|
13609
|
+
* Thrown when and attempt to operate on an unsupported key was made
|
13610
|
+
*/
|
13611
|
+
class UnsupportedKeyTypeError extends Error {
|
13612
|
+
static name = 'UnsupportedKeyTypeError';
|
13613
|
+
constructor(message = 'Unsupported key type') {
|
13614
|
+
super(message);
|
13615
|
+
this.name = 'UnsupportedKeyTypeError';
|
14329
13616
|
}
|
14330
13617
|
}
|
14331
13618
|
|
@@ -14345,25 +13632,16 @@ async function unmarshalPrivateKey(buf) {
|
|
14345
13632
|
* ```
|
14346
13633
|
*/
|
14347
13634
|
const inspect = Symbol.for('nodejs.util.inspect.custom');
|
14348
|
-
const baseDecoder = Object
|
14349
|
-
.values(bases)
|
14350
|
-
.map(codec => codec.decoder)
|
14351
|
-
// @ts-expect-error https://github.com/multiformats/js-multiformats/issues/141
|
14352
|
-
.reduce((acc, curr) => acc.or(curr), bases.identity.decoder);
|
14353
13635
|
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
|
14354
13636
|
const LIBP2P_KEY_CODE = 0x72;
|
14355
|
-
const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36;
|
14356
|
-
const MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH = 37;
|
14357
13637
|
class PeerIdImpl {
|
14358
13638
|
type;
|
14359
13639
|
multihash;
|
14360
|
-
privateKey;
|
14361
13640
|
publicKey;
|
14362
13641
|
string;
|
14363
13642
|
constructor(init) {
|
14364
13643
|
this.type = init.type;
|
14365
13644
|
this.multihash = init.multihash;
|
14366
|
-
this.privateKey = init.privateKey;
|
14367
13645
|
// mark string cache as non-enumerable
|
14368
13646
|
Object.defineProperty(this, 'string', {
|
14369
13647
|
enumerable: false,
|
@@ -14380,17 +13658,14 @@ class PeerIdImpl {
|
|
14380
13658
|
}
|
14381
13659
|
return this.string;
|
14382
13660
|
}
|
13661
|
+
toMultihash() {
|
13662
|
+
return this.multihash;
|
13663
|
+
}
|
14383
13664
|
// return self-describing String representation
|
14384
13665
|
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
|
14385
13666
|
toCID() {
|
14386
13667
|
return CID.createV1(LIBP2P_KEY_CODE, this.multihash);
|
14387
13668
|
}
|
14388
|
-
toBytes() {
|
14389
|
-
return this.multihash.bytes;
|
14390
|
-
}
|
14391
|
-
/**
|
14392
|
-
* Returns Multiaddr as a JSON string
|
14393
|
-
*/
|
14394
13669
|
toJSON() {
|
14395
13670
|
return this.toString();
|
14396
13671
|
}
|
@@ -14405,10 +13680,10 @@ class PeerIdImpl {
|
|
14405
13680
|
return equals(this.multihash.bytes, id);
|
14406
13681
|
}
|
14407
13682
|
else if (typeof id === 'string') {
|
14408
|
-
return
|
13683
|
+
return this.toString() === id;
|
14409
13684
|
}
|
14410
|
-
else if (id?.
|
14411
|
-
return equals(this.multihash.bytes, id.
|
13685
|
+
else if (id?.toMultihash()?.bytes != null) {
|
13686
|
+
return equals(this.multihash.bytes, id.toMultihash().bytes);
|
14412
13687
|
}
|
14413
13688
|
else {
|
14414
13689
|
throw new Error('not valid Id');
|
@@ -14430,7 +13705,7 @@ class PeerIdImpl {
|
|
14430
13705
|
return `PeerId(${this.toString()})`;
|
14431
13706
|
}
|
14432
13707
|
}
|
14433
|
-
class
|
13708
|
+
class RSAPeerId extends PeerIdImpl {
|
14434
13709
|
type = 'RSA';
|
14435
13710
|
publicKey;
|
14436
13711
|
constructor(init) {
|
@@ -14438,153 +13713,67 @@ class RSAPeerIdImpl extends PeerIdImpl {
|
|
14438
13713
|
this.publicKey = init.publicKey;
|
14439
13714
|
}
|
14440
13715
|
}
|
14441
|
-
class
|
13716
|
+
class Ed25519PeerId extends PeerIdImpl {
|
14442
13717
|
type = 'Ed25519';
|
14443
13718
|
publicKey;
|
14444
13719
|
constructor(init) {
|
14445
13720
|
super({ ...init, type: 'Ed25519' });
|
14446
|
-
this.publicKey = init.
|
13721
|
+
this.publicKey = init.publicKey;
|
14447
13722
|
}
|
14448
13723
|
}
|
14449
|
-
class
|
13724
|
+
class Secp256k1PeerId extends PeerIdImpl {
|
14450
13725
|
type = 'secp256k1';
|
14451
13726
|
publicKey;
|
14452
13727
|
constructor(init) {
|
14453
13728
|
super({ ...init, type: 'secp256k1' });
|
14454
|
-
this.publicKey = init.
|
14455
|
-
}
|
14456
|
-
}
|
14457
|
-
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
|
14458
|
-
const TRANSPORT_IPFS_GATEWAY_HTTP_CODE = 0x0920;
|
14459
|
-
class URLPeerIdImpl {
|
14460
|
-
type = 'url';
|
14461
|
-
multihash;
|
14462
|
-
privateKey;
|
14463
|
-
publicKey;
|
14464
|
-
url;
|
14465
|
-
constructor(url) {
|
14466
|
-
this.url = url.toString();
|
14467
|
-
this.multihash = identity.digest(fromString(this.url));
|
14468
|
-
}
|
14469
|
-
[inspect]() {
|
14470
|
-
return `PeerId(${this.url})`;
|
14471
|
-
}
|
14472
|
-
[peerIdSymbol] = true;
|
14473
|
-
toString() {
|
14474
|
-
return this.toCID().toString();
|
14475
|
-
}
|
14476
|
-
toCID() {
|
14477
|
-
return CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, this.multihash);
|
14478
|
-
}
|
14479
|
-
toBytes() {
|
14480
|
-
return this.toCID().bytes;
|
14481
|
-
}
|
14482
|
-
equals(other) {
|
14483
|
-
if (other == null) {
|
14484
|
-
return false;
|
14485
|
-
}
|
14486
|
-
if (other instanceof Uint8Array) {
|
14487
|
-
other = toString$1(other);
|
14488
|
-
}
|
14489
|
-
return other.toString() === this.toString();
|
14490
|
-
}
|
14491
|
-
}
|
14492
|
-
function peerIdFromString(str, decoder) {
|
14493
|
-
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
|
14494
|
-
// identity hash ed25519/secp256k1 key or sha2-256 hash of
|
14495
|
-
// rsa public key - base58btc encoded either way
|
14496
|
-
const multihash = decode$2(base58btc.decode(`z${str}`));
|
14497
|
-
if (str.startsWith('12D')) {
|
14498
|
-
return new Ed25519PeerIdImpl({ multihash });
|
14499
|
-
}
|
14500
|
-
else if (str.startsWith('16U')) {
|
14501
|
-
return new Secp256k1PeerIdImpl({ multihash });
|
14502
|
-
}
|
14503
|
-
else {
|
14504
|
-
return new RSAPeerIdImpl({ multihash });
|
14505
|
-
}
|
14506
|
-
}
|
14507
|
-
return peerIdFromBytes(baseDecoder.decode(str));
|
14508
|
-
}
|
14509
|
-
function peerIdFromBytes(buf) {
|
14510
|
-
try {
|
14511
|
-
const multihash = decode$2(buf);
|
14512
|
-
if (multihash.code === identity.code) {
|
14513
|
-
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
14514
|
-
return new Ed25519PeerIdImpl({ multihash });
|
14515
|
-
}
|
14516
|
-
else if (multihash.digest.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
|
14517
|
-
return new Secp256k1PeerIdImpl({ multihash });
|
14518
|
-
}
|
14519
|
-
}
|
14520
|
-
if (multihash.code === sha256$1.code) {
|
14521
|
-
return new RSAPeerIdImpl({ multihash });
|
14522
|
-
}
|
14523
|
-
}
|
14524
|
-
catch {
|
14525
|
-
return peerIdFromCID(CID.decode(buf));
|
14526
|
-
}
|
14527
|
-
throw new Error('Supplied PeerID CID is invalid');
|
14528
|
-
}
|
14529
|
-
function peerIdFromCID(cid) {
|
14530
|
-
if (cid?.multihash == null || cid.version == null || (cid.version === 1 && (cid.code !== LIBP2P_KEY_CODE) && cid.code !== TRANSPORT_IPFS_GATEWAY_HTTP_CODE)) {
|
14531
|
-
throw new Error('Supplied PeerID CID is invalid');
|
14532
|
-
}
|
14533
|
-
if (cid.code === TRANSPORT_IPFS_GATEWAY_HTTP_CODE) {
|
14534
|
-
const url = toString$1(cid.multihash.digest);
|
14535
|
-
return new URLPeerIdImpl(new URL(url));
|
14536
|
-
}
|
14537
|
-
const multihash = cid.multihash;
|
14538
|
-
if (multihash.code === sha256$1.code) {
|
14539
|
-
return new RSAPeerIdImpl({ multihash: cid.multihash });
|
14540
|
-
}
|
14541
|
-
else if (multihash.code === identity.code) {
|
14542
|
-
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
14543
|
-
return new Ed25519PeerIdImpl({ multihash: cid.multihash });
|
14544
|
-
}
|
14545
|
-
else if (multihash.digest.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
|
14546
|
-
return new Secp256k1PeerIdImpl({ multihash: cid.multihash });
|
14547
|
-
}
|
13729
|
+
this.publicKey = init.publicKey;
|
14548
13730
|
}
|
14549
|
-
throw new Error('Supplied PeerID CID is invalid');
|
14550
13731
|
}
|
13732
|
+
|
14551
13733
|
/**
|
14552
|
-
* @
|
14553
|
-
*
|
13734
|
+
* @packageDocumentation
|
13735
|
+
*
|
13736
|
+
* An implementation of a peer id
|
13737
|
+
*
|
13738
|
+
* @example
|
13739
|
+
*
|
13740
|
+
* ```TypeScript
|
13741
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
13742
|
+
* const peer = peerIdFromString('k51qzi5uqu5dkwkqm42v9j9kqcam2jiuvloi16g72i4i4amoo2m8u3ol3mqu6s')
|
13743
|
+
*
|
13744
|
+
* console.log(peer.toCID()) // CID(bafzaa...)
|
13745
|
+
* console.log(peer.toString()) // "12D3K..."
|
13746
|
+
* ```
|
14554
13747
|
*/
|
14555
|
-
|
14556
|
-
if (publicKey.
|
14557
|
-
return new
|
13748
|
+
function peerIdFromPublicKey(publicKey) {
|
13749
|
+
if (publicKey.type === 'Ed25519') {
|
13750
|
+
return new Ed25519PeerId({
|
13751
|
+
multihash: publicKey.toCID().multihash,
|
13752
|
+
publicKey
|
13753
|
+
});
|
14558
13754
|
}
|
14559
|
-
if (publicKey.
|
14560
|
-
return new
|
13755
|
+
else if (publicKey.type === 'secp256k1') {
|
13756
|
+
return new Secp256k1PeerId({
|
13757
|
+
multihash: publicKey.toCID().multihash,
|
13758
|
+
publicKey
|
13759
|
+
});
|
14561
13760
|
}
|
14562
|
-
|
13761
|
+
else if (publicKey.type === 'RSA') {
|
13762
|
+
return new RSAPeerId({
|
13763
|
+
multihash: publicKey.toCID().multihash,
|
13764
|
+
publicKey
|
13765
|
+
});
|
13766
|
+
}
|
13767
|
+
throw new UnsupportedKeyTypeError();
|
14563
13768
|
}
|
14564
13769
|
|
13770
|
+
const ERR_TYPE_NOT_IMPLEMENTED = "Keypair type not implemented";
|
14565
13771
|
function createPeerIdFromPublicKey(publicKey) {
|
14566
|
-
const
|
14567
|
-
|
14568
|
-
|
14569
|
-
function getPublicKeyFromPeerId(peerId) {
|
14570
|
-
if (peerId.type !== "secp256k1") {
|
14571
|
-
throw new Error("Unsupported peer id type");
|
14572
|
-
}
|
14573
|
-
if (!peerId.publicKey) {
|
14574
|
-
throw new Error("Public key not present on peer id");
|
14575
|
-
}
|
14576
|
-
return unmarshalPublicKey(peerId.publicKey).marshal();
|
14577
|
-
}
|
14578
|
-
// Only used in tests
|
14579
|
-
async function getPrivateKeyFromPeerId(peerId) {
|
14580
|
-
if (peerId.type !== "secp256k1") {
|
14581
|
-
throw new Error("Unsupported peer id type");
|
14582
|
-
}
|
14583
|
-
if (!peerId.privateKey) {
|
14584
|
-
throw new Error("Private key not present on peer id");
|
13772
|
+
const pubKey = publicKeyFromRaw(publicKey);
|
13773
|
+
if (pubKey.type !== "secp256k1") {
|
13774
|
+
throw new Error(ERR_TYPE_NOT_IMPLEMENTED);
|
14585
13775
|
}
|
14586
|
-
|
14587
|
-
return privateKey.marshal();
|
13776
|
+
return peerIdFromPublicKey(pubKey);
|
14588
13777
|
}
|
14589
13778
|
|
14590
13779
|
function decodeMultiaddrs(bytes) {
|
@@ -14831,12 +14020,12 @@ var TransportProtocolPerIpVersion;
|
|
14831
14020
|
class ENR extends RawEnr {
|
14832
14021
|
static RECORD_PREFIX = "enr:";
|
14833
14022
|
peerId;
|
14834
|
-
static
|
14023
|
+
static create(kvs = {}, seq = BigInt(1), signature) {
|
14835
14024
|
const enr = new ENR(kvs, seq, signature);
|
14836
14025
|
try {
|
14837
14026
|
const publicKey = enr.publicKey;
|
14838
14027
|
if (publicKey) {
|
14839
|
-
enr.peerId =
|
14028
|
+
enr.peerId = createPeerIdFromPublicKey(publicKey);
|
14840
14029
|
}
|
14841
14030
|
}
|
14842
14031
|
catch (e) {
|
@@ -14958,7 +14147,7 @@ class EnrCreator {
|
|
14958
14147
|
static fromPublicKey(publicKey, kvs = {}) {
|
14959
14148
|
// EIP-778 specifies that the key must be in compressed format, 33 bytes
|
14960
14149
|
if (publicKey.length !== 33) {
|
14961
|
-
publicKey = compressPublicKey
|
14150
|
+
publicKey = compressPublicKey(publicKey);
|
14962
14151
|
}
|
14963
14152
|
return ENR.create({
|
14964
14153
|
...kvs,
|
@@ -14969,7 +14158,7 @@ class EnrCreator {
|
|
14969
14158
|
static async fromPeerId(peerId, kvs = {}) {
|
14970
14159
|
switch (peerId.type) {
|
14971
14160
|
case "secp256k1":
|
14972
|
-
return EnrCreator.fromPublicKey(
|
14161
|
+
return EnrCreator.fromPublicKey(peerId.publicKey.raw, kvs);
|
14973
14162
|
default:
|
14974
14163
|
throw new Error();
|
14975
14164
|
}
|
@@ -15623,7 +14812,7 @@ async function fromValues(values) {
|
|
15623
14812
|
}
|
15624
14813
|
}
|
15625
14814
|
const _seq = decodeSeq(seq);
|
15626
|
-
const enr =
|
14815
|
+
const enr = ENR.create(obj, _seq, signature);
|
15627
14816
|
checkSignature(seq, kvs, enr, signature);
|
15628
14817
|
return enr;
|
15629
14818
|
}
|
@@ -15656,4 +14845,4 @@ function checkSignature(seq, kvs, enr, signature) {
|
|
15656
14845
|
}
|
15657
14846
|
}
|
15658
14847
|
|
15659
|
-
export { ENR, ERR_INVALID_ID, ERR_NO_SIGNATURE, EnrCreator, EnrDecoder, MAX_RECORD_SIZE, MULTIADDR_LENGTH_SIZE, TransportProtocol, TransportProtocolPerIpVersion, compressPublicKey
|
14848
|
+
export { ENR, ERR_INVALID_ID, ERR_NO_SIGNATURE, ERR_TYPE_NOT_IMPLEMENTED, EnrCreator, EnrDecoder, MAX_RECORD_SIZE, MULTIADDR_LENGTH_SIZE, TransportProtocol, TransportProtocolPerIpVersion, compressPublicKey, createPeerIdFromPublicKey, decodeWaku2, encodeWaku2, keccak256, sign$1 as sign, verifySignature };
|