ripple-binary-codec 1.11.0 → 2.0.0-beta.1

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 (247) hide show
  1. package/dist/binary.d.ts +12 -13
  2. package/dist/binary.js +7 -7
  3. package/dist/binary.js.map +1 -1
  4. package/dist/enums/bytes.d.ts +1 -2
  5. package/dist/enums/bytes.js +1 -2
  6. package/dist/enums/bytes.js.map +1 -1
  7. package/dist/enums/field.d.ts +1 -2
  8. package/dist/enums/field.js +1 -2
  9. package/dist/enums/field.js.map +1 -1
  10. package/dist/enums/index.js +4 -24
  11. package/dist/enums/index.js.map +1 -1
  12. package/dist/hash-prefixes.d.ts +1 -2
  13. package/dist/hash-prefixes.js +6 -6
  14. package/dist/hash-prefixes.js.map +1 -1
  15. package/dist/hashes.d.ts +6 -7
  16. package/dist/hashes.js +6 -7
  17. package/dist/hashes.js.map +1 -1
  18. package/dist/index.js +31 -46
  19. package/dist/index.js.map +1 -1
  20. package/dist/ledger-hashes.d.ts +1 -2
  21. package/dist/ledger-hashes.js +11 -30
  22. package/dist/ledger-hashes.js.map +1 -1
  23. package/dist/quality.d.ts +3 -4
  24. package/dist/quality.js +10 -8
  25. package/dist/quality.js.map +1 -1
  26. package/dist/serdes/binary-parser.d.ts +2 -3
  27. package/dist/serdes/binary-parser.js +14 -30
  28. package/dist/serdes/binary-parser.js.map +1 -1
  29. package/dist/serdes/binary-serializer.d.ts +6 -7
  30. package/dist/serdes/binary-serializer.js +12 -35
  31. package/dist/serdes/binary-serializer.js.map +1 -1
  32. package/dist/shamap.d.ts +4 -5
  33. package/dist/shamap.js +5 -5
  34. package/dist/shamap.js.map +1 -1
  35. package/dist/src/binary.d.ts +90 -0
  36. package/dist/src/binary.js +128 -0
  37. package/dist/src/binary.js.map +1 -0
  38. package/dist/src/coretypes.d.ts +9 -0
  39. package/dist/src/coretypes.js +48 -0
  40. package/dist/src/coretypes.js.map +1 -0
  41. package/dist/src/enums/bytes.d.ts +25 -0
  42. package/dist/src/enums/bytes.js +63 -0
  43. package/dist/src/enums/bytes.js.map +1 -0
  44. package/dist/src/enums/constants.d.ts +4 -0
  45. package/dist/src/enums/constants.js +8 -0
  46. package/dist/src/enums/constants.js.map +1 -0
  47. package/dist/src/enums/definitions.json +2866 -0
  48. package/dist/src/enums/field.d.ts +28 -0
  49. package/dist/src/enums/field.js +58 -0
  50. package/dist/src/enums/field.js.map +1 -0
  51. package/dist/src/enums/index.d.ts +12 -0
  52. package/dist/src/enums/index.js +31 -0
  53. package/dist/src/enums/index.js.map +1 -0
  54. package/dist/src/enums/utils-renumber.d.ts +101 -0
  55. package/dist/src/enums/utils-renumber.js +127 -0
  56. package/dist/src/enums/utils-renumber.js.map +1 -0
  57. package/dist/src/enums/xrpl-definitions-base.d.ts +44 -0
  58. package/dist/src/enums/xrpl-definitions-base.js +59 -0
  59. package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
  60. package/dist/src/enums/xrpl-definitions.d.ts +21 -0
  61. package/dist/src/enums/xrpl-definitions.js +29 -0
  62. package/dist/src/enums/xrpl-definitions.js.map +1 -0
  63. package/dist/src/hash-prefixes.d.ts +5 -0
  64. package/dist/src/hash-prefixes.js +41 -0
  65. package/dist/src/hash-prefixes.js.map +1 -0
  66. package/dist/src/hashes.d.ts +50 -0
  67. package/dist/src/hashes.js +76 -0
  68. package/dist/src/hashes.js.map +1 -0
  69. package/dist/src/index.d.ts +64 -0
  70. package/dist/src/index.js +123 -0
  71. package/dist/src/index.js.map +1 -0
  72. package/dist/src/ledger-hashes.d.ts +48 -0
  73. package/dist/src/ledger-hashes.js +141 -0
  74. package/dist/src/ledger-hashes.js.map +1 -0
  75. package/dist/src/quality.d.ts +21 -0
  76. package/dist/src/quality.js +42 -0
  77. package/dist/src/quality.js.map +1 -0
  78. package/dist/src/serdes/binary-parser.d.ts +100 -0
  79. package/dist/src/serdes/binary-parser.js +194 -0
  80. package/dist/src/serdes/binary-parser.js.map +1 -0
  81. package/dist/src/serdes/binary-serializer.d.ts +81 -0
  82. package/dist/src/serdes/binary-serializer.js +149 -0
  83. package/dist/src/serdes/binary-serializer.js.map +1 -0
  84. package/dist/src/shamap.d.ts +102 -0
  85. package/dist/src/shamap.js +172 -0
  86. package/dist/src/shamap.js.map +1 -0
  87. package/dist/src/types/account-id.d.ts +35 -0
  88. package/dist/src/types/account-id.js +69 -0
  89. package/dist/src/types/account-id.js.map +1 -0
  90. package/dist/src/types/amount.d.ts +67 -0
  91. package/dist/src/types/amount.js +212 -0
  92. package/dist/src/types/amount.js.map +1 -0
  93. package/dist/src/types/blob.d.ts +24 -0
  94. package/dist/src/types/blob.js +40 -0
  95. package/dist/src/types/blob.js.map +1 -0
  96. package/dist/src/types/currency.d.ts +28 -0
  97. package/dist/src/types/currency.js +127 -0
  98. package/dist/src/types/currency.js.map +1 -0
  99. package/dist/src/types/hash-128.d.ts +16 -0
  100. package/dist/src/types/hash-128.js +32 -0
  101. package/dist/src/types/hash-128.js.map +1 -0
  102. package/dist/src/types/hash-160.d.ts +10 -0
  103. package/dist/src/types/hash-160.js +19 -0
  104. package/dist/src/types/hash-160.js.map +1 -0
  105. package/dist/src/types/hash-256.d.ts +10 -0
  106. package/dist/src/types/hash-256.js +16 -0
  107. package/dist/src/types/hash-256.js.map +1 -0
  108. package/dist/src/types/hash.d.ts +40 -0
  109. package/dist/src/types/hash.js +73 -0
  110. package/dist/src/types/hash.js.map +1 -0
  111. package/dist/src/types/index.d.ts +18 -0
  112. package/dist/src/types/index.js +61 -0
  113. package/dist/src/types/index.js.map +1 -0
  114. package/dist/src/types/issue.d.ts +38 -0
  115. package/dist/src/types/issue.js +81 -0
  116. package/dist/src/types/issue.js.map +1 -0
  117. package/dist/src/types/path-set.d.ts +36 -0
  118. package/dist/src/types/path-set.js +233 -0
  119. package/dist/src/types/path-set.js.map +1 -0
  120. package/dist/src/types/serialized-type.d.ts +70 -0
  121. package/dist/src/types/serialized-type.js +105 -0
  122. package/dist/src/types/serialized-type.js.map +1 -0
  123. package/dist/src/types/st-array.d.ts +31 -0
  124. package/dist/src/types/st-array.js +83 -0
  125. package/dist/src/types/st-array.js.map +1 -0
  126. package/dist/src/types/st-object.d.ts +32 -0
  127. package/dist/src/types/st-object.js +155 -0
  128. package/dist/src/types/st-object.js.map +1 -0
  129. package/dist/src/types/uint-16.d.ts +24 -0
  130. package/dist/src/types/uint-16.js +45 -0
  131. package/dist/src/types/uint-16.js.map +1 -0
  132. package/dist/src/types/uint-32.d.ts +24 -0
  133. package/dist/src/types/uint-32.js +50 -0
  134. package/dist/src/types/uint-32.js.map +1 -0
  135. package/dist/src/types/uint-64.d.ts +37 -0
  136. package/dist/src/types/uint-64.js +86 -0
  137. package/dist/src/types/uint-64.js.map +1 -0
  138. package/dist/src/types/uint-8.d.ts +24 -0
  139. package/dist/src/types/uint-8.js +46 -0
  140. package/dist/src/types/uint-8.js.map +1 -0
  141. package/dist/src/types/uint.d.ts +29 -0
  142. package/dist/src/types/uint.js +47 -0
  143. package/dist/src/types/uint.js.map +1 -0
  144. package/dist/src/types/vector-256.d.ts +30 -0
  145. package/dist/src/types/vector-256.js +73 -0
  146. package/dist/src/types/vector-256.js.map +1 -0
  147. package/dist/src/types/xchain-bridge.d.ts +44 -0
  148. package/dist/src/types/xchain-bridge.js +102 -0
  149. package/dist/src/types/xchain-bridge.js.map +1 -0
  150. package/dist/src/utils.d.ts +47 -0
  151. package/dist/src/utils.js +137 -0
  152. package/dist/src/utils.js.map +1 -0
  153. package/dist/tsconfig.tsbuildinfo +1 -0
  154. package/dist/types/account-id.d.ts +1 -2
  155. package/dist/types/account-id.js +4 -6
  156. package/dist/types/account-id.js.map +1 -1
  157. package/dist/types/amount.d.ts +2 -3
  158. package/dist/types/amount.js +40 -35
  159. package/dist/types/amount.js.map +1 -1
  160. package/dist/types/blob.d.ts +1 -2
  161. package/dist/types/blob.js +2 -2
  162. package/dist/types/blob.js.map +1 -1
  163. package/dist/types/currency.d.ts +1 -2
  164. package/dist/types/currency.js +10 -10
  165. package/dist/types/currency.js.map +1 -1
  166. package/dist/types/hash-128.d.ts +1 -2
  167. package/dist/types/hash-128.js +3 -3
  168. package/dist/types/hash-128.js.map +1 -1
  169. package/dist/types/hash-160.d.ts +1 -2
  170. package/dist/types/hash-160.js +1 -2
  171. package/dist/types/hash-160.js.map +1 -1
  172. package/dist/types/hash-256.d.ts +1 -2
  173. package/dist/types/hash-256.js +1 -2
  174. package/dist/types/hash-256.js.map +1 -1
  175. package/dist/types/hash.d.ts +2 -3
  176. package/dist/types/hash.js +5 -4
  177. package/dist/types/hash.js.map +1 -1
  178. package/dist/types/issue.d.ts +1 -2
  179. package/dist/types/issue.js +4 -4
  180. package/dist/types/issue.js.map +1 -1
  181. package/dist/types/path-set.js +12 -12
  182. package/dist/types/path-set.js.map +1 -1
  183. package/dist/types/serialized-type.d.ts +18 -15
  184. package/dist/types/serialized-type.js +11 -6
  185. package/dist/types/serialized-type.js.map +1 -1
  186. package/dist/types/st-array.js +5 -5
  187. package/dist/types/st-array.js.map +1 -1
  188. package/dist/types/st-object.js +1 -2
  189. package/dist/types/st-object.js.map +1 -1
  190. package/dist/types/uint-16.d.ts +1 -2
  191. package/dist/types/uint-16.js +6 -5
  192. package/dist/types/uint-16.js.map +1 -1
  193. package/dist/types/uint-32.d.ts +1 -2
  194. package/dist/types/uint-32.js +7 -6
  195. package/dist/types/uint-32.js.map +1 -1
  196. package/dist/types/uint-64.d.ts +4 -6
  197. package/dist/types/uint-64.js +20 -21
  198. package/dist/types/uint-64.js.map +1 -1
  199. package/dist/types/uint-8.d.ts +1 -2
  200. package/dist/types/uint-8.js +7 -5
  201. package/dist/types/uint-8.js.map +1 -1
  202. package/dist/types/uint.d.ts +5 -6
  203. package/dist/types/uint.js +5 -0
  204. package/dist/types/uint.js.map +1 -1
  205. package/dist/types/vector-256.d.ts +1 -2
  206. package/dist/types/vector-256.js +2 -4
  207. package/dist/types/vector-256.js.map +1 -1
  208. package/dist/types/xchain-bridge.d.ts +1 -2
  209. package/dist/types/xchain-bridge.js +10 -10
  210. package/dist/types/xchain-bridge.js.map +1 -1
  211. package/dist/utils.d.ts +47 -0
  212. package/dist/utils.js +137 -0
  213. package/dist/utils.js.map +1 -0
  214. package/package.json +10 -12
  215. package/src/binary.ts +22 -16
  216. package/src/enums/bytes.ts +2 -3
  217. package/src/enums/field.ts +3 -4
  218. package/src/enums/index.ts +1 -1
  219. package/src/hash-prefixes.ts +8 -8
  220. package/src/hashes.ts +9 -10
  221. package/src/index.ts +36 -24
  222. package/src/ledger-hashes.ts +14 -10
  223. package/src/quality.ts +9 -10
  224. package/src/serdes/binary-parser.ts +17 -10
  225. package/src/serdes/binary-serializer.ts +17 -17
  226. package/src/shamap.ts +8 -8
  227. package/src/types/account-id.ts +8 -8
  228. package/src/types/amount.ts +40 -41
  229. package/src/types/blob.ts +3 -3
  230. package/src/types/currency.ts +16 -16
  231. package/src/types/hash-128.ts +4 -4
  232. package/src/types/hash-160.ts +2 -3
  233. package/src/types/hash-256.ts +2 -3
  234. package/src/types/hash.ts +8 -6
  235. package/src/types/issue.ts +5 -5
  236. package/src/types/path-set.ts +16 -16
  237. package/src/types/serialized-type.ts +21 -19
  238. package/src/types/st-array.ts +7 -7
  239. package/src/types/st-object.ts +2 -2
  240. package/src/types/uint-16.ts +10 -6
  241. package/src/types/uint-32.ts +10 -7
  242. package/src/types/uint-64.ts +26 -27
  243. package/src/types/uint-8.ts +9 -6
  244. package/src/types/uint.ts +13 -10
  245. package/src/types/vector-256.ts +3 -8
  246. package/src/types/xchain-bridge.ts +13 -13
  247. package/src/utils.ts +152 -0
