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.
Files changed (99) hide show
  1. package/dist/enums/definitions.json +250 -1
  2. package/dist/enums/src/enums/definitions.json +250 -1
  3. package/dist/types/index.js +2 -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 +2599 -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 +61 -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/test/amount.test.js +0 -43
  53. package/test/binary-json.test.js +0 -45
  54. package/test/binary-parser.test.js +0 -396
  55. package/test/binary-serializer.test.js +0 -289
  56. package/test/definitions.test.js +0 -160
  57. package/test/fixtures/account-tx-transactions.db +0 -0
  58. package/test/fixtures/codec-fixtures.json +0 -4466
  59. package/test/fixtures/data-driven-tests.json +0 -2919
  60. package/test/fixtures/delivermin-tx-binary.json +0 -1
  61. package/test/fixtures/delivermin-tx.json +0 -98
  62. package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
  63. package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
  64. package/test/fixtures/deposit-preauth-tx.json +0 -58
  65. package/test/fixtures/escrow-cancel-binary.json +0 -1
  66. package/test/fixtures/escrow-cancel-tx.json +0 -6
  67. package/test/fixtures/escrow-create-binary.json +0 -1
  68. package/test/fixtures/escrow-create-tx.json +0 -10
  69. package/test/fixtures/escrow-finish-binary.json +0 -1
  70. package/test/fixtures/escrow-finish-meta-binary.json +0 -1
  71. package/test/fixtures/escrow-finish-tx.json +0 -95
  72. package/test/fixtures/ledger-full-38129.json +0 -1
  73. package/test/fixtures/ledger-full-40000.json +0 -1
  74. package/test/fixtures/negative-unl.json +0 -12
  75. package/test/fixtures/nf-token.json +0 -547
  76. package/test/fixtures/payment-channel-claim-binary.json +0 -1
  77. package/test/fixtures/payment-channel-claim-tx.json +0 -8
  78. package/test/fixtures/payment-channel-create-binary.json +0 -1
  79. package/test/fixtures/payment-channel-create-tx.json +0 -11
  80. package/test/fixtures/payment-channel-fund-binary.json +0 -1
  81. package/test/fixtures/payment-channel-fund-tx.json +0 -7
  82. package/test/fixtures/signerlistset-tx-binary.json +0 -1
  83. package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
  84. package/test/fixtures/signerlistset-tx.json +0 -94
  85. package/test/fixtures/ticket-create-binary.json +0 -1
  86. package/test/fixtures/ticket-create-tx.json +0 -7
  87. package/test/fixtures/x-codec-fixtures.json +0 -188
  88. package/test/hash.test.js +0 -135
  89. package/test/ledger.test.js +0 -29
  90. package/test/lower-case-hex.test.js +0 -46
  91. package/test/pseudo-transaction.test.js +0 -38
  92. package/test/quality.test.js +0 -15
  93. package/test/shamap.test.js +0 -89
  94. package/test/signing-data-encoding.test.js +0 -242
  95. package/test/tx-encode-decode.test.js +0 -119
  96. package/test/types.test.js +0 -34
  97. package/test/uint.test.js +0 -148
  98. package/test/utils.js +0 -30
  99. package/test/x-address.test.js +0 -181
