From 9e8cfc6c47fb3d6e45f397f944321c2fe5cf610a Mon Sep 17 00:00:00 2001 From: solidsnakedev Date: Mon, 16 Dec 2024 10:28:18 -0700 Subject: [PATCH 1/2] fix: getAddressDetails --- .changeset/strong-steaks-juggle.md | 5 ++ packages/utils/src/address.ts | 43 ++-------- packages/utils/test/address.test.ts | 122 ++++++++++++++++++++++++++++ packages/utils/tsconfig.json | 4 +- 4 files changed, 135 insertions(+), 39 deletions(-) create mode 100644 .changeset/strong-steaks-juggle.md create mode 100644 packages/utils/test/address.test.ts diff --git a/.changeset/strong-steaks-juggle.md b/.changeset/strong-steaks-juggle.md new file mode 100644 index 00000000..7d29ad0b --- /dev/null +++ b/.changeset/strong-steaks-juggle.md @@ -0,0 +1,5 @@ +--- +"@lucid-evolution/utils": patch +--- + +use addressFromHexOrBech32 in getAddressDetails diff --git a/packages/utils/src/address.ts b/packages/utils/src/address.ts index d25cf2a2..097f3935 100644 --- a/packages/utils/src/address.ts +++ b/packages/utils/src/address.ts @@ -57,39 +57,27 @@ 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 { @@ -97,7 +85,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(), }, paymentCredential, @@ -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", @@ -143,22 +124,17 @@ 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 { @@ -166,7 +142,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(), }, paymentCredential, @@ -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 @@ -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, @@ -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 { @@ -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(), }, }; diff --git a/packages/utils/test/address.test.ts b/packages/utils/test/address.test.ts new file mode 100644 index 00000000..5b05b0c0 --- /dev/null +++ b/packages/utils/test/address.test.ts @@ -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", + }, + }); +}); diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json index a907789b..1970e590 100644 --- a/packages/utils/tsconfig.json +++ b/packages/utils/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "@lucid-evolution/typescript-config/base.json", "include": ["."], - "exclude": ["dist","node_modules", "test"] -} \ No newline at end of file + "exclude": ["dist","node_modules"] +} From a406b382d056526475f47ba41748cc3fdf7c637a Mon Sep 17 00:00:00 2001 From: solidsnakedev Date: Mon, 16 Dec 2024 10:33:18 -0700 Subject: [PATCH 2/2] fix: linting --- packages/utils/test/apply-param.test.ts | 4 ++-- packages/utils/test/assets.test.ts | 2 +- packages/utils/test/cbor.test.ts | 7 +++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/utils/test/apply-param.test.ts b/packages/utils/test/apply-param.test.ts index c7149c02..bce1495a 100644 --- a/packages/utils/test/apply-param.test.ts +++ b/packages/utils/test/apply-param.test.ts @@ -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 { diff --git a/packages/utils/test/assets.test.ts b/packages/utils/test/assets.test.ts index 264c923c..a272e16e 100644 --- a/packages/utils/test/assets.test.ts +++ b/packages/utils/test/assets.test.ts @@ -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: diff --git a/packages/utils/test/cbor.test.ts b/packages/utils/test/cbor.test.ts index b31706d2..4f3faf2a 100644 --- a/packages/utils/test/cbor.test.ts +++ b/packages/utils/test/cbor.test.ts @@ -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";