Skip to content

Commit 162a0da

Browse files
committed
fix: update LedgerResponseExpanded type for APIv2
Split expanded transaction types for Ledger (v2) and LedgerV1: - v2: transactions are flat objects with hash and metaData fields - v1: transactions are wrapped in { tx_json, meta, hash, ... } Add LedgerTransactionExpanded and LedgerTransactionExpandedV1 types to accurately reflect the different response structures. Move transactions property from BaseLedger to Ledger/LedgerV1 with the correct type for each API version. Update hashTxTree to use the new LedgerTransactionExpanded type. Fixes #2886
1 parent be8d9a2 commit 162a0da

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

packages/xrpl/src/models/ledger/Ledger.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,30 @@ interface BaseLedger {
5454
total_coins: string
5555
/** Hash of the transaction information included in this ledger, as hex. */
5656
transaction_hash: string
57-
/**
58-
* Transactions applied in this ledger version. By default, members are the
59-
* transactions' identifying Hash strings. If the request specified expand as
60-
* true, members are full representations of the transactions instead, in
61-
* either JSON or binary depending on whether the request specified binary
62-
* as true.
63-
*/
64-
transactions?: Array<
65-
Transaction & {
66-
hash: string
67-
metaData?: TransactionMetadata
68-
}
69-
>
57+
}
58+
59+
/**
60+
* Expanded transaction format in API version 2.
61+
* Transactions are returned as flat objects with the transaction fields
62+
* directly on the object, plus `hash` and `metaData`.
63+
*/
64+
export type LedgerTransactionExpanded = Transaction & {
65+
hash: string
66+
metaData?: TransactionMetadata
67+
}
68+
69+
/**
70+
* Expanded transaction format in API version 1.
71+
* Transactions are wrapped in an object with `tx_json` and `meta` fields.
72+
*/
73+
export interface LedgerTransactionExpandedV1 {
74+
tx_json: Transaction
75+
meta: TransactionMetadata
76+
hash: string
77+
validated: boolean
78+
ledger_index: number
79+
close_time_iso: string
80+
ledger_hash: string
7081
}
7182

7283
/**
@@ -80,6 +91,14 @@ export interface Ledger extends BaseLedger {
8091
* The ledger index of the ledger. Represented as a number.
8192
*/
8293
ledger_index: number
94+
/**
95+
* Transactions applied in this ledger version. By default, members are the
96+
* transactions' identifying Hash strings. If the request specified expand as
97+
* true, members are full representations of the transactions instead, in
98+
* either JSON or binary depending on whether the request specified binary
99+
* as true.
100+
*/
101+
transactions?: Array<LedgerTransactionExpanded>
83102
}
84103

85104
/**
@@ -95,6 +114,13 @@ export interface LedgerV1 extends BaseLedger {
95114
* integer; some display it as a number.
96115
*/
97116
ledger_index: string
117+
/**
118+
* Transactions applied in this ledger version. By default, members are the
119+
* transactions' identifying Hash strings. If the request specified expand as
120+
* true, members are full representations of the transactions instead,
121+
* wrapped in objects with `tx_json` and `meta` fields.
122+
*/
123+
transactions?: Array<LedgerTransactionExpandedV1>
98124
}
99125

100126
/**

packages/xrpl/src/models/ledger/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import FeeSettings, {
1717
FeeSettingsPostAmendmentFields,
1818
FEE_SETTINGS_ID,
1919
} from './FeeSettings'
20-
import { Ledger, LedgerV1 } from './Ledger'
20+
import {
21+
Ledger,
22+
LedgerV1,
23+
LedgerTransactionExpanded,
24+
LedgerTransactionExpandedV1,
25+
} from './Ledger'
2126
import { LedgerEntry, LedgerEntryFilter } from './LedgerEntry'
2227
import LedgerHashes from './LedgerHashes'
2328
import Loan, { LoanFlags } from './Loan'
@@ -58,6 +63,8 @@ export {
5863
FeeSettingsPostAmendmentFields,
5964
Ledger,
6065
LedgerV1,
66+
LedgerTransactionExpanded,
67+
LedgerTransactionExpandedV1,
6168
LedgerEntryFilter,
6269
LedgerEntry,
6370
LedgerHashes,

packages/xrpl/src/utils/hashes/hashLedger.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import { decode, encode } from 'ripple-binary-codec'
1010
import { ValidationError, XrplError } from '../../errors'
1111
import { APIVersion } from '../../models'
1212
import { LedgerEntry } from '../../models/ledger'
13-
import { LedgerVersionMap } from '../../models/ledger/Ledger'
14-
import { Transaction, TransactionMetadata } from '../../models/transactions'
13+
import {
14+
LedgerVersionMap,
15+
LedgerTransactionExpanded,
16+
} from '../../models/ledger/Ledger'
17+
import { Transaction } from '../../models/transactions'
1518
import { GlobalFlags } from '../../models/transactions/common'
1619
import { hasFlag } from '../../models/utils'
1720

@@ -131,7 +134,7 @@ export function hashLedgerHeader(
131134
* @category Utilities
132135
*/
133136
export function hashTxTree(
134-
transactions: Array<Transaction & { metaData?: TransactionMetadata }>,
137+
transactions: LedgerTransactionExpanded[],
135138
): string {
136139
const shamap = new SHAMap()
137140
for (const txJSON of transactions) {
@@ -177,7 +180,9 @@ function computeTransactionHash(
177180
throw new ValidationError('transactions is missing from the ledger')
178181
}
179182

180-
const transactionHash = hashTxTree(ledger.transactions)
183+
const transactionHash = hashTxTree(
184+
ledger.transactions as LedgerTransactionExpanded[],
185+
)
181186

182187
if (transaction_hash !== transactionHash) {
183188
throw new ValidationError(

0 commit comments

Comments
 (0)