From cd81e27a4bb8325adb0233856805d4281c28dfaa Mon Sep 17 00:00:00 2001 From: yushi Date: Thu, 22 Dec 2022 01:16:22 +0800 Subject: [PATCH 1/2] fix address format handling of tx summaries endpoint --- src/services/txSummariesForAddresses.ts | 28 ++++++++++++++++++------- src/utils/index.ts | 8 ++++++- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/services/txSummariesForAddresses.ts b/src/services/txSummariesForAddresses.ts index bc3d641c..eb1228cf 100644 --- a/src/services/txSummariesForAddresses.ts +++ b/src/services/txSummariesForAddresses.ts @@ -80,7 +80,9 @@ export const handleTxSummariesForAddresses = block.epoch_no AS epoch, block.slot_no AS slot, array_agg(output.address) AS addrs_out, - array_agg(output_of_input.address) AS addrs_in + array_agg(output_of_input.address) AS addrs_in, + array_agg(encode(output.payment_cred, 'hex')) AS payment_creds_out, + array_agg(encode(output_of_input.payment_cred, 'hex')) AS payment_creds_in FROM tx INNER JOIN block ON block.id = tx.block_id INNER JOIN tx_out AS output ON output.tx_id = tx.id @@ -104,19 +106,31 @@ export const handleTxSummariesForAddresses = const result: { [address: string]: any[] } = {}; const addressSet = new Set(addresses); + const paymentCredHashToBech32 = + (hash: string) => addressTypes.paymentCredsHashToBech32Mapping.get(hash); + for (const row of rows) { - for (const address of [...row.addrs_in, ...row.addrs_out]) { + for (const address of [ + ...row.addrs_in, + ...row.addrs_out, + ...row.payment_creds_out.map(paymentCredHashToBech32), + ...row.payment_creds_in.map(paymentCredHashToBech32)] + ) { if (addressSet.has(address)) { - if (!result[address]) { - result[address] = []; - } - result[address].push({ + const summary = { txHash: row.txHash.toString("hex"), blockHash: row.blockHash.toString("hex"), txBlockIndex: row.txBlockIndex, epoch: row.epoch, slot: row.slot, - }); + }; + if (!result[address]) { + result[address] = [summary]; + } else if ( + !result[address].some(({ txHash }) => txHash === summary.txHash) + ) { + result[address].push(summary); + } } } } diff --git a/src/utils/index.ts b/src/utils/index.ts index fec54b48..f5ff4f88 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -254,11 +254,14 @@ export function getAddressesByType(addresses: string[]): { bech32: string[]; paymentCreds: string[]; stakingKeys: string[]; + paymentCredsHashToBech32Mapping: Map; } { const legacyAddr = []; const bech32 = []; const paymentCreds = []; const stakingKeys = []; + const paymentCredsHashToBech32Mapping: Map = new Map(); + for (const address of addresses) { // 1) Check if it's a Byron-era address if (ByronAddress.is_valid(address)) { @@ -295,7 +298,9 @@ export function getAddressesByType(addresses: string[]): { } case Prefixes.PAYMENT_KEY_HASH: { const payload = fromWords(bech32Info.words); - paymentCreds.push(`\\x${Buffer.from(payload).toString("hex")}`); + const hash = Buffer.from(payload).toString("hex"); + paymentCredsHashToBech32Mapping.set(hash, address); + paymentCreds.push(`\\x${hash}`); break; } default: @@ -324,6 +329,7 @@ export function getAddressesByType(addresses: string[]): { bech32, paymentCreds, stakingKeys, + paymentCredsHashToBech32Mapping, }; } From f6986b1f04b4575b230158795a9f30fbf5205499 Mon Sep 17 00:00:00 2001 From: yushi Date: Thu, 22 Dec 2022 01:22:47 +0800 Subject: [PATCH 2/2] prettier --- src/services/txSummariesForAddresses.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/txSummariesForAddresses.ts b/src/services/txSummariesForAddresses.ts index eb1228cf..c05e87fc 100644 --- a/src/services/txSummariesForAddresses.ts +++ b/src/services/txSummariesForAddresses.ts @@ -106,16 +106,16 @@ export const handleTxSummariesForAddresses = const result: { [address: string]: any[] } = {}; const addressSet = new Set(addresses); - const paymentCredHashToBech32 = - (hash: string) => addressTypes.paymentCredsHashToBech32Mapping.get(hash); + const paymentCredHashToBech32 = (hash: string) => + addressTypes.paymentCredsHashToBech32Mapping.get(hash); for (const row of rows) { for (const address of [ ...row.addrs_in, ...row.addrs_out, ...row.payment_creds_out.map(paymentCredHashToBech32), - ...row.payment_creds_in.map(paymentCredHashToBech32)] - ) { + ...row.payment_creds_in.map(paymentCredHashToBech32), + ]) { if (addressSet.has(address)) { const summary = { txHash: row.txHash.toString("hex"),