ripple-binary-codec 1.1.3-beta.1 → 1.2.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/binary.d.ts +6 -6
- package/dist/binary.js.map +1 -1
- package/dist/coretypes.d.ts +8 -8
- package/dist/coretypes.js.map +1 -1
- package/dist/enums/definitions.json +190 -4
- package/dist/enums/index.d.ts +6 -5
- package/dist/enums/index.js +3 -2
- package/dist/enums/index.js.map +1 -1
- package/dist/enums/utils-renumber.js +7 -7
- package/dist/enums/utils-renumber.js.map +1 -1
- package/dist/hash-prefixes.d.ts +1 -1
- package/dist/hash-prefixes.js.map +1 -1
- package/dist/hashes.d.ts +3 -3
- package/dist/hashes.js +6 -4
- package/dist/hashes.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +13 -13
- package/dist/index.js.map +1 -1
- package/dist/ledger-hashes.d.ts +3 -3
- package/dist/ledger-hashes.js +5 -5
- package/dist/ledger-hashes.js.map +1 -1
- package/dist/quality.d.ts +2 -2
- package/dist/quality.js +2 -2
- package/dist/quality.js.map +1 -1
- package/dist/serdes/binary-parser.d.ts +3 -3
- package/dist/serdes/binary-parser.js +8 -8
- package/dist/serdes/binary-parser.js.map +1 -1
- package/dist/serdes/binary-serializer.d.ts +5 -5
- package/dist/serdes/binary-serializer.js +11 -8
- package/dist/serdes/binary-serializer.js.map +1 -1
- package/dist/shamap.d.ts +3 -3
- package/dist/shamap.js +5 -3
- package/dist/shamap.js.map +1 -1
- package/dist/types/account-id.d.ts +2 -2
- package/dist/types/account-id.js +12 -10
- package/dist/types/account-id.js.map +1 -1
- package/dist/types/amount.d.ts +3 -3
- package/dist/types/amount.js +18 -16
- package/dist/types/amount.js.map +1 -1
- package/dist/types/blob.d.ts +3 -3
- package/dist/types/blob.js +6 -4
- package/dist/types/blob.js.map +1 -1
- package/dist/types/currency.d.ts +5 -12
- package/dist/types/currency.js +31 -30
- package/dist/types/currency.js.map +1 -1
- package/dist/types/hash-128.d.ts +2 -2
- package/dist/types/hash-128.js +3 -1
- package/dist/types/hash-128.js.map +1 -1
- package/dist/types/hash-160.d.ts +2 -2
- package/dist/types/hash-160.js +3 -1
- package/dist/types/hash-160.js.map +1 -1
- package/dist/types/hash-256.d.ts +2 -2
- package/dist/types/hash-256.js +3 -1
- package/dist/types/hash-256.js.map +1 -1
- package/dist/types/hash.d.ts +3 -3
- package/dist/types/hash.js +6 -4
- package/dist/types/hash.js.map +1 -1
- package/dist/types/index.d.ts +15 -15
- package/dist/types/index.js +3 -3
- package/dist/types/index.js.map +1 -1
- package/dist/types/path-set.d.ts +2 -2
- package/dist/types/path-set.js +5 -3
- package/dist/types/path-set.js.map +1 -1
- package/dist/types/serialized-type.d.ts +4 -4
- package/dist/types/serialized-type.js +6 -4
- package/dist/types/serialized-type.js.map +1 -1
- package/dist/types/st-array.d.ts +2 -2
- package/dist/types/st-array.js +6 -4
- package/dist/types/st-array.js.map +1 -1
- package/dist/types/st-object.d.ts +2 -2
- package/dist/types/st-object.js +20 -13
- package/dist/types/st-object.js.map +1 -1
- package/dist/types/uint-16.d.ts +3 -3
- package/dist/types/uint-16.js +5 -3
- package/dist/types/uint-16.js.map +1 -1
- package/dist/types/uint-32.d.ts +3 -3
- package/dist/types/uint-32.js +6 -4
- package/dist/types/uint-32.js.map +1 -1
- package/dist/types/uint-64.d.ts +4 -4
- package/dist/types/uint-64.js +11 -9
- package/dist/types/uint-64.js.map +1 -1
- package/dist/types/uint-8.d.ts +3 -3
- package/dist/types/uint-8.js +5 -3
- package/dist/types/uint-8.js.map +1 -1
- package/dist/types/uint.d.ts +3 -3
- package/dist/types/uint.js +4 -2
- package/dist/types/uint.js.map +1 -1
- package/dist/types/vector-256.d.ts +3 -3
- package/dist/types/vector-256.js +7 -5
- package/dist/types/vector-256.js.map +1 -1
- package/package.json +13 -27
- package/test/amount.test.js +29 -29
- package/test/binary-json.test.js +24 -26
- package/test/binary-parser.test.js +226 -228
- package/test/binary-serializer.test.js +194 -169
- package/test/fixtures/data-driven-tests.json +2 -790
- package/test/fixtures/negative-unl.json +4 -4
- package/test/fixtures/nf-token.json +547 -0
- package/test/hash.test.js +79 -75
- package/test/ledger.test.js +19 -19
- package/test/lower-case-hex.test.js +29 -28
- package/test/pseudo-transaction.test.js +26 -26
- package/test/quality.test.js +13 -13
- package/test/shamap.test.js +54 -54
- package/test/signing-data-encoding.test.js +79 -79
- package/test/tx-encode-decode.test.js +63 -63
- package/test/types.test.js +23 -23
- package/test/uint.test.js +107 -107
- package/test/x-address.test.js +106 -106
- package/HISTORY.md +0 -86
package/test/hash.test.js
CHANGED
|
@@ -1,83 +1,87 @@
|
|
|
1
|
-
const { coreTypes } = require(
|
|
2
|
-
const { Hash160, Hash256, AccountID, Currency } = coreTypes
|
|
3
|
-
const { Buffer } = require(
|
|
1
|
+
const { coreTypes } = require('../dist/types')
|
|
2
|
+
const { Hash160, Hash256, AccountID, Currency } = coreTypes
|
|
3
|
+
const { Buffer } = require('buffer/')
|
|
4
4
|
|
|
5
|
-
describe(
|
|
6
|
-
test(
|
|
7
|
-
expect(Hash160.width).toBe(20)
|
|
8
|
-
})
|
|
9
|
-
test(
|
|
10
|
-
expect(AccountID.width).toBe(20)
|
|
11
|
-
expect(Currency.width).toBe(20)
|
|
12
|
-
})
|
|
13
|
-
test(
|
|
14
|
-
const h1 = Hash160.from(
|
|
15
|
-
const h2 = Hash160.from(
|
|
16
|
-
const h3 = Hash160.from(
|
|
17
|
-
expect(h1.lt(h2)).toBe(true)
|
|
18
|
-
expect(h3.lt(h2)).toBe(true)
|
|
19
|
-
})
|
|
20
|
-
test(
|
|
5
|
+
describe('Hash160', function () {
|
|
6
|
+
test('has a static width member', function () {
|
|
7
|
+
expect(Hash160.width).toBe(20)
|
|
8
|
+
})
|
|
9
|
+
test('inherited by subclasses', function () {
|
|
10
|
+
expect(AccountID.width).toBe(20)
|
|
11
|
+
expect(Currency.width).toBe(20)
|
|
12
|
+
})
|
|
13
|
+
test('can be compared against another', function () {
|
|
14
|
+
const h1 = Hash160.from('1000000000000000000000000000000000000000')
|
|
15
|
+
const h2 = Hash160.from('2000000000000000000000000000000000000000')
|
|
16
|
+
const h3 = Hash160.from('0000000000000000000000000000000000000003')
|
|
17
|
+
expect(h1.lt(h2)).toBe(true)
|
|
18
|
+
expect(h3.lt(h2)).toBe(true)
|
|
19
|
+
})
|
|
20
|
+
test('throws when constructed from invalid hash length', () => {
|
|
21
21
|
expect(() =>
|
|
22
|
-
Hash160.from(
|
|
23
|
-
).toThrow(
|
|
22
|
+
Hash160.from('10000000000000000000000000000000000000'),
|
|
23
|
+
).toThrow('Invalid Hash length 19')
|
|
24
24
|
expect(() =>
|
|
25
|
-
Hash160.from(
|
|
26
|
-
).toThrow(
|
|
27
|
-
})
|
|
28
|
-
})
|
|
25
|
+
Hash160.from('100000000000000000000000000000000000000000'),
|
|
26
|
+
).toThrow('Invalid Hash length 21')
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
29
|
|
|
30
|
-
describe(
|
|
31
|
-
test(
|
|
32
|
-
expect(Hash256.width).toBe(32)
|
|
33
|
-
})
|
|
34
|
-
test(
|
|
30
|
+
describe('Hash256', function () {
|
|
31
|
+
test('has a static width member', function () {
|
|
32
|
+
expect(Hash256.width).toBe(32)
|
|
33
|
+
})
|
|
34
|
+
test('has a ZERO_256 member', function () {
|
|
35
35
|
expect(Hash256.ZERO_256.toJSON()).toBe(
|
|
36
|
-
|
|
37
|
-
)
|
|
38
|
-
})
|
|
39
|
-
test(
|
|
36
|
+
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
test('supports getting the nibblet values at given positions', function () {
|
|
40
40
|
const h = Hash256.from(
|
|
41
|
-
|
|
42
|
-
)
|
|
43
|
-
expect(h.nibblet(0)).toBe(0x1)
|
|
44
|
-
expect(h.nibblet(1)).toBe(0x3)
|
|
45
|
-
expect(h.nibblet(2)).toBe(0x5)
|
|
46
|
-
expect(h.nibblet(3)).toBe(0x9)
|
|
47
|
-
expect(h.nibblet(4)).toBe(0x0b)
|
|
48
|
-
expect(h.nibblet(5)).toBe(0xd)
|
|
49
|
-
})
|
|
50
|
-
})
|
|
41
|
+
'1359BD0000000000000000000000000000000000000000000000000000000000',
|
|
42
|
+
)
|
|
43
|
+
expect(h.nibblet(0)).toBe(0x1)
|
|
44
|
+
expect(h.nibblet(1)).toBe(0x3)
|
|
45
|
+
expect(h.nibblet(2)).toBe(0x5)
|
|
46
|
+
expect(h.nibblet(3)).toBe(0x9)
|
|
47
|
+
expect(h.nibblet(4)).toBe(0x0b)
|
|
48
|
+
expect(h.nibblet(5)).toBe(0xd)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
51
|
|
|
52
|
-
describe(
|
|
53
|
-
test(
|
|
54
|
-
const bad = Currency.from("0000000000000000000000005852500000000000");
|
|
55
|
-
expect(bad.iso()).toBeUndefined();
|
|
56
|
-
expect(bad.isNative()).toBe(false);
|
|
57
|
-
});
|
|
58
|
-
test("Currency with lowercase letters decode to hex", () => {
|
|
59
|
-
expect(Currency.from("xRp").toJSON()).toBe(
|
|
60
|
-
"0000000000000000000000007852700000000000"
|
|
61
|
-
);
|
|
62
|
-
});
|
|
63
|
-
test("Currency codes with symbols decode to hex", () => {
|
|
64
|
-
expect(Currency.from("x|p").toJSON()).toBe(
|
|
65
|
-
"000000000000000000000000787C700000000000"
|
|
66
|
-
);
|
|
67
|
-
});
|
|
68
|
-
test("Currency codes with uppercase and 0-9 decode to ISO codes", () => {
|
|
69
|
-
expect(Currency.from("X8P").toJSON()).toBe("X8P");
|
|
70
|
-
expect(Currency.from("USD").toJSON()).toBe("USD");
|
|
71
|
-
});
|
|
72
|
-
test("can be constructed from a Buffer", function () {
|
|
73
|
-
const xrp = new Currency(Buffer.alloc(20));
|
|
74
|
-
expect(xrp.iso()).toBe("XRP");
|
|
75
|
-
});
|
|
76
|
-
test("throws on invalid reprs", function () {
|
|
77
|
-
expect(() => Currency.from(Buffer.alloc(19))).toThrow();
|
|
78
|
-
expect(() => Currency.from(1)).toThrow();
|
|
52
|
+
describe('Currency', function () {
|
|
53
|
+
test('Will throw an error for dodgy XRP ', function () {
|
|
79
54
|
expect(() =>
|
|
80
|
-
Currency.from(
|
|
81
|
-
).toThrow()
|
|
82
|
-
})
|
|
83
|
-
|
|
55
|
+
Currency.from('0000000000000000000000005852500000000000'),
|
|
56
|
+
).toThrow()
|
|
57
|
+
})
|
|
58
|
+
test('Currency with lowercase letters decode to hex', () => {
|
|
59
|
+
expect(Currency.from('xRp').toJSON()).toBe(
|
|
60
|
+
'0000000000000000000000007852700000000000',
|
|
61
|
+
)
|
|
62
|
+
})
|
|
63
|
+
test('Currency codes with symbols decode to hex', () => {
|
|
64
|
+
expect(Currency.from('x|p').toJSON()).toBe(
|
|
65
|
+
'000000000000000000000000787C700000000000',
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
test('Currency codes with uppercase and 0-9 decode to ISO codes', () => {
|
|
69
|
+
expect(Currency.from('X8P').toJSON()).toBe('X8P')
|
|
70
|
+
expect(Currency.from('USD').toJSON()).toBe('USD')
|
|
71
|
+
})
|
|
72
|
+
test('can be constructed from a Buffer', function () {
|
|
73
|
+
const xrp = new Currency(Buffer.alloc(20))
|
|
74
|
+
expect(xrp.iso()).toBe('XRP')
|
|
75
|
+
})
|
|
76
|
+
test('Can handle non-standard currency codes', () => {
|
|
77
|
+
const currency = '015841551A748AD2C1F76FF6ECB0CCCD00000000'
|
|
78
|
+
expect(Currency.from(currency).toJSON()).toBe(currency)
|
|
79
|
+
})
|
|
80
|
+
test('throws on invalid reprs', function () {
|
|
81
|
+
expect(() => Currency.from(Buffer.alloc(19))).toThrow()
|
|
82
|
+
expect(() => Currency.from(1)).toThrow()
|
|
83
|
+
expect(() =>
|
|
84
|
+
Currency.from('00000000000000000000000000000000000000m'),
|
|
85
|
+
).toThrow()
|
|
86
|
+
})
|
|
87
|
+
})
|
package/test/ledger.test.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
const { loadFixture } = require(
|
|
1
|
+
const { loadFixture } = require('./utils')
|
|
2
2
|
const {
|
|
3
3
|
transactionTreeHash,
|
|
4
4
|
ledgerHash,
|
|
5
5
|
accountStateHash,
|
|
6
|
-
} = require(
|
|
6
|
+
} = require('../dist/ledger-hashes')
|
|
7
7
|
|
|
8
|
-
describe(
|
|
8
|
+
describe('Ledger Hashes', function () {
|
|
9
9
|
function testFactory(ledgerFixture) {
|
|
10
10
|
describe(`can calculate hashes for ${ledgerFixture}`, function () {
|
|
11
|
-
const ledger = loadFixture(ledgerFixture)
|
|
12
|
-
test(
|
|
11
|
+
const ledger = loadFixture(ledgerFixture)
|
|
12
|
+
test('computes correct account state hash', function () {
|
|
13
13
|
expect(accountStateHash(ledger.accountState).toHex()).toBe(
|
|
14
|
-
ledger.account_hash
|
|
15
|
-
)
|
|
16
|
-
})
|
|
17
|
-
test(
|
|
14
|
+
ledger.account_hash,
|
|
15
|
+
)
|
|
16
|
+
})
|
|
17
|
+
test('computes correct transaction tree hash', function () {
|
|
18
18
|
expect(transactionTreeHash(ledger.transactions).toHex()).toBe(
|
|
19
|
-
ledger.transaction_hash
|
|
20
|
-
)
|
|
21
|
-
})
|
|
22
|
-
test(
|
|
23
|
-
expect(ledgerHash(ledger).toHex()).toBe(ledger.hash)
|
|
24
|
-
})
|
|
25
|
-
})
|
|
19
|
+
ledger.transaction_hash,
|
|
20
|
+
)
|
|
21
|
+
})
|
|
22
|
+
test('computes correct ledger header hash', function () {
|
|
23
|
+
expect(ledgerHash(ledger).toHex()).toBe(ledger.hash)
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
26
|
}
|
|
27
|
-
testFactory(
|
|
28
|
-
testFactory(
|
|
29
|
-
})
|
|
27
|
+
testFactory('ledger-full-40000.json')
|
|
28
|
+
testFactory('ledger-full-38129.json')
|
|
29
|
+
})
|
|
@@ -1,45 +1,46 @@
|
|
|
1
|
-
const { encode, decode } = require(
|
|
1
|
+
const { encode, decode } = require('../dist')
|
|
2
2
|
|
|
3
3
|
let str =
|
|
4
|
-
|
|
5
|
-
let lower = str.toLowerCase()
|
|
4
|
+
'1100612200000000240000000125000068652D0000000055B6632D6376A2D9319F20A1C6DCCB486432D1E4A79951229D4C3DE2946F51D56662400009184E72A00081140DD319918CD5AE792BF7EC80D63B0F01B4573BBC'
|
|
5
|
+
let lower = str.toLowerCase()
|
|
6
6
|
|
|
7
7
|
let bin =
|
|
8
|
-
|
|
8
|
+
'1100612200000000240000000125000000082D00000000550735A0B32B2A3F4C938B76D6933003E29447DB8C7CE382BBE089402FF12A03E56240000002540BE400811479927BAFFD3D04A26096C0C97B1B0D45B01AD3C0'
|
|
9
9
|
let json = {
|
|
10
10
|
OwnerCount: 0,
|
|
11
|
-
Account:
|
|
11
|
+
Account: 'rUnFEsHjxqTswbivzL2DNHBb34rhAgZZZK',
|
|
12
12
|
PreviousTxnLgrSeq: 8,
|
|
13
|
-
LedgerEntryType:
|
|
14
|
-
PreviousTxnID:
|
|
13
|
+
LedgerEntryType: 'AccountRoot',
|
|
14
|
+
PreviousTxnID:
|
|
15
|
+
'0735A0B32B2A3F4C938B76D6933003E29447DB8C7CE382BBE089402FF12A03E5'.toLowerCase(),
|
|
15
16
|
Flags: 0,
|
|
16
17
|
Sequence: 1,
|
|
17
|
-
Balance:
|
|
18
|
-
}
|
|
18
|
+
Balance: '10000000000',
|
|
19
|
+
}
|
|
19
20
|
|
|
20
21
|
let jsonUpper = {
|
|
21
22
|
OwnerCount: 0,
|
|
22
|
-
Account:
|
|
23
|
+
Account: 'rUnFEsHjxqTswbivzL2DNHBb34rhAgZZZK',
|
|
23
24
|
PreviousTxnLgrSeq: 8,
|
|
24
|
-
LedgerEntryType:
|
|
25
|
+
LedgerEntryType: 'AccountRoot',
|
|
25
26
|
PreviousTxnID:
|
|
26
|
-
|
|
27
|
+
'0735A0B32B2A3F4C938B76D6933003E29447DB8C7CE382BBE089402FF12A03E5',
|
|
27
28
|
Flags: 0,
|
|
28
29
|
Sequence: 1,
|
|
29
|
-
Balance:
|
|
30
|
-
}
|
|
30
|
+
Balance: '10000000000',
|
|
31
|
+
}
|
|
31
32
|
|
|
32
|
-
describe(
|
|
33
|
-
test(
|
|
34
|
-
expect(decode(lower)).toEqual(decode(str))
|
|
35
|
-
})
|
|
36
|
-
test(
|
|
37
|
-
expect(encode(decode(lower))).toEqual(str)
|
|
38
|
-
})
|
|
39
|
-
test(
|
|
40
|
-
expect(encode(json)).toBe(bin)
|
|
41
|
-
})
|
|
42
|
-
test(
|
|
43
|
-
expect(decode(encode(json))).toEqual(jsonUpper)
|
|
44
|
-
})
|
|
45
|
-
})
|
|
33
|
+
describe('Lowercase hex test', () => {
|
|
34
|
+
test('Correctly decodes', () => {
|
|
35
|
+
expect(decode(lower)).toEqual(decode(str))
|
|
36
|
+
})
|
|
37
|
+
test('Re-encodes to uppercase hex', () => {
|
|
38
|
+
expect(encode(decode(lower))).toEqual(str)
|
|
39
|
+
})
|
|
40
|
+
test('Encode when hex field lowercase', () => {
|
|
41
|
+
expect(encode(json)).toBe(bin)
|
|
42
|
+
})
|
|
43
|
+
test('Re-decodes to uppercase hex', () => {
|
|
44
|
+
expect(decode(encode(json))).toEqual(jsonUpper)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
const { encode, decode } = require(
|
|
1
|
+
const { encode, decode } = require('../dist')
|
|
2
2
|
|
|
3
3
|
let json = {
|
|
4
|
-
Account:
|
|
4
|
+
Account: 'rrrrrrrrrrrrrrrrrrrrrhoLvTp',
|
|
5
5
|
Sequence: 0,
|
|
6
|
-
Fee:
|
|
7
|
-
SigningPubKey:
|
|
8
|
-
Signature:
|
|
9
|
-
}
|
|
6
|
+
Fee: '0',
|
|
7
|
+
SigningPubKey: '',
|
|
8
|
+
Signature: '',
|
|
9
|
+
}
|
|
10
10
|
|
|
11
11
|
let json_blank_acct = {
|
|
12
|
-
Account:
|
|
12
|
+
Account: '',
|
|
13
13
|
Sequence: 0,
|
|
14
|
-
Fee:
|
|
15
|
-
SigningPubKey:
|
|
16
|
-
Signature:
|
|
17
|
-
}
|
|
14
|
+
Fee: '0',
|
|
15
|
+
SigningPubKey: '',
|
|
16
|
+
Signature: '',
|
|
17
|
+
}
|
|
18
18
|
|
|
19
19
|
let binary =
|
|
20
|
-
|
|
20
|
+
'24000000006840000000000000007300760081140000000000000000000000000000000000000000'
|
|
21
21
|
|
|
22
|
-
describe(
|
|
23
|
-
test(
|
|
24
|
-
expect(encode(json)).toEqual(binary)
|
|
25
|
-
})
|
|
22
|
+
describe('Can encode Pseudo Transactions', () => {
|
|
23
|
+
test('Correctly encodes Pseudo Transaciton', () => {
|
|
24
|
+
expect(encode(json)).toEqual(binary)
|
|
25
|
+
})
|
|
26
26
|
|
|
27
|
-
test(
|
|
28
|
-
expect(decode(encode(json))).toEqual(json)
|
|
29
|
-
})
|
|
27
|
+
test('Can decode account objects', () => {
|
|
28
|
+
expect(decode(encode(json))).toEqual(json)
|
|
29
|
+
})
|
|
30
30
|
|
|
31
|
-
test(
|
|
32
|
-
expect(encode(json_blank_acct)).toEqual(binary)
|
|
33
|
-
})
|
|
31
|
+
test('Blank AccountID is ACCOUNT_ZERO', () => {
|
|
32
|
+
expect(encode(json_blank_acct)).toEqual(binary)
|
|
33
|
+
})
|
|
34
34
|
|
|
35
|
-
test(
|
|
36
|
-
expect(decode(encode(json_blank_acct))).toEqual(json)
|
|
37
|
-
})
|
|
38
|
-
})
|
|
35
|
+
test('Decodes Blank AccountID', () => {
|
|
36
|
+
expect(decode(encode(json_blank_acct))).toEqual(json)
|
|
37
|
+
})
|
|
38
|
+
})
|
package/test/quality.test.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
const { quality } = require(
|
|
1
|
+
const { quality } = require('../dist/coretypes')
|
|
2
2
|
|
|
3
|
-
describe(
|
|
3
|
+
describe('Quality encode/decode', function () {
|
|
4
4
|
const bookDirectory =
|
|
5
|
-
|
|
6
|
-
const expectedQuality =
|
|
7
|
-
test(
|
|
8
|
-
const decimal = quality.decode(bookDirectory)
|
|
9
|
-
expect(decimal.toString()).toBe(expectedQuality)
|
|
10
|
-
})
|
|
11
|
-
test(
|
|
12
|
-
const bytes = quality.encode(expectedQuality)
|
|
13
|
-
expect(bytes.toString(
|
|
14
|
-
})
|
|
15
|
-
})
|
|
5
|
+
'4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5D06F4C3362FE1D0'
|
|
6
|
+
const expectedQuality = '195796912.5171664'
|
|
7
|
+
test('can decode', function () {
|
|
8
|
+
const decimal = quality.decode(bookDirectory)
|
|
9
|
+
expect(decimal.toString()).toBe(expectedQuality)
|
|
10
|
+
})
|
|
11
|
+
test('can encode', function () {
|
|
12
|
+
const bytes = quality.encode(expectedQuality)
|
|
13
|
+
expect(bytes.toString('hex').toUpperCase()).toBe(bookDirectory.slice(-16))
|
|
14
|
+
})
|
|
15
|
+
})
|
package/test/shamap.test.js
CHANGED
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
const { ShaMap } = require(
|
|
2
|
-
const { binary, HashPrefix } = require(
|
|
3
|
-
const { coreTypes } = require(
|
|
4
|
-
const { loadFixture } = require(
|
|
5
|
-
const { Buffer } = require(
|
|
1
|
+
const { ShaMap } = require('../dist/shamap.js')
|
|
2
|
+
const { binary, HashPrefix } = require('../dist/coretypes')
|
|
3
|
+
const { coreTypes } = require('../dist/types')
|
|
4
|
+
const { loadFixture } = require('./utils')
|
|
5
|
+
const { Buffer } = require('buffer/')
|
|
6
6
|
|
|
7
7
|
function now() {
|
|
8
|
-
return Number(Date.now()) / 1000
|
|
8
|
+
return Number(Date.now()) / 1000
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
const ZERO =
|
|
11
|
+
const ZERO = '0000000000000000000000000000000000000000000000000000000000000000'
|
|
12
12
|
|
|
13
13
|
function makeItem(indexArg) {
|
|
14
|
-
let str = indexArg
|
|
14
|
+
let str = indexArg
|
|
15
15
|
while (str.length < 64) {
|
|
16
|
-
str +=
|
|
16
|
+
str += '0'
|
|
17
17
|
}
|
|
18
|
-
const index = coreTypes.Hash256.from(str)
|
|
18
|
+
const index = coreTypes.Hash256.from(str)
|
|
19
19
|
const item = {
|
|
20
20
|
toBytesSink(sink) {
|
|
21
|
-
index.toBytesSink(sink)
|
|
21
|
+
index.toBytesSink(sink)
|
|
22
22
|
},
|
|
23
23
|
hashPrefix() {
|
|
24
|
-
return Buffer.from([1, 3, 3, 7])
|
|
24
|
+
return Buffer.from([1, 3, 3, 7])
|
|
25
25
|
},
|
|
26
|
-
}
|
|
27
|
-
return [index, item]
|
|
26
|
+
}
|
|
27
|
+
return [index, item]
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
describe(
|
|
31
|
-
now()
|
|
30
|
+
describe('ShaMap', () => {
|
|
31
|
+
now()
|
|
32
32
|
|
|
33
|
-
test(
|
|
34
|
-
const map = new ShaMap()
|
|
35
|
-
expect(map.hash().toHex()).toBe(ZERO)
|
|
36
|
-
})
|
|
37
|
-
test(
|
|
38
|
-
let map = new ShaMap()
|
|
33
|
+
test('hashes to zero when empty', () => {
|
|
34
|
+
const map = new ShaMap()
|
|
35
|
+
expect(map.hash().toHex()).toBe(ZERO)
|
|
36
|
+
})
|
|
37
|
+
test('creates the same hash no matter which order items are added', () => {
|
|
38
|
+
let map = new ShaMap()
|
|
39
39
|
const items = [
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
]
|
|
50
|
-
items.forEach((i) => map.addItem(...makeItem(i)))
|
|
51
|
-
const h1 = map.hash()
|
|
52
|
-
expect(h1.eq(h1)).toBe(true)
|
|
53
|
-
map = new ShaMap()
|
|
54
|
-
items.reverse().forEach((i) => map.addItem(...makeItem(i)))
|
|
55
|
-
expect(map.hash()).toStrictEqual(h1)
|
|
56
|
-
})
|
|
40
|
+
'0',
|
|
41
|
+
'1',
|
|
42
|
+
'11',
|
|
43
|
+
'7000DE445E22CB9BB7E1717589FA858736BAA5FD192310E20000000000000000',
|
|
44
|
+
'7000DE445E22CB9BB7E1717589FA858736BAA5FD192310E21000000000000000',
|
|
45
|
+
'7000DE445E22CB9BB7E1717589FA858736BAA5FD192310E22000000000000000',
|
|
46
|
+
'7000DE445E22CB9BB7E1717589FA858736BAA5FD192310E23000000000000000',
|
|
47
|
+
'12',
|
|
48
|
+
'122',
|
|
49
|
+
]
|
|
50
|
+
items.forEach((i) => map.addItem(...makeItem(i)))
|
|
51
|
+
const h1 = map.hash()
|
|
52
|
+
expect(h1.eq(h1)).toBe(true)
|
|
53
|
+
map = new ShaMap()
|
|
54
|
+
items.reverse().forEach((i) => map.addItem(...makeItem(i)))
|
|
55
|
+
expect(map.hash()).toStrictEqual(h1)
|
|
56
|
+
})
|
|
57
57
|
function factory(fixture) {
|
|
58
58
|
test(`recreate account state hash from ${fixture}`, () => {
|
|
59
|
-
const map = new ShaMap()
|
|
60
|
-
const ledger = loadFixture(fixture)
|
|
59
|
+
const map = new ShaMap()
|
|
60
|
+
const ledger = loadFixture(fixture)
|
|
61
61
|
// const t = now();
|
|
62
|
-
const leafNodePrefix = HashPrefix.accountStateEntry
|
|
62
|
+
const leafNodePrefix = HashPrefix.accountStateEntry
|
|
63
63
|
ledger.accountState
|
|
64
64
|
.map((e, i) => {
|
|
65
65
|
if ((i > 1000) & (i % 1000 === 0)) {
|
|
66
|
-
console.log(e.index)
|
|
67
|
-
console.log(i)
|
|
66
|
+
console.log(e.index)
|
|
67
|
+
console.log(i)
|
|
68
68
|
}
|
|
69
|
-
const bytes = binary.serializeObject(e)
|
|
69
|
+
const bytes = binary.serializeObject(e)
|
|
70
70
|
return {
|
|
71
71
|
index: coreTypes.Hash256.from(e.index),
|
|
72
72
|
hashPrefix() {
|
|
73
|
-
return leafNodePrefix
|
|
73
|
+
return leafNodePrefix
|
|
74
74
|
},
|
|
75
75
|
toBytesSink(sink) {
|
|
76
|
-
sink.put(bytes)
|
|
76
|
+
sink.put(bytes)
|
|
77
77
|
},
|
|
78
|
-
}
|
|
78
|
+
}
|
|
79
79
|
})
|
|
80
|
-
.forEach((so) => map.addItem(so.index, so))
|
|
81
|
-
expect(map.hash().toHex()).toBe(ledger.account_hash)
|
|
80
|
+
.forEach((so) => map.addItem(so.index, so))
|
|
81
|
+
expect(map.hash().toHex()).toBe(ledger.account_hash)
|
|
82
82
|
// console.log('took seconds: ', (now() - t));
|
|
83
|
-
})
|
|
83
|
+
})
|
|
84
84
|
}
|
|
85
|
-
factory(
|
|
86
|
-
factory(
|
|
85
|
+
factory('ledger-full-38129.json')
|
|
86
|
+
factory('ledger-full-40000.json')
|
|
87
87
|
// factory('ledger-4320277.json');
|
|
88
88
|
// factory('14280680.json');
|
|
89
|
-
})
|
|
89
|
+
})
|