@@ -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 { Buffer } from 'buffer/'
5
+ import { concat } from '@xrplf/isomorphic/utils'
6
6
 
7
- const ARRAY_END_MARKER = Buffer.from([0xf1])
7
+ const ARRAY_END_MARKER = Uint8Array.from([0xf1])
8
8
  const ARRAY_END_MARKER_NAME = 'ArrayEndMarker'
9
9
 
10
- const OBJECT_END_MARKER = Buffer.from([0xe1])
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<Buffer> = []
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(Buffer.concat(bytes))
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<Buffer> = []
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(Buffer.concat(bytes))
73
+ return new STArray(concat(bytes))
74
74
  }
75
75
 
76
76
  throw new Error('Cannot construct STArray from value given')
@@ -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
- import { Buffer } from 'buffer/'
11
+
12
12
  import { STArray } from './st-array'
13
13
 
14
- const OBJECT_END_MARKER_BYTE = Buffer.from([0xe1])
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'
@@ -1,15 +1,17 @@
1
1
  import { UInt } from './uint'
2
2
  import { BinaryParser } from '../serdes/binary-parser'
3
- import { Buffer } from 'buffer/'
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(Buffer.alloc(UInt16.width))
10
+ static readonly defaultUInt16: UInt16 = new UInt16(
11
+ new Uint8Array(UInt16.width),
12
+ )
11
13
 
