Skip to content

Commit

Permalink
wasm: Provide toWallyMap/fromWallyMap utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek authored and jgriffiths committed Nov 29, 2022
1 parent 59a4cde commit 8525f7b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/wasm_package/exports.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './core.js'
export * from './const.js'
export * from './functions.js'
export * from './functions.js'
export * from './util.js'
2 changes: 1 addition & 1 deletion src/wasm_package/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export * from './core'
declare const OPAQUE_TAG: unique symbol;
type OpaqueRef<S> = number & { readonly [OPAQUE_TAG]: S};

export type Ref = OpaqueRef<'unknown'>;
export type Ref = OpaqueRef<'unknown'> | null;
export type Ref_words = OpaqueRef<'words'> | null;
export type Ref_ext_key = OpaqueRef<'ext_key'> | null; // TODO check whether this is nullable anywhere in the C interface
export type Ref_wally_map = OpaqueRef<'wally_map'> | null;
Expand Down
14 changes: 14 additions & 0 deletions src/wasm_package/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
WALLY_NETWORK_BITCOIN_MAINNET, WALLY_ADDRESS_VERSION_WIF_MAINNET, WALLY_WIF_FLAG_COMPRESSED,
BIP32_FLAG_KEY_PUBLIC, BIP32_INITIAL_HARDENED_CHILD,
} from './index.js'
import { toWallyMap, fromWallyMap } from './util.js'

// Test simple invocation, with a return code only and no destination pointer
wally.hex_verify('00') // should not throw
Expand Down Expand Up @@ -77,6 +78,19 @@ const try_scrypt = size => wally.scrypt(Buffer.from("password"), Buffer.from("Na
assert.equal(try_scrypt(32).toString('hex'), 'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b373162')
assert.equal(try_scrypt(64).toString('hex'), 'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640')

// Test JS<->wally map conversion
let m1, m2
const js_map = new Map([
[0, Buffer.from('zero')],
[1, Buffer.from('one')],
['k1', Buffer.from('v1')],
['k2', Buffer.from('v2')],
])
assert.deepEqual(fromWallyMap(m1=toWallyMap(js_map)), js_map)
// works with plain objects and string values (auto-converted to a Map of Buffer values)
assert.deepEqual(fromWallyMap(m2=toWallyMap({ 'foo': 'bar' })), new Map([['foo', Buffer.from('bar')]]))
wally.map_free(m1); wally.map_free(m2)

// Test base58 conversion (depends on a JS length function)
assert.equal(wally.base58_to_bytes('1EMBaSSyxMQPV2fmUsdB7mMfMoocgfiMNw', 0).toString('hex'), '00926ac8843cbca0ee59aa857188324d6d5b76c1c6f0bcc3b0')
assert.equal(wally.base58_n_to_bytes('1EMBaSSyxMQPV2fmUsdB7mMfMoocgfiMNw', 34, 0).toString('hex'), '00926ac8843cbca0ee59aa857188324d6d5b76c1c6f0bcc3b0')
Expand Down
36 changes: 36 additions & 0 deletions src/wasm_package/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { map_get_num_items, map_get_item_key, map_get_item_key_length, map_get_item_integer_key, map_get_item, map_init, map_add, map_add_integer } from "./functions.js"

export function fromWallyMap(wally_map) {
const js_map = new Map, map_len = map_get_num_items(wally_map)

for (let i = 0; i < map_len; i++) {
const is_int_key = map_get_item_key_length(wally_map, i) == 0
, key = is_int_key ? map_get_item_integer_key(wally_map, i)
: map_get_item_key(wally_map, i).toString()
js_map.set(key, map_get_item(wally_map, i))
}

return js_map
}

export function toWallyMap(js_map) {
if (js_map && js_map.constructor == Object) {
// Convert plain objects into a Map
js_map = new Map(Object.entries(js_map))
} else if (!(js_map instanceof Map)) {
throw new Error('Invalid map for toWallyMap')
}

const wally_map = map_init(js_map.size, null)

js_map.forEach((val, key) => {
val = Buffer.from(val)
if (typeof key == 'number') {
map_add_integer(wally_map, key, val)
} else {
map_add(wally_map, Buffer.from(key), val)
}
})

return wally_map
}

0 comments on commit 8525f7b

Please sign in to comment.