Skip to content

Commit 72babca

Browse files
committed
fix: correct V1/V2 API format type naming and implementation
Root cause: Type names LedgerTransactionExpanded and LedgerTransactionExpandedV1 were inverted from the actual API versions they represented. - API v1 returns flat format (fields directly on object + metaData) - API v2 returns wrapped format (tx_json + meta as sibling fields) But types were named backwards: - LedgerTransactionExpanded (no V suffix) = claimed v2 but format was flat (v1) - LedgerTransactionExpandedV1 = claimed v1 but format was wrapped (v2) Fixed by: - Renamed LedgerTransactionExpanded -> LedgerTransactionExpandedV2 (now correctly wrapped) - Renamed LedgerTransactionExpandedV1 -> stays V1 (now correctly flat) - Added deprecated alias LedgerTransactionExpanded = V2 for backwards compatibility - Updated Ledger (v2) interface to use V2 type (wrapped) - Updated LedgerV1 interface to use V1 type (flat) - Renamed normalizeToV2 -> normalizeToV1 (function actually converts v2 wrapped -> v1 flat) - Updated hashTxTree to expect v1 flat format (what it actually uses) - Fixed JSDoc comments to accurately describe format differences Verified against rippled API-VERSION-2.md which clearly documents: - API v1: old flat format - API v2: new wrapped format with tx_json + meta
1 parent c3cab13 commit 72babca

3 files changed

Lines changed: 36 additions & 22 deletions

File tree

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ interface BaseLedger {
5757
}
5858

