@@ -5,7 +5,7 @@ import { hashes } from '../../src/utils'
55import requests from '../fixtures/requests'
66import responses from '../fixtures/responses'
77
8- const { hashLedger } = hashes
8+ const { hashLedger, hashTxTree } = hashes
99const { hashLedger : REQUEST_FIXTURES } = requests
1010
1111describe ( 'hashLedger' , function ( ) {
@@ -150,4 +150,49 @@ describe('hashLedger', function () {
150150 'transactionHash in header does not match computed hash of transactions' ,
151151 )
152152 } )
153+
154+ it ( 'hashLedger - normalizes v2 wrapped transactions to v1 for hashing' , function ( ) {
155+ // Convert the v1 flat transactions to v2 wrapped format, then verify
156+ // hashLedger still computes the correct transaction_hash by normalizing
157+ // them back to v1 internally.
158+ const v1Ledger = JSON . parse ( JSON . stringify ( ledger ) )
159+ v1Ledger . parent_close_time = v1Ledger . close_time
160+
161+ // Wrap each v1 flat tx into v2 format: { tx_json, meta, hash, ... }
162+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- test conversion
163+ const v2Transactions = v1Ledger . transactions . map ( ( tx : any ) => {
164+ const { hash, metaData, ledger_index : _li , ...txFields } = tx
165+ return {
166+ tx_json : txFields ,
167+ meta : metaData ,
168+ hash,
169+ validated : true ,
170+ ledger_index : v1Ledger . ledger_index ,
171+ close_time_iso : '2013-Jan-02 03:21:10.000000000 UTC' ,
172+ ledger_hash : v1Ledger . ledger_hash ,
173+ }
174+ } )
175+
176+ const v2Ledger = {
177+ ...v1Ledger ,
178+ transactions : v2Transactions ,
179+ }
180+
181+ // Should produce the same hash because normalizeToV1 converts v2 back
182+ const hash = hashLedger ( v2Ledger , { computeTreeHashes : true } )
183+ assert . strictEqual (
184+ hash ,
185+ hashLedger ( v1Ledger , { computeTreeHashes : true } ) ,
186+ )
187+ } )
188+
189+ it ( 'hashLedger - hashTxTree matches expected transaction_hash' , function ( ) {
190+ // Direct test of hashTxTree against the known transaction_hash from fixtures
191+ const v1Ledger = JSON . parse ( JSON . stringify ( ledger ) )
192+
193+ assert . strictEqual (
194+ hashTxTree ( v1Ledger . transactions ) ,
195+ v1Ledger . transaction_hash ,
196+ )
197+ } )
153198} )
0 commit comments