Skip to content

Commit

Permalink
fix: type of checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
eum602 committed Aug 2, 2023
1 parent 40a1585 commit 1136195
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/lac1/lac1Did.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 1136195

Please sign in to comment.