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.
Files changed (247) hide show
  1. package/dist/binary.d.ts +12 -13
  2. package/dist/binary.js +6 -5
  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/definitions.json +36 -11
  8. package/dist/enums/field.d.ts +1 -2
  9. package/dist/enums/field.js +1 -2
  10. package/dist/enums/field.js.map +1 -1
  11. package/dist/enums/index.js +4 -24
  12. package/dist/enums/index.js.map +1 -1
  13. package/dist/enums/src/enums/definitions.json +36 -11
  14. package/dist/hash-prefixes.d.ts +1 -2
  15. package/dist/hash-prefixes.js +6 -6
  16. package/dist/hash-prefixes.js.map +1 -1
  17. package/dist/hashes.d.ts +5 -6
  18. package/dist/hashes.js +1 -2
  19. package/dist/hashes.js.map +1 -1
  20. package/dist/index.js +7 -14
  21. package/dist/index.js.map +1 -1
  22. package/dist/quality.d.ts +1 -2
  23. package/dist/quality.js +3 -3
  24. package/dist/quality.js.map +1 -1
  25. package/dist/serdes/binary-parser.d.ts +2 -3
  26. package/dist/serdes/binary-parser.js +2 -2
  27. package/dist/serdes/binary-parser.js.map +1 -1
  28. package/dist/serdes/binary-serializer.d.ts +6 -7
  29. package/dist/serdes/binary-serializer.js +9 -9
  30. package/dist/serdes/binary-serializer.js.map +1 -1
  31. package/dist/shamap.d.ts +4 -5
  32. package/dist/shamap.js +2 -3
  33. package/dist/shamap.js.map +1 -1
  34. package/dist/src/binary.d.ts +90 -0
  35. package/dist/src/binary.js +128 -0
  36. package/dist/src/binary.js.map +1 -0
  37. package/dist/src/coretypes.d.ts +9 -0
  38. package/dist/src/coretypes.js +48 -0
  39. package/dist/src/coretypes.js.map +1 -0
  40. package/dist/src/enums/bytes.d.ts +25 -0
  41. package/dist/src/enums/bytes.js +63 -0
  42. package/dist/src/enums/bytes.js.map +1 -0
  43. package/dist/src/enums/constants.d.ts +4 -0
  44. package/dist/src/enums/constants.js +8 -0
  45. package/dist/src/enums/constants.js.map +1 -0
  46. package/dist/src/enums/definitions.json +2866 -0
  47. package/dist/src/enums/field.d.ts +28 -0
  48. package/dist/src/enums/field.js +58 -0
  49. package/dist/src/enums/field.js.map +1 -0
  50. package/dist/src/enums/index.d.ts +12 -0
  51. package/dist/src/enums/index.js +31 -0
  52. package/dist/src/enums/index.js.map +1 -0
  53. package/dist/src/enums/utils-renumber.d.ts +101 -0
  54. package/dist/src/enums/utils-renumber.js +127 -0
  55. package/dist/src/enums/utils-renumber.js.map +1 -0
  56. package/dist/src/enums/xrpl-definitions-base.d.ts +44 -0
  57. package/dist/src/enums/xrpl-definitions-base.js +59 -0
  58. package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
  59. package/dist/src/enums/xrpl-definitions.d.ts +21 -0
  60. package/dist/src/enums/xrpl-definitions.js +29 -0
  61. package/dist/src/enums/xrpl-definitions.js.map +1 -0
  62. package/dist/src/hash-prefixes.d.ts +5 -0
  63. package/dist/src/hash-prefixes.js +41 -0
  64. package/dist/src/hash-prefixes.js.map +1 -0
  65. package/dist/src/hashes.d.ts +50 -0
  66. package/dist/src/hashes.js +76 -0
  67. package/dist/src/hashes.js.map +1 -0
  68. package/dist/src/index.d.ts +64 -0
  69. package/dist/src/index.js +123 -0
  70. package/dist/src/index.js.map +1 -0
  71. package/dist/src/ledger-hashes.d.ts +48 -0
  72. package/dist/src/ledger-hashes.js +141 -0
  73. package/dist/src/ledger-hashes.js.map +1 -0
  74. package/dist/src/quality.d.ts +21 -0
  75. package/dist/src/quality.js +42 -0
  76. package/dist/src/quality.js.map +1 -0
  77. package/dist/src/serdes/binary-parser.d.ts +100 -0
  78. package/dist/src/serdes/binary-parser.js +194 -0
  79. package/dist/src/serdes/binary-parser.js.map +1 -0
  80. package/dist/src/serdes/binary-serializer.d.ts +81 -0
  81. package/dist/src/serdes/binary-serializer.js +149 -0
  82. package/dist/src/serdes/binary-serializer.js.map +1 -0
  83. package/dist/src/shamap.d.ts +102 -0
  84. package/dist/src/shamap.js +172 -0
  85. package/dist/src/shamap.js.map +1 -0
  86. package/dist/src/types/account-id.d.ts +35 -0
  87. package/dist/src/types/account-id.js +69 -0
  88. package/dist/src/types/account-id.js.map +1 -0
  89. package/dist/src/types/amount.d.ts +67 -0
  90. package/dist/src/types/amount.js +212 -0
  91. package/dist/src/types/amount.js.map +1 -0
  92. package/dist/src/types/blob.d.ts +24 -0
  93. package/dist/src/types/blob.js +40 -0
  94. package/dist/src/types/blob.js.map +1 -0
  95. package/dist/src/types/currency.d.ts +28 -0
  96. package/dist/src/types/currency.js +127 -0
  97. package/dist/src/types/currency.js.map +1 -0
  98. package/dist/src/types/hash-128.d.ts +16 -0
  99. package/dist/src/types/hash-128.js +32 -0
  100. package/dist/src/types/hash-128.js.map +1 -0
  101. package/dist/src/types/hash-160.d.ts +10 -0
  102. package/dist/src/types/hash-160.js +19 -0
  103. package/dist/src/types/hash-160.js.map +1 -0
  104. package/dist/src/types/hash-256.d.ts +10 -0
  105. package/dist/src/types/hash-256.js +16 -0
  106. package/dist/src/types/hash-256.js.map +1 -0
  107. package/dist/src/types/hash.d.ts +40 -0
  108. package/dist/src/types/hash.js +73 -0
  109. package/dist/src/types/hash.js.map +1 -0
  110. package/dist/src/types/index.d.ts +18 -0
  111. package/dist/src/types/index.js +61 -0
  112. package/dist/src/types/index.js.map +1 -0
  113. package/dist/src/types/issue.d.ts +38 -0
  114. package/dist/src/types/issue.js +81 -0
  115. package/dist/src/types/issue.js.map +1 -0
  116. package/dist/src/types/path-set.d.ts +36 -0
  117. package/dist/src/types/path-set.js +233 -0
  118. package/dist/src/types/path-set.js.map +1 -0
  119. package/dist/src/types/serialized-type.d.ts +70 -0
  120. package/dist/src/types/serialized-type.js +105 -0
  121. package/dist/src/types/serialized-type.js.map +1 -0
  122. package/dist/src/types/st-array.d.ts +31 -0
  123. package/dist/src/types/st-array.js +83 -0
  124. package/dist/src/types/st-array.js.map +1 -0
  125. package/dist/src/types/st-object.d.ts +32 -0
  126. package/dist/src/types/st-object.js +155 -0
  127. package/dist/src/types/st-object.js.map +1 -0
  128. package/dist/src/types/uint-16.d.ts +24 -0
  129. package/dist/src/types/uint-16.js +45 -0
  130. package/dist/src/types/uint-16.js.map +1 -0
  131. package/dist/src/types/uint-32.d.ts +24 -0
  132. package/dist/src/types/uint-32.js +50 -0
  133. package/dist/src/types/uint-32.js.map +1 -0
  134. package/dist/src/types/uint-64.d.ts +37 -0
  135. package/dist/src/types/uint-64.js +86 -0
  136. package/dist/src/types/uint-64.js.map +1 -0
  137. package/dist/src/types/uint-8.d.ts +24 -0
  138. package/dist/src/types/uint-8.js +46 -0
  139. package/dist/src/types/uint-8.js.map +1 -0
  140. package/dist/src/types/uint.d.ts +29 -0
  141. package/dist/src/types/uint.js +47 -0
  142. package/dist/src/types/uint.js.map +1 -0
  143. package/dist/src/types/vector-256.d.ts +30 -0
  144. package/dist/src/types/vector-256.js +73 -0
  145. package/dist/src/types/vector-256.js.map +1 -0
  146. package/dist/src/types/xchain-bridge.d.ts +44 -0
  147. package/dist/src/types/xchain-bridge.js +102 -0
  148. package/dist/src/types/xchain-bridge.js.map +1 -0
  149. package/dist/src/utils.d.ts +47 -0
  150. package/dist/src/utils.js +137 -0
  151. package/dist/src/utils.js.map +1 -0
  152. package/dist/tsconfig.tsbuildinfo +1 -0
  153. package/dist/types/account-id.d.ts +1 -2
  154. package/dist/types/account-id.js +4 -6
  155. package/dist/types/account-id.js.map +1 -1
  156. package/dist/types/amount.d.ts +1 -2
  157. package/dist/types/amount.js +16 -15
  158. package/dist/types/amount.js.map +1 -1
  159. package/dist/types/blob.d.ts +1 -2
  160. package/dist/types/blob.js +2 -2
  161. package/dist/types/blob.js.map +1 -1
  162. package/dist/types/currency.d.ts +1 -2
  163. package/dist/types/currency.js +10 -10
  164. package/dist/types/currency.js.map +1 -1
  165. package/dist/types/hash-128.d.ts +1 -2
  166. package/dist/types/hash-128.js +3 -3
  167. package/dist/types/hash-128.js.map +1 -1
  168. package/dist/types/hash-160.d.ts +1 -2
  169. package/dist/types/hash-160.js +1 -2
  170. package/dist/types/hash-160.js.map +1 -1
  171. package/dist/types/hash-256.d.ts +1 -2
  172. package/dist/types/hash-256.js +1 -2
  173. package/dist/types/hash-256.js.map +1 -1
  174. package/dist/types/hash.d.ts +2 -3
  175. package/dist/types/hash.js +5 -4
  176. package/dist/types/hash.js.map +1 -1
  177. package/dist/types/issue.d.ts +1 -2
  178. package/dist/types/issue.js +4 -4
  179. package/dist/types/issue.js.map +1 -1
  180. package/dist/types/path-set.js +12 -12
  181. package/dist/types/path-set.js.map +1 -1
  182. package/dist/types/serialized-type.d.ts +17 -13
  183. package/dist/types/serialized-type.js +11 -6
  184. package/dist/types/serialized-type.js.map +1 -1
  185. package/dist/types/st-array.js +5 -5
  186. package/dist/types/st-array.js.map +1 -1
  187. package/dist/types/st-object.js +1 -2
  188. package/dist/types/st-object.js.map +1 -1
  189. package/dist/types/uint-16.d.ts +1 -2
  190. package/dist/types/uint-16.js +6 -5
  191. package/dist/types/uint-16.js.map +1 -1
  192. package/dist/types/uint-32.d.ts +1 -2
  193. package/dist/types/uint-32.js +7 -6
  194. package/dist/types/uint-32.js.map +1 -1
  195. package/dist/types/uint-64.d.ts +2 -3
  196. package/dist/types/uint-64.js +16 -15
  197. package/dist/types/uint-64.js.map +1 -1
  198. package/dist/types/uint-8.d.ts +1 -2
  199. package/dist/types/uint-8.js +7 -5
  200. package/dist/types/uint-8.js.map +1 -1
  201. package/dist/types/uint.d.ts +4 -4
  202. package/dist/types/uint.js +5 -0
  203. package/dist/types/uint.js.map +1 -1
  204. package/dist/types/vector-256.d.ts +1 -2
  205. package/dist/types/vector-256.js +2 -4
  206. package/dist/types/vector-256.js.map +1 -1
  207. package/dist/types/xchain-bridge.d.ts +1 -2
  208. package/dist/types/xchain-bridge.js +20 -20
  209. package/dist/types/xchain-bridge.js.map +1 -1
  210. package/dist/utils.d.ts +47 -0
  211. package/dist/utils.js +137 -0
  212. package/dist/utils.js.map +1 -0
  213. package/package.json +8 -11
  214. package/src/README.md +3 -1
  215. package/src/binary.ts +21 -14
  216. package/src/enums/bytes.ts +2 -3
  217. package/src/enums/definitions.json +36 -11
  218. package/src/enums/field.ts +3 -4
  219. package/src/enums/index.ts +1 -1
  220. package/src/hash-prefixes.ts +8 -8
  221. package/src/hashes.ts +6 -7
  222. package/src/index.ts +12 -15
  223. package/src/quality.ts +4 -4
  224. package/src/serdes/binary-parser.ts +5 -5
  225. package/src/serdes/binary-serializer.ts +14 -14
  226. package/src/shamap.ts +5 -6
  227. package/src/types/account-id.ts +8 -8
  228. package/src/types/amount.ts +17 -18
  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 +20 -15
  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 +20 -17
  243. package/src/types/uint-8.ts +9 -6
  244. package/src/types/uint.ts +11 -4
  245. package/src/types/vector-256.ts +3 -8
  246. package/src/types/xchain-bridge.ts +22 -22
  247. package/src/utils.ts +152 -0
