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 address format handling of tx summaries endpoint #411

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
28 changes: 21 additions & 7 deletions src/services/txSummariesForAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,14 @@ export function getAddressesByType(addresses: string[]): {
bech32: string[];
paymentCreds: string[];
stakingKeys: string[];
paymentCredsHashToBech32Mapping: Map<string, string>;
} {
const legacyAddr = [];
const bech32 = [];
const paymentCreds = [];
const stakingKeys = [];
const paymentCredsHashToBech32Mapping: Map<string, string> = new Map();

for (const address of addresses) {
// 1) Check if it's a Byron-era address
if (ByronAddress.is_valid(address)) {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -324,6 +329,7 @@ export function getAddressesByType(addresses: string[]): {
bech32,
paymentCreds,
stakingKeys,
paymentCredsHashToBech32Mapping,
};
}

Expand Down