Skip to content

Commit 8525f7b

Browse files
shesekjgriffiths
authored andcommitted
wasm: Provide toWallyMap/fromWallyMap utility functions
1 parent 59a4cde commit 8525f7b

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/wasm_package/exports.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './core.js'
22
export * from './const.js'
3-
export * from './functions.js'
3+
export * from './functions.js'
4+
export * from './util.js'

src/wasm_package/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export * from './core'
66
declare const OPAQUE_TAG: unique symbol;
77
type OpaqueRef<S> = number & { readonly [OPAQUE_TAG]: S};
88

9-
export type Ref = OpaqueRef<'unknown'>;
9+
export type Ref = OpaqueRef<'unknown'> | null;
1010
export type Ref_words = OpaqueRef<'words'> | null;
1111
export type Ref_ext_key = OpaqueRef<'ext_key'> | null; // TODO check whether this is nullable anywhere in the C interface
1212
export type Ref_wally_map = OpaqueRef<'wally_map'> | null;

src/wasm_package/test.js

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
WALLY_NETWORK_BITCOIN_MAINNET, WALLY_ADDRESS_VERSION_WIF_MAINNET, WALLY_WIF_FLAG_COMPRESSED,
66
BIP32_FLAG_KEY_PUBLIC, BIP32_INITIAL_HARDENED_CHILD,
77
} from './index.js'
8+
import { toWallyMap, fromWallyMap } from './util.js'
89

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

81+
// Test JS<->wally map conversion
82+
let m1, m2
83+
const js_map = new Map([
84+
[0, Buffer.from('zero')],
85+
[1, Buffer.from('one')],
86+
['k1', Buffer.from('v1')],
87+
['k2', Buffer.from('v2')],
88+
])
89+
assert.deepEqual(fromWallyMap(m1=toWallyMap(js_map)), js_map)
90+
// works with plain objects and string values (auto-converted to a Map of Buffer values)
91+
assert.deepEqual(fromWallyMap(m2=toWallyMap({ 'foo': 'bar' })), new Map([['foo', Buffer.from('bar')]]))
92+
wally.map_free(m1); wally.map_free(m2)
93+
8094
// Test base58 conversion (depends on a JS length function)
8195
assert.equal(wally.base58_to_bytes('1EMBaSSyxMQPV2fmUsdB7mMfMoocgfiMNw', 0).toString('hex'), '00926ac8843cbca0ee59aa857188324d6d5b76c1c6f0bcc3b0')
8296
assert.equal(wally.base58_n_to_bytes('1EMBaSSyxMQPV2fmUsdB7mMfMoocgfiMNw', 34, 0).toString('hex'), '00926ac8843cbca0ee59aa857188324d6d5b76c1c6f0bcc3b0')

src/wasm_package/util.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
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"
2+
3+
export function fromWallyMap(wally_map) {
4+
const js_map = new Map, map_len = map_get_num_items(wally_map)
5+
6+
for (let i = 0; i < map_len; i++) {
7+
const is_int_key = map_get_item_key_length(wally_map, i) == 0
8+
, key = is_int_key ? map_get_item_integer_key(wally_map, i)
9+
: map_get_item_key(wally_map, i).toString()
10+
js_map.set(key, map_get_item(wally_map, i))
11+
}
12+
13+
return js_map
14+
}
15+
16+
export function toWallyMap(js_map) {
17+
if (js_map && js_map.constructor == Object) {
18+
// Convert plain objects into a Map
19+
js_map = new Map(Object.entries(js_map))
20+
} else if (!(js_map instanceof Map)) {
21+
throw new Error('Invalid map for toWallyMap')
22+
}
23+
24+
const wally_map = map_init(js_map.size, null)
25+
26+
js_map.forEach((val, key) => {
27+
val = Buffer.from(val)
28+
if (typeof key == 'number') {
29+
map_add_integer(wally_map, key, val)
30+
} else {
31+
map_add(wally_map, Buffer.from(key), val)
32+
}
33+
})
34+
35+
return wally_map
36+
}

0 commit comments

Comments
 (0)