Skip to content

Commit

Permalink
Merge pull request #452 from Anastasia-Labs/fix/hex-address
Browse files Browse the repository at this point in the history
fix: getAddressDetails
  • Loading branch information
solidsnakedev authored Dec 16, 2024
2 parents edc8663 + a406b38 commit 8fe9277
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-steaks-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lucid-evolution/utils": patch
---

use addressFromHexOrBech32 in getAddressDetails
43 changes: 6 additions & 37 deletions packages/utils/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,47 +57,34 @@ export function validatorToRewardAddress(
export function getAddressDetails(address: string): AddressDetails {
// Base Address
try {
// const parsedAddress = CML.BaseAddress.from_address(
// addressFromHexOrBech32(address),
// )!;
const parsedAddress = CML.BaseAddress.from_address(
CML.Address.from_bech32(address),
addressFromHexOrBech32(address),
)!;
const paymentCredential: Credential =
parsedAddress.payment().kind() === 0
? {
type: "Key",
// hash: toHex(
// parsedAddress.payment_cred().to_keyhash()!.to_bytes(),
// ),
hash: parsedAddress.payment().as_pub_key()!.to_hex(),
}
: {
type: "Script",
// hash: toHex(
// parsedAddress.payment_cred().to_scripthash()!.to_bytes(),
// ),
hash: parsedAddress.payment().as_script()!.to_hex(),
};
const stakeCredential: Credential =
parsedAddress.stake().kind() === 0
? // parsedAddress.stake_cred().kind() === 0
{
? {
type: "Key",
hash: parsedAddress.stake().as_pub_key()!.to_hex(),
// hash: toHex(parsedAddress.stake_cred().to_keyhash()!.to_bytes()),
}
: {
type: "Script",
// hash: toHex(parsedAddress.stake_cred().to_scripthash()!.to_bytes()),
hash: parsedAddress.stake().as_script()!.to_hex(),
};
return {
type: "Base",
networkId: parsedAddress.to_address().network_id(),
address: {
bech32: parsedAddress.to_address().to_bech32(undefined),
// hex: toHex(parsedAddress.to_address().to_bytes()),
hex: parsedAddress.to_address().to_hex(),
},
paymentCredential,
Expand All @@ -109,24 +96,18 @@ export function getAddressDetails(address: string): AddressDetails {

// Enterprise Address
try {
// const parsedAddress = CML.EnterpriseAddress.from_address(
// addressFromHexOrBech32(address)
// )!;
const parsedAddress = CML.EnterpriseAddress.from_address(
CML.Address.from_bech32(address),
addressFromHexOrBech32(address),
)!;
const paymentCredential: Credential =
parsedAddress.payment().kind() === 0
? {
type: "Key",
// hash: toHex(parsedAddress.payment_cred().to_keyhash()!.to_bytes()),
hash: parsedAddress.payment().as_pub_key()!.to_hex(),
}
: {
type: "Script",
hash:
// parsedAddress.payment_cred().to_scripthash()!.to_bytes()
parsedAddress.payment().as_script()!.to_hex(),
hash: parsedAddress.payment().as_script()!.to_hex(),
};
return {
type: "Enterprise",
Expand All @@ -143,30 +124,24 @@ export function getAddressDetails(address: string): AddressDetails {

// Pointer Address
try {
// const parsedAddress = CML.PointerAddress.from_address(
// addressFromHexOrBech32(address)
// )!;
const parsedAddress = CML.PointerAddress.from_address(
CML.Address.from_bech32(address),
addressFromHexOrBech32(address),
)!;
const paymentCredential: Credential =
parsedAddress?.payment().kind() === 0
? {
type: "Key",
// hash: toHex(parsedAddress.payment_cred().to_keyhash()!.to_bytes()),
hash: parsedAddress.payment().as_pub_key()!.to_hex(),
}
: {
type: "Script",
// hash: toHex( parsedAddress.payment_cred().to_scripthash()!.to_bytes()),
hash: parsedAddress.payment().as_script()!.to_hex(),
};
return {
type: "Pointer",
networkId: parsedAddress.to_address().network_id(),
address: {
bech32: parsedAddress.to_address().to_bech32(undefined),
// hex: toHex(parsedAddress.to_address().to_bytes()),
hex: parsedAddress.to_address().to_hex(),
},
paymentCredential,
Expand All @@ -177,11 +152,8 @@ export function getAddressDetails(address: string): AddressDetails {

// Reward Address
try {
// const parsedAddress = CML.RewardAddress.from_address(
// addressFromHexOrBech32(address)
// )!;
const parsedAddress = CML.RewardAddress.from_address(
CML.Address.from_bech32(address),
addressFromHexOrBech32(address),
)!;
const stakeCredential: Credential =
parsedAddress.payment().kind() === 0
Expand All @@ -198,7 +170,6 @@ export function getAddressDetails(address: string): AddressDetails {
networkId: parsedAddress.to_address().network_id(),
address: {
bech32: parsedAddress.to_address().to_bech32(undefined),
// hex: toHex(parsedAddress.to_address().to_bytes()),
hex: parsedAddress.to_address().to_hex(),
},
stakeCredential,
Expand All @@ -211,7 +182,6 @@ export function getAddressDetails(address: string): AddressDetails {
try {
const parsedAddress = ((address: string): CML.ByronAddress => {
try {
// return CML.ByronAddress.from_bytes(fromHex(address));
return CML.ByronAddress.from_cbor_hex(address);
} catch (_e) {
try {
Expand All @@ -227,7 +197,6 @@ export function getAddressDetails(address: string): AddressDetails {
networkId: parsedAddress.content().network_id(),
address: {
bech32: "",
// hex: toHex(parsedAddress.to_address().to_bytes()),
hex: parsedAddress.to_address().to_hex(),
},
};
Expand Down
122 changes: 122 additions & 0 deletions packages/utils/test/address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { expect, test } from "vitest";
import { getAddressDetails } from "../src/address.js";

test("Address parsing - paymentCredential and stakeCredential in bech32", () => {
const addressDetails = getAddressDetails(
"addr_test1qpq0sac2x0vfqdn88n3yafgpeefusdhuuznjf97a724hlhvz4nd0684hr2k2kwj5g56v8ptavh32u95gxsunst4hskxqnwj6fq",
);
expect(addressDetails).toStrictEqual({
type: "Base",
networkId: 0,
address: {
bech32:
"addr_test1qpq0sac2x0vfqdn88n3yafgpeefusdhuuznjf97a724hlhvz4nd0684hr2k2kwj5g56v8ptavh32u95gxsunst4hskxqnwj6fq",
hex: "0040f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd82acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
},
paymentCredential: {
type: "Key",
hash: "40f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd",
},
stakeCredential: {
type: "Key",
hash: "82acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
},
});
});

test("Address parsing - paymentCredential and stakeCredential in hex", () => {
const addressDetails = getAddressDetails(
"0040f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd82acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
);
expect(addressDetails).toStrictEqual({
type: "Base",
networkId: 0,
address: {
bech32:
"addr_test1qpq0sac2x0vfqdn88n3yafgpeefusdhuuznjf97a724hlhvz4nd0684hr2k2kwj5g56v8ptavh32u95gxsunst4hskxqnwj6fq",
hex: "0040f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd82acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
},
paymentCredential: {
type: "Key",
hash: "40f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd",
},
stakeCredential: {
type: "Key",
hash: "82acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
},
});
});

test("Address parsing - paymentCredential in bech32", () => {
const addressDetails = getAddressDetails(
"addr_test1vpq0sac2x0vfqdn88n3yafgpeefusdhuuznjf97a724hlhgc3l3we",
);
expect(addressDetails).toStrictEqual({
type: "Enterprise",
networkId: 0,
address: {
bech32: "addr_test1vpq0sac2x0vfqdn88n3yafgpeefusdhuuznjf97a724hlhgc3l3we",
hex: "6040f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd",
},
paymentCredential: {
type: "Key",
hash: "40f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd",
},
});
});

test("Address parsing - paymentCredential in hex", () => {
const addressDetails = getAddressDetails(
"6040f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd",
);
expect(addressDetails).toStrictEqual({
type: "Enterprise",
networkId: 0,
address: {
bech32: "addr_test1vpq0sac2x0vfqdn88n3yafgpeefusdhuuznjf97a724hlhgc3l3we",
hex: "6040f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd",
},
paymentCredential: {
type: "Key",
hash: "40f8770a33d89036673ce24ea501ce53c836fce0a72497ddf2ab7fdd",
},
});
});

test("Address parsing - stakeCredential in bech32", () => {
const addressDetails = getAddressDetails(
"stake_test1uzp2ekhar6m34t9t8f2y2dxrs47ktc4wz6yrgwfc96mctrqxdrmv4",
);
expect(addressDetails).toStrictEqual({
type: "Reward",
networkId: 0,
address: {
bech32:
"stake_test1uzp2ekhar6m34t9t8f2y2dxrs47ktc4wz6yrgwfc96mctrqxdrmv4",
hex: "e082acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
},
stakeCredential: {
type: "Key",
hash: "82acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
},
});
});

test("Address parsing - stakeCredential in hex", () => {
const addressDetails = getAddressDetails(
"e082acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
);
expect(addressDetails).toStrictEqual({
type: "Reward",
networkId: 0,
address: {
bech32:
"stake_test1uzp2ekhar6m34t9t8f2y2dxrs47ktc4wz6yrgwfc96mctrqxdrmv4",
hex: "e082acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
},
stakeCredential: {
type: "Key",
hash: "82acdafd1eb71aacab3a544534c3857d65e2ae16883439382eb7858c",
},
});
});
4 changes: 2 additions & 2 deletions packages/utils/test/apply-param.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Constr } from "@lucid-evolution/plutus";
import { applyParamsToScript, validatorToScriptHash } from "../src/scripts";
import { applyParamsToScript, validatorToScriptHash } from "../src/scripts.js";
import { assert, test } from "vitest";
import { WithdrawalValidator } from "@lucid-evolution/core-types";
import { applyDoubleCborEncoding } from "../src/cbor";
import { applyDoubleCborEncoding } from "../src/cbor.js";

// Parametrized Contract Spec
// type Redeemer {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/test/assets.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert, test } from "vitest";
import { assetsToValue, sortCanonical } from "../src/value";
import { assetsToValue, sortCanonical } from "../src/value.js";

const unsortedAssets = {
b7cafbba7e8d5dde2557c07254cd0e296bb581e72917a5179b4a00b04275726e61626c65546f6b656e:
Expand Down
7 changes: 5 additions & 2 deletions packages/utils/test/cbor.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { applyDoubleCborEncoding, applySingleCborEncoding } from "../src/cbor";
import { assert, expect, test } from "vitest";
import {
applyDoubleCborEncoding,
applySingleCborEncoding,
} from "../src/cbor.js";
import { assert, test } from "vitest";

const helloFlat =
"01000032323232323223223225333006323253330083371e6eb8c008c028dd5002a4410d48656c6c6f2c20576f726c642100100114a06644646600200200644a66601c00229404c94ccc030cdc79bae301000200414a226600600600260200026eb0c02cc030c030c030c030c030c030c030c030c024dd5180098049baa002375c600260126ea80188c02c0045261365653330043370e900018029baa001132325333009300b002149858dd7180480098031baa0011653330023370e900018019baa0011323253330073009002149858dd7180380098021baa001165734aae7555cf2ab9f5742ae881";
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@lucid-evolution/typescript-config/base.json",
"include": ["."],
"exclude": ["dist","node_modules", "test"]
}
"exclude": ["dist","node_modules"]
}

0 comments on commit 8fe9277

Please sign in to comment.