fix(ripple-keypairs): hexToBytes should produce [] for empty input, not [0] (#1977)

BN.toArray intentionally returns [0] rather than [] for length zero,
which may make sense for BigNum data, but not for byte strings.
This commit is contained in:
Pi Delport
2022-04-28 23:15:07 +02:00
committed by GitHub
parent 86001d51f9
commit 0b9c27ff5f
3 changed files with 9 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
# ripple-keypairs Release History
## Unreleased
- `hexToBytes` now produces empty output for empty input, rather than `[0]`.
- Extend `bytesToHex` to work correctly with any input type accepted by `Array.from`.
In particular, it now produces correct output for typed arrays such as `UInt8Array`.

View File

@@ -11,7 +11,10 @@ function bytesToHex(a: Iterable<number> | ArrayLike<number>): string {
function hexToBytes(a): number[] {
assert.ok(a.length % 2 === 0)
return new BN(a, 16).toArray(null, a.length / 2)
// Special-case length zero to return [].
// BN.toArray intentionally returns [0] rather than [] for length zero,
// which may make sense for BigNum data, but not for byte strings.
return a.length === 0 ? [] : new BN(a, 16).toArray(null, a.length / 2)
}
function computePublicKeyHash(publicKeyBytes: Buffer): Buffer {

View File

@@ -4,6 +4,10 @@ const assert = require('assert')
const utils = require('../dist/utils')
describe('utils', () => {
it('hexToBytes - empty', () => {
assert.deepEqual(utils.hexToBytes(''), [])
})
it('hexToBytes - zero', () => {
assert.deepEqual(utils.hexToBytes('000000'), [0, 0, 0])
})