Use ripple-hashes

This commit is contained in:
Chris Clark
2015-10-14 15:45:01 -07:00
parent 462e440d5b
commit 0dc000839b
11 changed files with 46 additions and 562 deletions

View File

@@ -1,93 +0,0 @@
/* eslint-disable max-len, valid-jsdoc */
'use strict';
const assert = require('assert');
const fs = require('fs');
const Ledger = require('ripple-lib').Ledger;
/**
* @param ledger_index {Number}
* Expects a corresponding ledger dump in $repo/test/fixtures/ folder
*/
function create_ledger_test(ledger_index) {
describe(String(ledger_index), function() {
const path = __dirname + '/fixtures/ledger-full-' + ledger_index + '.json';
const ledger_raw = fs.readFileSync(path);
const ledger_json = JSON.parse(ledger_raw);
const ledger = Ledger.from_json(ledger_json);
const hasAccounts = Array.isArray(ledger_json.accountState)
&& ledger_json.accountState.length > 0;
if (hasAccounts) {
it('has account_hash of ' + ledger_json.account_hash, function() {
assert.equal(ledger_json.account_hash, ledger.calc_account_hash());
});
}
it('has transaction_hash of ' + ledger_json.transaction_hash, function() {
assert.equal(ledger_json.transaction_hash,
ledger.calc_tx_hash());
});
});
}
describe('Ledger', function() {
// This is the first recorded ledger with a non empty transaction set
create_ledger_test(38129);
// Because, why not.
create_ledger_test(40000);
// 1311 AffectedNodes, no accounts
create_ledger_test(7501326);
describe('#calcAccountRootEntryHash', function() {
it('will calculate the AccountRoot entry hash for rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', function() {
const account = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
const expectedEntryHash = '2B6AC232AA4C4BE41BF49D2459FA4A0347E1B543A4C92FCEE0821C0201E2E9A8';
const actualEntryHash = Ledger.calcAccountRootEntryHash(account);
assert.equal(actualEntryHash, expectedEntryHash);
});
});
describe('#calcRippleStateEntryHash', function() {
it('will calculate the RippleState entry hash for rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh and rB5TihdPbKgMrkFqrqUC3yLdE8hhv4BdeY in USD', function() {
const account1 = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
const account2 = 'rB5TihdPbKgMrkFqrqUC3yLdE8hhv4BdeY';
const currency = 'USD';
const expectedEntryHash = 'C683B5BB928F025F1E860D9D69D6C554C2202DE0D45877ADB3077DA4CB9E125C';
const actualEntryHash1 = Ledger.calcRippleStateEntryHash(account1, account2, currency);
const actualEntryHash2 = Ledger.calcRippleStateEntryHash(account2, account1, currency);
assert.equal(actualEntryHash1, expectedEntryHash);
assert.equal(actualEntryHash2, expectedEntryHash);
});
it('will calculate the RippleState entry hash for r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV and rUAMuQTfVhbfqUDuro7zzy4jj4Wq57MPTj in UAM', function() {
const account1 = 'r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV';
const account2 = 'rUAMuQTfVhbfqUDuro7zzy4jj4Wq57MPTj';
const currency = 'UAM';
const expectedEntryHash = 'AE9ADDC584358E5847ADFC971834E471436FC3E9DE6EA1773DF49F419DC0F65E';
const actualEntryHash1 = Ledger.calcRippleStateEntryHash(account1, account2, currency);
const actualEntryHash2 = Ledger.calcRippleStateEntryHash(account2, account1, currency);
assert.equal(actualEntryHash1, expectedEntryHash);
assert.equal(actualEntryHash2, expectedEntryHash);
});
});
describe('#calcOfferEntryHash', function() {
it('will calculate the Offer entry hash for r32UufnaCGL82HubijgJGDmdE5hac7ZvLw, sequence 137', function() {
const account = 'r32UufnaCGL82HubijgJGDmdE5hac7ZvLw';
const sequence = 137;
const expectedEntryHash = '03F0AED09DEEE74CEF85CD57A0429D6113507CF759C597BABB4ADB752F734CE3';
const actualEntryHash = Ledger.calcOfferEntryHash(account, sequence);
assert.equal(actualEntryHash, expectedEntryHash);
});
});
});
// vim:sw=2:sts=2:ts=8:et

View File

@@ -637,25 +637,8 @@ describe('Transaction', function() {
transaction.tx_json.Sequence = 1;
transaction.tx_json.TransactionType = 'AccountSet';
assert.strictEqual(transaction.hash('HASH_TX_SIGN'), 'D1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE');
assert.strictEqual(transaction.hash('HASH_TX_SIGN_TESTNET'), '9FE7D27FC5B9891076B66591F99A683E01E0912986A629235459A3BD1961F341');
done();
});
it('Get hash - invalid prefix', function(done) {
const transaction = new Transaction();
transaction._secret = 'sh2pTicynUEG46jjR4EoexHcQEoij';
transaction.tx_json.SigningPubKey = '021FED5FD081CE5C4356431267D04C6E2167E4112C897D5E10335D4E22B4DA49ED';
transaction.tx_json.Account = 'rMWwx3Ma16HnqSd4H6saPisihX9aKpXxHJ';
transaction.tx_json.Flags = 0;
transaction.tx_json.Fee = '10';
transaction.tx_json.Sequence = 1;
transaction.tx_json.TransactionType = 'AccountSet';
assert.throws(function() {
transaction.hash('HASH_TX_SIGNZ');
});
assert.strictEqual(transaction.signingHash(),
'D1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE');
done();
});
@@ -2258,10 +2241,11 @@ describe('Transaction', function() {
const txHex = binary.encode(
lodash.merge(transaction.tx_json, {SigningPubKey: ''}));
const abytes = decodeAddress(a1);
const prefix = require('ripple-lib')._test.HashPrefixes.HASH_TX_MULTISIGN_BYTES;
const prefix = 0x534D5400;
const prefixHex = prefix.toString(16);
assert.deepEqual(new Buffer(d1, 'hex'),
Buffer.concat([new Buffer(prefix), new Buffer(txHex, 'hex'),
Buffer.concat([new Buffer(prefixHex, 'hex'), new Buffer(txHex, 'hex'),
new Buffer(abytes)]));
});