mirror of
https://github.com/Xahau/xahau.js.git
synced 2026-08-23 16:40:52 +00:00
fix(ripple-keypairs): make bytesToHex work with typed arrays (Uint8Array) (#1975)
This works the same as before for Array inputs, but it should now also
work correctly with a wider range of Iterable or ArrayLike input types.
In particular, this makes it work with typed arrays such as Uint8Array,
which previously produced invalid output due to the hex numerals being
coerced back to the element type before the call to `join('')`.
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
# ripple-keypairs Release History
|
||||
|
||||
## Unreleased
|
||||
- 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`.
|
||||
|
||||
## 1.1.1 (2021-12-1)
|
||||
- Fix issue where npm < 7 was not allowed to install the library
|
||||
|
||||
|
||||
@@ -2,13 +2,11 @@ import * as assert from 'assert'
|
||||
import * as hashjs from 'hash.js'
|
||||
import * as BN from 'bn.js'
|
||||
|
||||
function bytesToHex(a): string {
|
||||
return a
|
||||
.map((byteValue) => {
|
||||
const hex = byteValue.toString(16).toUpperCase()
|
||||
return hex.length > 1 ? hex : `0${hex}`
|
||||
})
|
||||
.join('')
|
||||
function bytesToHex(a: Iterable<number> | ArrayLike<number>): string {
|
||||
return Array.from(a, (byteValue) => {
|
||||
const hex = byteValue.toString(16).toUpperCase()
|
||||
return hex.length > 1 ? hex : `0${hex}`
|
||||
}).join('')
|
||||
}
|
||||
|
||||
function hexToBytes(a): number[] {
|
||||
|
||||
@@ -11,4 +11,12 @@ describe('utils', () => {
|
||||
it('hexToBytes - DEADBEEF', () => {
|
||||
assert.deepEqual(utils.hexToBytes('DEADBEEF'), [222, 173, 190, 239])
|
||||
})
|
||||
|
||||
it('bytesToHex - DEADBEEF', () => {
|
||||
assert.deepEqual(utils.bytesToHex([222, 173, 190, 239]), 'DEADBEEF')
|
||||
});
|
||||
|
||||
it('bytesToHex - DEADBEEF (Uint8Array)', () => {
|
||||
assert.deepEqual(utils.bytesToHex(new Uint8Array([222, 173, 190, 239])), 'DEADBEEF')
|
||||
});
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user