computeLedgerHash - add requireRawTransactions option

This commit is contained in:
Elliot Lee
2018-08-28 17:31:12 -07:00
parent b9c953fce6
commit 3f2d9d198e
2 changed files with 16 additions and 5 deletions

View File

@@ -25,9 +25,15 @@ function hashLedgerHeader(ledgerHeader) {
return hashes.computeLedgerHash(header)
}
function computeTransactionHash(ledger, version) {
function computeTransactionHash(ledger, version,
options: ComputeLedgerHashOptions) {
if (ledger.rawTransactions === undefined) {
return ledger.transactionHash
if (options.requireRawTransactions !== true) {
return ledger.transactionHash
} else {
throw new common.errors.ValidationError('rawTransactions'
+ ' property is missing from the ledger')
}
}
const transactions: any[] = JSON.parse(ledger.rawTransactions)
const txs = _.map(transactions, tx => {
@@ -60,10 +66,15 @@ function computeStateHash(ledger, version) {
const sLCF_SHAMapV2 = 0x02
function computeLedgerHash(ledger: any): string {
export type ComputeLedgerHashOptions = {
requireRawTransactions?: boolean
}
function computeLedgerHash(ledger: any,
options: ComputeLedgerHashOptions = {}): string {
const version = ((ledger.closeFlags & sLCF_SHAMapV2) === 0) ? 1 : 2
const subhashes = {
transactionHash: computeTransactionHash(ledger, version),
transactionHash: computeTransactionHash(ledger, version, options),
stateHash: computeStateHash(ledger, version)
}
return hashLedgerHeader(_.assign({}, ledger, subhashes))

View File

@@ -2550,7 +2550,7 @@ describe('RippleAPI', function () {
.then(response => {
const ledger = _.assign({}, response,
{ parentCloseTime: response.closeTime });
const hash = this.api.computeLedgerHash(ledger);
const hash = this.api.computeLedgerHash(ledger, {requireRawTransactions: true});
assert.strictEqual(hash,
'E6DB7365949BF9814D76BCC730B01818EB9136A89DB224F3F9F5AAE4569D758E');
});