ripple-binary-codec 2.0.0-beta.0 → 2.0.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/binary.d.ts +12 -13
- package/dist/binary.js +6 -5
- package/dist/binary.js.map +1 -1
- package/dist/enums/bytes.d.ts +1 -2
- package/dist/enums/bytes.js +1 -2
- package/dist/enums/bytes.js.map +1 -1
- package/dist/enums/definitions.json +36 -11
- package/dist/enums/field.d.ts +1 -2
- package/dist/enums/field.js +1 -2
- package/dist/enums/field.js.map +1 -1
- package/dist/enums/index.js +4 -24
- package/dist/enums/index.js.map +1 -1
- package/dist/enums/src/enums/definitions.json +36 -11
- package/dist/hash-prefixes.d.ts +1 -2
- package/dist/hash-prefixes.js +6 -6
- package/dist/hash-prefixes.js.map +1 -1
- package/dist/hashes.d.ts +5 -6
- package/dist/hashes.js +1 -2
- package/dist/hashes.js.map +1 -1
- package/dist/index.js +7 -14
- package/dist/index.js.map +1 -1
- package/dist/quality.d.ts +1 -2
- package/dist/quality.js +3 -3
- package/dist/quality.js.map +1 -1
- package/dist/serdes/binary-parser.d.ts +2 -3
- package/dist/serdes/binary-parser.js +2 -2
- package/dist/serdes/binary-parser.js.map +1 -1
- package/dist/serdes/binary-serializer.d.ts +6 -7
- package/dist/serdes/binary-serializer.js +9 -9
- package/dist/serdes/binary-serializer.js.map +1 -1
- package/dist/shamap.d.ts +4 -5
- package/dist/shamap.js +2 -3
- package/dist/shamap.js.map +1 -1
- package/dist/src/binary.d.ts +90 -0
- package/dist/src/binary.js +128 -0
- package/dist/src/binary.js.map +1 -0
- package/dist/src/coretypes.d.ts +9 -0
- package/dist/src/coretypes.js +48 -0
- package/dist/src/coretypes.js.map +1 -0
- package/dist/src/enums/bytes.d.ts +25 -0
- package/dist/src/enums/bytes.js +63 -0
- package/dist/src/enums/bytes.js.map +1 -0
- package/dist/src/enums/constants.d.ts +4 -0
- package/dist/src/enums/constants.js +8 -0
- package/dist/src/enums/constants.js.map +1 -0
- package/dist/src/enums/definitions.json +2866 -0
- package/dist/src/enums/field.d.ts +28 -0
- package/dist/src/enums/field.js +58 -0
- package/dist/src/enums/field.js.map +1 -0
- package/dist/src/enums/index.d.ts +12 -0
- package/dist/src/enums/index.js +31 -0
- package/dist/src/enums/index.js.map +1 -0
- package/dist/src/enums/utils-renumber.d.ts +101 -0
- package/dist/src/enums/utils-renumber.js +127 -0
- package/dist/src/enums/utils-renumber.js.map +1 -0
- package/dist/src/enums/xrpl-definitions-base.d.ts +44 -0
- package/dist/src/enums/xrpl-definitions-base.js +59 -0
- package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
- package/dist/src/enums/xrpl-definitions.d.ts +21 -0
- package/dist/src/enums/xrpl-definitions.js +29 -0
- package/dist/src/enums/xrpl-definitions.js.map +1 -0
- package/dist/src/hash-prefixes.d.ts +5 -0
- package/dist/src/hash-prefixes.js +41 -0
- package/dist/src/hash-prefixes.js.map +1 -0
- package/dist/src/hashes.d.ts +50 -0
- package/dist/src/hashes.js +76 -0
- package/dist/src/hashes.js.map +1 -0
- package/dist/src/index.d.ts +64 -0
- package/dist/src/index.js +123 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/ledger-hashes.d.ts +48 -0
- package/dist/src/ledger-hashes.js +141 -0
- package/dist/src/ledger-hashes.js.map +1 -0
- package/dist/src/quality.d.ts +21 -0
- package/dist/src/quality.js +42 -0
- package/dist/src/quality.js.map +1 -0
- package/dist/src/serdes/binary-parser.d.ts +100 -0
- package/dist/src/serdes/binary-parser.js +194 -0
- package/dist/src/serdes/binary-parser.js.map +1 -0
- package/dist/src/serdes/binary-serializer.d.ts +81 -0
- package/dist/src/serdes/binary-serializer.js +149 -0
- package/dist/src/serdes/binary-serializer.js.map +1 -0
- package/dist/src/shamap.d.ts +102 -0
- package/dist/src/shamap.js +172 -0
- package/dist/src/shamap.js.map +1 -0
- package/dist/src/types/account-id.d.ts +35 -0
- package/dist/src/types/account-id.js +69 -0
- package/dist/src/types/account-id.js.map +1 -0
- package/dist/src/types/amount.d.ts +67 -0
- package/dist/src/types/amount.js +212 -0
- package/dist/src/types/amount.js.map +1 -0
- package/dist/src/types/blob.d.ts +24 -0
- package/dist/src/types/blob.js +40 -0
- package/dist/src/types/blob.js.map +1 -0
- package/dist/src/types/currency.d.ts +28 -0
- package/dist/src/types/currency.js +127 -0
- package/dist/src/types/currency.js.map +1 -0
- package/dist/src/types/hash-128.d.ts +16 -0
- package/dist/src/types/hash-128.js +32 -0
- package/dist/src/types/hash-128.js.map +1 -0
- package/dist/src/types/hash-160.d.ts +10 -0
- package/dist/src/types/hash-160.js +19 -0
- package/dist/src/types/hash-160.js.map +1 -0
- package/dist/src/types/hash-256.d.ts +10 -0
- package/dist/src/types/hash-256.js +16 -0
- package/dist/src/types/hash-256.js.map +1 -0
- package/dist/src/types/hash.d.ts +40 -0
- package/dist/src/types/hash.js +73 -0
- package/dist/src/types/hash.js.map +1 -0
- package/dist/src/types/index.d.ts +18 -0
- package/dist/src/types/index.js +61 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/issue.d.ts +38 -0
- package/dist/src/types/issue.js +81 -0
- package/dist/src/types/issue.js.map +1 -0
- package/dist/src/types/path-set.d.ts +36 -0
- package/dist/src/types/path-set.js +233 -0
- package/dist/src/types/path-set.js.map +1 -0
- package/dist/src/types/serialized-type.d.ts +70 -0
- package/dist/src/types/serialized-type.js +105 -0
- package/dist/src/types/serialized-type.js.map +1 -0
- package/dist/src/types/st-array.d.ts +31 -0
- package/dist/src/types/st-array.js +83 -0
- package/dist/src/types/st-array.js.map +1 -0
- package/dist/src/types/st-object.d.ts +32 -0
- package/dist/src/types/st-object.js +155 -0
- package/dist/src/types/st-object.js.map +1 -0
- package/dist/src/types/uint-16.d.ts +24 -0
- package/dist/src/types/uint-16.js +45 -0
- package/dist/src/types/uint-16.js.map +1 -0
- package/dist/src/types/uint-32.d.ts +24 -0
- package/dist/src/types/uint-32.js +50 -0
- package/dist/src/types/uint-32.js.map +1 -0
- package/dist/src/types/uint-64.d.ts +37 -0
- package/dist/src/types/uint-64.js +86 -0
- package/dist/src/types/uint-64.js.map +1 -0
- package/dist/src/types/uint-8.d.ts +24 -0
- package/dist/src/types/uint-8.js +46 -0
- package/dist/src/types/uint-8.js.map +1 -0
- package/dist/src/types/uint.d.ts +29 -0
- package/dist/src/types/uint.js +47 -0
- package/dist/src/types/uint.js.map +1 -0
- package/dist/src/types/vector-256.d.ts +30 -0
- package/dist/src/types/vector-256.js +73 -0
- package/dist/src/types/vector-256.js.map +1 -0
- package/dist/src/types/xchain-bridge.d.ts +44 -0
- package/dist/src/types/xchain-bridge.js +102 -0
- package/dist/src/types/xchain-bridge.js.map +1 -0
- package/dist/src/utils.d.ts +47 -0
- package/dist/src/utils.js +137 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/account-id.d.ts +1 -2
- package/dist/types/account-id.js +4 -6
- package/dist/types/account-id.js.map +1 -1
- package/dist/types/amount.d.ts +1 -2
- package/dist/types/amount.js +16 -15
- package/dist/types/amount.js.map +1 -1
- package/dist/types/blob.d.ts +1 -2
- package/dist/types/blob.js +2 -2
- package/dist/types/blob.js.map +1 -1
- package/dist/types/currency.d.ts +1 -2
- package/dist/types/currency.js +10 -10
- package/dist/types/currency.js.map +1 -1
- package/dist/types/hash-128.d.ts +1 -2
- package/dist/types/hash-128.js +3 -3
- package/dist/types/hash-128.js.map +1 -1
- package/dist/types/hash-160.d.ts +1 -2
- package/dist/types/hash-160.js +1 -2
- package/dist/types/hash-160.js.map +1 -1
- package/dist/types/hash-256.d.ts +1 -2
- package/dist/types/hash-256.js +1 -2
- package/dist/types/hash-256.js.map +1 -1
- package/dist/types/hash.d.ts +2 -3
- package/dist/types/hash.js +5 -4
- package/dist/types/hash.js.map +1 -1
- package/dist/types/issue.d.ts +1 -2
- package/dist/types/issue.js +4 -4
- package/dist/types/issue.js.map +1 -1
- package/dist/types/path-set.js +12 -12
- package/dist/types/path-set.js.map +1 -1
- package/dist/types/serialized-type.d.ts +17 -13
- package/dist/types/serialized-type.js +11 -6
- package/dist/types/serialized-type.js.map +1 -1
- package/dist/types/st-array.js +5 -5
- package/dist/types/st-array.js.map +1 -1
- package/dist/types/st-object.js +1 -2
- package/dist/types/st-object.js.map +1 -1
- package/dist/types/uint-16.d.ts +1 -2
- package/dist/types/uint-16.js +6 -5
- package/dist/types/uint-16.js.map +1 -1
- package/dist/types/uint-32.d.ts +1 -2
- package/dist/types/uint-32.js +7 -6
- package/dist/types/uint-32.js.map +1 -1
- package/dist/types/uint-64.d.ts +2 -3
- package/dist/types/uint-64.js +16 -15
- package/dist/types/uint-64.js.map +1 -1
- package/dist/types/uint-8.d.ts +1 -2
- package/dist/types/uint-8.js +7 -5
- package/dist/types/uint-8.js.map +1 -1
- package/dist/types/uint.d.ts +4 -4
- package/dist/types/uint.js +5 -0
- package/dist/types/uint.js.map +1 -1
- package/dist/types/vector-256.d.ts +1 -2
- package/dist/types/vector-256.js +2 -4
- package/dist/types/vector-256.js.map +1 -1
- package/dist/types/xchain-bridge.d.ts +1 -2
- package/dist/types/xchain-bridge.js +20 -20
- package/dist/types/xchain-bridge.js.map +1 -1
- package/dist/utils.d.ts +47 -0
- package/dist/utils.js +137 -0
- package/dist/utils.js.map +1 -0
- package/package.json +8 -11
- package/src/README.md +3 -1
- package/src/binary.ts +21 -14
- package/src/enums/bytes.ts +2 -3
- package/src/enums/definitions.json +36 -11
- package/src/enums/field.ts +3 -4
- package/src/enums/index.ts +1 -1
- package/src/hash-prefixes.ts +8 -8
- package/src/hashes.ts +6 -7
- package/src/index.ts +12 -15
- package/src/quality.ts +4 -4
- package/src/serdes/binary-parser.ts +5 -5
- package/src/serdes/binary-serializer.ts +14 -14
- package/src/shamap.ts +5 -6
- package/src/types/account-id.ts +8 -8
- package/src/types/amount.ts +17 -18
- package/src/types/blob.ts +3 -3
- package/src/types/currency.ts +16 -16
- package/src/types/hash-128.ts +4 -4
- package/src/types/hash-160.ts +2 -3
- package/src/types/hash-256.ts +2 -3
- package/src/types/hash.ts +8 -6
- package/src/types/issue.ts +5 -5
- package/src/types/path-set.ts +16 -16
- package/src/types/serialized-type.ts +20 -15
- package/src/types/st-array.ts +7 -7
- package/src/types/st-object.ts +2 -2
- package/src/types/uint-16.ts +10 -6
- package/src/types/uint-32.ts +10 -7
- package/src/types/uint-64.ts +20 -17
- package/src/types/uint-8.ts +9 -6
- package/src/types/uint.ts +11 -4
- package/src/types/vector-256.ts +3 -8
- package/src/types/xchain-bridge.ts +22 -22
- package/src/utils.ts +152 -0
package/src/types/hash.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { Comparable } from './serialized-type'
|
|
2
2
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
-
import {
|
|
3
|
+
import { hexToBytes } from '@xrplf/isomorphic/utils'
|
|
4
|
+
import { compare } from '../utils'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Base class defining how to encode and decode hashes
|
|
7
8
|
*/
|
|
8
|
-
class Hash extends Comparable {
|
|
9
|
+
class Hash extends Comparable<Hash | string> {
|
|
9
10
|
static readonly width: number
|
|
10
11
|
|
|
11
|
-
constructor(bytes:
|
|
12
|
+
constructor(bytes: Uint8Array) {
|
|
12
13
|
super(bytes)
|
|
13
|
-
if (this.bytes.
|
|
14
|
+
if (this.bytes.length !== (this.constructor as typeof Hash).width) {
|
|
14
15
|
throw new Error(`Invalid Hash length ${this.bytes.byteLength}`)
|
|
15
16
|
}
|
|
16
17
|
}
|
|
@@ -26,7 +27,7 @@ class Hash extends Comparable {
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
if (typeof value === 'string') {
|
|
29
|
-
return new this(
|
|
30
|
+
return new this(hexToBytes(value))
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
throw new Error('Cannot construct Hash from given value')
|
|
@@ -48,7 +49,8 @@ class Hash extends Comparable {
|
|
|
48
49
|
* @param other The Hash to compare this to
|
|
49
50
|
*/
|
|
50
51
|
compareTo(other: Hash): number {
|
|
51
|
-
return
|
|
52
|
+
return compare(
|
|
53
|
+
this.bytes,
|
|
52
54
|
(this.constructor as typeof Hash).from(other).bytes,
|
|
53
55
|
)
|
|
54
56
|
}
|
package/src/types/issue.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { concat } from '@xrplf/isomorphic/utils'
|
|
1
2
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
2
3
|
|
|
3
4
|
import { AccountID } from './account-id'
|
|
4
5
|
import { Currency } from './currency'
|
|
5
6
|
import { JsonObject, SerializedType } from './serialized-type'
|
|
6
|
-
import { Buffer } from 'buffer/'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Interface for JSON objects that represent amounts
|
|
@@ -28,9 +28,9 @@ function isIssueObject(arg): arg is IssueObject {
|
|
|
28
28
|
* Class for serializing/Deserializing Amounts
|
|
29
29
|
*/
|
|
30
30
|
class Issue extends SerializedType {
|
|
31
|
-
static readonly ZERO_ISSUED_CURRENCY: Issue = new Issue(
|
|
31
|
+
static readonly ZERO_ISSUED_CURRENCY: Issue = new Issue(new Uint8Array(20))
|
|
32
32
|
|
|
33
|
-
constructor(bytes:
|
|
33
|
+
constructor(bytes: Uint8Array) {
|
|
34
34
|
super(bytes ?? Issue.ZERO_ISSUED_CURRENCY.bytes)
|
|
35
35
|
}
|
|
36
36
|
|
|
@@ -52,7 +52,7 @@ class Issue extends SerializedType {
|
|
|
52
52
|
return new Issue(currency)
|
|
53
53
|
}
|
|
54
54
|
const issuer = AccountID.from(value.issuer).toBytes()
|
|
55
|
-
return new Issue(
|
|
55
|
+
return new Issue(concat([currency, issuer]))
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
throw new Error('Invalid type to construct an Amount')
|
|
@@ -70,7 +70,7 @@ class Issue extends SerializedType {
|
|
|
70
70
|
return new Issue(currency)
|
|
71
71
|
}
|
|
72
72
|
const currencyAndIssuer = [currency, parser.read(20)]
|
|
73
|
-
return new Issue(
|
|
73
|
+
return new Issue(concat(currencyAndIssuer))
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
/**
|
package/src/types/path-set.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { AccountID } from './account-id'
|
|
|
2
2
|
import { Currency } from './currency'
|
|
3
3
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
4
4
|
import { SerializedType, JsonObject } from './serialized-type'
|
|
5
|
-
import {
|
|
5
|
+
import { bytesToHex, concat } from '@xrplf/isomorphic/utils'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Constants for separating Paths in a PathSet
|
|
@@ -63,7 +63,7 @@ class Hop extends SerializedType {
|
|
|
63
63
|
return value
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
const bytes: Array<
|
|
66
|
+
const bytes: Array<Uint8Array> = [Uint8Array.from([0])]
|
|
67
67
|
|
|
68
68
|
if (value.account) {
|
|
69
69
|
bytes.push(AccountID.from(value.account).toBytes())
|
|
@@ -80,7 +80,7 @@ class Hop extends SerializedType {
|
|
|
80
80
|
bytes[0][0] |= TYPE_ISSUER
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
return new Hop(
|
|
83
|
+
return new Hop(concat(bytes))
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
/**
|
|
@@ -91,7 +91,7 @@ class Hop extends SerializedType {
|
|
|
91
91
|
*/
|
|
92
92
|
static fromParser(parser: BinaryParser): Hop {
|
|
93
93
|
const type = parser.readUInt8()
|
|
94
|
-
const bytes: Array<
|
|
94
|
+
const bytes: Array<Uint8Array> = [Uint8Array.from([type])]
|
|
95
95
|
|
|
96
96
|
if (type & TYPE_ACCOUNT) {
|
|
97
97
|
bytes.push(parser.read(AccountID.width))
|
|
@@ -105,7 +105,7 @@ class Hop extends SerializedType {
|
|
|
105
105
|
bytes.push(parser.read(AccountID.width))
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
return new Hop(
|
|
108
|
+
return new Hop(concat(bytes))
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
/**
|
|
@@ -114,7 +114,7 @@ class Hop extends SerializedType {
|
|
|
114
114
|
* @returns a HopObject, an JS object with optional account, issuer, and currency
|
|
115
115
|
*/
|
|
116
116
|
toJSON(): HopObject {
|
|
117
|
-
const hopParser = new BinaryParser(this.bytes
|
|
117
|
+
const hopParser = new BinaryParser(bytesToHex(this.bytes))
|
|
118
118
|
const type = hopParser.readUInt8()
|
|
119
119
|
|
|
120
120
|
let account, currency, issuer
|
|
@@ -171,12 +171,12 @@ class Path extends SerializedType {
|
|
|
171
171
|
return value
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
const bytes: Array<
|
|
174
|
+
const bytes: Array<Uint8Array> = []
|
|
175
175
|
value.forEach((hop: HopObject) => {
|
|
176
176
|
bytes.push(Hop.from(hop).toBytes())
|
|
177
177
|
})
|
|
178
178
|
|
|
179
|
-
return new Path(
|
|
179
|
+
return new Path(concat(bytes))
|
|
180
180
|
}
|
|
181
181
|
|
|
182
182
|
/**
|
|
@@ -186,7 +186,7 @@ class Path extends SerializedType {
|
|
|
186
186
|
* @returns the Path represented by the bytes read from the BinaryParser
|
|
187
187
|
*/
|
|
188
188
|
static fromParser(parser: BinaryParser): Path {
|
|
189
|
-
const bytes: Array<
|
|
189
|
+
const bytes: Array<Uint8Array> = []
|
|
190
190
|
while (!parser.end()) {
|
|
191
191
|
bytes.push(Hop.fromParser(parser).toBytes())
|
|
192
192
|
|
|
@@ -197,7 +197,7 @@ class Path extends SerializedType {
|
|
|
197
197
|
break
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
|
-
return new Path(
|
|
200
|
+
return new Path(concat(bytes))
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
/**
|
|
@@ -233,16 +233,16 @@ class PathSet extends SerializedType {
|
|
|
233
233
|
}
|
|
234
234
|
|
|
235
235
|
if (isPathSet(value)) {
|
|
236
|
-
const bytes: Array<
|
|
236
|
+
const bytes: Array<Uint8Array> = []
|
|
237
237
|
|
|
238
238
|
value.forEach((path: Array<HopObject>) => {
|
|
239
239
|
bytes.push(Path.from(path).toBytes())
|
|
240
|
-
bytes.push(
|
|
240
|
+
bytes.push(Uint8Array.from([PATH_SEPARATOR_BYTE]))
|
|
241
241
|
})
|
|
242
242
|
|
|
243
|
-
bytes[bytes.length - 1] =
|
|
243
|
+
bytes[bytes.length - 1] = Uint8Array.from([PATHSET_END_BYTE])
|
|
244
244
|
|
|
245
|
-
return new PathSet(
|
|
245
|
+
return new PathSet(concat(bytes))
|
|
246
246
|
}
|
|
247
247
|
|
|
248
248
|
throw new Error('Cannot construct PathSet from given value')
|
|
@@ -255,7 +255,7 @@ class PathSet extends SerializedType {
|
|
|
255
255
|
* @returns the PathSet read from parser
|
|
256
256
|
*/
|
|
257
257
|
static fromParser(parser: BinaryParser): PathSet {
|
|
258
|
-
const bytes: Array<
|
|
258
|
+
const bytes: Array<Uint8Array> = []
|
|
259
259
|
|
|
260
260
|
while (!parser.end()) {
|
|
261
261
|
bytes.push(Path.fromParser(parser).toBytes())
|
|
@@ -266,7 +266,7 @@ class PathSet extends SerializedType {
|
|
|
266
266
|
}
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
-
return new PathSet(
|
|
269
|
+
return new PathSet(concat(bytes))
|
|
270
270
|
}
|
|
271
271
|
|
|
272
272
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BytesList } from '../serdes/binary-serializer'
|
|
2
2
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
-
import { Buffer } from 'buffer/'
|
|
4
3
|
import { XrplDefinitionsBase } from '../enums'
|
|
4
|
+
import { bytesToHex } from '@xrplf/isomorphic/utils'
|
|
5
5
|
|
|
6
6
|
type JSON = string | number | boolean | null | undefined | JSON[] | JsonObject
|
|
7
7
|
|
|
@@ -11,10 +11,10 @@ type JsonObject = { [key: string]: JSON }
|
|
|
11
11
|
* The base class for all binary-codec types
|
|
12
12
|
*/
|
|
13
13
|
class SerializedType {
|
|
14
|
-
protected readonly bytes:
|
|
14
|
+
protected readonly bytes: Uint8Array = new Uint8Array(0)
|
|
15
15
|
|
|
16
|
-
constructor(bytes
|
|
17
|
-
this.bytes = bytes ??
|
|
16
|
+
constructor(bytes?: Uint8Array) {
|
|
17
|
+
this.bytes = bytes ?? new Uint8Array(0)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
static fromParser(parser: BinaryParser, hint?: number): SerializedType {
|
|
@@ -42,15 +42,15 @@ class SerializedType {
|
|
|
42
42
|
* @returns hex String of this.bytes
|
|
43
43
|
*/
|
|
44
44
|
toHex(): string {
|
|
45
|
-
return this.toBytes()
|
|
45
|
+
return bytesToHex(this.toBytes())
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
49
|
* Get the bytes representation of a SerializedType
|
|
50
50
|
*
|
|
51
|
-
* @returns A
|
|
51
|
+
* @returns A Uint8Array of the bytes
|
|
52
52
|
*/
|
|
53
|
-
toBytes():
|
|
53
|
+
toBytes(): Uint8Array {
|
|
54
54
|
if (this.bytes) {
|
|
55
55
|
return this.bytes
|
|
56
56
|
}
|
|
@@ -80,26 +80,31 @@ class SerializedType {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
/**
|
|
83
|
-
* Base class for SerializedTypes that are comparable
|
|
83
|
+
* Base class for SerializedTypes that are comparable.
|
|
84
|
+
*
|
|
85
|
+
* @template T - What types you want to allow comparisons between. You must specify all types. Primarily used to allow
|
|
86
|
+
* comparisons between built-in types (like `string`) and SerializedType subclasses (like `Hash`).
|
|
87
|
+
*
|
|
88
|
+
* Ex. `class Hash extends Comparable<Hash | string>`
|
|
84
89
|
*/
|
|
85
|
-
class Comparable extends SerializedType {
|
|
86
|
-
lt(other:
|
|
90
|
+
class Comparable<T extends Object> extends SerializedType {
|
|
91
|
+
lt(other: T): boolean {
|
|
87
92
|
return this.compareTo(other) < 0
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
eq(other:
|
|
95
|
+
eq(other: T): boolean {
|
|
91
96
|
return this.compareTo(other) === 0
|
|
92
97
|
}
|
|
93
98
|
|
|
94
|
-
gt(other:
|
|
99
|
+
gt(other: T): boolean {
|
|
95
100
|
return this.compareTo(other) > 0
|
|
96
101
|
}
|
|
97
102
|
|
|
98
|
-
gte(other:
|
|
103
|
+
gte(other: T): boolean {
|
|
99
104
|
return this.compareTo(other) > -1
|
|
100
105
|
}
|
|
101
106
|
|
|
102
|
-
lte(other:
|
|
107
|
+
lte(other: T): boolean {
|
|
103
108
|
return this.compareTo(other) < 1
|
|
104
109
|
}
|
|
105
110
|
|
|
@@ -109,7 +114,7 @@ class Comparable extends SerializedType {
|
|
|
109
114
|
* @param other The comparable object to compare this to
|
|
110
115
|
* @returns A number denoting the relationship of this and other
|
|
111
116
|
*/
|
|
112
|
-
compareTo(other:
|
|
117
|
+
compareTo(other: T): number {
|
|
113
118
|
throw new Error(`cannot compare ${this.toString()} and ${other.toString()}`)
|
|
114
119
|
}
|
|
115
120
|
}
|
package/src/types/st-array.ts
CHANGED
|
@@ -2,12 +2,12 @@ import { DEFAULT_DEFINITIONS, XrplDefinitionsBase } from '../enums'
|
|
|
2
2
|
import { SerializedType, JsonObject } from './serialized-type'
|
|
3
3
|
import { STObject } from './st-object'
|
|
4
4
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
5
|
-
import {
|
|
5
|
+
import { concat } from '@xrplf/isomorphic/utils'
|
|
6
6
|
|
|
7
|
-
const ARRAY_END_MARKER =
|
|
7
|
+
const ARRAY_END_MARKER = Uint8Array.from([0xf1])
|
|
8
8
|
const ARRAY_END_MARKER_NAME = 'ArrayEndMarker'
|
|
9
9
|
|
|
10
|
-
const OBJECT_END_MARKER =
|
|
10
|
+
const OBJECT_END_MARKER = Uint8Array.from([0xe1])
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* TypeGuard for Array<JsonObject>
|
|
@@ -29,7 +29,7 @@ class STArray extends SerializedType {
|
|
|
29
29
|
* @returns An STArray Object
|
|
30
30
|
*/
|
|
31
31
|
static fromParser(parser: BinaryParser): STArray {
|
|
32
|
-
const bytes: Array<
|
|
32
|
+
const bytes: Array<Uint8Array> = []
|
|
33
33
|
|
|
34
34
|
while (!parser.end()) {
|
|
35
35
|
const field = parser.readField()
|
|
@@ -45,7 +45,7 @@ class STArray extends SerializedType {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
bytes.push(ARRAY_END_MARKER)
|
|
48
|
-
return new STArray(
|
|
48
|
+
return new STArray(concat(bytes))
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
@@ -64,13 +64,13 @@ class STArray extends SerializedType {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
if (isObjects(value)) {
|
|
67
|
-
const bytes: Array<
|
|
67
|
+
const bytes: Array<Uint8Array> = []
|
|
68
68
|
value.forEach((obj) => {
|
|
69
69
|
bytes.push(STObject.from(obj, undefined, definitions).toBytes())
|
|
70
70
|
})
|
|
71
71
|
|
|
72
72
|
bytes.push(ARRAY_END_MARKER)
|
|
73
|
-
return new STArray(
|
|
73
|
+
return new STArray(concat(bytes))
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
throw new Error('Cannot construct STArray from value given')
|
package/src/types/st-object.ts
CHANGED
|
@@ -8,10 +8,10 @@ import { SerializedType, JsonObject } from './serialized-type'
|
|
|
8
8
|
import { xAddressToClassicAddress, isValidXAddress } from 'ripple-address-codec'
|
|
9
9
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
10
10
|
import { BinarySerializer, BytesList } from '../serdes/binary-serializer'
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
import { STArray } from './st-array'
|
|
13
13
|
|
|
14
|
-
const OBJECT_END_MARKER_BYTE =
|
|
14
|
+
const OBJECT_END_MARKER_BYTE = Uint8Array.from([0xe1])
|
|
15
15
|
const OBJECT_END_MARKER = 'ObjectEndMarker'
|
|
16
16
|
const ST_OBJECT = 'STObject'
|
|
17
17
|
const DESTINATION = 'Destination'
|
package/src/types/uint-16.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { UInt } from './uint'
|
|
2
2
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
-
import {
|
|
3
|
+
import { readUInt16BE, writeUInt16BE } from '../utils'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Derived UInt class for serializing/deserializing 16 bit UInt
|
|
7
7
|
*/
|
|
8
8
|
class UInt16 extends UInt {
|
|
9
9
|
protected static readonly width: number = 16 / 8 // 2
|
|
10
|
-
static readonly defaultUInt16: UInt16 = new UInt16(
|
|
10
|
+
static readonly defaultUInt16: UInt16 = new UInt16(
|
|
11
|
+
new Uint8Array(UInt16.width),
|
|
12
|
+
)
|
|
11
13
|
|
|
12
|
-
constructor(bytes:
|
|
14
|
+
constructor(bytes: Uint8Array) {
|
|
13
15
|
super(bytes ?? UInt16.defaultUInt16.bytes)
|
|
14
16
|
}
|
|
15
17
|
|
|
@@ -28,8 +30,10 @@ class UInt16 extends UInt {
|
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
if (typeof val === 'number') {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
UInt16.checkUintRange(val, 0, 0xffff)
|
|
34
|
+
|
|
35
|
+
const buf = new Uint8Array(UInt16.width)
|
|
36
|
+
writeUInt16BE(buf, val, 0)
|
|
33
37
|
return new UInt16(buf)
|
|
34
38
|
}
|
|
35
39
|
|
|
@@ -42,7 +46,7 @@ class UInt16 extends UInt {
|
|
|
42
46
|
* @returns the number represented by this.bytes
|
|
43
47
|
*/
|
|
44
48
|
valueOf(): number {
|
|
45
|
-
return this.bytes
|
|
49
|
+
return parseInt(readUInt16BE(this.bytes, 0))
|
|
46
50
|
}
|
|
47
51
|
}
|
|
48
52
|
|
package/src/types/uint-32.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { UInt } from './uint'
|
|
2
2
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
-
import {
|
|
3
|
+
import { readUInt32BE, writeUInt32BE } from '../utils'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Derived UInt class for serializing/deserializing 32 bit UInt
|
|
7
7
|
*/
|
|
8
8
|
class UInt32 extends UInt {
|
|
9
9
|
protected static readonly width: number = 32 / 8 // 4
|
|
10
|
-
static readonly defaultUInt32: UInt32 = new UInt32(
|
|
10
|
+
static readonly defaultUInt32: UInt32 = new UInt32(
|
|
11
|
+
new Uint8Array(UInt32.width),
|
|
12
|
+
)
|
|
11
13
|
|
|
12
|
-
constructor(bytes:
|
|
14
|
+
constructor(bytes: Uint8Array) {
|
|
13
15
|
super(bytes ?? UInt32.defaultUInt32.bytes)
|
|
14
16
|
}
|
|
15
17
|
|
|
@@ -27,16 +29,17 @@ class UInt32 extends UInt {
|
|
|
27
29
|
return val
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
const buf =
|
|
32
|
+
const buf = new Uint8Array(UInt32.width)
|
|
31
33
|
|
|
32
34
|
if (typeof val === 'string') {
|
|
33
35
|
const num = Number.parseInt(val)
|
|
34
|
-
|
|
36
|
+
writeUInt32BE(buf, num, 0)
|
|
35
37
|
return new UInt32(buf)
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
if (typeof val === 'number') {
|
|
39
|
-
|
|
41
|
+
UInt32.checkUintRange(val, 0, 0xffffffff)
|
|
42
|
+
writeUInt32BE(buf, val, 0)
|
|
40
43
|
return new UInt32(buf)
|
|
41
44
|
}
|
|
42
45
|
|
|
@@ -49,7 +52,7 @@ class UInt32 extends UInt {
|
|
|
49
52
|
* @returns the number represented by this.bytes
|
|
50
53
|
*/
|
|
51
54
|
valueOf(): number {
|
|
52
|
-
return this.bytes
|
|
55
|
+
return parseInt(readUInt32BE(this.bytes, 0), 10)
|
|
53
56
|
}
|
|
54
57
|
}
|
|
55
58
|
|
package/src/types/uint-64.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { UInt } from './uint'
|
|
2
2
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
-
import {
|
|
3
|
+
import { bytesToHex, concat, hexToBytes } from '@xrplf/isomorphic/utils'
|
|
4
|
+
import { readUInt32BE, writeUInt32BE } from '../utils'
|
|
4
5
|
|
|
5
6
|
const HEX_REGEX = /^[a-fA-F0-9]{1,16}$/
|
|
6
7
|
const mask = BigInt(0x00000000ffffffff)
|
|
@@ -10,9 +11,11 @@ const mask = BigInt(0x00000000ffffffff)
|
|
|
10
11
|
*/
|
|
11
12
|
class UInt64 extends UInt {
|
|
12
13
|
protected static readonly width: number = 64 / 8 // 8
|
|
13
|
-
static readonly defaultUInt64: UInt64 = new UInt64(
|
|
14
|
+
static readonly defaultUInt64: UInt64 = new UInt64(
|
|
15
|
+
new Uint8Array(UInt64.width),
|
|
16
|
+
)
|
|
14
17
|
|
|
15
|
-
constructor(bytes:
|
|
18
|
+
constructor(bytes: Uint8Array) {
|
|
16
19
|
super(bytes ?? UInt64.defaultUInt64.bytes)
|
|
17
20
|
}
|
|
18
21
|
|
|
@@ -31,7 +34,7 @@ class UInt64 extends UInt {
|
|
|
31
34
|
return val
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
let buf =
|
|
37
|
+
let buf = new Uint8Array(UInt64.width)
|
|
35
38
|
|
|
36
39
|
if (typeof val === 'number') {
|
|
37
40
|
if (val < 0) {
|
|
@@ -40,11 +43,11 @@ class UInt64 extends UInt {
|
|
|
40
43
|
|
|
41
44
|
const number = BigInt(val)
|
|
42
45
|
|
|
43
|
-
const intBuf = [
|
|
44
|
-
intBuf[0]
|
|
45
|
-
intBuf[1]
|
|
46
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)]
|
|
47
|
+
writeUInt32BE(intBuf[0], Number(number >> BigInt(32)), 0)
|
|
48
|
+
writeUInt32BE(intBuf[1], Number(number & BigInt(mask)), 0)
|
|
46
49
|
|
|
47
|
-
return new UInt64(
|
|
50
|
+
return new UInt64(concat(intBuf))
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
if (typeof val === 'string') {
|
|
@@ -53,16 +56,16 @@ class UInt64 extends UInt {
|
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
const strBuf = val.padStart(16, '0')
|
|
56
|
-
buf =
|
|
59
|
+
buf = hexToBytes(strBuf)
|
|
57
60
|
return new UInt64(buf)
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
if (typeof val === 'bigint') {
|
|
61
|
-
const intBuf = [
|
|
62
|
-
intBuf[0]
|
|
63
|
-
intBuf[1]
|
|
64
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)]
|
|
65
|
+
writeUInt32BE(intBuf[0], Number(Number(val >> BigInt(32))), 0)
|
|
66
|
+
writeUInt32BE(intBuf[1], Number(val & BigInt(mask)), 0)
|
|
64
67
|
|
|
65
|
-
return new UInt64(
|
|
68
|
+
return new UInt64(concat(intBuf))
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
throw new Error('Cannot construct UInt64 from given value')
|
|
@@ -74,7 +77,7 @@ class UInt64 extends UInt {
|
|
|
74
77
|
* @returns a hex-string
|
|
75
78
|
*/
|
|
76
79
|
toJSON(): string {
|
|
77
|
-
return this.bytes
|
|
80
|
+
return bytesToHex(this.bytes)
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
/**
|
|
@@ -83,8 +86,8 @@ class UInt64 extends UInt {
|
|
|
83
86
|
* @returns the number represented buy this.bytes
|
|
84
87
|
*/
|
|
85
88
|
valueOf(): bigint {
|
|
86
|
-
const msb = BigInt(this.bytes.slice(0, 4)
|
|
87
|
-
const lsb = BigInt(this.bytes.slice(4)
|
|
89
|
+
const msb = BigInt(readUInt32BE(this.bytes.slice(0, 4), 0))
|
|
90
|
+
const lsb = BigInt(readUInt32BE(this.bytes.slice(4), 0))
|
|
88
91
|
return (msb << BigInt(32)) | lsb
|
|
89
92
|
}
|
|
90
93
|
|
|
@@ -93,7 +96,7 @@ class UInt64 extends UInt {
|
|
|
93
96
|
*
|
|
94
97
|
* @returns 8 bytes representing the UInt64
|
|
95
98
|
*/
|
|
96
|
-
toBytes():
|
|
99
|
+
toBytes(): Uint8Array {
|
|
97
100
|
return this.bytes
|
|
98
101
|
}
|
|
99
102
|
}
|
package/src/types/uint-8.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { UInt } from './uint'
|
|
2
2
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
-
import {
|
|
3
|
+
import { bytesToHex } from '@xrplf/isomorphic/utils'
|
|
4
|
+
import { writeUInt8 } from '../utils'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Derived UInt class for serializing/deserializing 8 bit UInt
|
|
7
8
|
*/
|
|
8
9
|
class UInt8 extends UInt {
|
|
9
10
|
protected static readonly width: number = 8 / 8 // 1
|
|
10
|
-
static readonly defaultUInt8: UInt8 = new UInt8(
|
|
11
|
+
static readonly defaultUInt8: UInt8 = new UInt8(new Uint8Array(UInt8.width))
|
|
11
12
|
|
|
12
|
-
constructor(bytes:
|
|
13
|
+
constructor(bytes: Uint8Array) {
|
|
13
14
|
super(bytes ?? UInt8.defaultUInt8.bytes)
|
|
14
15
|
}
|
|
15
16
|
|
|
@@ -28,8 +29,10 @@ class UInt8 extends UInt {
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
if (typeof val === 'number') {
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
UInt8.checkUintRange(val, 0, 0xff)
|
|
33
|
+
|
|
34
|
+
const buf = new Uint8Array(UInt8.width)
|
|
35
|
+
writeUInt8(buf, val, 0)
|
|
33
36
|
return new UInt8(buf)
|
|
34
37
|
}
|
|
35
38
|
|
|
@@ -42,7 +45,7 @@ class UInt8 extends UInt {
|
|
|
42
45
|
* @returns the number represented by this.bytes
|
|
43
46
|
*/
|
|
44
47
|
valueOf(): number {
|
|
45
|
-
return this.bytes
|
|
48
|
+
return parseInt(bytesToHex(this.bytes), 16)
|
|
46
49
|
}
|
|
47
50
|
}
|
|
48
51
|
|
package/src/types/uint.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Comparable } from './serialized-type'
|
|
2
|
-
import { Buffer } from 'buffer/'
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Compare numbers and bigInts n1 and n2
|
|
@@ -15,10 +14,10 @@ function compare(n1: number | bigint, n2: number | bigint): number {
|
|
|
15
14
|
/**
|
|
16
15
|
* Base class for serializing and deserializing unsigned integers.
|
|
17
16
|
*/
|
|
18
|
-
abstract class UInt extends Comparable {
|
|
17
|
+
abstract class UInt extends Comparable<UInt | number> {
|
|
19
18
|
protected static width: number
|
|
20
19
|
|
|
21
|
-
constructor(bytes:
|
|
20
|
+
constructor(bytes: Uint8Array) {
|
|
22
21
|
super(bytes)
|
|
23
22
|
}
|
|
24
23
|
|
|
@@ -28,7 +27,7 @@ abstract class UInt extends Comparable {
|
|
|
28
27
|
* @param other other UInt to compare this to
|
|
29
28
|
* @returns -1, 0, or 1 depending on how the objects relate to each other
|
|
30
29
|
*/
|
|
31
|
-
compareTo(other: UInt): number {
|
|
30
|
+
compareTo(other: UInt | number): number {
|
|
32
31
|
return compare(this.valueOf(), other.valueOf())
|
|
33
32
|
}
|
|
34
33
|
|
|
@@ -48,6 +47,14 @@ abstract class UInt extends Comparable {
|
|
|
48
47
|
* @returns the value
|
|
49
48
|
*/
|
|
50
49
|
abstract valueOf(): number | bigint
|
|
50
|
+
|
|
51
|
+
static checkUintRange(val: number, min: number, max: number): void {
|
|
52
|
+
if (val < min || val > max) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Invalid ${this.constructor.name}: ${val} must be >= ${min} and <= ${max}`,
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
51
58
|
}
|
|
52
59
|
|
|
53
60
|
export { UInt }
|
package/src/types/vector-256.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { SerializedType } from './serialized-type'
|
|
|
2
2
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
3
|
import { Hash256 } from './hash-256'
|
|
4
4
|
import { BytesList } from '../serdes/binary-serializer'
|
|
5
|
-
import {
|
|
5
|
+
import { bytesToHex } from '@xrplf/isomorphic/utils'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* TypeGuard for Array<string>
|
|
@@ -15,7 +15,7 @@ function isStrings(arg): arg is Array<string> {
|
|
|
15
15
|
* Class for serializing and deserializing vectors of Hash256
|
|
16
16
|
*/
|
|
17
17
|
class Vector256 extends SerializedType {
|
|
18
|
-
constructor(bytes:
|
|
18
|
+
constructor(bytes: Uint8Array) {
|
|
19
19
|
super(bytes)
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -70,12 +70,7 @@ class Vector256 extends SerializedType {
|
|
|
70
70
|
|
|
71
71
|
const result: Array<string> = []
|
|
72
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
|
-
)
|
|
73
|
+
result.push(bytesToHex(this.bytes.slice(i, i + 32)))
|
|
79
74
|
}
|
|
80
75
|
return result
|
|
81
76
|
}
|