mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-04 13:05:49 +00:00
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:
@@ -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`.
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user