@@ -134,9 +134,7 @@ export function hashLedgerHeader(
134134 * @returns The root hash of the SHAMap.
135135 * @category Utilities
136136 */
137- export function hashTxTree (
138- transactions : LedgerTransactionExpanded [ ] ,
139- ) : string {
137+ export function hashTxTree ( transactions : LedgerTransactionExpanded [ ] ) : string {
140138 const shamap = new SHAMap ( )
141139 for ( const txJSON of transactions ) {
142140 const txBlobHex = encode ( txJSON )
@@ -167,6 +165,18 @@ export function hashStateTree(entries: LedgerEntry[]): string {
167165 return shamap . hash
168166}
169167
168+ /**
169+ * Normalize a v1 wrapped transaction to v2 flat format.
170+ */
171+ function normalizeToV2 (
172+ tx : LedgerTransactionExpanded | LedgerTransactionExpandedV1 ,
173+ ) : LedgerTransactionExpanded {
174+ if ( 'tx_json' in tx ) {
175+ return { ...tx . tx_json , hash : tx . hash , metaData : tx . meta }
176+ }
177+ return tx
178+ }
179+
170180function computeTransactionHash (
171181 ledger : LedgerVersionMap < APIVersion > ,
172182 options : HashLedgerHeaderOptions ,
@@ -181,30 +191,17 @@ function computeTransactionHash(
181191 throw new ValidationError ( 'transactions is missing from the ledger' )
182192 }
183193
184- // Normalize transactions to the v2 flat format expected by hashTxTree.
185- // V1 expanded transactions wrap data in { tx_json, meta }, while v2 has
186- // the transaction fields directly on the object with metaData.
187- const normalizedTransactions = (
188- ledger . transactions as Array <
189- string | LedgerTransactionExpanded | LedgerTransactionExpandedV1
190- >
191- )
194+ // Normalize transactions to the v2 flat format expected by hashTxTree.
195+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- ledger is a version union
196+ const txs = ledger . transactions as Array <
197+ string | LedgerTransactionExpanded | LedgerTransactionExpandedV1
198+ >
199+ const normalizedTransactions = txs
192200 . filter (
193- (
194- tx ,
195- ) : tx is LedgerTransactionExpanded | LedgerTransactionExpandedV1 =>
201+ ( tx ) : tx is LedgerTransactionExpanded | LedgerTransactionExpandedV1 =>
196202 typeof tx !== 'string' ,
197203 )
198- . map ( ( tx ) => {
199- if ( 'tx_json' in tx ) {
200- // V1 wrapped format — normalize to v2 flat format
201- return Object . assign ( { } , tx . tx_json , {
202- hash : tx . hash ,
203- metaData : tx . meta ,
204- } ) as LedgerTransactionExpanded
205- }
206- return tx
207- } )
204+ . map ( normalizeToV2 )
208205
209206 const transactionHash = hashTxTree ( normalizedTransactions )
210207
0 commit comments