Calculate peer id at creation

As the peer id method calculation method will become async.
This commit is contained in:
Franck Royer 2022-05-13 16:46:47 +10:00
parent c5ca3d60b3
commit e8ba7f6440
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -31,6 +31,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
public static readonly RECORD_PREFIX = "enr:";
public seq: SequenceNumber;
public signature: Uint8Array | null;
public peerId?: PeerId;
constructor(
kvs: Record<ENRKey, ENRValue> = {},
@ -40,6 +41,16 @@ export class ENR extends Map<ENRKey, ENRValue> {
super(Object.entries(kvs));
this.seq = seq;
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(
@ -96,6 +107,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
}
// If seq is an empty array, translate as value 0
const hexSeq = "0x" + (seq.length ? bytesToHex(seq) : "00");
const enr = new ENR(obj, BigInt(hexSeq), signature);
const rlpEncodedBytes = hexToBytes(RLP.encode([seq, ...kvs]));
@ -157,10 +169,6 @@ export class ENR extends Map<ENRKey, ENRValue> {
return;
}
get peerId(): PeerId | undefined {
return this.keypair ? createPeerIdFromKeypair(this.keypair) : undefined;
}
get nodeId(): NodeId | undefined {
switch (this.id) {
case "v4":