diff --git a/packages/ripple-binary-codec/src/index.js b/packages/ripple-binary-codec/src/index.js index 3248e521..18cc5874 100644 --- a/packages/ripple-binary-codec/src/index.js +++ b/packages/ripple-binary-codec/src/index.js @@ -6,7 +6,24 @@ const {quality, signingClaimData, multiSigningData, binaryToJSON, - serializeObject}} = coreTypes; + serializeObject, + BinaryParser}} = coreTypes; + +function decodeLedgerData(binary) { + assert(typeof binary === 'string', 'binary must be a hex string'); + const parser = new BinaryParser(binary) + return { + ledger_index: parser.readUInt32(), + total_coins: parser.readType(coreTypes.UInt64).valueOf().toString(), + parent_hash: parser.readType(coreTypes.Hash256).toHex(), + transaction_hash: parser.readType(coreTypes.Hash256).toHex(), + account_hash: parser.readType(coreTypes.Hash256).toHex(), + parent_close_time: parser.readUInt32(), + close_time: parser.readUInt32(), + close_time_resolution: parser.readUInt8(), + close_flags: parser.readUInt8() + } +} function decode(binary) { assert(typeof binary === 'string', 'binary must be a hex string'); @@ -51,5 +68,6 @@ module.exports = { encodeForSigningClaim, encodeForMultisigning, encodeQuality, - decodeQuality + decodeQuality, + decodeLedgerData }; diff --git a/packages/ripple-binary-codec/test/binary-json-test.js b/packages/ripple-binary-codec/test/binary-json-test.js index 521795be..e05884dd 100644 --- a/packages/ripple-binary-codec/test/binary-json-test.js +++ b/packages/ripple-binary-codec/test/binary-json-test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const fixtures = require('./fixtures/codec-fixtures.json'); -const {decode, encode} = require('../src'); +const {decode, encode, decodeLedgerData} = require('../src'); function json(object) { return JSON.stringify(object); @@ -24,4 +24,14 @@ describe('ripple-binary-codec', function() { } makeSuite('transactions', fixtures.transactions); makeSuite('accountState', fixtures.accountState); + + describe('ledgerData', function() { + fixtures.ledgerData.forEach((t, test_n) => { + it(`ledgerData[${test_n}] can decode ${t.binary} to ${json(t.json)}`, + () => { + const decoded = decodeLedgerData(t.binary); + assert.deepEqual(t.json, decoded); + }); + }); + }) }); diff --git a/packages/ripple-binary-codec/test/fixtures/codec-fixtures.json b/packages/ripple-binary-codec/test/fixtures/codec-fixtures.json index d25b22b6..15aa20f1 100644 --- a/packages/ripple-binary-codec/test/fixtures/codec-fixtures.json +++ b/packages/ripple-binary-codec/test/fixtures/codec-fixtures.json @@ -4448,5 +4448,19 @@ "Flags": 0, "Sequence": 62 } + }], + "ledgerData": [{ + "binary": "01E91435016340767BF1C4A3EACEB081770D8ADE216C85445DD6FB002C6B5A2930F2DECE006DA18150CB18F6DD33F6F0990754C962A7CCE62F332FF9C13939B03B864117F0BDA86B6E9B4F873B5C3E520634D343EF5D9D9A4246643D64DAD278BA95DC0EAC6EB5350CF970D521276CDE21276CE60A00", + "json": { + "account_hash": "3B5C3E520634D343EF5D9D9A4246643D64DAD278BA95DC0EAC6EB5350CF970D5", + "close_flags": 0, + "close_time": 556231910, + "close_time_resolution": 10, + "ledger_index": 32052277, + "parent_close_time": 556231902, + "parent_hash": "EACEB081770D8ADE216C85445DD6FB002C6B5A2930F2DECE006DA18150CB18F6", + "total_coins": "99994494362043555", + "transaction_hash": "DD33F6F0990754C962A7CCE62F332FF9C13939B03B864117F0BDA86B6E9B4F87" + } }] } \ No newline at end of file