ripple-binary-codec 2.7.0 → 2.8.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/src/quality.ts CHANGED
@@ -13,8 +13,13 @@ class quality {
13
13
  * @returns Serialized quality
14
14
  */
15
15
  static encode(quality: string): Uint8Array {
16
- const decimal = BigNumber(quality)
17
- const exponent = (decimal?.e || 0) - 15
16
+ let decimal: BigNumber
17
+ try {
18
+ decimal = new BigNumber(quality)
19
+ } catch (_err) {
20
+ throw new Error(`${quality} is not a valid quality`)
21
+ }
22
+ const exponent = (decimal.e || 0) - 15
18
23
  const qualityString = decimal.times(`1e${-exponent}`).abs().toString()
19
24
  const bytes = coreTypes.UInt64.from(BigInt(qualityString)).toBytes()
20
25
  bytes[0] = exponent + 100
@@ -30,7 +35,12 @@ class quality {
30
35
  static decode(quality: string): BigNumber {
31
36
  const bytes = hexToBytes(quality).slice(-8)
32
37
  const exponent = bytes[0] - 100
33
- const mantissa = new BigNumber(`0x${bytesToHex(bytes.slice(1))}`)
38
+ let mantissa: BigNumber
39
+ try {
40
+ mantissa = new BigNumber(`0x${bytesToHex(bytes.slice(1))}`)
41
+ } catch (_err) {
42
+ throw new Error(`${quality} is not a valid quality`)
43
+ }
34
44
  return mantissa.times(`1e${exponent}`)
35
45
  }
36
46
  }
@@ -46,8 +46,13 @@ class BinaryParser {
46
46
  * @param n the number of bytes to skip
47
47
  */
48
48
  skip(n: number): void {
49
+ if (n < 0) {
50
+ throw new Error(`skip: negative length ${n}`)
51
+ }
49
52
  if (n > this.bytes.byteLength) {
50
- throw new Error()
53
+ throw new Error(
54
+ `skip: requested ${n} bytes but only ${this.bytes.byteLength} available`,
55
+ )
51
56
  }
52
57
  this.bytes = this.bytes.slice(n)
53
58
  }
@@ -59,8 +64,13 @@ class BinaryParser {
59
64
  * @return The bytes
60
65
  */
61
66
  read(n: number): Uint8Array {
67
+ if (n < 0) {
68
+ throw new Error(`read: negative length ${n}`)
69
+ }
62
70
  if (n > this.bytes.byteLength) {
63
- throw new Error()
71
+ throw new Error(
72
+ `read: requested ${n} bytes but only ${this.bytes.byteLength} available`,
73
+ )
64
74
  }
65
75
 
66
76
  const slice = this.bytes.slice(0, n)
@@ -92,7 +92,7 @@ class Amount extends SerializedType {
92
92
  return value
93
93
  }
94
94
 
95
- let amount = new Uint8Array(8)
95
+ let amount: Uint8Array = new Uint8Array(8)
96
96
  if (typeof value === 'string') {
97
97
  Amount.assertXrpIsValid(value)
98
98
 
@@ -110,7 +110,12 @@ class Amount extends SerializedType {
110
110
  }
111
111
 
112
112
  if (isAmountObjectIOU(value)) {
113
- const number = new BigNumber(value.value)
113
+ let number: BigNumber
114
+ try {
115
+ number = new BigNumber(value.value)
116
+ } catch (_err) {
117
+ throw new Error(`${value.value} is an illegal amount`)
118
+ }
114
119
  Amount.assertIouIsValid(number)
115
120
 
116
121
  if (number.isZero()) {
@@ -189,7 +194,7 @@ class Amount extends SerializedType {
189
194
  */
190
195
  toJSON(): AmountObject | string {
191
196
  if (this.isNative()) {
192
- const bytes = this.bytes
197
+ const bytes = this.bytes.slice()
193
198
  const isPositive = bytes[0] & 0x40
194
199
  const sign = isPositive ? '' : '-'
195
200
  bytes[0] &= 0x3f
@@ -261,7 +266,12 @@ class Amount extends SerializedType {
261
266
  throw new Error(`${amount.toString()} is an illegal amount`)
262
267
  }
263
268
 
264
- const decimal = new BigNumber(amount)
269
+ let decimal: BigNumber
270
+ try {
271
+ decimal = new BigNumber(amount)
272
+ } catch (_err) {
273
+ throw new Error(`${amount.toString()} is an illegal amount`)
274
+ }
265
275
  if (!decimal.isZero()) {
266
276
  if (decimal.lt(MIN_XRP) || decimal.gt(MAX_DROPS)) {
267
277
  throw new Error(`${amount.toString()} is an illegal amount`)
@@ -301,7 +311,12 @@ class Amount extends SerializedType {
301
311
  throw new Error(`${amount.toString()} is an illegal amount`)
302
312
  }
303
313
 
304
- const decimal = new BigNumber(amount)
314
+ let decimal: BigNumber
315
+ try {
316
+ decimal = new BigNumber(amount)
317
+ } catch (_err) {
318
+ throw new Error(`${amount.toString()} is an illegal amount`)
319
+ }
305
320
  if (!decimal.isZero()) {
306
321
  if (decimal < BigNumber(0)) {
307
322
  throw new Error(`${amount.toString()} is an illegal amount`)
@@ -51,7 +51,7 @@ class UInt64 extends UInt {
51
51
  return val
52
52
  }
53
53
 
54
- let buf = new Uint8Array(UInt64.width)
54
+ let buf: Uint8Array = new Uint8Array(UInt64.width)
55
55
 
56
56
  if (typeof val === 'number' && Number.isInteger(val)) {
57
57
  if (val < 0) {
package/src/utils.ts CHANGED
@@ -196,7 +196,7 @@ export function compare(a: TypedArray, b: TypedArray): 1 | -1 | 0 {
196
196
  throw new Error('Cannot compare arrays of different length')
197
197
  }
198
198
 
199
- for (let i = 0; i < a.length - 1; i += 1) {
199
+ for (let i = 0; i < a.length; i += 1) {
200
200
  if (a[i] > b[i]) return 1
201
201
  if (a[i] < b[i]) return -1
202
202
  }