5959
/**
60-
* Expanded transaction format in API version 2.
60+
* Expanded transaction format in API version 1.
6161
* Transactions are returned as flat objects with the transaction fields
6262
* directly on the object, plus `hash` and `metaData`.
6363
*/
64-
export type LedgerTransactionExpanded = Transaction & {
64+
export type LedgerTransactionExpandedV1 = Transaction & {
6565
hash: string
6666
metaData?: TransactionMetadata
6767
}
6868

6969
/**
70-
* Expanded transaction format in API version 1.
70+
* Expanded transaction format in API version 2.
7171
* Transactions are wrapped in an object with `tx_json` and `meta` fields.
7272
*/
73-
export interface LedgerTransactionExpandedV1 {
73+
export interface LedgerTransactionExpandedV2 {
7474
tx_json: Transaction
7575
meta: TransactionMetadata
7676
hash: string
@@ -80,6 +80,13 @@ export interface LedgerTransactionExpandedV1 {
8080
ledger_hash: string
8181
}
8282

83+
/**
84+
* @deprecated Use LedgerTransactionExpandedV2 instead. The old name was backwards:
85+
* "Expanded" referred to API v2 (wrapped), while "ExpandedV1" was actually the v1 format (flat).
86+
* For clarity: LedgerTransactionExpandedV1 = API v1 flat format, LedgerTransactionExpandedV2 = API v2 wrapped format.
87+
*/
88+
export type LedgerTransactionExpanded = LedgerTransactionExpandedV2
89+
8390
/**
8491
* A ledger is a block of transactions and shared state data. It has a unique
8592
* header that describes its contents using cryptographic hashes.
@@ -93,10 +100,10 @@ export interface Ledger extends BaseLedger {
93100
ledger_index: number
94101
/**
95102
* Transactions applied in this ledger version. When expanded, members are
96-
* full representations of the transactions as flat objects with the
97-
* transaction fields directly on the object, plus `hash` and `metaData`.
103+
* full representations of the transactions wrapped in objects with
104+
* `tx_json` and `meta` fields.
98105
*/
99-
transactions?: Array<string | LedgerTransactionExpanded>
106+
transactions?: Array<string | LedgerTransactionExpandedV2>
100107
}
101108

102109
/**
@@ -114,8 +121,8 @@ export interface LedgerV1 extends BaseLedger {
114121
ledger_index: string
115122
/**
116123
* Transactions applied in this ledger version. When expanded, members are
117-
* full representations of the transactions wrapped in objects with
118-
* `tx_json` and `meta` fields.
124+
* full representations of the transactions as flat objects with the
125+
* transaction fields directly on the object, plus `hash` and `metaData`.
119126
*/
120127
transactions?: Array<string | LedgerTransactionExpandedV1>
121128
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
LedgerV1,
2323
LedgerTransactionExpanded,
2424
LedgerTransactionExpandedV1,
25+
LedgerTransactionExpandedV2,
2526
} from './Ledger'
2627
import { LedgerEntry, LedgerEntryFilter } from './LedgerEntry'
2728
import LedgerHashes from './LedgerHashes'
@@ -65,6 +66,7 @@ export {
6566
LedgerV1,
6667
LedgerTransactionExpanded,
6768
LedgerTransactionExpandedV1,
69+
LedgerTransactionExpandedV2,
6870
LedgerEntryFilter,
6971
LedgerEntry,
7072
LedgerHashes,

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
LedgerVersionMap,
1515
LedgerTransactionExpanded,
1616
LedgerTransactionExpandedV1,
17+
LedgerTransactionExpandedV2,
1718
} from '../../models/ledger/Ledger'
1819
import { Transaction } from '../../models/transactions'
1920
import { GlobalFlags } from '../../models/transactions/common'
@@ -129,12 +130,13 @@ export function hashLedgerHeader(
129130

130131
/**
131132
* Compute the root hash of the SHAMap containing all transactions.
133+
* Transactions must be in v1 flat format (fields directly on object with metaData field).
132134
*
133-
* @param transactions - List of Transactions.
135+
* @param transactions - List of v1 flat formatted Transactions.
134136
* @returns The root hash of the SHAMap.
135137
* @category Utilities
136138
*/
137-
export function hashTxTree(transactions: LedgerTransactionExpanded[]): string {
139+
export function hashTxTree(transactions: LedgerTransactionExpandedV1[]): string {
138140
const shamap = new SHAMap()
139141
for (const txJSON of transactions) {
140142
const txBlobHex = encode(txJSON)
@@ -166,15 +168,17 @@ export function hashStateTree(entries: LedgerEntry[]): string {
166168
}
167169

168170
/**
169-
* Normalize a v1 wrapped transaction to v2 flat format.
171+
* Normalize a transaction to v1 flat format.
172+
* Converts v2 wrapped format (tx_json + meta) to v1 flat format (fields directly on object with metaData).
170173
*
171-
* @param tx - The transaction to normalize.
172-
* @returns The normalized v2 transaction.
174+
* @param tx - The transaction to normalize (can be v1 flat or v2 wrapped).
175+
* @returns The normalized v1 flat transaction.
173176
*/
174-
function normalizeToV2(
175-
tx: LedgerTransactionExpanded | LedgerTransactionExpandedV1,
176-
): LedgerTransactionExpanded {
177+
function normalizeToV1(
178+
tx: LedgerTransactionExpandedV1 | LedgerTransactionExpandedV2,
179+
): LedgerTransactionExpandedV1 {
177180
if ('tx_json' in tx) {
181+
// v2 wrapped format: extract tx_json and meta, convert to v1 flat
178182
// Transaction union type cannot be spread directly (TS2698), so we
179183
// widen to a plain record first.
180184
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- required to spread Transaction union
@@ -184,8 +188,9 @@ function normalizeToV2(
184188
...txJson,
185189
hash: tx.hash,
186190
metaData: tx.meta,
187-
} as LedgerTransactionExpanded
191+
} as LedgerTransactionExpandedV1
188192
}
193+
// already v1 flat format
189194
return tx
190195
}
191196

@@ -203,17 +208,17 @@ function computeTransactionHash(
203208
throw new ValidationError('transactions is missing from the ledger')
204209
}
205210

206-
// Normalize transactions to the v2 flat format expected by hashTxTree.
211+
// Normalize transactions to v1 flat format expected by hashTxTree.
207212
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- ledger is a version union
208213
const txs = ledger.transactions as Array<
209-
string | LedgerTransactionExpanded | LedgerTransactionExpandedV1
214+
string | LedgerTransactionExpandedV1 | LedgerTransactionExpandedV2
210215
>
211216
const normalizedTransactions = txs
212217
.filter(
213-
(tx): tx is LedgerTransactionExpanded | LedgerTransactionExpandedV1 =>
218+
(tx): tx is LedgerTransactionExpandedV1 | LedgerTransactionExpandedV2 =>
214219
typeof tx !== 'string',
215220
)
216-
.map(normalizeToV2)
221+
.map(normalizeToV1)
217222

218223
const transactionHash = hashTxTree(normalizedTransactions)
219224

0 commit comments

Comments
 (0)