ripple-binary-codec 1.8.0 → 1.10.0-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/dist/enums/definitions.json +489 -1
- package/dist/enums/src/enums/definitions.json +489 -1
- package/dist/types/index.js +4 -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 +2839 -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 +63 -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/src/types/xchain-bridge.ts +128 -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 }
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
2
|
+
|
|
3
|
+
import { AccountID } from './account-id'
|
|
4
|
+
import { JsonObject, SerializedType } from './serialized-type'
|
|
5
|
+
import { Buffer } from 'buffer/'
|
|
6
|
+
import { Issue, IssueObject } from './issue'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Interface for JSON objects that represent cross-chain bridges
|
|
10
|
+
*/
|
|
11
|
+
interface XChainBridgeObject extends JsonObject {
|
|
12
|
+
LockingChainDoor: string
|
|
13
|
+
LockingChainIssue: IssueObject | string
|
|
14
|
+
IssuingChainDoor: string
|
|
15
|
+
IssuingChainIssue: IssueObject | string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Type guard for XChainBridgeObject
|
|
20
|
+
*/
|
|
21
|
+
function isXChainBridgeObject(arg): arg is XChainBridgeObject {
|
|
22
|
+
const keys = Object.keys(arg).sort()
|
|
23
|
+
return (
|
|
24
|
+
keys.length === 4 &&
|
|
25
|
+
keys[0] === 'IssuingChainDoor' &&
|
|
26
|
+
keys[1] === 'IssuingChainIssue' &&
|
|
27
|
+
keys[2] === 'LockingChainDoor' &&
|
|
28
|
+
keys[3] === 'LockingChainIssue'
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Class for serializing/deserializing XChainBridges
|
|
34
|
+
*/
|
|
35
|
+
class XChainBridge extends SerializedType {
|
|
36
|
+
static readonly ZERO_XCHAIN_BRIDGE: XChainBridge = new XChainBridge(
|
|
37
|
+
Buffer.concat([
|
|
38
|
+
Buffer.from([0x14]),
|
|
39
|
+
Buffer.alloc(40),
|
|
40
|
+
Buffer.from([0x14]),
|
|
41
|
+
Buffer.alloc(40),
|
|
42
|
+
]),
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
static readonly TYPE_ORDER: { name: string; type: typeof SerializedType }[] =
|
|
46
|
+
[
|
|
47
|
+
{ name: 'LockingChainDoor', type: AccountID },
|
|
48
|
+
{ name: 'LockingChainIssue', type: Issue },
|
|
49
|
+
{ name: 'IssuingChainDoor', type: AccountID },
|
|
50
|
+
{ name: 'IssuingChainIssue', type: Issue },
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
constructor(bytes: Buffer) {
|
|
54
|
+
super(bytes ?? XChainBridge.ZERO_XCHAIN_BRIDGE.bytes)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Construct a cross-chain bridge from a JSON
|
|
59
|
+
*
|
|
60
|
+
* @param value XChainBridge or JSON to parse into a XChainBridge
|
|
61
|
+
* @returns A XChainBridge object
|
|
62
|
+
*/
|
|
63
|
+
static from<T extends XChainBridge | XChainBridgeObject>(
|
|
64
|
+
value: T,
|
|
65
|
+
): XChainBridge {
|
|
66
|
+
if (value instanceof XChainBridge) {
|
|
67
|
+
return value
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (isXChainBridgeObject(value)) {
|
|
71
|
+
const bytes: Array<Buffer> = []
|
|
72
|
+
this.TYPE_ORDER.forEach((item) => {
|
|
73
|
+
const { name, type } = item
|
|
74
|
+
if (type === AccountID) {
|
|
75
|
+
bytes.push(Buffer.from([0x14]))
|
|
76
|
+
}
|
|
77
|
+
const object = type.from(value[name])
|
|
78
|
+
bytes.push(object.toBytes())
|
|
79
|
+
})
|
|
80
|
+
return new XChainBridge(Buffer.concat(bytes))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
throw new Error('Invalid type to construct a XChainBridge')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Read a XChainBridge from a BinaryParser
|
|
88
|
+
*
|
|
89
|
+
* @param parser BinaryParser to read the XChainBridge from
|
|
90
|
+
* @returns A XChainBridge object
|
|
91
|
+
*/
|
|
92
|
+
static fromParser(parser: BinaryParser): XChainBridge {
|
|
93
|
+
const bytes: Array<Buffer> = []
|
|
94
|
+
|
|
95
|
+
this.TYPE_ORDER.forEach((item) => {
|
|
96
|
+
const { type } = item
|
|
97
|
+
if (type === AccountID) {
|
|
98
|
+
parser.skip(1)
|
|
99
|
+
bytes.push(Buffer.from([0x14]))
|
|
100
|
+
}
|
|
101
|
+
const object = type.fromParser(parser)
|
|
102
|
+
bytes.push(object.toBytes())
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
return new XChainBridge(Buffer.concat(bytes))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get the JSON representation of this XChainBridge
|
|
110
|
+
*
|
|
111
|
+
* @returns the JSON interpretation of this.bytes
|
|
112
|
+
*/
|
|
113
|
+
toJSON(): XChainBridgeObject {
|
|
114
|
+
const parser = new BinaryParser(this.toString())
|
|
115
|
+
const json = {}
|
|
116
|
+
XChainBridge.TYPE_ORDER.forEach((item) => {
|
|
117
|
+
const { name, type } = item
|
|
118
|
+
if (type === AccountID) {
|
|
119
|
+
parser.skip(1)
|
|
120
|
+
}
|
|
121
|
+
const object = type.fromParser(parser).toJSON()
|
|
122
|
+
json[name] = object
|
|
123
|
+
})
|
|
124
|
+
return json as XChainBridgeObject
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { XChainBridge, XChainBridgeObject }
|
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
|
-
})
|