@@ -1,21 +1,21 @@
1
- import { Buffer } from 'buffer/'
1
+ import { writeUInt32BE } from './utils'
2
2
 
3
3
  /**
4
- * Write a 32 bit integer to a Buffer
4
+ * Write a 32 bit integer to a Uint8Array
5
5
  *
6
- * @param uint32 32 bit integer to write to buffer
7
- * @returns a buffer with the bytes representation of uint32
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): Buffer {
10
- const result = Buffer.alloc(4)
11
- result.writeUInt32BE(uint32, 0)
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, Buffer> = {
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: Buffer): Sha512Half {
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: Buffer): Sha512Half {
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(): Buffer {
41
- return Buffer.from(this.hash.digest().slice(0, 32))
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: Buffer[]): Buffer {
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: Buffer): Hash256 {
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 signingData(json as JsonObject, HashPrefix.transactionSig, {
68
- definitions,
69
- })
70
- .toString('hex')
71
- .toUpperCase()
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 multiSigningData(json as JsonObject, signer, definitionsOpt)
112
- .toString('hex')
113
- .toUpperCase()
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).toString('hex').toUpperCase()
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): Buffer {
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 = Buffer.from(quality, 'hex').slice(-8)
31
+ const bytes = hexToBytes(quality).slice(-8)
32
32
  const exponent = bytes[0] - 100
33
- const mantissa = new BigNumber(`0x${bytes.slice(1).toString('hex')}`)
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 { Buffer } from 'buffer/'
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: Buffer
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 = Buffer.from(hexBytes, 'hex')
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): Buffer {
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(): Buffer {
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 { Buffer } from 'buffer/'
3
+ import { bytesToHex, concat } from '@xrplf/isomorphic/utils'
4
4
 
5
5
  /**
6
- * Bytes list is a collection of buffer objects
6
+ * Bytes list is a collection of Uint8Array objects
7
7
  */
