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/hash-prefixes.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { writeUInt32BE } from './utils'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Write a 32 bit integer to a
|
|
4
|
+
* Write a 32 bit integer to a Uint8Array
|
|
5
5
|
*
|
|
6
|
-
* @param uint32 32 bit integer to write to
|
|
7
|
-
* @returns a
|
|
6
|
+
* @param uint32 32 bit integer to write to Uint8Array
|
|
7
|
+
* @returns a Uint8Array with the bytes representation of uint32
|
|
8
8
|
*/
|
|
9
|
-
function bytes(uint32: number):
|
|
10
|
-
const result =
|
|
11
|
-
|
|
9
|
+
function bytes(uint32: number): Uint8Array {
|
|
10
|
+
const result = new Uint8Array(4)
|
|
11
|
+
writeUInt32BE(result, uint32, 0)
|
|
12
12
|
return result
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Maps HashPrefix names to their byte representation
|
|
17
17
|
*/
|
|
18
|
-
const HashPrefix: Record<string,
|
|
18
|
+
const HashPrefix: Record<string, Uint8Array> = {
|
|
19
19
|
transactionID: bytes(0x54584e00),
|
|
20
20
|
// transaction plus metadata
|
|
21
21
|
transaction: bytes(0x534e4400),
|
package/src/hashes.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { HashPrefix } from './hash-prefixes'
|
|
2
2
|
import { Hash256 } from './types'
|
|
3
3
|
import { BytesList } from './serdes/binary-serializer'
|
|
4
|
-
import { Buffer } from 'buffer/'
|
|
5
4
|
import { sha512 } from '@xrplf/isomorphic/sha512'
|
|
6
5
|
|
|
7
6
|
/**
|
|
@@ -17,7 +16,7 @@ class Sha512Half extends BytesList {
|
|
|
17
16
|
* @param bytes bytes to write to this.hash
|
|
18
17
|
* @returns the new Sha512Hash object
|
|
19
18
|
*/
|
|
20
|
-
static put(bytes:
|
|
19
|
+
static put(bytes: Uint8Array): Sha512Half {
|
|
21
20
|
return new Sha512Half().put(bytes)
|
|
22
21
|
}
|
|
23
22
|
|
|
@@ -27,7 +26,7 @@ class Sha512Half extends BytesList {
|
|
|
27
26
|
* @param bytes bytes to write to object
|
|
28
27
|
* @returns the Sha512 object
|
|
29
28
|
*/
|
|
30
|
-
put(bytes:
|
|
29
|
+
put(bytes: Uint8Array): Sha512Half {
|
|
31
30
|
this.hash.update(bytes)
|
|
32
31
|
return this
|
|
33
32
|
}
|
|
@@ -37,8 +36,8 @@ class Sha512Half extends BytesList {
|
|
|
37
36
|
*
|
|
38
37
|
* @returns half of a SHA512 hash
|
|
39
38
|
*/
|
|
40
|
-
finish256():
|
|
41
|
-
return
|
|
39
|
+
finish256(): Uint8Array {
|
|
40
|
+
return Uint8Array.from(this.hash.digest().slice(0, 32))
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
/**
|
|
@@ -57,7 +56,7 @@ class Sha512Half extends BytesList {
|
|
|
57
56
|
* @param args zero or more arguments to hash
|
|
58
57
|
* @returns the sha512half hash of the arguments.
|
|
59
58
|
*/
|
|
60
|
-
function sha512Half(...args:
|
|
59
|
+
function sha512Half(...args: Uint8Array[]): Uint8Array {
|
|
61
60
|
const hash = new Sha512Half()
|
|
62
61
|
args.forEach((a) => hash.put(a))
|
|
63
62
|
return hash.finish256()
|
|
@@ -69,7 +68,7 @@ function sha512Half(...args: Buffer[]): Buffer {
|
|
|
69
68
|
* @param serialized bytes to hash
|
|
70
69
|
* @returns a Hash256 object
|
|
71
70
|
*/
|
|
72
|
-
function transactionID(serialized:
|
|
71
|
+
function transactionID(serialized: Uint8Array): Hash256 {
|
|
73
72
|
return new Hash256(sha512Half(HashPrefix.transactionID, serialized))
|
|
74
73
|
}
|
|
75
74
|
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
} from './enums'
|
|
10
10
|
import { XrplDefinitions } from './enums/xrpl-definitions'
|
|
11
11
|
import { coreTypes } from './types'
|
|
12
|
+
import { bytesToHex } from '@xrplf/isomorphic/utils'
|
|
12
13
|
|
|
13
14
|
const {
|
|
14
15
|
signingData,
|
|
@@ -44,9 +45,7 @@ function encode(json: object, definitions?: XrplDefinitionsBase): string {
|
|
|
44
45
|
if (typeof json !== 'object') {
|
|
45
46
|
throw new Error()
|
|
46
47
|
}
|
|
47
|
-
return serializeObject(json as JsonObject, { definitions })
|
|
48
|
-
.toString('hex')
|
|
49
|
-
.toUpperCase()
|
|
48
|
+
return bytesToHex(serializeObject(json as JsonObject, { definitions }))
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
/**
|
|
@@ -64,11 +63,11 @@ function encodeForSigning(
|
|
|
64
63
|
if (typeof json !== 'object') {
|
|
65
64
|
throw new Error()
|
|
66
65
|
}
|
|
67
|
-
return
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
return bytesToHex(
|
|
67
|
+
signingData(json as JsonObject, HashPrefix.transactionSig, {
|
|
68
|
+
definitions,
|
|
69
|
+
}),
|
|
70
|
+
)
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
/**
|
|
@@ -83,9 +82,7 @@ function encodeForSigningClaim(json: object): string {
|
|
|
83
82
|
if (typeof json !== 'object') {
|
|
84
83
|
throw new Error()
|
|
85
84
|
}
|
|
86
|
-
return signingClaimData(json as ClaimObject)
|
|
87
|
-
.toString('hex')
|
|
88
|
-
.toUpperCase()
|
|
85
|
+
return bytesToHex(signingClaimData(json as ClaimObject))
|
|
89
86
|
}
|
|
90
87
|
|
|
91
88
|
/**
|
|
@@ -108,9 +105,9 @@ function encodeForMultisigning(
|
|
|
108
105
|
throw new Error()
|
|
109
106
|
}
|
|
110
107
|
const definitionsOpt = definitions ? { definitions } : undefined
|
|
111
|
-
return
|
|
112
|
-
|
|
113
|
-
|
|
108
|
+
return bytesToHex(
|
|
109
|
+
multiSigningData(json as JsonObject, signer, definitionsOpt),
|
|
110
|
+
)
|
|
114
111
|
}
|
|
115
112
|
|
|
116
113
|
/**
|
|
@@ -123,7 +120,7 @@ function encodeQuality(value: string): string {
|
|
|
123
120
|
if (typeof value !== 'string') {
|
|
124
121
|
throw new Error()
|
|
125
122
|
}
|
|
126
|
-
return quality.encode(value)
|
|
123
|
+
return bytesToHex(quality.encode(value))
|
|
127
124
|
}
|
|
128
125
|
|
|
129
126
|
/**
|
package/src/quality.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { coreTypes } from './types'
|
|
2
|
-
import { Buffer } from 'buffer/'
|
|
3
2
|
import BigNumber from 'bignumber.js'
|
|
3
|
+
import { bytesToHex, hexToBytes } from '@xrplf/isomorphic/utils'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* class for encoding and decoding quality
|
|
@@ -12,7 +12,7 @@ class quality {
|
|
|
12
12
|
* @param arg string representation of an amount
|
|
13
13
|
* @returns Serialized quality
|
|
14
14
|
*/
|
|
15
|
-
static encode(quality: string):
|
|
15
|
+
static encode(quality: string): Uint8Array {
|
|
16
16
|
const decimal = BigNumber(quality)
|
|
17
17
|
const exponent = (decimal?.e || 0) - 15
|
|
18
18
|
const qualityString = decimal.times(`1e${-exponent}`).abs().toString()
|
|
@@ -28,9 +28,9 @@ class quality {
|
|
|
28
28
|
* @returns deserialized quality
|
|
29
29
|
*/
|
|
30
30
|
static decode(quality: string): BigNumber {
|
|
31
|
-
const bytes =
|
|
31
|
+
const bytes = hexToBytes(quality).slice(-8)
|
|
32
32
|
const exponent = bytes[0] - 100
|
|
33
|
-
const mantissa = new BigNumber(`0x${bytes.slice(1)
|
|
33
|
+
const mantissa = new BigNumber(`0x${bytesToHex(bytes.slice(1))}`)
|
|
34
34
|
return mantissa.times(`1e${exponent}`)
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -4,13 +4,13 @@ import {
|
|
|
4
4
|
FieldInstance,
|
|
5
5
|
} from '../enums'
|
|
6
6
|
import { type SerializedType } from '../types/serialized-type'
|
|
7
|
-
import {
|
|
7
|
+
import { hexToBytes } from '@xrplf/isomorphic/utils'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* BinaryParser is used to compute fields and values from a HexString
|
|
11
11
|
*/
|
|
12
12
|
class BinaryParser {
|
|
13
|
-
private bytes:
|
|
13
|
+
private bytes: Uint8Array
|
|
14
14
|
definitions: XrplDefinitionsBase
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -24,7 +24,7 @@ class BinaryParser {
|
|
|
24
24
|
hexBytes: string,
|
|
25
25
|
definitions: XrplDefinitionsBase = DEFAULT_DEFINITIONS,
|
|
26
26
|
) {
|
|
27
|
-
this.bytes =
|
|
27
|
+
this.bytes = hexToBytes(hexBytes)
|
|
28
28
|
this.definitions = definitions
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -58,7 +58,7 @@ class BinaryParser {
|
|
|
58
58
|
* @param n The number of bytes to read
|
|
59
59
|
* @return The bytes
|
|
60
60
|
*/
|
|
61
|
-
read(n: number):
|
|
61
|
+
read(n: number): Uint8Array {
|
|
62
62
|
if (n > this.bytes.byteLength) {
|
|
63
63
|
throw new Error()
|
|
64
64
|
}
|
|
@@ -107,7 +107,7 @@ class BinaryParser {
|
|
|
107
107
|
*
|
|
108
108
|
* @return The variable length bytes
|
|
109
109
|
*/
|
|
110
|
-
readVariableLength():
|
|
110
|
+
readVariableLength(): Uint8Array {
|
|
111
111
|
return this.read(this.readVariableLengthLength())
|
|
112
112
|
}
|
|
113
113
|
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { FieldInstance } from '../enums'
|
|
2
2
|
import { type SerializedType } from '../types/serialized-type'
|
|
3
|
-
import {
|
|
3
|
+
import { bytesToHex, concat } from '@xrplf/isomorphic/utils'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Bytes list is a collection of
|
|
6
|
+
* Bytes list is a collection of Uint8Array objects
|
|
7
7
|
*/
|
|
8
8
|
class BytesList {
|
|
9
|
-
private bytesArray: Array<
|
|
9
|
+
private bytesArray: Array<Uint8Array> = []
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Get the total number of bytes in the BytesList
|
|
@@ -14,17 +14,17 @@ class BytesList {
|
|
|
14
14
|
* @return the number of bytes
|
|
15
15
|
*/
|
|
16
16
|
public getLength(): number {
|
|
17
|
-
return
|
|
17
|
+
return concat(this.bytesArray).byteLength
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Put bytes in the BytesList
|
|
22
22
|
*
|
|
23
|
-
* @param bytesArg A
|
|
23
|
+
* @param bytesArg A Uint8Array
|
|
24
24
|
* @return this BytesList
|
|
25
25
|
*/
|
|
26
|
-
public put(bytesArg:
|
|
27
|
-
const bytes =
|
|
26
|
+
public put(bytesArg: Uint8Array): BytesList {
|
|
27
|
+
const bytes = Uint8Array.from(bytesArg) // Temporary, to catch instances of Uint8Array being passed in
|
|
28
28
|
this.bytesArray.push(bytes)
|
|
29
29
|
return this
|
|
30
30
|
}
|
|
@@ -38,17 +38,17 @@ class BytesList {
|
|
|
38
38
|
list.put(this.toBytes())
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
public toBytes():
|
|
42
|
-
return
|
|
41
|
+
public toBytes(): Uint8Array {
|
|
42
|
+
return concat(this.bytesArray)
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
toHex(): string {
|
|
46
|
-
return this.toBytes()
|
|
46
|
+
return bytesToHex(this.toBytes())
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
|
-
* BinarySerializer is used to write fields and values to
|
|
51
|
+
* BinarySerializer is used to write fields and values to Uint8Arrays
|
|
52
52
|
*/
|
|
53
53
|
class BinarySerializer {
|
|
54
54
|
private sink: BytesList = new BytesList()
|
|
@@ -71,7 +71,7 @@ class BinarySerializer {
|
|
|
71
71
|
*
|
|
72
72
|
* @param bytes the bytes to write
|
|
73
73
|
*/
|
|
74
|
-
put(bytes:
|
|
74
|
+
put(bytes: Uint8Array): void {
|
|
75
75
|
this.sink.put(bytes)
|
|
76
76
|
}
|
|
77
77
|
|
|
@@ -99,8 +99,8 @@ class BinarySerializer {
|
|
|
99
99
|
*
|
|
100
100
|
* @param length the length of the bytes
|
|
101
101
|
*/
|
|
102
|
-
private encodeVariableLength(length: number):
|
|
103
|
-
const lenBytes =
|
|
102
|
+
private encodeVariableLength(length: number): Uint8Array {
|
|
103
|
+
const lenBytes = new Uint8Array(3)
|
|
104
104
|
if (length <= 192) {
|
|
105
105
|
lenBytes[0] = length
|
|
106
106
|
return lenBytes.slice(0, 1)
|
package/src/shamap.ts
CHANGED
|
@@ -3,13 +3,12 @@ import { HashPrefix } from './hash-prefixes'
|
|
|
3
3
|
import { Sha512Half } from './hashes'
|
|
4
4
|
import { Hash256 } from './types/hash-256'
|
|
5
5
|
import { BytesList } from './serdes/binary-serializer'
|
|
6
|
-
import { Buffer } from 'buffer/'
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Abstract class describing a SHAMapNode
|
|
10
9
|
*/
|
|
11
10
|
abstract class ShaMapNode {
|
|
12
|
-
abstract hashPrefix():
|
|
11
|
+
abstract hashPrefix(): Uint8Array
|
|
13
12
|
abstract isLeaf(): boolean
|
|
14
13
|
abstract isInner(): boolean
|
|
15
14
|
abstract toBytesSink(list: BytesList): void
|
|
@@ -41,10 +40,10 @@ class ShaMapLeaf extends ShaMapNode {
|
|
|
41
40
|
/**
|
|
42
41
|
* Get the prefix of the this.item
|
|
43
42
|
*
|
|
44
|
-
* @returns The hash prefix, unless this.item is undefined, then it returns an empty
|
|
43
|
+
* @returns The hash prefix, unless this.item is undefined, then it returns an empty Uint8Array
|
|
45
44
|
*/
|
|
46
|
-
hashPrefix():
|
|
47
|
-
return this.item === undefined ?
|
|
45
|
+
hashPrefix(): Uint8Array {
|
|
46
|
+
return this.item === undefined ? new Uint8Array(0) : this.item.hashPrefix()
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
/**
|
|
@@ -100,7 +99,7 @@ class ShaMapInner extends ShaMapNode {
|
|
|
100
99
|
*
|
|
101
100
|
* @returns hash prefix describing an inner node
|
|
102
101
|
*/
|
|
103
|
-
hashPrefix():
|
|
102
|
+
hashPrefix(): Uint8Array {
|
|
104
103
|
return HashPrefix.innerNode
|
|
105
104
|
}
|
|
106
105
|
|
package/src/types/account-id.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
xAddressToClassicAddress,
|
|
6
6
|
} from 'ripple-address-codec'
|
|
7
7
|
import { Hash160 } from './hash-160'
|
|
8
|
-
import {
|
|
8
|
+
import { hexToBytes } from '@xrplf/isomorphic/utils'
|
|
9
9
|
|
|
10
10
|
const HEX_REGEX = /^[A-F0-9]{40}$/
|
|
11
11
|
|
|
@@ -13,9 +13,11 @@ const HEX_REGEX = /^[A-F0-9]{40}$/
|
|
|
13
13
|
* Class defining how to encode and decode an AccountID
|
|
14
14
|
*/
|
|
15
15
|
class AccountID extends Hash160 {
|
|
16
|
-
static readonly defaultAccountID: AccountID = new AccountID(
|
|
16
|
+
static readonly defaultAccountID: AccountID = new AccountID(
|
|
17
|
+
new Uint8Array(20),
|
|
18
|
+
)
|
|
17
19
|
|
|
18
|
-
constructor(bytes?:
|
|
20
|
+
constructor(bytes?: Uint8Array) {
|
|
19
21
|
super(bytes ?? AccountID.defaultAccountID.bytes)
|
|
20
22
|
}
|
|
21
23
|
|
|
@@ -36,7 +38,7 @@ class AccountID extends Hash160 {
|
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
return HEX_REGEX.test(value)
|
|
39
|
-
? new AccountID(
|
|
41
|
+
? new AccountID(hexToBytes(value))
|
|
40
42
|
: this.fromBase58(value)
|
|
41
43
|
}
|
|
42
44
|
|
|
@@ -59,7 +61,7 @@ class AccountID extends Hash160 {
|
|
|
59
61
|
value = classic.classicAddress
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
return new AccountID(
|
|
64
|
+
return new AccountID(Uint8Array.from(decodeAccountID(value)))
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
/**
|
|
@@ -77,9 +79,7 @@ class AccountID extends Hash160 {
|
|
|
77
79
|
* @returns the base58 string defined by this.bytes
|
|
78
80
|
*/
|
|
79
81
|
toBase58(): string {
|
|
80
|
-
|
|
81
|
-
return encodeAccountID(this.bytes as any)
|
|
82
|
-
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
82
|
+
return encodeAccountID(this.bytes)
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
|
package/src/types/amount.ts
CHANGED
|
@@ -3,8 +3,9 @@ import { BinaryParser } from '../serdes/binary-parser'
|
|
|
3
3
|
import { AccountID } from './account-id'
|
|
4
4
|
import { Currency } from './currency'
|
|
5
5
|
import { JsonObject, SerializedType } from './serialized-type'
|
|
6
|
-
import { Buffer } from 'buffer/'
|
|
7
6
|
import BigNumber from 'bignumber.js'
|
|
7
|
+
import { bytesToHex, concat, hexToBytes } from '@xrplf/isomorphic/utils'
|
|
8
|
+
import { readUInt32BE, writeUInt32BE } from '../utils'
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Constants for validating amounts
|
|
@@ -52,11 +53,9 @@ function isAmountObject(arg): arg is AmountObject {
|
|
|
52
53
|
* Class for serializing/Deserializing Amounts
|
|
53
54
|
*/
|
|
54
55
|
class Amount extends SerializedType {
|
|
55
|
-
static defaultAmount: Amount = new Amount(
|
|
56
|
-
Buffer.from('4000000000000000', 'hex'),
|
|
57
|
-
)
|
|
56
|
+
static defaultAmount: Amount = new Amount(hexToBytes('4000000000000000'))
|
|
58
57
|
|
|
59
|
-
constructor(bytes:
|
|
58
|
+
constructor(bytes: Uint8Array) {
|
|
60
59
|
super(bytes ?? Amount.defaultAmount.bytes)
|
|
61
60
|
}
|
|
62
61
|
|
|
@@ -72,17 +71,17 @@ class Amount extends SerializedType {
|
|
|
72
71
|
return value
|
|
73
72
|
}
|
|
74
73
|
|
|
75
|
-
let amount =
|
|
74
|
+
let amount = new Uint8Array(8)
|
|
76
75
|
if (typeof value === 'string') {
|
|
77
76
|
Amount.assertXrpIsValid(value)
|
|
78
77
|
|
|
79
78
|
const number = BigInt(value)
|
|
80
79
|
|
|
81
|
-
const intBuf = [
|
|
82
|
-
intBuf[0]
|
|
83
|
-
intBuf[1]
|
|
80
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)]
|
|
81
|
+
writeUInt32BE(intBuf[0], Number(number >> BigInt(32)), 0)
|
|
82
|
+
writeUInt32BE(intBuf[1], Number(number & BigInt(mask)), 0)
|
|
84
83
|
|
|
85
|
-
amount =
|
|
84
|
+
amount = concat(intBuf)
|
|
86
85
|
|
|
87
86
|
amount[0] |= 0x40
|
|
88
87
|
|
|
@@ -102,11 +101,11 @@ class Amount extends SerializedType {
|
|
|
102
101
|
.toString()
|
|
103
102
|
|
|
104
103
|
const num = BigInt(integerNumberString)
|
|
105
|
-
const intBuf = [
|
|
106
|
-
intBuf[0]
|
|
107
|
-
intBuf[1]
|
|
104
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)]
|
|
105
|
+
writeUInt32BE(intBuf[0], Number(num >> BigInt(32)), 0)
|
|
106
|
+
writeUInt32BE(intBuf[1], Number(num & BigInt(mask)), 0)
|
|
108
107
|
|
|
109
|
-
amount =
|
|
108
|
+
amount = concat(intBuf)
|
|
110
109
|
|
|
111
110
|
amount[0] |= 0x80
|
|
112
111
|
|
|
@@ -122,7 +121,7 @@ class Amount extends SerializedType {
|
|
|
122
121
|
|
|
123
122
|
const currency = Currency.from(value.currency).toBytes()
|
|
124
123
|
const issuer = AccountID.from(value.issuer).toBytes()
|
|
125
|
-
return new Amount(
|
|
124
|
+
return new Amount(concat([amount, currency, issuer]))
|
|
126
125
|
}
|
|
127
126
|
|
|
128
127
|
throw new Error('Invalid type to construct an Amount')
|
|
@@ -152,8 +151,8 @@ class Amount extends SerializedType {
|
|
|
152
151
|
const sign = isPositive ? '' : '-'
|
|
153
152
|
bytes[0] &= 0x3f
|
|
154
153
|
|
|
155
|
-
const msb = BigInt(bytes.slice(0, 4)
|
|
156
|
-
const lsb = BigInt(bytes.slice(4)
|
|
154
|
+
const msb = BigInt(readUInt32BE(bytes.slice(0, 4), 0))
|
|
155
|
+
const lsb = BigInt(readUInt32BE(bytes.slice(4), 0))
|
|
157
156
|
const num = (msb << BigInt(32)) | lsb
|
|
158
157
|
|
|
159
158
|
return `${sign}${num.toString()}`
|
|
@@ -172,7 +171,7 @@ class Amount extends SerializedType {
|
|
|
172
171
|
|
|
173
172
|
mantissa[0] = 0
|
|
174
173
|
mantissa[1] &= 0x3f
|
|
175
|
-
const value = new BigNumber(`${sign}0x${mantissa
|
|
174
|
+
const value = new BigNumber(`${sign}0x${bytesToHex(mantissa)}`).times(
|
|
176
175
|
`1e${exponent}`,
|
|
177
176
|
)
|
|
178
177
|
Amount.assertIouIsValid(value)
|
package/src/types/blob.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { SerializedType } from './serialized-type'
|
|
2
2
|
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
-
import {
|
|
3
|
+
import { hexToBytes } from '@xrplf/isomorphic/utils'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Variable length encoded type
|
|
7
7
|
*/
|
|
8
8
|
class Blob extends SerializedType {
|
|
9
|
-
constructor(bytes:
|
|
9
|
+
constructor(bytes: Uint8Array) {
|
|
10
10
|
super(bytes)
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -33,7 +33,7 @@ class Blob extends SerializedType {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
if (typeof value === 'string') {
|
|
36
|
-
return new Blob(
|
|
36
|
+
return new Blob(hexToBytes(value))
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
throw new Error('Cannot construct Blob from value given')
|
package/src/types/currency.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Hash160 } from './hash-160'
|
|
2
|
-
import {
|
|
2
|
+
import { bytesToHex, hexToBytes, hexToString } from '@xrplf/isomorphic/utils'
|
|
3
3
|
|
|
4
4
|
const XRP_HEX_REGEX = /^0{40}$/
|
|
5
5
|
const ISO_REGEX = /^[A-Z0-9a-z?!@#$%^&*(){}[\]|]{3}$/
|
|
@@ -10,8 +10,8 @@ const STANDARD_FORMAT_HEX_REGEX = /^0{24}[\x00-\x7F]{6}0{10}$/
|
|
|
10
10
|
/**
|
|
11
11
|
* Convert an ISO code to a currency bytes representation
|
|
12
12
|
*/
|
|
13
|
-
function isoToBytes(iso: string):
|
|
14
|
-
const bytes =
|
|
13
|
+
function isoToBytes(iso: string): Uint8Array {
|
|
14
|
+
const bytes = new Uint8Array(20)
|
|
15
15
|
if (iso !== 'XRP') {
|
|
16
16
|
const isoBytes = iso.split('').map((c) => c.charCodeAt(0))
|
|
17
17
|
bytes.set(isoBytes, 12)
|
|
@@ -26,8 +26,8 @@ function isIsoCode(iso: string): boolean {
|
|
|
26
26
|
return ISO_REGEX.test(iso)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
function isoCodeFromHex(code:
|
|
30
|
-
const iso = code
|
|
29
|
+
function isoCodeFromHex(code: Uint8Array): string | null {
|
|
30
|
+
const iso = hexToString(bytesToHex(code))
|
|
31
31
|
if (iso === 'XRP') {
|
|
32
32
|
return null
|
|
33
33
|
}
|
|
@@ -52,41 +52,41 @@ function isStringRepresentation(input: string): boolean {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
|
-
* Tests if a
|
|
55
|
+
* Tests if a Uint8Array is a valid representation of a currency
|
|
56
56
|
*/
|
|
57
|
-
function isBytesArray(bytes:
|
|
57
|
+
function isBytesArray(bytes: Uint8Array): boolean {
|
|
58
58
|
return bytes.byteLength === 20
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
62
|
* Ensures that a value is a valid representation of a currency
|
|
63
63
|
*/
|
|
64
|
-
function isValidRepresentation(input:
|
|
65
|
-
return input instanceof
|
|
64
|
+
function isValidRepresentation(input: Uint8Array | string): boolean {
|
|
65
|
+
return input instanceof Uint8Array
|
|
66
66
|
? isBytesArray(input)
|
|
67
67
|
: isStringRepresentation(input)
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
/**
|
|
71
|
-
* Generate bytes from a string or
|
|
71
|
+
* Generate bytes from a string or UInt8Array representation of a currency
|
|
72
72
|
*/
|
|
73
|
-
function bytesFromRepresentation(input: string):
|
|
73
|
+
function bytesFromRepresentation(input: string): Uint8Array {
|
|
74
74
|
if (!isValidRepresentation(input)) {
|
|
75
75
|
throw new Error(`Unsupported Currency representation: ${input}`)
|
|
76
76
|
}
|
|
77
|
-
return input.length === 3 ? isoToBytes(input) :
|
|
77
|
+
return input.length === 3 ? isoToBytes(input) : hexToBytes(input)
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/**
|
|
81
81
|
* Class defining how to encode and decode Currencies
|
|
82
82
|
*/
|
|
83
83
|
class Currency extends Hash160 {
|
|
84
|
-
static readonly XRP = new Currency(
|
|
84
|
+
static readonly XRP = new Currency(new Uint8Array(20))
|
|
85
85
|
private readonly _iso: string | null
|
|
86
86
|
|
|
87
|
-
constructor(byteBuf:
|
|
87
|
+
constructor(byteBuf: Uint8Array) {
|
|
88
88
|
super(byteBuf ?? Currency.XRP.bytes)
|
|
89
|
-
const hex = this.bytes
|
|
89
|
+
const hex = bytesToHex(this.bytes)
|
|
90
90
|
|
|
91
91
|
if (XRP_HEX_REGEX.test(hex)) {
|
|
92
92
|
this._iso = 'XRP'
|
|
@@ -133,7 +133,7 @@ class Currency extends Hash160 {
|
|
|
133
133
|
if (iso !== null) {
|
|
134
134
|
return iso
|
|
135
135
|
}
|
|
136
|
-
return this.bytes
|
|
136
|
+
return bytesToHex(this.bytes)
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
|
package/src/types/hash-128.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { Hash } from './hash'
|
|
2
|
-
import {
|
|
2
|
+
import { bytesToHex } from '@xrplf/isomorphic/utils'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Hash with a width of 128 bits
|
|
6
6
|
*/
|
|
7
7
|
class Hash128 extends Hash {
|
|
8
8
|
static readonly width = 16
|
|
9
|
-
static readonly ZERO_128: Hash128 = new Hash128(
|
|
9
|
+
static readonly ZERO_128: Hash128 = new Hash128(new Uint8Array(Hash128.width))
|
|
10
10
|
|
|
11
|
-
constructor(bytes:
|
|
11
|
+
constructor(bytes: Uint8Array) {
|
|
12
12
|
if (bytes && bytes.byteLength === 0) {
|
|
13
13
|
bytes = Hash128.ZERO_128.bytes
|
|
14
14
|
}
|
|
@@ -22,7 +22,7 @@ class Hash128 extends Hash {
|
|
|
22
22
|
* @returns hex String of this.bytes
|
|
23
23
|
*/
|
|
24
24
|
toHex(): string {
|
|
25
|
-
const hex = this.toBytes()
|
|
25
|
+
const hex = bytesToHex(this.toBytes())
|
|
26
26
|
if (/^0+$/.exec(hex)) {
|
|
27
27
|
return ''
|
|
28
28
|
}
|
package/src/types/hash-160.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { Hash } from './hash'
|
|
2
|
-
import { Buffer } from 'buffer/'
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Hash with a width of 160 bits
|
|
6
5
|
*/
|
|
7
6
|
class Hash160 extends Hash {
|
|
8
7
|
static readonly width = 20
|
|
9
|
-
static readonly ZERO_160: Hash160 = new Hash160(
|
|
8
|
+
static readonly ZERO_160: Hash160 = new Hash160(new Uint8Array(Hash160.width))
|
|
10
9
|
|
|
11
|
-
constructor(bytes?:
|
|
10
|
+
constructor(bytes?: Uint8Array) {
|
|
12
11
|
if (bytes && bytes.byteLength === 0) {
|
|
13
12
|
bytes = Hash160.ZERO_160.bytes
|
|
14
13
|
}
|
package/src/types/hash-256.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { Hash } from './hash'
|
|
2
|
-
import { Buffer } from 'buffer/'
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Hash with a width of 256 bits
|
|
6
5
|
*/
|
|
7
6
|
class Hash256 extends Hash {
|
|
8
7
|
static readonly width = 32
|
|
9
|
-
static readonly ZERO_256 = new Hash256(
|
|
8
|
+
static readonly ZERO_256 = new Hash256(new Uint8Array(Hash256.width))
|
|
10
9
|
|
|
11
|
-
constructor(bytes:
|
|
10
|
+
constructor(bytes: Uint8Array) {
|
|
12
11
|
super(bytes ?? Hash256.ZERO_256.bytes)
|
|
13
12
|
}
|
|
14
13
|
}
|