Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Clark
committed
Aug 14, 2015
1 parent
8c431b4
commit 92fbc61
Showing
7 changed files
with
594 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* @flow */ | ||
'use strict'; | ||
const _ = require('lodash'); | ||
const common = require('../common'); | ||
|
||
function convertLedgerHeader(header) { | ||
return { | ||
accepted: header.accepted, | ||
closed: header.closed, | ||
account_hash: header.accountHash, | ||
close_time: header.closeTime, | ||
close_time_resolution: header.closeTimeResolution, | ||
close_flags: header.closeFlags, | ||
hash: header.ledgerHash, | ||
ledger_hash: header.ledgerHash, | ||
ledger_index: header.ledgerVersion.toString(), | ||
seqNum: header.ledgerVersion.toString(), | ||
parent_hash: header.parentLedgerHash, | ||
parent_close_time: header.parentCloseTime, | ||
total_coins: header.totalDrops, | ||
totalCoins: header.totalDrops, | ||
transaction_hash: header.transactionHash | ||
}; | ||
} | ||
|
||
function hashLedgerHeader(ledgerHeader) { | ||
const header = convertLedgerHeader(ledgerHeader); | ||
return common.core.Ledger.calculateLedgerHash(header); | ||
} | ||
|
||
function computeLedgerHash(ledgerHeader: Object, transactions: Array<Object> | ||
): string { | ||
if (transactions) { | ||
const txs = _.map(transactions, tx => { | ||
const mergeTx = _.assign({}, _.omit(tx, 'tx'), tx.tx || {}); | ||
const renameMeta = _.assign({}, _.omit(mergeTx, 'meta'), | ||
tx.meta ? {metaData: tx.meta} : {}); | ||
return renameMeta; | ||
}); | ||
const ledger = common.core.Ledger.from_json({transactions: txs}); | ||
const transactionHash = ledger.calc_tx_hash().to_hex(); | ||
if (ledgerHeader.transaction_hash !== undefined | ||
&& ledgerHeader.transaction_hash !== transactionHash) { | ||
throw new common.errors.ValidationError('transaction_hash in header' | ||
+ ' does not match computed hash of transactions'); | ||
} | ||
return hashLedgerHeader(_.assign({}, ledgerHeader, {transactionHash})); | ||
} | ||
return hashLedgerHeader(ledgerHeader); | ||
} | ||
|
||
module.exports = computeLedgerHash; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.