mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-10 19:36:59 +00:00
Calculate peer id at creation
As the peer id method calculation method will become async.
This commit is contained in:
parent
c5ca3d60b3
commit
e8ba7f6440
@ -31,6 +31,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
public static readonly RECORD_PREFIX = "enr:";
|
public static readonly RECORD_PREFIX = "enr:";
|
||||||
public seq: SequenceNumber;
|
public seq: SequenceNumber;
|
||||||
public signature: Uint8Array | null;
|
public signature: Uint8Array | null;
|
||||||
|
public peerId?: PeerId;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
kvs: Record<ENRKey, ENRValue> = {},
|
kvs: Record<ENRKey, ENRValue> = {},
|
||||||
@ -40,6 +41,16 @@ 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;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const publicKey = this.publicKey;
|
||||||
|
if (publicKey) {
|
||||||
|
const keypair = createKeypair(this.keypairType, undefined, publicKey);
|
||||||
|
this.peerId = createPeerIdFromKeypair(keypair);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
dbg("Could not calculate peer id for ENR", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static createV4(
|
static createV4(
|
||||||
@ -96,6 +107,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
}
|
}
|
||||||
// If seq is an empty array, translate as value 0
|
// If seq is an empty array, translate as value 0
|
||||||
const hexSeq = "0x" + (seq.length ? bytesToHex(seq) : "00");
|
const hexSeq = "0x" + (seq.length ? bytesToHex(seq) : "00");
|
||||||
|
|
||||||
const enr = new ENR(obj, BigInt(hexSeq), signature);
|
const enr = new ENR(obj, BigInt(hexSeq), signature);
|
||||||
|
|
||||||
const rlpEncodedBytes = hexToBytes(RLP.encode([seq, ...kvs]));
|
const rlpEncodedBytes = hexToBytes(RLP.encode([seq, ...kvs]));
|
||||||
@ -157,10 +169,6 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
get peerId(): PeerId | undefined {
|
|
||||||
return this.keypair ? createPeerIdFromKeypair(this.keypair) : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
get nodeId(): NodeId | undefined {
|
get nodeId(): NodeId | undefined {
|
||||||
switch (this.id) {
|
switch (this.id) {
|
||||||
case "v4":
|
case "v4":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user