Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/serialize address obj #533

Merged
merged 4 commits into from
Mar 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions packages/mesh-core-cst/src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -189,7 +189,7 @@ const plutusDataToAddrBech32 = (
}
const cardanoPaymentCredential = {
hash: Hash28ByteBase16(Buffer.from(paymentBytes).toString("hex")),
type: paymentConstrData.getAlternative() === BigInt(0) ? 0 : 1,
type: Number(paymentConstrData.getAlternative()),
};

const delegationData = plutusDataList.get(1);
@@ -235,17 +235,32 @@ const plutusDataToAddrBech32 = (
);
}
// Credential
const delegationBytes = delegationDataInnerList.get(0).asBoundedBytes();
const delegationCredential = delegationDataInnerList
.get(0)
.asConstrPlutusData();
if (!delegationCredential) {
throw new Error(
"Error: serializeAddressObj: Delegation inner part must be a constructor",
);
}

const delegationBytesList = delegationCredential.getData();
if (delegationBytesList.getLength() !== 1) {
throw new Error(
"Error: serializeAddressObj: Delegation bytes part must contain 1 element",
);
}

const delegationBytes = delegationBytesList.get(0).asBoundedBytes();
if (!delegationBytes) {
throw new Error(
"Error: serializeAddressObj: Delegation inner part must be bytes",
"Error: serializeAddressObj: Delegation bytes part must be of type bytes",
);
}

const cardanoStakeCredential = {
hash: Hash28ByteBase16(Buffer.from(delegationBytes).toString("hex")),
type:
delegationDataInnerConstrData.getAlternative() === BigInt(0) ? 0 : 1,
type: Number(delegationCredential.getAlternative()),
};

return BaseAddress.fromCredentials(
81 changes: 81 additions & 0 deletions packages/mesh-core-cst/test/resolvers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
NativeScript,
PlutusScript,
PubKeyAddress,
resolveFingerprint,
ScriptAddress,
} from "@meshsdk/common";
import {
resolveDataHash,
@@ -12,6 +14,7 @@ import {
resolvePlutusScriptHash,
resolveRewardAddress,
resolveStakeKeyHash,
serializeAddressObj,
} from "@meshsdk/core-cst";

describe("resolveDataHash", () => {
@@ -138,3 +141,81 @@ describe("resolveStakeKeyHash", () => {
).toEqual("6233a60cac644e38f5232a67093bb7690dc29ca5ba30489072a50174");
});
});

describe("serializeAddressObj", () => {
it("should return the correct address", () => {
const addressObj: PubKeyAddress = {
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
bytes: "51374dcf4a79090cfab6c03c276bf8f994fbedca2c1ed90a085bede7",
},
],
},
{
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
bytes:
"7b8e3dfad1c97edb1e01c7dfdd7ac506e13b6659ed5def247a9d7942",
},
],
},
],
},
],
},
],
};
expect(serializeAddressObj(addressObj)).toEqual(
"addr_test1qpgnwnw0ffusjr86kmqrcfmtlruef7ldegkpakg2ppd7memm3c7l45wf0md3uqw8mlwh43gxuyakvk0dthhjg75a09pqeap7m2",
);
});

it("should return the correct address for script key", () => {
const addressObj: ScriptAddress = {
constructor: 0,
fields: [
{
constructor: 1,
fields: [
{
bytes: "51374dcf4a79090cfab6c03c276bf8f994fbedca2c1ed90a085bede7",
},
],
},
{
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
constructor: 1,
fields: [
{
bytes:
"7b8e3dfad1c97edb1e01c7dfdd7ac506e13b6659ed5def247a9d7942",
},
],
},
],
},
],
},
],
};
expect(serializeAddressObj(addressObj)).toEqual(
"addr_test1xpgnwnw0ffusjr86kmqrcfmtlruef7ldegkpakg2ppd7memm3c7l45wf0md3uqw8mlwh43gxuyakvk0dthhjg75a09pqgvr6sl",
);
});
});