8
8
  class BytesList {
9
- private bytesArray: Array<Buffer> = []
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 Buffer.concat(this.bytesArray).byteLength
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 Buffer
23
+ * @param bytesArg A Uint8Array
24
24
  * @return this BytesList
25
25
  */
26
- public put(bytesArg: Buffer): BytesList {
27
- const bytes = Buffer.from(bytesArg) // Temporary, to catch instances of Uint8Array being passed in
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(): Buffer {
42
- return Buffer.concat(this.bytesArray)
41
+ public toBytes(): Uint8Array {
42
+ return concat(this.bytesArray)
43
43
  }
44
44
 
45
45
  toHex(): string {
46
- return this.toBytes().toString('hex').toUpperCase()
46
+ return bytesToHex(this.toBytes())
47
47
  }
48
48
  }
49
49
 
50
50
  /**
51
- * BinarySerializer is used to write fields and values to buffers
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: Buffer): void {
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): Buffer {
103
- const lenBytes = Buffer.alloc(3)
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(): Buffer
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 Buffer
43
+ * @returns The hash prefix, unless this.item is undefined, then it returns an empty Uint8Array
45
44
  */
46
- hashPrefix(): Buffer {
47
- return this.item === undefined ? Buffer.alloc(0) : this.item.hashPrefix()
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(): Buffer {
102
+ hashPrefix(): Uint8Array {
104
103
  return HashPrefix.innerNode
105
104
  }
106
105
 
@@ -5,7 +5,7 @@ import {
5
5
  xAddressToClassicAddress,
6
6
  } from 'ripple-address-codec'
7
7
  import { Hash160 } from './hash-160'
8
- import { Buffer } from 'buffer/'
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(Buffer.alloc(20))
16
+ static readonly defaultAccountID: AccountID = new AccountID(
17
+ new Uint8Array(20),
18
+ )
17
19
 
18
- constructor(bytes?: Buffer) {
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(Buffer.from(value, 'hex'))
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(Buffer.from(decodeAccountID(value)))
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
- /* eslint-disable @typescript-eslint/no-explicit-any */
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
 
@@ -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: Buffer) {
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 = Buffer.alloc(8)
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 = [Buffer.alloc(4), Buffer.alloc(4)]
82
- intBuf[0].writeUInt32BE(Number(number >> BigInt(32)), 0)
83
- intBuf[1].writeUInt32BE(Number(number & BigInt(mask)), 0)
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 = Buffer.concat(intBuf)
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 = [Buffer.alloc(4), Buffer.alloc(4)]
106
- intBuf[0].writeUInt32BE(Number(num >> BigInt(32)), 0)
107
- intBuf[1].writeUInt32BE(Number(num & BigInt(mask)), 0)
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 = Buffer.concat(intBuf)
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(Buffer.concat([amount, currency, issuer]))
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).readUInt32BE(0))
156
- const lsb = BigInt(bytes.slice(4).readUInt32BE(0))
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.toString('hex')}`).times(
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 { Buffer } from 'buffer/'
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: Buffer) {
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(Buffer.from(value, 'hex'))
36
+ return new Blob(hexToBytes(value))
37
37
  }
38
38
 
39
39
  throw new Error('Cannot construct Blob from value given')
@@ -1,5 +1,5 @@
1
1
  import { Hash160 } from './hash-160'
2
- import { Buffer } from 'buffer/'
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): Buffer {
14
- const bytes = Buffer.alloc(20)
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: Buffer): string | null {
30
- const iso = code.toString()
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 Buffer is a valid representation of a currency
55
+ * Tests if a Uint8Array is a valid representation of a currency
56
56
  */
57
- function isBytesArray(bytes: Buffer): boolean {
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: Buffer | string): boolean {
65
- return input instanceof Buffer
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 buffer representation of a currency
71
+ * Generate bytes from a string or UInt8Array representation of a currency
72
72
  */
73
- function bytesFromRepresentation(input: string): Buffer {
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) : Buffer.from(input, 'hex')
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(Buffer.alloc(20))
84
+ static readonly XRP = new Currency(new Uint8Array(20))
85
85
  private readonly _iso: string | null
86
86
 
87
- constructor(byteBuf: Buffer) {
87
+ constructor(byteBuf: Uint8Array) {
88
88
  super(byteBuf ?? Currency.XRP.bytes)
89
- const hex = this.bytes.toString('hex')
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.toString('hex').toUpperCase()
136
+ return bytesToHex(this.bytes)
137
137
  }
138
138
  }
139
139
 
@@ -1,14 +1,14 @@
1
1
  import { Hash } from './hash'
2
- import { Buffer } from 'buffer/'
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(Buffer.alloc(Hash128.width))
9
+ static readonly ZERO_128: Hash128 = new Hash128(new Uint8Array(Hash128.width))
10
10
 
11
- constructor(bytes: Buffer) {
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().toString('hex').toUpperCase()
25
+ const hex = bytesToHex(this.toBytes())
26
26
  if (/^0+$/.exec(hex)) {
27
27
  return ''
28
28
  }
@@ -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(Buffer.alloc(Hash160.width))
8
+ static readonly ZERO_160: Hash160 = new Hash160(new Uint8Array(Hash160.width))
10
9
 
11
- constructor(bytes?: Buffer) {
10
+ constructor(bytes?: Uint8Array) {
12
11
  if (bytes && bytes.byteLength === 0) {
13
12
  bytes = Hash160.ZERO_160.bytes
14
13
  }
@@ -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(Buffer.alloc(Hash256.width))
8
+ static readonly ZERO_256 = new Hash256(new Uint8Array(Hash256.width))
10
9
 
11
- constructor(bytes: Buffer) {
10
+ constructor(bytes: Uint8Array) {
12
11
  super(bytes ?? Hash256.ZERO_256.bytes)
13
12
  }
14
13
  }