@@ -0,0 +1,144 @@
1
+ # Definitions
2
+
3
+ This file is used to serialize/deserialize transactions and ledger objects for the XRPL. It's broken into 5 sections laid out below.
4
+
5
+ At the bottom of this README you can find instructions and examples for how to define your own types in a definitions file in order to work on a custom sidechain or develop new amendments.
6
+
7
+ ## Types
8
+
9
+ These are the [types](https://xrpl.org/serialization.html#type-list) associated with a given Serialization Field. Each type has an arbitrary [type_code](https://xrpl.org/serialization.html#type-codes), with lower codes sorting first.
10
+
11
+ ## Ledger Entry Types
12
+
13
+ Each ledger's state tree contain [ledger objects](https://xrpl.org/ledger-object-types.html), which represent all settings, balances, and relationships in the shared ledger.
14
+
15
+ ## Fields
16
+
17
+ These are Serialization Fields (`sf`) [defined in rippled's SField.cpp](https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/impl/SField.cpp). Fields with undefined values are omitted before encoding.
18
+
19
+ ### Key
20
+
21
+ The key is the string defined in the rippled source code, such as "LedgerEntry", "Transaction", etc.
22
+
23
+ ### nth
24
+
25
+ `nth` is the sort code, meaning "nth of type." It is is combined with the type code in order to construct the Field ID of this field. The Field ID is only used for sorting the fields. Since there are multiple fields with the same data type, the `nth` is used to deterministically order each field among other fields of the same data type.
26
+
27
+ Each field has a Field ID, which is used to sort fields that have the same type as one another with lower codes sorting first.
28
+
29
+ - [Field definitions](https://github.com/ripple/rippled/blob/72e6005f562a8f0818bc94803d222ac9345e1e40/src/ripple/protocol/impl/SField.cpp#L72-L266)
30
+ - [Constructing the `SField` field codes](https://github.com/ripple/rippled/blob/eaff9a0e6aec0ad077f118501791c7684debcfd5/src/ripple/protocol/SField.h#L95-L98)
31
+
32
+ For example, the `Account` field has sort code (nth) `1`, so it comes before the `Destination` field which has sort code `3`.
33
+
34
+ Sort code numbers are reused for fields of different types, but different fields of the same type never have the same sort code. When you combine the type code with the sort code, you get the field's unique _Field ID_.
35
+
36
+ The unique [Field ID](https://xrpl.org/serialization.html#field-ids) is prefixed before the field in the final serialized blob. The size of the Field ID is one to three bytes depending on the type code and the field codes it combines.
37
+
38
+ ### isVLEncoded
39
+
40
+ If true, the field is Variable Length encoded and [length-prefixed](https://xrpl.org/serialization.html#length-prefixing). The variable-length encoded fields are `STI_VL`/`Blob`, `STI_ACCOUNT`/`AccountID`, and `STI_VECTOR256`/`Vector256`.
41
+
42
+ ### isSerialized
43
+
44
+ Fields are serialized if they are not [one of these](https://github.com/ripple/rippled/blob/eaff9a0e6aec0ad077f118501791c7684debcfd5/src/ripple/protocol/impl/SField.cpp#L71-L78) or if they are not an SField.
45
+
46
+ - https://github.com/ripple/ripple-binary-codec/blob/14e76e68ead7e4bcd83c942dbdc9064d5a66869b/src/enums/definitions.json#L832
47
+ - https://github.com/ripple/rippled/search?utf8=%E2%9C%93&q=taker_gets_funded&type=
48
+
49
+ ### isSigningField
50
+
51
+ True unless the field is [specified with `SField::notSigning`](https://github.com/ripple/rippled/blob/eaff9a0e6aec0ad077f118501791c7684debcfd5/src/ripple/protocol/impl/SField.cpp#L198).
52
+
53
+ ## Transaction Results
54
+
55
+ See:
56
+
57
+ - https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/TER.h
58
+ - https://xrpl.org/transaction-results.html
59
+
60
+ To generate a new definitions file from rippled source code, use this tool: https://github.com/RichardAH/xrpl-codec-gen
61
+
62
+ ## Transaction Types
63
+
64
+ See https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/TxFormats.h
65
+
66
+ # Defining Your Own Definitions
67
+
68
+ If you're building your own sidechain or writing an amendment for the XRPL, you may need to create new XRPL definitions.
69
+
70
+ To do that there are a couple things you need to do:
71
+
72
+ 1. Generate your own `definitions.json` file from rippled source code using [this tool](https://github.com/RichardAH/xrpl-codec-gen) (The default `definitions.json` for mainnet can be found [here](https://github.com/XRPLF/xrpl.js/blob/main/packages/ripple-binary-codec/src/enums/definitions.json))
73
+ 2. Create new SerializedType classes for any new Types (So that encode/decode behavior is defined). The SerializedType classes correspond to "ST..." classes in Rippled. Note: This is very rarely required.
74
+
75
+ - For examples of how to implement that you can look at objects in the [`types` folder](../types/), such as `Amount`, `UInt8`, or `STArray`.
76
+
77
+ 3. Import your `definitions.json` file to construct your own `XrplDefinitions` object.
78
+ 4. Pass the `XrplDefinitions` object whenever you `encode` or `decode` a transaction.
79
+ 5. If you added any new transaction types, you should create an `interface` for the transaction that extends `BaseTransaction` from the `xrpl` repo to use it with the functions on `Client` (See the below example of adding a new transaction type)
80
+
81
+ ## Example of adding a new Transaction type
82
+
83
+ ```
84
+ // newDefinitionsJson is where you can import your custom defined definitions.json file
85
+ const newDefinitionsJson = require('./new-transaction-type-definitions.json')
86
+ const { XrplDefinitions, Client } = require('xrpl')
87
+
88
+ const newDefs = new XrplDefinitions(newDefinitionsJson)
89
+
90
+ // Change to point at the server you care about
91
+ const serverAddress = 'wss://s.devnet.rippletest.net:51233'
92
+ const client = new Client(serverAddress)
93
+ const wallet1 = await client.fundWallet()
94
+
95
+ // Extending BaseTransaction allows typescript to recognize this as a transaction type
96
+ interface NewTx extends BaseTransaction {
97
+ Amount: Amount
98
+ }
99
+
100
+ const tx: NewTx = {
101
+ // The TransactionType here needs to match what you added in your newDefinitionsJson file
102
+ TransactionType: 'NewTx',
103
+ Account: wallet1.address,
104
+ Amount: '100',
105
+ }
106
+
107
+ // By passing in your newDefs, your new transaction should be serializable.
108
+ // Rippled will still throw an error though if it's not a supported transaction type.
109
+ const result = await client.submitAndWait(tx, {
110
+ wallet: wallet1,
111
+ definitions: newDefs,
112
+ })
113
+ ```
114
+
115
+ ## Example of adding a new serializable Type
116
+
117
+ ```
118
+ const { XrplDefinitions } = require('../dist/coretypes')
119
+
120
+ // newDefinitionsJson is where you can import your custom defined definitions.json file
121
+ const newDefinitionsJson = require('./fixtures/new-definitions.json')
122
+
123
+
124
+ // For any new Types you create, you'll need to make a class with the same name which extends a SerializedType object
125
+ // In order to define how to serialize/deserialize that field. Here we simply make our NewType act like a UInt32.
126
+
127
+ const { UInt32 } = require('../dist/types/uint-32')
128
+ class NewType extends UInt32 {
129
+ // Should be the same as UInt32
130
+ }
131
+
132
+ const extendedCoreTypes = { NewType }
133
+
134
+ const newDefs = new XrplDefinitions(newDefinitionsJson, extendedCoreTypes)
135
+
136
+ // From this point on, we should be able to serialize / deserialize Transactions with fields that have 'NewType' as their Type.
137
+
138
+ const encoded = encode(my_tx, newDefs)
139
+ const decoded = decode(encoded, newDefs)
140
+ ```
141
+
142
+ ## Other examples
143
+
144
+ You can find other examples of how to modify `definitions.json` in `definition.test.js` which contains tests for this feature, and uses various example modified `definition` files. You can find the tests and the corresponding example `definition` files in [this folder of test cases](https://github.com/XRPLF/xrpl.js/tree/main/packages/ripple-binary-codec/test)
@@ -0,0 +1,75 @@
1
+ import { BytesList, BinaryParser } from '../binary'
2
+ import { Buffer } from 'buffer/'
3
+
4
+ /*
5
+ * @brief: Bytes, name, and ordinal representing one type, ledger_type, transaction type, or result
6
+ */
7
+ export class Bytes {
8
+ readonly bytes: Buffer
9
+
10
+ constructor(
11
+ readonly name: string,
12
+ readonly ordinal: number,
13
+ readonly ordinalWidth: number,
14
+ ) {
15
+ this.bytes = Buffer.alloc(ordinalWidth)
16
+ for (let i = 0; i < ordinalWidth; i++) {
17
+ this.bytes[ordinalWidth - i - 1] = (ordinal >>> (i * 8)) & 0xff
18
+ }
19
+ }
20
+
21
+ toJSON(): string {
22
+ return this.name
23
+ }
24
+
25
+ toBytesSink(sink: BytesList): void {
26
+ sink.put(this.bytes)
27
+ }
28
+
29
+ toBytes(): Uint8Array {
30
+ return this.bytes
31
+ }
32
+ }
33
+
34
+ /*
35
+ * @brief: Collection of Bytes objects, mapping bidirectionally
36
+ */
37
+ export class BytesLookup {
38
+ constructor(types: Record<string, number>, readonly ordinalWidth: number) {
39
+ Object.entries(types).forEach(([k, v]) => {
40
+ this.add(k, v)
41
+ })
42
+ }
43
+
44
+ /**
45
+ * Add a new name value pair to the BytesLookup.
46
+ *
47
+ * @param name - A human readable name for the field.
48
+ * @param value - The numeric value for the field.
49
+ * @throws if the name or value already exist in the lookup because it's unclear how to decode.
50
+ */
51
+ add(name: string, value: number): void {
52
+ if (this[name]) {
53
+ throw new SyntaxError(
54
+ `Attempted to add a value with a duplicate name "${name}". This is not allowed because it is unclear how to decode.`,
55
+ )
56
+ }
57
+ if (this[value.toString()]) {
58
+ throw new SyntaxError(
59
+ `Attempted to add a duplicate value under a different name (Given name: "${name}" and previous name: "${
60
+ this[value.toString()]
61
+ }. This is not allowed because it is unclear how to decode.\nGiven value: ${value.toString()}`,
62
+ )
63
+ }
64
+ this[name] = new Bytes(name, value, this.ordinalWidth)
65
+ this[value.toString()] = this[name]
66
+ }
67
+
68
+ from(value: Bytes | string): Bytes {
69
+ return value instanceof Bytes ? value : (this[value] as Bytes)
70
+ }
71
+
72
+ fromParser(parser: BinaryParser): Bytes {
73
+ return this.from(parser.readUIntN(this.ordinalWidth).toString())
74
+ }
75
+ }
@@ -0,0 +1,4 @@
1
+ export const TYPE_WIDTH = 2
2
+ export const LEDGER_ENTRY_WIDTH = 2
3
+ export const TRANSACTION_TYPE_WIDTH = 2
4
+ export const TRANSACTION_RESULT_WIDTH = 1