chore: split functions

This commit is contained in:
fryorcraken.eth 2023-03-03 14:17:26 +11:00
parent 22ffcf571a
commit af5c5733a5
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 62 additions and 37 deletions

View File

@ -18,25 +18,13 @@ export class EnrDecoder {
static fromRLP(encoded: Uint8Array): Promise<ENR> { static fromRLP(encoded: Uint8Array): Promise<ENR> {
const decoded = RLP.decode(encoded).map(hexToBytes); const decoded = RLP.decode(encoded).map(hexToBytes);
return EnrDecoder.fromValues(decoded); return fromValues(decoded);
} }
}
async function fromValues(values: Uint8Array[]): Promise<ENR> {
const { signature, seq, kvs } = checkValues(values);
private static async fromValues(decoded: Uint8Array[]): Promise<ENR> {
if (!Array.isArray(decoded)) {
throw new Error("Decoded ENR must be an array");
}
if (decoded.length % 2 !== 0) {
throw new Error("Decoded ENR must have an even number of elements");
}
const [signature, seq, ...kvs] = decoded;
if (!signature || Array.isArray(signature)) {
throw new Error("Decoded ENR invalid signature: must be a byte array");
}
if (!seq || Array.isArray(seq)) {
throw new Error(
"Decoded ENR invalid sequence number: must be a byte array"
);
}
const obj: Record<ENRKey, ENRValue> = {}; const obj: Record<ENRKey, ENRValue> = {};
for (let i = 0; i < kvs.length; i += 2) { for (let i = 0; i < kvs.length; i += 2) {
try { try {
@ -45,15 +33,52 @@ export class EnrDecoder {
log("Failed to decode ENR key to UTF-8, skipping it", kvs[i], e); log("Failed to decode ENR key to UTF-8, skipping it", kvs[i], e);
} }
} }
const _seq = decodeSeq(seq);
const enr = await ENR.create(obj, _seq, signature);
checkSignature(seq, kvs, enr, signature);
return enr;
}
function decodeSeq(seq: Uint8Array): bigint {
// 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"); if (!seq.length) return BigInt(0);
const enr = await ENR.create(obj, BigInt(hexSeq), signature); return BigInt("0x" + bytesToHex(seq));
}
function checkValues(values: Uint8Array[]): {
signature: Uint8Array;
seq: Uint8Array;
kvs: Uint8Array[];
} {
if (!Array.isArray(values)) {
throw new Error("Decoded ENR must be an array");
}
if (values.length % 2 !== 0) {
throw new Error("Decoded ENR must have an even number of elements");
}
const [signature, seq, ...kvs] = values;
if (!signature || Array.isArray(signature)) {
throw new Error("Decoded ENR invalid signature: must be a byte array");
}
if (!seq || Array.isArray(seq)) {
throw new Error(
"Decoded ENR invalid sequence number: must be a byte array"
);
}
return { signature, seq, kvs };
}
function checkSignature(
seq: Uint8Array,
kvs: Uint8Array[],
enr: ENR,
signature: Uint8Array
): void {
const rlpEncodedBytes = hexToBytes(RLP.encode([seq, ...kvs])); const rlpEncodedBytes = hexToBytes(RLP.encode([seq, ...kvs]));
if (!enr.verify(rlpEncodedBytes, signature)) { if (!enr.verify(rlpEncodedBytes, signature)) {
throw new Error("Unable to verify ENR signature"); throw new Error("Unable to verify ENR signature");
} }
return enr;
}
} }