Skip to content

Commit a116f5b

Browse files
shesekjgriffiths
authored andcommitted
wasm: Wrap returned Uint8Arrays as a Buffer
With this, the Buffer string conversion methods can be used directly, making the hexToBytes/bytesToHex helpers unnecessary.
1 parent bb257dc commit a116f5b

2 files changed

Lines changed: 18 additions & 23 deletions

File tree

src/wasm_package/core.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const _wally_free_string = Module.cwrap('wally_free_string', 'number', ['number'
4343
// Array of integers. Used for byte buffers and 32/64 bits numbers.
4444
// Passed to C as two arguments, the pointer and the array length.
4545
// Represented in JS as the IntArrayType, which is a Uint{8,32,64}Array.
46-
types.IntArray = (IntArrayType, heap) => ({
46+
types.IntArray = (IntArrayType, heap, wrap=x=>x) => ({
4747
wasm_types: ['number', 'number'],
4848
to_wasm: int_arr => {
4949
if (!Array.isArray(int_arr) && !(int_arr instanceof IntArrayType)) {
@@ -63,17 +63,19 @@ types.IntArray = (IntArrayType, heap) => ({
6363

6464
read_ptr_sized: (ptr, array_size) => {
6565
const heap_offset = ptr / IntArrayType.BYTES_PER_ELEMENT
66-
return new IntArrayType(heap.subarray(heap_offset, heap_offset + array_size))
66+
return wrap(new IntArrayType(heap.subarray(heap_offset, heap_offset + array_size)))
6767
},
6868

6969
free_ptr: ptr => Module._free(ptr),
7070

71-
init_empty: _ => new IntArrayType,
71+
init_empty: _ => wrap(new IntArrayType),
7272
})
7373

74-
types.Bytes = types.IntArray(Uint8Array, Module.HEAPU8)
7574
types.Uint32Array = types.IntArray(Uint32Array, Module.HEAPU32)
7675
types.Uint64Array = types.IntArray(BigUint64Array, Module.HEAPU64)
76+
// Return Uint8Arrays wrapped as a Buffer, which is a subclass of Uint8Array
77+
// but with some additional useful functionality (like toString('hex'))
78+
types.Bytes = types.IntArray(Uint8Array, Module.HEAPU8, Buffer.from)
7779

7880
// An opaque reference returned via DestPtrPtr that can be handed back to libwally
7981
types.OpaqueRef = {
@@ -200,13 +202,6 @@ const getArraySize = (size_source, this_arg, all_args) =>
200202
: typeof size_source == 'function' ? size_source(...all_args)
201203
: size_source
202204

203-
//
204-
// Utilities
205-
//
206-
207-
export const hexToBytes = hex => new Uint8Array(Buffer.from(hex, 'hex'))
208-
209-
export const bytesToHex = bytes => Buffer.from(bytes).toString('hex')
210205

211206
//
212207
// Errors

src/wasm_package/test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import assert from 'assert'
22

33
import * as wally from './index.js'
4-
import { bytesToHex, hexToBytes } from './index.js'
54
import {
65
WALLY_NETWORK_BITCOIN_MAINNET, WALLY_ADDRESS_VERSION_WIF_MAINNET, WALLY_WIF_FLAG_COMPRESSED,
76
BIP32_FLAG_KEY_PUBLIC, BIP32_INITIAL_HARDENED_CHILD,
@@ -15,25 +14,25 @@ assert.throws(_ => wally.hex_verify('001'), 'WALLY_EINVAL')
1514
assert.equal(wally.bip39_get_word(null, 10), 'access')
1615

1716
// Test string argument and a string destination pointer
18-
assert.equal(bytesToHex(wally.address_to_scriptpubkey("1EMBaSSyxMQPV2fmUsdB7mMfMoocgfiMNw", WALLY_NETWORK_BITCOIN_MAINNET)),
17+
assert.equal(wally.address_to_scriptpubkey("1EMBaSSyxMQPV2fmUsdB7mMfMoocgfiMNw", WALLY_NETWORK_BITCOIN_MAINNET).toString('hex'),
1918
'76a914926ac8843cbca0ee59aa857188324d6d5b76c1c688ac')
2019

2120
// Test bytes buffer argument
22-
assert.equal(wally.bip39_mnemonic_from_bytes(null, hexToBytes('b5bb9d8014a0f9b1d61e21e796d78dcc')),
21+
assert.equal(wally.bip39_mnemonic_from_bytes(null, Buffer.from('b5bb9d8014a0f9b1d61e21e796d78dcc', 'hex')),
2322
'remember table gas citizen auto suggest flash service travel repeat toddler occur')
2423

2524
// Test `written` pointer as the return value
2625
assert.equal(wally.wif_is_uncompressed("L5EZftvrYaSudiozVRzTqLcHLNDoVn7H5HSfM9BAN6tMJX8oTWz6"), 0)
2726
assert.equal(wally.wif_is_uncompressed("5Kdc3UAwGmHHuj6fQD1LDmKR6J3SwYyFWyHgxKAZ2cKRzVCRETY"), 1)
2827

29-
assert.equal(wally.wif_from_bytes(hexToBytes('b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'), WALLY_ADDRESS_VERSION_WIF_MAINNET, WALLY_WIF_FLAG_COMPRESSED),
28+
assert.equal(wally.wif_from_bytes(Buffer.from('b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c', 'hex'), WALLY_ADDRESS_VERSION_WIF_MAINNET, WALLY_WIF_FLAG_COMPRESSED),
3029
'L3JyYy6eC7JRohrc1XH1Y7caP966K5rbhFH7JEjpygufxTzEqR1Q')
3130

3231
// Test the use of returned opaque references
3332
const txhex = '020000000001015720d1aa6ac6bed17730b5e852c82191b73d1bd14cae6d7ccb8a4deab03081390000000000fdffffff018e5a0f00000000001976a914422e4acaed40191100fc4d13632574b62ee2ca2588ac0247304402203371fbdb07d9fefbf9e0f6d31700979297f36a871962a5c601e3495874dbeeaa022071e323524a4aa2d849a1295430d68e8e2e46fc99662d1b8e46435ffb47a699ca01210381722a93622de13f6848663f854a896d5910aadf5461937bcf3f02464cd36a0cd1860b00'
34-
const tx1 = wally.tx_from_hex(txhex, null)
33+
const tx1 = wally.tx_from_hex(txhex, 0)
3534
assert.equal(wally.tx_get_witness_count(tx1), 1)
36-
assert.equal(bytesToHex(wally.tx_get_txid(tx1)), "bc54928ad07bbe606ec27c8f9af9266b5c73cf01219f5cc545f849135c44bc90")
35+
assert.equal(wally.tx_get_txid(tx1).toString('hex'), "bc54928ad07bbe606ec27c8f9af9266b5c73cf01219f5cc545f849135c44bc90")
3736

3837
// Test bigint return value
3938
assert.equal(wally.tx_get_total_output_satoshi(tx1), 1006222n)
@@ -58,10 +57,10 @@ wally.bip32_key_free(hdkey_child)
5857
// Test varlen buffers (https://wally.readthedocs.io/en/latest/conventions/#variable-length-output-buffers)
5958
const longhex = Array(200).join('00')
6059
const bytes = wally.hex_to_bytes(longhex)
61-
assert.equal(bytesToHex(bytes), longhex)
60+
assert.equal(bytes.toString('hex'), longhex)
6261

63-
const vbytes = wally.varbuff_to_bytes(hexToBytes('133337'));
64-
assert.equal(bytesToHex(vbytes), '03133337')
62+
const vbytes = wally.varbuff_to_bytes(Buffer.from('133337', 'hex'));
63+
assert.equal(vbytes.toString('hex'), '03133337')
6564

6665
// Test uint32 array as an argument and return value
6766
const keypaths = wally.map_keypath_public_key_init(1)
@@ -71,10 +70,11 @@ const keypaths = wally.map_keypath_public_key_init(1)
7170

7271
wally.map_keypath_add(keypaths, dummy_pubkey, dummy_fingerprint, dummy_path)
7372
assert.equal(wally.map_keypath_get_item_path(keypaths, 0).join(','), dummy_path.join(','))
73+
wally.map_free(keypaths)
7474

7575
// Test output buffers with a user-specified length (scrypt is the only instance of this)
7676
const try_scrypt = size => wally.scrypt(Buffer.from("password"), Buffer.from("NaCl"), 1024, 8, 16, size)
77-
assert(wally.bytesToHex(try_scrypt(32)), 'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b373162')
78-
assert(wally.bytesToHex(try_scrypt(64)), 'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640')
77+
assert(try_scrypt(32).toString('hex'), 'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b373162')
78+
assert(try_scrypt(64).toString('hex'), 'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640')
7979

80-
console.log('Tests passed.')
80+
console.log('Tests passed.')

0 commit comments

Comments
 (0)