12
- constructor(bytes: Buffer) {
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
- const buf = Buffer.alloc(UInt16.width)
32
- buf.writeUInt16BE(val, 0)
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.readUInt16BE(0)
49
+ return parseInt(readUInt16BE(this.bytes, 0))
46
50
  }
47
51
  }
48
52
 
@@ -1,15 +1,17 @@
1
1
  import { UInt } from './uint'
2
2
  import { BinaryParser } from '../serdes/binary-parser'
3
- import { Buffer } from 'buffer/'
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(Buffer.alloc(UInt32.width))
10
+ static readonly defaultUInt32: UInt32 = new UInt32(
11
+ new Uint8Array(UInt32.width),
12
+ )
11
13
 
12
- constructor(bytes: Buffer) {
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 = Buffer.alloc(UInt32.width)
32
+ const buf = new Uint8Array(UInt32.width)
31
33
 
32
34
  if (typeof val === 'string') {
33
35
  const num = Number.parseInt(val)
34
- buf.writeUInt32BE(num, 0)
36
+ writeUInt32BE(buf, num, 0)
35
37
  return new UInt32(buf)
36
38
  }
37
39
 
38
40
  if (typeof val === 'number') {
39
- buf.writeUInt32BE(val, 0)
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.readUInt32BE(0)
55
+ return parseInt(readUInt32BE(this.bytes, 0), 10)
53
56
  }
54
57
  }
