ripple-binary-codec 1.7.1 → 1.9.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/dist/enums/definitions.json +250 -1
- package/dist/enums/src/enums/definitions.json +250 -1
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/issue.d.ts +39 -0
- package/dist/types/issue.js +81 -0
- package/dist/types/issue.js.map +1 -0
- package/dist/types/xchain-bridge.d.ts +45 -0
- package/dist/types/xchain-bridge.js +102 -0
- package/dist/types/xchain-bridge.js.map +1 -0
- package/package.json +3 -4
- package/src/README.md +3 -0
- package/src/binary.ts +188 -0
- package/src/coretypes.ts +31 -0
- package/src/enums/README.md +144 -0
- package/src/enums/bytes.ts +75 -0
- package/src/enums/constants.ts +4 -0
- package/src/enums/definitions.json +2599 -0
- package/src/enums/field.ts +85 -0
- package/src/enums/index.ts +34 -0
- package/src/enums/utils-renumber.ts +134 -0
- package/src/enums/xrpl-definitions-base.ts +111 -0
- package/src/enums/xrpl-definitions.ts +32 -0
- package/src/hash-prefixes.ts +40 -0
- package/src/hashes.ts +76 -0
- package/src/index.ts +141 -0
- package/src/ledger-hashes.ts +187 -0
- package/src/quality.ts +39 -0
- package/src/serdes/binary-parser.ts +217 -0
- package/src/serdes/binary-serializer.ts +166 -0
- package/src/shamap.ts +186 -0
- package/src/types/account-id.ts +86 -0
- package/src/types/amount.ts +256 -0
- package/src/types/blob.ts +43 -0
- package/src/types/currency.ts +140 -0
- package/src/types/hash-128.ts +33 -0
- package/src/types/hash-160.ts +20 -0
- package/src/types/hash-256.ts +16 -0
- package/src/types/hash.ts +81 -0
- package/src/types/index.ts +61 -0
- package/src/types/issue.ts +96 -0
- package/src/types/path-set.ts +290 -0
- package/src/types/serialized-type.ts +120 -0
- package/src/types/st-array.ts +107 -0
- package/src/types/st-object.ts +192 -0
- package/src/types/uint-16.ts +49 -0
- package/src/types/uint-32.ts +56 -0
- package/src/types/uint-64.ts +105 -0
- package/src/types/uint-8.ts +49 -0
- package/src/types/uint.ts +57 -0
- package/src/types/vector-256.ts +84 -0
- package/test/amount.test.js +0 -43
- package/test/binary-json.test.js +0 -45
- package/test/binary-parser.test.js +0 -396
- package/test/binary-serializer.test.js +0 -289
- package/test/definitions.test.js +0 -160
- package/test/fixtures/account-tx-transactions.db +0 -0
- package/test/fixtures/codec-fixtures.json +0 -4466
- package/test/fixtures/data-driven-tests.json +0 -2919
- package/test/fixtures/delivermin-tx-binary.json +0 -1
- package/test/fixtures/delivermin-tx.json +0 -98
- package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
- package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
- package/test/fixtures/deposit-preauth-tx.json +0 -58
- package/test/fixtures/escrow-cancel-binary.json +0 -1
- package/test/fixtures/escrow-cancel-tx.json +0 -6
- package/test/fixtures/escrow-create-binary.json +0 -1
- package/test/fixtures/escrow-create-tx.json +0 -10
- package/test/fixtures/escrow-finish-binary.json +0 -1
- package/test/fixtures/escrow-finish-meta-binary.json +0 -1
- package/test/fixtures/escrow-finish-tx.json +0 -95
- package/test/fixtures/ledger-full-38129.json +0 -1
- package/test/fixtures/ledger-full-40000.json +0 -1
- package/test/fixtures/negative-unl.json +0 -12
- package/test/fixtures/nf-token.json +0 -547
- package/test/fixtures/payment-channel-claim-binary.json +0 -1
- package/test/fixtures/payment-channel-claim-tx.json +0 -8
- package/test/fixtures/payment-channel-create-binary.json +0 -1
- package/test/fixtures/payment-channel-create-tx.json +0 -11
- package/test/fixtures/payment-channel-fund-binary.json +0 -1
- package/test/fixtures/payment-channel-fund-tx.json +0 -7
- package/test/fixtures/signerlistset-tx-binary.json +0 -1
- package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
- package/test/fixtures/signerlistset-tx.json +0 -94
- package/test/fixtures/ticket-create-binary.json +0 -1
- package/test/fixtures/ticket-create-tx.json +0 -7
- package/test/fixtures/x-codec-fixtures.json +0 -188
- package/test/hash.test.js +0 -135
- package/test/ledger.test.js +0 -29
- package/test/lower-case-hex.test.js +0 -46
- package/test/pseudo-transaction.test.js +0 -38
- package/test/quality.test.js +0 -15
- package/test/shamap.test.js +0 -89
- package/test/signing-data-encoding.test.js +0 -242
- package/test/tx-encode-decode.test.js +0 -119
- package/test/types.test.js +0 -34
- package/test/uint.test.js +0 -148
- package/test/utils.js +0 -30
- package/test/x-address.test.js +0 -181
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import bigInt = require('big-integer')
|
|
2
|
+
import { Comparable } from './serialized-type'
|
|
3
|
+
import { Buffer } from 'buffer/'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Compare numbers and bigInts n1 and n2
|
|
7
|
+
*
|
|
8
|
+
* @param n1 First object to compare
|
|
9
|
+
* @param n2 Second object to compare
|
|
10
|
+
* @returns -1, 0, or 1, depending on how the two objects compare
|
|
11
|
+
*/
|
|
12
|
+
function compare(
|
|
13
|
+
n1: number | bigInt.BigInteger,
|
|
14
|
+
n2: number | bigInt.BigInteger,
|
|
15
|
+
): number {
|
|
16
|
+
return n1 < n2 ? -1 : n1 == n2 ? 0 : 1
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Base class for serializing and deserializing unsigned integers.
|
|
21
|
+
*/
|
|
22
|
+
abstract class UInt extends Comparable {
|
|
23
|
+
protected static width: number
|
|
24
|
+
|
|
25
|
+
constructor(bytes: Buffer) {
|
|
26
|
+
super(bytes)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Overload of compareTo for Comparable
|
|
31
|
+
*
|
|
32
|
+
* @param other other UInt to compare this to
|
|
33
|
+
* @returns -1, 0, or 1 depending on how the objects relate to each other
|
|
34
|
+
*/
|
|
35
|
+
compareTo(other: UInt): number {
|
|
36
|
+
return compare(this.valueOf(), other.valueOf())
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Convert a UInt object to JSON
|
|
41
|
+
*
|
|
42
|
+
* @returns number or string represented by this.bytes
|
|
43
|
+
*/
|
|
44
|
+
toJSON(): number | string {
|
|
45
|
+
const val = this.valueOf()
|
|
46
|
+
return typeof val === 'number' ? val : val.toString()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get the value of the UInt represented by this.bytes
|
|
51
|
+
*
|
|
52
|
+
* @returns the value
|
|
53
|
+
*/
|
|
54
|
+
abstract valueOf(): number | bigInt.BigInteger
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { UInt }
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { SerializedType } from './serialized-type'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
import { Hash256 } from './hash-256'
|
|
4
|
+
import { BytesList } from '../serdes/binary-serializer'
|
|
5
|
+
import { Buffer } from 'buffer/'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* TypeGuard for Array<string>
|
|
9
|
+
*/
|
|
10
|
+
function isStrings(arg): arg is Array<string> {
|
|
11
|
+
return Array.isArray(arg) && (arg.length === 0 || typeof arg[0] === 'string')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Class for serializing and deserializing vectors of Hash256
|
|
16
|
+
*/
|
|
17
|
+
class Vector256 extends SerializedType {
|
|
18
|
+
constructor(bytes: Buffer) {
|
|
19
|
+
super(bytes)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Construct a Vector256 from a BinaryParser
|
|
24
|
+
*
|
|
25
|
+
* @param parser BinaryParser to
|
|
26
|
+
* @param hint length of the vector, in bytes, optional
|
|
27
|
+
* @returns a Vector256 object
|
|
28
|
+
*/
|
|
29
|
+
static fromParser(parser: BinaryParser, hint?: number): Vector256 {
|
|
30
|
+
const bytesList = new BytesList()
|
|
31
|
+
const bytes = hint ?? parser.size()
|
|
32
|
+
const hashes = bytes / 32
|
|
33
|
+
for (let i = 0; i < hashes; i++) {
|
|
34
|
+
Hash256.fromParser(parser).toBytesSink(bytesList)
|
|
35
|
+
}
|
|
36
|
+
return new Vector256(bytesList.toBytes())
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Construct a Vector256 object from an array of hashes
|
|
41
|
+
*
|
|
42
|
+
* @param value A Vector256 object or array of hex-strings representing Hash256's
|
|
43
|
+
* @returns a Vector256 object
|
|
44
|
+
*/
|
|
45
|
+
static from<T extends Vector256 | Array<string>>(value: T): Vector256 {
|
|
46
|
+
if (value instanceof Vector256) {
|
|
47
|
+
return value
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (isStrings(value)) {
|
|
51
|
+
const bytesList = new BytesList()
|
|
52
|
+
value.forEach((hash) => {
|
|
53
|
+
Hash256.from(hash).toBytesSink(bytesList)
|
|
54
|
+
})
|
|
55
|
+
return new Vector256(bytesList.toBytes())
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
throw new Error('Cannot construct Vector256 from given value')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Return an Array of hex-strings represented by this.bytes
|
|
63
|
+
*
|
|
64
|
+
* @returns An Array of strings representing the Hash256 objects
|
|
65
|
+
*/
|
|
66
|
+
toJSON(): Array<string> {
|
|
67
|
+
if (this.bytes.byteLength % 32 !== 0) {
|
|
68
|
+
throw new Error('Invalid bytes for Vector256')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const result: Array<string> = []
|
|
72
|
+
for (let i = 0; i < this.bytes.byteLength; i += 32) {
|
|
73
|
+
result.push(
|
|
74
|
+
this.bytes
|
|
75
|
+
.slice(i, i + 32)
|
|
76
|
+
.toString('hex')
|
|
77
|
+
.toUpperCase(),
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
return result
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export { Vector256 }
|
package/test/amount.test.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
const { loadFixture } = require('./utils')
|
|
2
|
-
const { coreTypes } = require('../src/types')
|
|
3
|
-
const { Amount } = coreTypes
|
|
4
|
-
const fixtures = loadFixture('data-driven-tests.json')
|
|
5
|
-
|
|
6
|
-
function amountErrorTests() {
|
|
7
|
-
fixtures.values_tests
|
|
8
|
-
.filter((obj) => obj.type === 'Amount')
|
|
9
|
-
.forEach((f) => {
|
|
10
|
-
// We only want these with errors
|
|
11
|
-
if (!f.error) {
|
|
12
|
-
return
|
|
13
|
-
}
|
|
14
|
-
const testName =
|
|
15
|
-
`${JSON.stringify(f.test_json)}\n\tis invalid ` + `because: ${f.error}`
|
|
16
|
-
it(testName, () => {
|
|
17
|
-
expect(() => {
|
|
18
|
-
Amount.from(f.test_json)
|
|
19
|
-
JSON.stringify(f.test_json)
|
|
20
|
-
}).toThrow()
|
|
21
|
-
})
|
|
22
|
-
})
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
describe('Amount', function () {
|
|
26
|
-
it('can be parsed from', function () {
|
|
27
|
-
expect(Amount.from('1000000') instanceof Amount).toBe(true)
|
|
28
|
-
expect(Amount.from('1000000').toJSON()).toEqual('1000000')
|
|
29
|
-
const fixture = {
|
|
30
|
-
value: '1',
|
|
31
|
-
issuer: '0000000000000000000000000000000000000000',
|
|
32
|
-
currency: 'USD',
|
|
33
|
-
}
|
|
34
|
-
const amt = Amount.from(fixture)
|
|
35
|
-
const rewritten = {
|
|
36
|
-
value: '1',
|
|
37
|
-
issuer: 'rrrrrrrrrrrrrrrrrrrrrhoLvTp',
|
|
38
|
-
currency: 'USD',
|
|
39
|
-
}
|
|
40
|
-
expect(amt.toJSON()).toEqual(rewritten)
|
|
41
|
-
})
|
|
42
|
-
amountErrorTests()
|
|
43
|
-
})
|
package/test/binary-json.test.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const fixtures = require('./fixtures/codec-fixtures.json')
|
|
2
|
-
const { decode, encode, decodeLedgerData } = require('../src')
|
|
3
|
-
|
|
4
|
-
function json(object) {
|
|
5
|
-
return JSON.stringify(object)
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
function truncateForDisplay(longStr) {
|
|
9
|
-
return `${longStr.slice(0, 10)} ... ${longStr.slice(-10)}`
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
describe('ripple-binary-codec', function () {
|
|
13
|
-
function makeSuite(name, entries) {
|
|
14
|
-
describe(name, function () {
|
|
15
|
-
entries.forEach((t, testN) => {
|
|
16
|
-
test(`${name}[${testN}] can encode ${truncateForDisplay(
|
|
17
|
-
json(t.json),
|
|
18
|
-
)} to ${truncateForDisplay(t.binary)}`, () => {
|
|
19
|
-
expect(encode(t.json)).toEqual(t.binary)
|
|
20
|
-
})
|
|
21
|
-
test(`${name}[${testN}] can decode ${truncateForDisplay(
|
|
22
|
-
t.binary,
|
|
23
|
-
)} to ${truncateForDisplay(json(t.json))}`, () => {
|
|
24
|
-
const decoded = decode(t.binary)
|
|
25
|
-
expect(decoded).toEqual(t.json)
|
|
26
|
-
})
|
|
27
|
-
})
|
|
28
|
-
})
|
|
29
|
-
}
|
|
30
|
-
makeSuite('transactions', fixtures.transactions)
|
|
31
|
-
makeSuite('accountState', fixtures.accountState)
|
|
32
|
-
|
|
33
|
-
describe('ledgerData', function () {
|
|
34
|
-
if (fixtures.ledgerData) {
|
|
35
|
-
fixtures.ledgerData.forEach((t, testN) => {
|
|
36
|
-
test(`ledgerData[${testN}] can decode ${t.binary} to ${json(
|
|
37
|
-
t.json,
|
|
38
|
-
)}`, () => {
|
|
39
|
-
const decoded = decodeLedgerData(t.binary)
|
|
40
|
-
expect(t.json).toEqual(decoded)
|
|
41
|
-
})
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
})
|
|
@@ -1,396 +0,0 @@
|
|
|
1
|
-
const { coreTypes } = require('../src/types')
|
|
2
|
-
const Decimal = require('decimal.js')
|
|
3
|
-
|
|
4
|
-
const { encodeAccountID } = require('ripple-address-codec')
|
|
5
|
-
const { binary } = require('../src/coretypes')
|
|
6
|
-
const { Amount, Hash160 } = coreTypes
|
|
7
|
-
const { makeParser, readJSON } = binary
|
|
8
|
-
const { Field, TransactionType } = require('./../src/enums')
|
|
9
|
-
const { parseHexOnly, hexOnly, loadFixture } = require('./utils')
|
|
10
|
-
const fixtures = loadFixture('data-driven-tests.json')
|
|
11
|
-
const { BytesList } = require('../src/serdes/binary-serializer')
|
|
12
|
-
const { Buffer } = require('buffer/')
|
|
13
|
-
|
|
14
|
-
const __ = hexOnly
|
|
15
|
-
function toJSON(v) {
|
|
16
|
-
return v.toJSON ? v.toJSON() : v
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function assertEqualAmountJSON(actual, expected) {
|
|
20
|
-
expect(typeof actual === typeof expected).toBe(true)
|
|
21
|
-
if (typeof actual === 'string') {
|
|
22
|
-
expect(actual).toEqual(expected)
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
expect(actual.currency).toEqual(expected.currency)
|
|
26
|
-
expect(actual.issuer).toEqual(expected.issuer)
|
|
27
|
-
expect(
|
|
28
|
-
actual.value === expected.value ||
|
|
29
|
-
new Decimal(actual.value).equals(new Decimal(expected.value)),
|
|
30
|
-
).toBe(true)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function basicApiTests() {
|
|
34
|
-
const bytes = parseHexOnly('00,01020304,0506', Uint8Array)
|
|
35
|
-
test('can read slices of bytes', () => {
|
|
36
|
-
const parser = makeParser(bytes)
|
|
37
|
-
expect(parser.bytes instanceof Buffer).toBe(true)
|
|
38
|
-
const read1 = parser.read(1)
|
|
39
|
-
expect(read1 instanceof Buffer).toBe(true)
|
|
40
|
-
expect(read1).toEqual(Buffer.from([0]))
|
|
41
|
-
expect(parser.read(4)).toEqual(Buffer.from([1, 2, 3, 4]))
|
|
42
|
-
expect(parser.read(2)).toEqual(Buffer.from([5, 6]))
|
|
43
|
-
expect(() => parser.read(1)).toThrow()
|
|
44
|
-
})
|
|
45
|
-
test('can read a Uint32 at full', () => {
|
|
46
|
-
const parser = makeParser('FFFFFFFF')
|
|
47
|
-
expect(parser.readUInt32()).toEqual(0xffffffff)
|
|
48
|
-
})
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function transactionParsingTests() {
|
|
52
|
-
const transaction = {
|
|
53
|
-
json: {
|
|
54
|
-
Account: 'raD5qJMAShLeHZXf9wjUmo6vRK4arj9cF3',
|
|
55
|
-
Fee: '10',
|
|
56
|
-
Flags: 0,
|
|
57
|
-
Sequence: 103929,
|
|
58
|
-
SigningPubKey:
|
|
59
|
-
'028472865AF4CB32AA285834B57576B7290AA8C31B459047DB27E16F418D6A7166',
|
|
60
|
-
TakerGets: {
|
|
61
|
-
currency: 'ILS',
|
|
62
|
-
issuer: 'rNPRNzBB92BVpAhhZr4iXDTveCgV5Pofm9',
|
|
63
|
-
value: '1694.768',
|
|
64
|
-
},
|
|
65
|
-
TakerPays: '98957503520',
|
|
66
|
-
TransactionType: 'OfferCreate',
|
|
67
|
-
TxnSignature: __(`
|
|
68
|
-
304502202ABE08D5E78D1E74A4C18F2714F64E87B8BD57444AF
|
|
69
|
-
A5733109EB3C077077520022100DB335EE97386E4C0591CAC02
|
|
70
|
-
4D50E9230D8F171EEB901B5E5E4BD6D1E0AEF98C`),
|
|
71
|
-
},
|
|
72
|
-
binary: __(`
|
|
73
|
-
120007220000000024000195F964400000170A53AC2065D5460561E
|
|
74
|
-
C9DE000000000000000000000000000494C53000000000092D70596
|
|
75
|
-
8936C419CE614BF264B5EEB1CEA47FF468400000000000000A73210
|
|
76
|
-
28472865AF4CB32AA285834B57576B7290AA8C31B459047DB27E16F
|
|
77
|
-
418D6A71667447304502202ABE08D5E78D1E74A4C18F2714F64E87B
|
|
78
|
-
8BD57444AFA5733109EB3C077077520022100DB335EE97386E4C059
|
|
79
|
-
1CAC024D50E9230D8F171EEB901B5E5E4BD6D1E0AEF98C811439408
|
|
80
|
-
A69F0895E62149CFCC006FB89FA7D1E6E5D`),
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const tx_json = transaction.json
|
|
84
|
-
// These tests are basically development logs
|
|
85
|
-
|
|
86
|
-
test('can be done with low level apis', () => {
|
|
87
|
-
const parser = makeParser(transaction.binary)
|
|
88
|
-
|
|
89
|
-
expect(parser.readField()).toEqual(Field.TransactionType)
|
|
90
|
-
expect(parser.readUInt16()).toEqual(7)
|
|
91
|
-
expect(parser.readField()).toEqual(Field.Flags)
|
|
92
|
-
expect(parser.readUInt32()).toEqual(0)
|
|
93
|
-
expect(parser.readField()).toEqual(Field.Sequence)
|
|
94
|
-
expect(parser.readUInt32()).toEqual(103929)
|
|
95
|
-
expect(parser.readField()).toEqual(Field.TakerPays)
|
|
96
|
-
parser.read(8)
|
|
97
|
-
expect(parser.readField()).toEqual(Field.TakerGets)
|
|
98
|
-
// amount value
|
|
99
|
-
expect(parser.read(8)).not.toBe([])
|
|
100
|
-
// amount currency
|
|
101
|
-
expect(Hash160.fromParser(parser)).not.toBe([])
|
|
102
|
-
expect(encodeAccountID(parser.read(20))).toEqual(tx_json.TakerGets.issuer)
|
|
103
|
-
expect(parser.readField()).toEqual(Field.Fee)
|
|
104
|
-
expect(parser.read(8)).not.toEqual([])
|
|
105
|
-
expect(parser.readField()).toEqual(Field.SigningPubKey)
|
|
106
|
-
expect(parser.readVariableLengthLength()).toBe(33)
|
|
107
|
-
expect(parser.read(33).toString('hex').toUpperCase()).toEqual(
|
|
108
|
-
tx_json.SigningPubKey,
|
|
109
|
-
)
|
|
110
|
-
expect(parser.readField()).toEqual(Field.TxnSignature)
|
|
111
|
-
expect(parser.readVariableLength().toString('hex').toUpperCase()).toEqual(
|
|
112
|
-
tx_json.TxnSignature,
|
|
113
|
-
)
|
|
114
|
-
expect(parser.readField()).toEqual(Field.Account)
|
|
115
|
-
expect(encodeAccountID(parser.readVariableLength())).toEqual(
|
|
116
|
-
tx_json.Account,
|
|
117
|
-
)
|
|
118
|
-
expect(parser.end()).toBe(true)
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
test('can be done with high level apis', () => {
|
|
122
|
-
const parser = makeParser(transaction.binary)
|
|
123
|
-
function readField() {
|
|
124
|
-
return parser.readFieldAndValue()
|
|
125
|
-
}
|
|
126
|
-
{
|
|
127
|
-
const [field, value] = readField()
|
|
128
|
-
expect(field).toEqual(Field.TransactionType)
|
|
129
|
-
expect(value).toEqual(TransactionType.OfferCreate)
|
|
130
|
-
}
|
|
131
|
-
{
|
|
132
|
-
const [field, value] = readField()
|
|
133
|
-
expect(field).toEqual(Field.Flags)
|
|
134
|
-
expect(value.valueOf()).toEqual(0)
|
|
135
|
-
}
|
|
136
|
-
{
|
|
137
|
-
const [field, value] = readField()
|
|
138
|
-
expect(field).toEqual(Field.Sequence)
|
|
139
|
-
expect(value.valueOf()).toEqual(103929)
|
|
140
|
-
}
|
|
141
|
-
{
|
|
142
|
-
const [field, value] = readField()
|
|
143
|
-
expect(field).toEqual(Field.TakerPays)
|
|
144
|
-
expect(value.isNative()).toEqual(true)
|
|
145
|
-
expect(value.toJSON()).toEqual('98957503520')
|
|
146
|
-
}
|
|
147
|
-
{
|
|
148
|
-
const [field, value] = readField()
|
|
149
|
-
expect(field).toEqual(Field.TakerGets)
|
|
150
|
-
expect(value.isNative()).toEqual(false)
|
|
151
|
-
expect(value.toJSON().issuer).toEqual(tx_json.TakerGets.issuer)
|
|
152
|
-
}
|
|
153
|
-
{
|
|
154
|
-
const [field, value] = readField()
|
|
155
|
-
expect(field).toEqual(Field.Fee)
|
|
156
|
-
expect(value.isNative()).toEqual(true)
|
|
157
|
-
}
|
|
158
|
-
{
|
|
159
|
-
const [field, value] = readField()
|
|
160
|
-
expect(field).toEqual(Field.SigningPubKey)
|
|
161
|
-
expect(value.toJSON()).toEqual(tx_json.SigningPubKey)
|
|
162
|
-
}
|
|
163
|
-
{
|
|
164
|
-
const [field, value] = readField()
|
|
165
|
-
expect(field).toEqual(Field.TxnSignature)
|
|
166
|
-
expect(value.toJSON()).toEqual(tx_json.TxnSignature)
|
|
167
|
-
}
|
|
168
|
-
{
|
|
169
|
-
const [field, value] = readField()
|
|
170
|
-
expect(field).toEqual(Field.Account)
|
|
171
|
-
expect(value.toJSON()).toEqual(tx_json.Account)
|
|
172
|
-
}
|
|
173
|
-
expect(parser.end()).toBe(true)
|
|
174
|
-
})
|
|
175
|
-
|
|
176
|
-
test('can be done with higher level apis', () => {
|
|
177
|
-
const parser = makeParser(transaction.binary)
|
|
178
|
-
const jsonFromBinary = readJSON(parser)
|
|
179
|
-
expect(jsonFromBinary).toEqual(tx_json)
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
test('readJSON (binary.decode) does not return STObject ', () => {
|
|
183
|
-
const parser = makeParser(transaction.binary)
|
|
184
|
-
const jsonFromBinary = readJSON(parser)
|
|
185
|
-
expect(jsonFromBinary instanceof coreTypes.STObject).toBe(false)
|
|
186
|
-
expect(jsonFromBinary instanceof Object).toBe(true)
|
|
187
|
-
expect(jsonFromBinary.prototype).toBe(undefined)
|
|
188
|
-
})
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function amountParsingTests() {
|
|
192
|
-
fixtures.values_tests
|
|
193
|
-
.filter((obj) => obj.type === 'Amount')
|
|
194
|
-
.forEach((f, i) => {
|
|
195
|
-
if (f.error) {
|
|
196
|
-
return
|
|
197
|
-
}
|
|
198
|
-
const parser = makeParser(f.expected_hex)
|
|
199
|
-
const testName = `values_tests[${i}] parses ${f.expected_hex.slice(
|
|
200
|
-
0,
|
|
201
|
-
16,
|
|
202
|
-
)}...
|
|
203
|
-
as ${JSON.stringify(f.test_json)}`
|
|
204
|
-
test(testName, () => {
|
|
205
|
-
const value = parser.readType(Amount)
|
|
206
|
-
// May not actually be in canonical form. The fixtures are to be used
|
|
207
|
-
// also for json -> binary;
|
|
208
|
-
const json = toJSON(value)
|
|
209
|
-
assertEqualAmountJSON(json, f.test_json)
|
|
210
|
-
if (f.exponent) {
|
|
211
|
-
const exponent = new Decimal(json.value)
|
|
212
|
-
expect(exponent.e - 15).toEqual(f.exponent)
|
|
213
|
-
}
|
|
214
|
-
})
|
|
215
|
-
})
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function fieldParsingTests() {
|
|
219
|
-
fixtures.fields_tests.forEach((f, i) => {
|
|
220
|
-
const parser = makeParser(f.expected_hex)
|
|
221
|
-
test(`fields[${i}]: parses ${f.expected_hex} as ${f.name}`, () => {
|
|
222
|
-
const field = parser.readField()
|
|
223
|
-
expect(field.name).toEqual(f.name)
|
|
224
|
-
expect(field.type.name).toEqual(f.type_name)
|
|
225
|
-
})
|
|
226
|
-
})
|
|
227
|
-
test('Field throws when type code out of range', () => {
|
|
228
|
-
const parser = makeParser('0101')
|
|
229
|
-
expect(() => parser.readField()).toThrow(
|
|
230
|
-
new Error('Cannot read FieldOrdinal, type_code out of range'),
|
|
231
|
-
)
|
|
232
|
-
})
|
|
233
|
-
test('Field throws when field code out of range', () => {
|
|
234
|
-
const parser = makeParser('1001')
|
|
235
|
-
expect(() => parser.readFieldOrdinal()).toThrowError(
|
|
236
|
-
new Error('Cannot read FieldOrdinal, field_code out of range'),
|
|
237
|
-
)
|
|
238
|
-
})
|
|
239
|
-
test('Field throws when both type and field code out of range', () => {
|
|
240
|
-
const parser = makeParser('000101')
|
|
241
|
-
expect(() => parser.readFieldOrdinal()).toThrowError(
|
|
242
|
-
new Error('Cannot read FieldOrdinal, type_code out of range'),
|
|
243
|
-
)
|
|
244
|
-
})
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
function assertRecyclable(json, forField) {
|
|
248
|
-
const Type = forField.associatedType
|
|
249
|
-
const recycled = Type.from(json).toJSON()
|
|
250
|
-
expect(recycled).toEqual(json)
|
|
251
|
-
const sink = new BytesList()
|
|
252
|
-
Type.from(recycled).toBytesSink(sink)
|
|
253
|
-
const recycledAgain = makeParser(sink.toHex()).readType(Type).toJSON()
|
|
254
|
-
expect(recycledAgain).toEqual(json)
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function nestedObjectTests() {
|
|
258
|
-
fixtures.whole_objects.forEach((f, i) => {
|
|
259
|
-
test(`whole_objects[${i}]: can parse blob into
|
|
260
|
-
${JSON.stringify(
|
|
261
|
-
f.tx_json,
|
|
262
|
-
)}`, /* */ () => {
|
|
263
|
-
const parser = makeParser(f.blob_with_no_signing)
|
|
264
|
-
let ix = 0
|
|
265
|
-
while (!parser.end()) {
|
|
266
|
-
const [field, value] = parser.readFieldAndValue()
|
|
267
|
-
const expected = f.fields[ix]
|
|
268
|
-
const expectedJSON = expected[1].json
|
|
269
|
-
const expectedField = expected[0]
|
|
270
|
-
const actual = toJSON(value)
|
|
271
|
-
|
|
272
|
-
try {
|
|
273
|
-
expect(actual).toEqual(expectedJSON)
|
|
274
|
-
} catch (e) {
|
|
275
|
-
throw new Error(`${e} ${field} a: ${actual} e: ${expectedJSON}`)
|
|
276
|
-
}
|
|
277
|
-
expect(field.name).toEqual(expectedField)
|
|
278
|
-
assertRecyclable(actual, field)
|
|
279
|
-
ix++
|
|
280
|
-
}
|
|
281
|
-
})
|
|
282
|
-
})
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
function pathSetBinaryTests() {
|
|
286
|
-
const bytes = __(
|
|
287
|
-
`1200002200000000240000002E2E00004BF161D4C71AFD498D00000000000000
|
|
288
|
-
0000000000000055534400000000000A20B3C85F482532A9578DBB3950B85CA0
|
|
289
|
-
6594D168400000000000000A69D446F8038585E9400000000000000000000000
|
|
290
|
-
00425443000000000078CA21A6014541AB7B26C3929B9E0CD8C284D61C732103
|
|
291
|
-
A4665B1F0B7AE2BCA12E2DB80A192125BBEA660F80E9CEE137BA444C1B0769EC
|
|
292
|
-
7447304502205A964536805E35785C659D1F9670D057749AE39668175D6AA75D
|
|
293
|
-
25B218FE682E0221009252C0E5DDD5F2712A48F211669DE17B54113918E0D2C2
|
|
294
|
-
66F818095E9339D7D3811478CA21A6014541AB7B26C3929B9E0CD8C284D61C83
|
|
295
|
-
140A20B3C85F482532A9578DBB3950B85CA06594D1011231585E1F3BD02A15D6
|
|
296
|
-
185F8BB9B57CC60DEDDB37C10000000000000000000000004254430000000000
|
|
297
|
-
585E1F3BD02A15D6185F8BB9B57CC60DEDDB37C131E4FE687C90257D3D2D694C
|
|
298
|
-
8531CDEECBE84F33670000000000000000000000004254430000000000E4FE68
|
|
299
|
-
7C90257D3D2D694C8531CDEECBE84F3367310A20B3C85F482532A9578DBB3950
|
|
300
|
-
B85CA06594D100000000000000000000000042544300000000000A20B3C85F48
|
|
301
|
-
2532A9578DBB3950B85CA06594D1300000000000000000000000005553440000
|
|
302
|
-
0000000A20B3C85F482532A9578DBB3950B85CA06594D1FF31585E1F3BD02A15
|
|
303
|
-
D6185F8BB9B57CC60DEDDB37C100000000000000000000000042544300000000
|
|
304
|
-
00585E1F3BD02A15D6185F8BB9B57CC60DEDDB37C131E4FE687C90257D3D2D69
|
|
305
|
-
4C8531CDEECBE84F33670000000000000000000000004254430000000000E4FE
|
|
306
|
-
687C90257D3D2D694C8531CDEECBE84F33673115036E2D3F5437A83E5AC3CAEE
|
|
307
|
-
34FF2C21DEB618000000000000000000000000425443000000000015036E2D3F
|
|
308
|
-
5437A83E5AC3CAEE34FF2C21DEB6183000000000000000000000000055534400
|
|
309
|
-
000000000A20B3C85F482532A9578DBB3950B85CA06594D1FF31585E1F3BD02A
|
|
310
|
-
15D6185F8BB9B57CC60DEDDB37C1000000000000000000000000425443000000
|
|
311
|
-
0000585E1F3BD02A15D6185F8BB9B57CC60DEDDB37C13157180C769B66D942EE
|
|
312
|
-
69E6DCC940CA48D82337AD000000000000000000000000425443000000000057
|
|
313
|
-
180C769B66D942EE69E6DCC940CA48D82337AD10000000000000000000000000
|
|
314
|
-
00000000000000003000000000000000000000000055534400000000000A20B3
|
|
315
|
-
C85F482532A9578DBB3950B85CA06594D100`,
|
|
316
|
-
)
|
|
317
|
-
|
|
318
|
-
const expectedJSON = [
|
|
319
|
-
[
|
|
320
|
-
{
|
|
321
|
-
account: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
|
|
322
|
-
currency: 'BTC',
|
|
323
|
-
issuer: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
|
|
324
|
-
},
|
|
325
|
-
{
|
|
326
|
-
account: 'rM1oqKtfh1zgjdAgbFmaRm3btfGBX25xVo',
|
|
327
|
-
currency: 'BTC',
|
|
328
|
-
issuer: 'rM1oqKtfh1zgjdAgbFmaRm3btfGBX25xVo',
|
|
329
|
-
},
|
|
330
|
-
{
|
|
331
|
-
account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
|
|
332
|
-
currency: 'BTC',
|
|
333
|
-
issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
|
|
334
|
-
},
|
|
335
|
-
{
|
|
336
|
-
currency: 'USD',
|
|
337
|
-
issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
|
|
338
|
-
},
|
|
339
|
-
],
|
|
340
|
-
[
|
|
341
|
-
{
|
|
342
|
-
account: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
|
|
343
|
-
currency: 'BTC',
|
|
344
|
-
issuer: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
|
|
345
|
-
},
|
|
346
|
-
{
|
|
347
|
-
account: 'rM1oqKtfh1zgjdAgbFmaRm3btfGBX25xVo',
|
|
348
|
-
currency: 'BTC',
|
|
349
|
-
issuer: 'rM1oqKtfh1zgjdAgbFmaRm3btfGBX25xVo',
|
|
350
|
-
},
|
|
351
|
-
{
|
|
352
|
-
account: 'rpvfJ4mR6QQAeogpXEKnuyGBx8mYCSnYZi',
|
|
353
|
-
currency: 'BTC',
|
|
354
|
-
issuer: 'rpvfJ4mR6QQAeogpXEKnuyGBx8mYCSnYZi',
|
|
355
|
-
},
|
|
356
|
-
{
|
|
357
|
-
currency: 'USD',
|
|
358
|
-
issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
|
|
359
|
-
},
|
|
360
|
-
],
|
|
361
|
-
[
|
|
362
|
-
{
|
|
363
|
-
account: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
|
|
364
|
-
currency: 'BTC',
|
|
365
|
-
issuer: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
|
|
366
|
-
},
|
|
367
|
-
{
|
|
368
|
-
account: 'r3AWbdp2jQLXLywJypdoNwVSvr81xs3uhn',
|
|
369
|
-
currency: 'BTC',
|
|
370
|
-
issuer: 'r3AWbdp2jQLXLywJypdoNwVSvr81xs3uhn',
|
|
371
|
-
},
|
|
372
|
-
{ currency: 'XRP' },
|
|
373
|
-
{
|
|
374
|
-
currency: 'USD',
|
|
375
|
-
issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
|
|
376
|
-
},
|
|
377
|
-
],
|
|
378
|
-
]
|
|
379
|
-
|
|
380
|
-
test('works with long paths', () => {
|
|
381
|
-
const parser = makeParser(bytes)
|
|
382
|
-
const txn = readJSON(parser)
|
|
383
|
-
expect(txn.Paths).toEqual(expectedJSON)
|
|
384
|
-
// TODO: this should go elsewhere
|
|
385
|
-
expect(coreTypes.PathSet.from(txn.Paths).toJSON()).toEqual(expectedJSON)
|
|
386
|
-
})
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
describe('Binary Parser', function () {
|
|
390
|
-
describe('pathSetBinaryTests', () => pathSetBinaryTests())
|
|
391
|
-
describe('nestedObjectTests', () => nestedObjectTests())
|
|
392
|
-
describe('fieldParsingTests', () => fieldParsingTests())
|
|
393
|
-
describe('amountParsingTests', () => amountParsingTests())
|
|
394
|
-
describe('transactionParsingTests', () => transactionParsingTests())
|
|
395
|
-
describe('basicApiTests', () => basicApiTests())
|
|
396
|
-
})
|