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.
Files changed (100) hide show
  1. package/dist/enums/definitions.json +489 -1
  2. package/dist/enums/src/enums/definitions.json +489 -1
  3. package/dist/types/index.js +4 -0
  4. package/dist/types/index.js.map +1 -1
  5. package/dist/types/issue.d.ts +39 -0
  6. package/dist/types/issue.js +81 -0
  7. package/dist/types/issue.js.map +1 -0
  8. package/dist/types/xchain-bridge.d.ts +45 -0
  9. package/dist/types/xchain-bridge.js +102 -0
  10. package/dist/types/xchain-bridge.js.map +1 -0
  11. package/package.json +3 -4
  12. package/src/README.md +3 -0
  13. package/src/binary.ts +188 -0
  14. package/src/coretypes.ts +31 -0
  15. package/src/enums/README.md +144 -0
  16. package/src/enums/bytes.ts +75 -0
  17. package/src/enums/constants.ts +4 -0
  18. package/src/enums/definitions.json +2839 -0
  19. package/src/enums/field.ts +85 -0
  20. package/src/enums/index.ts +34 -0
  21. package/src/enums/utils-renumber.ts +134 -0
  22. package/src/enums/xrpl-definitions-base.ts +111 -0
  23. package/src/enums/xrpl-definitions.ts +32 -0
  24. package/src/hash-prefixes.ts +40 -0
  25. package/src/hashes.ts +76 -0
  26. package/src/index.ts +141 -0
  27. package/src/ledger-hashes.ts +187 -0
  28. package/src/quality.ts +39 -0
  29. package/src/serdes/binary-parser.ts +217 -0
  30. package/src/serdes/binary-serializer.ts +166 -0
  31. package/src/shamap.ts +186 -0
  32. package/src/types/account-id.ts +86 -0
  33. package/src/types/amount.ts +256 -0
  34. package/src/types/blob.ts +43 -0
  35. package/src/types/currency.ts +140 -0
  36. package/src/types/hash-128.ts +33 -0
  37. package/src/types/hash-160.ts +20 -0
  38. package/src/types/hash-256.ts +16 -0
  39. package/src/types/hash.ts +81 -0
  40. package/src/types/index.ts +63 -0
  41. package/src/types/issue.ts +96 -0
  42. package/src/types/path-set.ts +290 -0
  43. package/src/types/serialized-type.ts +120 -0
  44. package/src/types/st-array.ts +107 -0
  45. package/src/types/st-object.ts +192 -0
  46. package/src/types/uint-16.ts +49 -0
  47. package/src/types/uint-32.ts +56 -0
  48. package/src/types/uint-64.ts +105 -0
  49. package/src/types/uint-8.ts +49 -0
  50. package/src/types/uint.ts +57 -0
  51. package/src/types/vector-256.ts +84 -0
  52. package/src/types/xchain-bridge.ts +128 -0
  53. package/test/amount.test.js +0 -43
  54. package/test/binary-json.test.js +0 -45
  55. package/test/binary-parser.test.js +0 -396
  56. package/test/binary-serializer.test.js +0 -289
  57. package/test/definitions.test.js +0 -160
  58. package/test/fixtures/account-tx-transactions.db +0 -0
  59. package/test/fixtures/codec-fixtures.json +0 -4466
  60. package/test/fixtures/data-driven-tests.json +0 -2919
  61. package/test/fixtures/delivermin-tx-binary.json +0 -1
  62. package/test/fixtures/delivermin-tx.json +0 -98
  63. package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
  64. package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
  65. package/test/fixtures/deposit-preauth-tx.json +0 -58
  66. package/test/fixtures/escrow-cancel-binary.json +0 -1
  67. package/test/fixtures/escrow-cancel-tx.json +0 -6
  68. package/test/fixtures/escrow-create-binary.json +0 -1
  69. package/test/fixtures/escrow-create-tx.json +0 -10
  70. package/test/fixtures/escrow-finish-binary.json +0 -1
  71. package/test/fixtures/escrow-finish-meta-binary.json +0 -1
  72. package/test/fixtures/escrow-finish-tx.json +0 -95
  73. package/test/fixtures/ledger-full-38129.json +0 -1
  74. package/test/fixtures/ledger-full-40000.json +0 -1
  75. package/test/fixtures/negative-unl.json +0 -12
  76. package/test/fixtures/nf-token.json +0 -547
  77. package/test/fixtures/payment-channel-claim-binary.json +0 -1
  78. package/test/fixtures/payment-channel-claim-tx.json +0 -8
  79. package/test/fixtures/payment-channel-create-binary.json +0 -1
  80. package/test/fixtures/payment-channel-create-tx.json +0 -11
  81. package/test/fixtures/payment-channel-fund-binary.json +0 -1
  82. package/test/fixtures/payment-channel-fund-tx.json +0 -7
  83. package/test/fixtures/signerlistset-tx-binary.json +0 -1
  84. package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
  85. package/test/fixtures/signerlistset-tx.json +0 -94
  86. package/test/fixtures/ticket-create-binary.json +0 -1
  87. package/test/fixtures/ticket-create-tx.json +0 -7
  88. package/test/fixtures/x-codec-fixtures.json +0 -188
  89. package/test/hash.test.js +0 -135
  90. package/test/ledger.test.js +0 -29
  91. package/test/lower-case-hex.test.js +0 -46
  92. package/test/pseudo-transaction.test.js +0 -38
  93. package/test/quality.test.js +0 -15
  94. package/test/shamap.test.js +0 -89
  95. package/test/signing-data-encoding.test.js +0 -242
  96. package/test/tx-encode-decode.test.js +0 -119
  97. package/test/types.test.js +0 -34
  98. package/test/uint.test.js +0 -148
  99. package/test/utils.js +0 -30
  100. 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 }
@@ -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
- })
@@ -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
- })