55
58
 
@@ -1,20 +1,21 @@
1
1
  import { UInt } from './uint'
2
2
  import { BinaryParser } from '../serdes/binary-parser'
3
- import bigInt = require('big-integer')
4
- import { isInstance } from 'big-integer'
5
- import { Buffer } from 'buffer/'
3
+ import { bytesToHex, concat, hexToBytes } from '@xrplf/isomorphic/utils'
4
+ import { readUInt32BE, writeUInt32BE } from '../utils'
6
5
 
7
6
  const HEX_REGEX = /^[a-fA-F0-9]{1,16}$/
8
- const mask = bigInt(0x00000000ffffffff)
7
+ const mask = BigInt(0x00000000ffffffff)
9
8
 
10
9
  /**
11
10
  * Derived UInt class for serializing/deserializing 64 bit UInt
12
11
  */
13
12
  class UInt64 extends UInt {
14
13
  protected static readonly width: number = 64 / 8 // 8
15
- static readonly defaultUInt64: UInt64 = new UInt64(Buffer.alloc(UInt64.width))
14
+ static readonly defaultUInt64: UInt64 = new UInt64(
15
+ new Uint8Array(UInt64.width),
16
+ )
16
17
 
17
- constructor(bytes: Buffer) {
18
+ constructor(bytes: Uint8Array) {
18
19
  super(bytes ?? UInt64.defaultUInt64.bytes)
19
20
  }
20
21
 
@@ -28,27 +29,25 @@ class UInt64 extends UInt {
28
29
  * @param val A UInt64, hex-string, bigInt, or number
29
30
  * @returns A UInt64 object
30
31
  */
31
- static from<T extends UInt64 | string | bigInt.BigInteger | number>(
32
- val: T,
33
- ): UInt64 {
32
+ static from<T extends UInt64 | string | bigint | number>(val: T): UInt64 {
34
33
  if (val instanceof UInt64) {
35
34
  return val
36
35
  }
37
36
 
38
- let buf = Buffer.alloc(UInt64.width)
37
+ let buf = new Uint8Array(UInt64.width)
39
38
 
40
39
  if (typeof val === 'number') {
41
40
  if (val < 0) {
42
41
  throw new Error('value must be an unsigned integer')
43
42
  }
44
43
 
45
- const number = bigInt(val)
44
+ const number = BigInt(val)
46
45
 
47
- const intBuf = [Buffer.alloc(4), Buffer.alloc(4)]
48
- intBuf[0].writeUInt32BE(Number(number.shiftRight(32)), 0)
49
- intBuf[1].writeUInt32BE(Number(number.and(mask)), 0)
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)
50
49
 
51
- return new UInt64(Buffer.concat(intBuf))
50
+ return new UInt64(concat(intBuf))
52
51
  }
53
52
 
54
53
  if (typeof val === 'string') {
@@ -57,16 +56,16 @@ class UInt64 extends UInt {
57
56
  }
58
57
 
59
58
  const strBuf = val.padStart(16, '0')
60
- buf = Buffer.from(strBuf, 'hex')
59
+ buf = hexToBytes(strBuf)
61
60
  return new UInt64(buf)
62
61
  }
63
62
 
64
- if (isInstance(val)) {
65
- const intBuf = [Buffer.alloc(4), Buffer.alloc(4)]
66
- intBuf[0].writeUInt32BE(Number(val.shiftRight(bigInt(32))), 0)
67
- intBuf[1].writeUInt32BE(Number(val.and(mask)), 0)
63
+ if (typeof val === 'bigint') {
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)
68
67
 
69
- return new UInt64(Buffer.concat(intBuf))
68
+ return new UInt64(concat(intBuf))
70
69
  }
71
70
 
72
71
  throw new Error('Cannot construct UInt64 from given value')
@@ -78,7 +77,7 @@ class UInt64 extends UInt {
78
77
  * @returns a hex-string
79
78
  */
80
79
  toJSON(): string {
81
- return this.bytes.toString('hex').toUpperCase()
80
+ return bytesToHex(this.bytes)
82
81
  }
83
82
 
84
83
  /**
@@ -86,10 +85,10 @@ class UInt64 extends UInt {
86
85
  *
87
86
  * @returns the number represented buy this.bytes
88
87
  */
89
- valueOf(): bigInt.BigInteger {
90
- const msb = bigInt(this.bytes.slice(0, 4).readUInt32BE(0))
91
- const lsb = bigInt(this.bytes.slice(4).readUInt32BE(0))
92
- return msb.shiftLeft(bigInt(32)).or(lsb)
88
+ valueOf(): bigint {
89
+ const msb = BigInt(readUInt32BE(this.bytes.slice(0, 4), 0))
90
+ const lsb = BigInt(readUInt32BE(this.bytes.slice(4), 0))
91
+ return (msb << BigInt(32)) | lsb
93
92
  }
94
93
 
95
94
  /**
@@ -97,7 +96,7 @@ class UInt64 extends UInt {
97
96
  *
98
97
  * @returns 8 bytes representing the UInt64
99
98
  */
100
- toBytes(): Buffer {
99
+ toBytes(): Uint8Array {
101
100
  return this.bytes
102
101
  }
103
102
  }
@@ -1,15 +1,16 @@
1
1
  import { UInt } from './uint'
2
2
  import { BinaryParser } from '../serdes/binary-parser'
3
- import { Buffer } from 'buffer/'
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(Buffer.alloc(UInt8.width))
11
+ static readonly defaultUInt8: UInt8 = new UInt8(new Uint8Array(UInt8.width))
11
12
 
12
- constructor(bytes: Buffer) {
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
- const buf = Buffer.alloc(UInt8.width)
32
- buf.writeUInt8(val, 0)
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.readUInt8(0)
48
+ return parseInt(bytesToHex(this.bytes), 16)
46
49
  }
47
50
  }
48
51
 
package/src/types/uint.ts CHANGED
@@ -1,6 +1,4 @@
1
- import bigInt = require('big-integer')
2
1
  import { Comparable } from './serialized-type'
3
- import { Buffer } from 'buffer/'
4
2
 
5
3
  /**
6
4
  * Compare numbers and bigInts n1 and n2
@@ -9,20 +7,17 @@ import { Buffer } from 'buffer/'
9
7
  * @param n2 Second object to compare
10
8
  * @returns -1, 0, or 1, depending on how the two objects compare
11
9
  */
12
- function compare(
13
- n1: number | bigInt.BigInteger,
14
- n2: number | bigInt.BigInteger,
15
- ): number {
10
+ function compare(n1: number | bigint, n2: number | bigint): number {
16
11
  return n1 < n2 ? -1 : n1 == n2 ? 0 : 1
17
12
  }
18
13
 
19
14
  /**
20
15
  * Base class for serializing and deserializing unsigned integers.
21
16
  */
22
- abstract class UInt extends Comparable {
17
+ abstract class UInt extends Comparable<UInt | number> {
23
18
  protected static width: number
24
19
 
25
- constructor(bytes: Buffer) {
20
+ constructor(bytes: Uint8Array) {
26
21
  super(bytes)
27
22
  }
28
23
 
@@ -32,7 +27,7 @@ abstract class UInt extends Comparable {
32
27
  * @param other other UInt to compare this to
33
28
  * @returns -1, 0, or 1 depending on how the objects relate to each other
34
29
  */
35
- compareTo(other: UInt): number {
30
+ compareTo(other: UInt | number): number {
36
31
  return compare(this.valueOf(), other.valueOf())
37
32
  }
38
33
 
@@ -51,7 +46,15 @@ abstract class UInt extends Comparable {
51
46
  *
52
47
  * @returns the value
53
48
  */
54
- abstract valueOf(): number | bigInt.BigInteger
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
+ }
55
58
  }
56
59
 
57
60
  export { UInt }
@@ -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 { Buffer } from 'buffer/'
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: Buffer) {
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
  }
@@ -2,8 +2,8 @@ import { BinaryParser } from '../serdes/binary-parser'
2
2
 
3
3
  import { AccountID } from './account-id'
4
4
  import { JsonObject, SerializedType } from './serialized-type'
5
- import { Buffer } from 'buffer/'
6
5
  import { Issue, IssueObject } from './issue'
6
+ import { concat } from '@xrplf/isomorphic/utils'
7
7
 
8
8
  /**
9
9
  * Interface for JSON objects that represent cross-chain bridges
@@ -34,11 +34,11 @@ function isXChainBridgeObject(arg): arg is XChainBridgeObject {
34
34
  */
35
35
  class XChainBridge extends SerializedType {
36
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),
37
+ concat([
38
+ Uint8Array.from([0x14]),
39
+ new Uint8Array(40),
40
+ Uint8Array.from([0x14]),
41
+ new Uint8Array(40),
42
42
  ]),
43
43
  )
44
44
 
@@ -50,7 +50,7 @@ class XChainBridge extends SerializedType {
50
50
  { name: 'IssuingChainIssue', type: Issue },
51
51
  ]
52
52
 
53
- constructor(bytes: Buffer) {
53
+ constructor(bytes: Uint8Array) {
54
54
  super(bytes ?? XChainBridge.ZERO_XCHAIN_BRIDGE.bytes)
55
55
  }
56
56
 
@@ -71,16 +71,16 @@ class XChainBridge extends SerializedType {
71
71
  throw new Error('Invalid type to construct an XChainBridge')
72
72
  }
73
73
 
74
- const bytes: Array<Buffer> = []
74
+ const bytes: Array<Uint8Array> = []
75
75
  this.TYPE_ORDER.forEach((item) => {
76
76
  const { name, type } = item
77
77
  if (type === AccountID) {
78
- bytes.push(Buffer.from([0x14]))
78
+ bytes.push(Uint8Array.from([0x14]))
79
79
  }
80
80
  const object = type.from(value[name])
81
81
  bytes.push(object.toBytes())
82
82
  })
83
- return new XChainBridge(Buffer.concat(bytes))
83
+ return new XChainBridge(concat(bytes))
84
84
  }
85
85
 
86
86
  /**
@@ -90,19 +90,19 @@ class XChainBridge extends SerializedType {
90
90
  * @returns An XChainBridge object
91
91
  */
92
92
  static fromParser(parser: BinaryParser): XChainBridge {
93
- const bytes: Array<Buffer> = []
93
+ const bytes: Array<Uint8Array> = []
94
94
 
95
95
  this.TYPE_ORDER.forEach((item) => {
96
96
  const { type } = item
97
97
  if (type === AccountID) {
98
98
  parser.skip(1)
99
- bytes.push(Buffer.from([0x14]))
99
+ bytes.push(Uint8Array.from([0x14]))
100
100
  }
101
101
  const object = type.fromParser(parser)
102
102
  bytes.push(object.toBytes())
103
103
  })
104
104
 
105
- return new XChainBridge(Buffer.concat(bytes))
105
+ return new XChainBridge(concat(bytes))
106
106
  }
107
107
 
108
108
  /**
package/src/utils.ts ADDED
@@ -0,0 +1,152 @@
1
+ // Even though this comes from NodeJS it is valid in the browser
2
+ import TypedArray = NodeJS.TypedArray
3
+
4
+ /**
5
+ * Writes value to array at the specified offset. The value must be a valid unsigned 8-bit integer.
6
+ * @param array Uint8Array to be written to
7
+ * @param value Number to be written to array.
8
+ * @param offset plus the number of bytes written.
9
+ */
10
+ export function writeUInt8(
11
+ array: Uint8Array,
12
+ value: number,
13
+ offset: number,
14
+ ): void {
15
+ value = Number(value)
16
+ array[offset] = value
17
+ }
18
+
19
+ /**
20
+ * Writes value to array at the specified offset as big-endian. The value must be a valid unsigned 16-bit integer.
21
+ * @param array Uint8Array to be written to
22
+ * @param value Number to be written to array.
23
+ * @param offset plus the number of bytes written.
24
+ */
25
+ export function writeUInt16BE(
26
+ array: Uint8Array,
27
+ value: number,
28
+ offset: number,
29
+ ): void {
30
+ value = Number(value)
31
+
32
+ array[offset] = value >>> 8
33
+ array[offset + 1] = value
34
+ }
35
+
36
+ /**
37
+ * Writes value to array at the specified offset as big-endian. The value must be a valid unsigned 32-bit integer.
38
+ * @param array Uint8Array to be written to
39
+ * @param value Number to be written to array.
40
+ * @param offset plus the number of bytes written.
41
+ */
42
+ export function writeUInt32BE(
43
+ array: Uint8Array,
44
+ value: number,
45
+ offset: number,
46
+ ): void {
47
+ array[offset] = (value >>> 24) & 0xff
48
+ array[offset + 1] = (value >>> 16) & 0xff
49
+ array[offset + 2] = (value >>> 8) & 0xff
50
+ array[offset + 3] = value & 0xff
51
+ }
52
+
53
+ /**
54
+ * Reads an unsigned, big-endian 16-bit integer from the array at the specified offset.
55
+ * @param array Uint8Array to read
56
+ * @param offset Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
57
+ */
58
+ export function readUInt16BE(array: Uint8Array, offset: number): string {
59
+ return new DataView(array.buffer).getUint16(offset, false).toString(10)
60
+ }
61
+
62
+ /**
63
+ * Reads an unsigned, big-endian 16-bit integer from the array at the specified offset.
64
+ * @param array Uint8Array to read
65
+ * @param offset Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
66
+ */
67
+ export function readUInt32BE(array: Uint8Array, offset: number): string {
68
+ return new DataView(array.buffer).getUint32(offset, false).toString(10)
69
+ }
70
+
71
+ /**
72
+ * Compares two Uint8Array or ArrayBuffers
73
+ * @param a first array to compare
74
+ * @param b second array to compare
75
+ */
76
+ export function equal(
77
+ a: Uint8Array | ArrayBuffer,
78
+ b: Uint8Array | ArrayBuffer,
79
+ ): boolean {
80
+ const aUInt = a instanceof ArrayBuffer ? new Uint8Array(a, 0) : a
81
+ const bUInt = b instanceof ArrayBuffer ? new Uint8Array(b, 0) : b
82
+ if (aUInt.byteLength != bUInt.byteLength) return false
83
+ if (aligned32(aUInt) && aligned32(bUInt)) return compare32(aUInt, bUInt) === 0
84
+ if (aligned16(aUInt) && aligned16(bUInt)) return compare16(aUInt, bUInt) === 0
85
+ return compare8(aUInt, bUInt) === 0
86
+ }
87
+
88
+ /**
89
+ * Compares two 8 bit aligned arrays
90
+ * @param a first array to compare
91
+ * @param b second array to compare
92
+ */
93
+ function compare8(a, b) {
94
+ const ua = new Uint8Array(a.buffer, a.byteOffset, a.byteLength)
95
+ const ub = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
96
+ return compare(ua, ub)
97
+ }
98
+
99
+ /**
100
+ * Compares two 16 bit aligned arrays
101
+ * @param a first array to compare
102
+ * @param b second array to compare
103
+ */
104
+ function compare16(a: Uint8Array, b: Uint8Array) {
105
+ const ua = new Uint16Array(a.buffer, a.byteOffset, a.byteLength / 2)
106
+ const ub = new Uint16Array(b.buffer, b.byteOffset, b.byteLength / 2)
107
+ return compare(ua, ub)
108
+ }
109
+
110
+ /**
111
+ * Compares two 32 bit aligned arrays
112
+ * @param a first array to compare
113
+ * @param b second array to compare
114
+ */
115
+ function compare32(a: Uint8Array, b: Uint8Array) {
116
+ const ua = new Uint32Array(a.buffer, a.byteOffset, a.byteLength / 4)
117
+ const ub = new Uint32Array(b.buffer, b.byteOffset, b.byteLength / 4)
118
+ return compare(ua, ub)
119
+ }
120
+
121
+ /**
122
+ * Compare two TypedArrays
123
+ * @param a first array to compare
124
+ * @param b second array to compare
125
+ */
126
+ export function compare(a: TypedArray, b: TypedArray): 1 | -1 | 0 {
127
+ if (a.byteLength !== b.byteLength) {
128
+ throw new Error('Cannot compare arrays of different length')
129
+ }
130
+
131
+ for (let i = 0; i < a.length - 1; i += 1) {
132
+ if (a[i] > b[i]) return 1
133
+ if (a[i] < b[i]) return -1
134
+ }
135
+ return 0
136
+ }
137
+
138
+ /**
139
+ * Determine if TypedArray is 16 bit aligned
140
+ * @param array The array to check
141
+ */
142
+ function aligned16(array: TypedArray) {
143
+ return array.byteOffset % 2 === 0 && array.byteLength % 2 === 0
144
+ }
145
+
146
+ /**
147
+ * Determine if TypedArray is 32 bit aligned
148
+ * @param array The array to check
149
+ */
150
+ function aligned32(array: TypedArray) {
151
+ return array.byteOffset % 4 === 0 && array.byteLength % 4 === 0
152
+ }