From 1136195d359a089c4c34bd41be9ff8b039eacf03 Mon Sep 17 00:00:00 2001 From: eum602 Date: Wed, 2 Aug 2023 15:55:16 -0500 Subject: [PATCH] fix: type of checksum --- lib/lac1/lac1Did.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/lac1/lac1Did.js b/lib/lac1/lac1Did.js index 3268ad8..28fddfd 100644 --- a/lib/lac1/lac1Did.js +++ b/lib/lac1/lac1Did.js @@ -92,10 +92,11 @@ export default class DIDLac1 extends DID { } static checksum(payload) { - return Buffer.from( + const trimmed = Buffer.from( keccak256(Buffer.concat(payload)).replace("0x", ""), "hex" ).subarray(0, 4); + return Buffer.from(trimmed); } static encode(didType, chainId, address, didRegistry, didVersion) { @@ -124,27 +125,27 @@ export default class DIDLac1 extends DID { const trimmed = did.replace("did:lac1:", ""); const data = Buffer.from(base58.decode(trimmed)); const len = data.length; - const encodedPayload = data.subarray(0, len - 4); + const encodedPayload = Buffer.from(data.subarray(0, len - 4)); const computedChecksum = DIDLac1.checksum([encodedPayload]); - const checksum = data.subarray(len - 4, len); + const checksum = Buffer.from(data.subarray(len - 4, len)); if (!computedChecksum.equals(checksum)) { const message = "Checksum mismatch"; throw new Error(message); } - const version = data.subarray(0, 2).toString("hex"); - const didType = data.subarray(2, 4).toString("hex"); + const version = Buffer.from(data.subarray(0, 2)).toString("hex"); + const didType = Buffer.from(data.subarray(2, 4)).toString("hex"); // TODO handle better versioning if (didType !== LAC1_DID_TYPE_CODE) { const message = "Unsupported did type"; throw new Error(message); } const address = ethers.utils.getAddress( - "0x" + data.subarray(4, 24).toString("hex") + "0x" + Buffer.from(data.subarray(4, 24)).toString("hex") ); const didRegistryAddress = ethers.utils.getAddress( - "0x" + data.subarray(24, 44).toString("hex") + "0x" + Buffer.from(data.subarray(24, 44)).toString("hex") ); - let c = data.subarray(44, len - 4).toString("hex"); + let c = Buffer.from(data.subarray(44, len - 4)).toString("hex"); if (c[0] === "0") { c = c.substring(1); }