Skip to content

Commit

Permalink
bump csl
Browse files Browse the repository at this point in the history
  • Loading branch information
twwu123 committed Jan 31, 2025
1 parent b854809 commit f5aed91
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 298 deletions.
463 changes: 225 additions & 238 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/mesh-core-csl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
},
"dependencies": {
"@meshsdk/common": "1.9.0-beta.0",
"@sidan-lab/sidan-csl-rs-browser": "0.9.16",
"@sidan-lab/sidan-csl-rs-nodejs": "0.9.16",
"@sidan-lab/sidan-csl-rs-browser": "0.9.18",
"@sidan-lab/sidan-csl-rs-nodejs": "0.9.18",
"@types/base32-encoding": "^1.0.2",
"base32-encoding": "^1.0.0",
"bech32": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/mesh-core-csl/src/deser/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const resolveNativeScriptHash = (script: NativeScript) => {
export const resolveScriptHashDRepId = (scriptHash: string) => {
return csl.DRep.new_script_hash(
csl.ScriptHash.from_hex(scriptHash),
).to_bech32();
).to_bech32(true);
};

export const resolveRewardAddress = (bech32: string) => {
Expand Down
56 changes: 3 additions & 53 deletions packages/mesh-core-csl/src/utils/drep.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base32 from "base32-encoding";
import { bech32 } from "bech32";

import { csl } from "../deser";
Expand All @@ -9,59 +8,10 @@ export const getDRepIds = (
cip105: string;
cip129: string;
} => {
const cslDrep = csl.DRep.from_bech32(dRepId);
let result = {
cip105: "",
cip129: "",
cip105: cslDrep.to_bech32(false),
cip129: cslDrep.to_bech32(true),
};
if (dRepId.length === 58) {
result.cip129 = dRepId;
const { prefix, words } = bech32.decode(dRepId);
if (prefix !== "drep") {
throw new Error("Malformed CIP129 DRepId");
}
const bytes = base32.decode(new Uint8Array(words));
if (bytes[0] === 0x22) {
result.cip105 = csl.DRep.new_key_hash(
csl.Ed25519KeyHash.from_hex(bytes.subarray(1).toString("hex")),
).to_bech32();
} else if (bytes[0] === 0x23) {
result.cip105 = csl.DRep.new_script_hash(
csl.ScriptHash.from_hex(bytes.subarray(1).toString("hex")),
).to_bech32();
} else {
throw new Error("Malformed CIP129 DRepId");
}
} else {
result.cip105 = dRepId;
try {
const cslDRep = csl.DRep.from_bech32(dRepId);
if (cslDRep.kind() === csl.DRepKind.KeyHash) {
let rawBytes = cslDRep.to_key_hash()?.to_bytes();
if (!rawBytes) {
throw new Error("Malformed key hash in DRepId");
}
let rawBytesWithPrefix = new Uint8Array(rawBytes.length + 1);
rawBytesWithPrefix.set([0x22]);
rawBytesWithPrefix.set(rawBytes, 1);
let base32RawBytes = base32.encode(rawBytesWithPrefix);
result.cip129 = bech32.encode("drep", base32RawBytes);
} else if (cslDRep.kind() === csl.DRepKind.ScriptHash) {
let rawBytes = cslDRep.to_script_hash()?.to_bytes();
if (!rawBytes) {
throw new Error("Malformed script hash in DRepId");
}
let rawBytesWithPrefix = new Uint8Array(rawBytes.length + 1);
rawBytesWithPrefix.set([0x23]);
rawBytesWithPrefix.set(rawBytes, 1);
let base32RawBytes = base32.encode(rawBytesWithPrefix);
result.cip129 = bech32.encode("drep", base32RawBytes);
} else {
throw new Error("Can only calculate DRepIds for script/key DReps");
}
} catch (e) {
console.error(e);
throw new Error("Malformed DRepId");
}
}
return result;
};
10 changes: 6 additions & 4 deletions packages/mesh-core-csl/test/utils/drep.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDRepIds } from "@meshsdk/core-cst";
import { getDRepIds } from "@meshsdk/core-csl";

describe("DRep", () => {
test("Get DRepIds of a CIP105 script", () => {
Expand All @@ -23,9 +23,11 @@ describe("DRep", () => {

test("Get DRepIds of a CIP105 key hash", () => {
expect(
getDRepIds("drep100gzgh095hxsgarhdvacsz8m98sdwkyhm5924w74au5djs8u5ud"),
getDRepIds(
"drep_vkh100gzgh095hxsgarhdvacsz8m98sdwkyhm5924w74au5djkq8f4h",
),
).toEqual({
cip105: "drep100gzgh095hxsgarhdvacsz8m98sdwkyhm5924w74au5djs8u5ud",
cip105: "drep_vkh100gzgh095hxsgarhdvacsz8m98sdwkyhm5924w74au5djkq8f4h",
cip129: "drep1yfaaqfzaukju6pr5wa4nhzqglv57p46cjlws424m6hhj3kg2k9vj7",
});
});
Expand All @@ -34,7 +36,7 @@ describe("DRep", () => {
expect(
getDRepIds("drep1yfaaqfzaukju6pr5wa4nhzqglv57p46cjlws424m6hhj3kg2k9vj7"),
).toEqual({
cip105: "drep100gzgh095hxsgarhdvacsz8m98sdwkyhm5924w74au5djs8u5ud",
cip105: "drep_vkh100gzgh095hxsgarhdvacsz8m98sdwkyhm5924w74au5djkq8f4h",
cip129: "drep1yfaaqfzaukju6pr5wa4nhzqglv57p46cjlws424m6hhj3kg2k9vj7",
});
});
Expand Down
41 changes: 41 additions & 0 deletions packages/mesh-core-cst/test/utils/drep.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getDRepIds } from "@meshsdk/core-cst";

describe("DRep", () => {
test("Get DRepIds of a CIP105 script", () => {
expect(
getDRepIds(
"drep_script190xv8v32n43luhu9ag05s5mvcs6079lg4zghzyg3j9vlgle68y3",
),
).toEqual({
cip105: "drep_script190xv8v32n43luhu9ag05s5mvcs6079lg4zghzyg3j9vlgle68y3",
cip129: "drep1yv4uesaj92wk8ljlsh4p7jzndnzrflchaz5fzug3zxg4naqkpeas3",
});
});

test("Get DRepIds of a CIP129 script", () => {
expect(
getDRepIds("drep1yv4uesaj92wk8ljlsh4p7jzndnzrflchaz5fzug3zxg4naqkpeas3"),
).toEqual({
cip105: "drep_script190xv8v32n43luhu9ag05s5mvcs6079lg4zghzyg3j9vlgle68y3",
cip129: "drep1yv4uesaj92wk8ljlsh4p7jzndnzrflchaz5fzug3zxg4naqkpeas3",
});
});

test("Get DRepIds of a CIP105 key hash", () => {
expect(
getDRepIds("drep100gzgh095hxsgarhdvacsz8m98sdwkyhm5924w74au5djs8u5ud"),
).toEqual({
cip105: "drep100gzgh095hxsgarhdvacsz8m98sdwkyhm5924w74au5djs8u5ud",
cip129: "drep1yfaaqfzaukju6pr5wa4nhzqglv57p46cjlws424m6hhj3kg2k9vj7",
});
});

test("Get DRepIds of a CIP129 key hash", () => {
expect(
getDRepIds("drep1yfaaqfzaukju6pr5wa4nhzqglv57p46cjlws424m6hhj3kg2k9vj7"),
).toEqual({
cip105: "drep100gzgh095hxsgarhdvacsz8m98sdwkyhm5924w74au5djs8u5ud",
cip129: "drep1yfaaqfzaukju6pr5wa4nhzqglv57p46cjlws424m6hhj3kg2k9vj7",
});
});
});

0 comments on commit f5aed91

Please sign in to comment.