Replace deprecated `multihashes` with `multiformats`

This commit is contained in:
Franck Royer 2022-05-13 17:03:13 +10:00
parent e8ba7f6440
commit 1d8e3837ea
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 26 additions and 81 deletions

80
package-lock.json generated
View File

@ -28,7 +28,7 @@
"libp2p-websockets": "^0.16.1", "libp2p-websockets": "^0.16.1",
"long": "^4.0.0", "long": "^4.0.0",
"multiaddr": "^10.0.1", "multiaddr": "^10.0.1",
"multihashes": "^4.0.3", "multiformats": "^9.6.5",
"peer-id": "^0.16.0", "peer-id": "^0.16.0",
"protobufjs": "^6.8.8", "protobufjs": "^6.8.8",
"uint8arrays": "^3.0.0", "uint8arrays": "^3.0.0",
@ -1091,11 +1091,6 @@
"resolved": "https://registry.npmjs.org/@leichtgewicht/ip-codec/-/ip-codec-2.0.3.tgz", "resolved": "https://registry.npmjs.org/@leichtgewicht/ip-codec/-/ip-codec-2.0.3.tgz",
"integrity": "sha512-nkalE/f1RvRGChwBnEIoBfSEYOXnCRdleKuv6+lePbMDrMZXeDQnqak5XDOeBgrPPyPfAdcCu/B5z+v3VhplGg==" "integrity": "sha512-nkalE/f1RvRGChwBnEIoBfSEYOXnCRdleKuv6+lePbMDrMZXeDQnqak5XDOeBgrPPyPfAdcCu/B5z+v3VhplGg=="
}, },
"node_modules/@multiformats/base-x": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@multiformats/base-x/-/base-x-4.0.1.tgz",
"integrity": "sha512-eMk0b9ReBbV23xXU693TAIrLyeO5iTgBZGSJfpqriG8UkYvr/hC9u9pyMlAakDNHWmbhMZCDs6KQO0jzKD8OTw=="
},
"node_modules/@noble/ed25519": { "node_modules/@noble/ed25519": {
"version": "1.5.1", "version": "1.5.1",
"resolved": "https://registry.npmjs.org/@noble/ed25519/-/ed25519-1.5.1.tgz", "resolved": "https://registry.npmjs.org/@noble/ed25519/-/ed25519-1.5.1.tgz",
@ -8570,41 +8565,10 @@
"multiaddr": "^10.0.0" "multiaddr": "^10.0.0"
} }
}, },
"node_modules/multibase": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/multibase/-/multibase-4.0.4.tgz",
"integrity": "sha512-8/JmrdSGzlw6KTgAJCOqUBSGd1V6186i/X8dDCGy/lbCKrQ+1QB6f3HE+wPr7Tpdj4U3gutaj9jG2rNX6UpiJg==",
"dependencies": {
"@multiformats/base-x": "^4.0.1"
},
"engines": {
"node": ">=12.0.0",
"npm": ">=6.0.0"
}
},
"node_modules/multiformats": { "node_modules/multiformats": {
"version": "9.6.4", "version": "9.6.5",
"resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.6.4.tgz", "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.6.5.tgz",
"integrity": "sha512-fCCB6XMrr6CqJiHNjfFNGT0v//dxOBMrOMqUIzpPc/mmITweLEyhvMpY9bF+jZ9z3vaMAau5E8B68DW77QMXkg==" "integrity": "sha512-vMwf/FUO+qAPvl3vlSZEgEVFY/AxeZq5yg761ScF3CZsXgmTi/HGkicUiNN0CI4PW8FiY2P0OLklOcmQjdQJhw=="
},
"node_modules/multihashes": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/multihashes/-/multihashes-4.0.3.tgz",
"integrity": "sha512-0AhMH7Iu95XjDLxIeuCOOE4t9+vQZsACyKZ9Fxw2pcsRmlX4iCn1mby0hS0bb+nQOVpdQYWPpnyusw4da5RPhA==",
"dependencies": {
"multibase": "^4.0.1",
"uint8arrays": "^3.0.0",
"varint": "^5.0.2"
},
"engines": {
"node": ">=12.0.0",
"npm": ">=6.0.0"
}
},
"node_modules/multihashes/node_modules/varint": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/varint/-/varint-5.0.2.tgz",
"integrity": "sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow=="
}, },
"node_modules/multistream-select": { "node_modules/multistream-select": {
"version": "3.0.2", "version": "3.0.2",
@ -13593,11 +13557,6 @@
"resolved": "https://registry.npmjs.org/@leichtgewicht/ip-codec/-/ip-codec-2.0.3.tgz", "resolved": "https://registry.npmjs.org/@leichtgewicht/ip-codec/-/ip-codec-2.0.3.tgz",
"integrity": "sha512-nkalE/f1RvRGChwBnEIoBfSEYOXnCRdleKuv6+lePbMDrMZXeDQnqak5XDOeBgrPPyPfAdcCu/B5z+v3VhplGg==" "integrity": "sha512-nkalE/f1RvRGChwBnEIoBfSEYOXnCRdleKuv6+lePbMDrMZXeDQnqak5XDOeBgrPPyPfAdcCu/B5z+v3VhplGg=="
}, },
"@multiformats/base-x": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@multiformats/base-x/-/base-x-4.0.1.tgz",
"integrity": "sha512-eMk0b9ReBbV23xXU693TAIrLyeO5iTgBZGSJfpqriG8UkYvr/hC9u9pyMlAakDNHWmbhMZCDs6KQO0jzKD8OTw=="
},
"@noble/ed25519": { "@noble/ed25519": {
"version": "1.5.1", "version": "1.5.1",
"resolved": "https://registry.npmjs.org/@noble/ed25519/-/ed25519-1.5.1.tgz", "resolved": "https://registry.npmjs.org/@noble/ed25519/-/ed25519-1.5.1.tgz",
@ -19422,35 +19381,10 @@
"multiaddr": "^10.0.0" "multiaddr": "^10.0.0"
} }
}, },
"multibase": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/multibase/-/multibase-4.0.4.tgz",
"integrity": "sha512-8/JmrdSGzlw6KTgAJCOqUBSGd1V6186i/X8dDCGy/lbCKrQ+1QB6f3HE+wPr7Tpdj4U3gutaj9jG2rNX6UpiJg==",
"requires": {
"@multiformats/base-x": "^4.0.1"
}
},
"multiformats": { "multiformats": {
"version": "9.6.4", "version": "9.6.5",
"resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.6.4.tgz", "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.6.5.tgz",
"integrity": "sha512-fCCB6XMrr6CqJiHNjfFNGT0v//dxOBMrOMqUIzpPc/mmITweLEyhvMpY9bF+jZ9z3vaMAau5E8B68DW77QMXkg==" "integrity": "sha512-vMwf/FUO+qAPvl3vlSZEgEVFY/AxeZq5yg761ScF3CZsXgmTi/HGkicUiNN0CI4PW8FiY2P0OLklOcmQjdQJhw=="
},
"multihashes": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/multihashes/-/multihashes-4.0.3.tgz",
"integrity": "sha512-0AhMH7Iu95XjDLxIeuCOOE4t9+vQZsACyKZ9Fxw2pcsRmlX4iCn1mby0hS0bb+nQOVpdQYWPpnyusw4da5RPhA==",
"requires": {
"multibase": "^4.0.1",
"uint8arrays": "^3.0.0",
"varint": "^5.0.2"
},
"dependencies": {
"varint": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/varint/-/varint-5.0.2.tgz",
"integrity": "sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow=="
}
}
}, },
"multistream-select": { "multistream-select": {
"version": "3.0.2", "version": "3.0.2",

View File

@ -87,7 +87,7 @@
"libp2p-websockets": "^0.16.1", "libp2p-websockets": "^0.16.1",
"long": "^4.0.0", "long": "^4.0.0",
"multiaddr": "^10.0.1", "multiaddr": "^10.0.1",
"multihashes": "^4.0.3", "multiformats": "^9.6.5",
"peer-id": "^0.16.0", "peer-id": "^0.16.0",
"protobufjs": "^6.8.8", "protobufjs": "^6.8.8",
"uint8arrays": "^3.0.0", "uint8arrays": "^3.0.0",

View File

@ -41,16 +41,25 @@ export class ENR extends Map<ENRKey, ENRValue> {
super(Object.entries(kvs)); super(Object.entries(kvs));
this.seq = seq; this.seq = seq;
this.signature = signature; this.signature = signature;
}
static async create(
kvs: Record<ENRKey, ENRValue> = {},
seq: SequenceNumber = BigInt(1),
signature: Uint8Array | null = null
): Promise<ENR> {
const enr = new ENR(kvs, seq, signature);
try { try {
const publicKey = this.publicKey; const publicKey = enr.publicKey;
if (publicKey) { if (publicKey) {
const keypair = createKeypair(this.keypairType, undefined, publicKey); const keypair = createKeypair(enr.keypairType, undefined, publicKey);
this.peerId = createPeerIdFromKeypair(keypair); enr.peerId = await createPeerIdFromKeypair(keypair);
} }
} catch (e) { } catch (e) {
dbg("Could not calculate peer id for ENR", e); dbg("Could not calculate peer id for ENR", e);
} }
return enr;
} }
static createV4( static createV4(

View File

@ -1,5 +1,5 @@
import { keys } from "libp2p-crypto"; import { keys } from "libp2p-crypto";
import mh from "multihashes"; import { identity } from "multiformats/hashes/identity";
import PeerId from "peer-id"; import PeerId from "peer-id";
const { keysPBM, supportedKeys } = keys; const { keysPBM, supportedKeys } = keys;
@ -33,7 +33,9 @@ export function createKeypair(
} }
} }
export function createPeerIdFromKeypair(keypair: IKeypair): PeerId { export async function createPeerIdFromKeypair(
keypair: IKeypair
): Promise<PeerId> {
switch (keypair.type) { switch (keypair.type) {
case KeypairType.secp256k1: { case KeypairType.secp256k1: {
// manually create a peer id to avoid expensive ops // manually create a peer id to avoid expensive ops
@ -47,8 +49,8 @@ export function createPeerIdFromKeypair(keypair: IKeypair): PeerId {
const pubKey = new supportedKeys.secp256k1.Secp256k1PublicKey( const pubKey = new supportedKeys.secp256k1.Secp256k1PublicKey(
keypair.publicKey keypair.publicKey
); );
const id = mh.encode(pubKey.bytes, "identity"); const id = await identity.digest(pubKey.bytes);
return new PeerId(id, privKey, pubKey); return new PeerId(id.bytes, privKey, pubKey);
} }
default: default:
throw new Error(ERR_TYPE_NOT_IMPLEMENTED); throw new Error(ERR_TYPE_NOT_IMPLEMENTED);