Skip to content

Commit 7ff438e

Browse files
committed
Expose wally_scriptpubkey_multisig_from_bytes to nodejs
1 parent df058f3 commit 7ff438e

File tree

4 files changed

+138
-11
lines changed

4 files changed

+138
-11
lines changed

src/wrap_js/example.js

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
var wally = require('./wally');
1+
const wally = require('./wally');
2+
const EC_PUBLIC_KEY_LEN = 33;
3+
const VERSION_PREFIX_LIQUID = '4b'
4+
var seed = Buffer.from('00000000000000000000000000000000', 'hex');
25

36
wally.wally_sha256(Buffer.from('test', 'ascii')).then(function(uint8Array) {
47
console.log(Buffer.from(uint8Array).toString('hex'))
@@ -9,17 +12,95 @@ wally.wally_base58_from_bytes(Buffer.from('xyz', 'ascii'), 0).then(function(s) {
912
console.log(Buffer.from(bytes_).toString('ascii'));
1013
});
1114
});
12-
var zeroes = [];
13-
for (var i = 0; i < 16; ++i) {
14-
zeroes.push(0);
15-
}
16-
wally.bip32_key_from_seed(Buffer.from(zeroes), 0x0488ADE4, 0).then(function(s) {
15+
16+
wally.bip32_key_from_seed(seed, 0x0488ADE4, 0).then(function(s) {
1717
wally.wally_base58_from_bytes(s, 1).then(function (s) {
18-
console.log('privkey:', s);
18+
console.log('xpriv m/0:', s);
19+
});
20+
21+
wally.wally_ec_public_key_from_private_key(s.slice(46, 78)).then(function(master_pubkey) {
22+
console.log('M/0: ', Buffer.from(master_pubkey));
1923
});
20-
wally.bip32_pubkey_from_parent(s, 1, 0).then(function (pub) {
21-
wally.wally_base58_from_bytes(pub, 1).then(function (s) {
22-
console.log('pubkey:', s);
24+
25+
wally.bip32_privkey_from_parent(s, 0, 0).then(function (xpriv_0_0) {
26+
wally.wally_base58_from_bytes(xpriv_0_0, 1).then(function (base58_xpriv) {
27+
console.log('xpriv m/0/0:', base58_xpriv);
2328
});
2429
});
30+
31+
wally.bip32_pubkey_from_parent(s, 0, 0).then(function (xpub_0_0) {
32+
wally.wally_base58_from_bytes(xpub_0_0, 1).then(function (base58_xpub) {
33+
console.log('xpub M/0/0:', base58_xpub);
34+
});
35+
36+
wally.bip32_pubkey_from_parent(xpub_0_0, 1, 1).then(function (xpub_0_0_1) {
37+
wally.wally_base58_from_bytes(xpub_0_0_1, 1).then(function (base58_xpub) {
38+
console.log('xpub M/0/0/1:', base58_xpub);
39+
});
40+
41+
var version = Buffer.from('0014', 'hex');
42+
43+
wally.wally_hash160(xpub_0_0_1.slice(45, 78)).then((hash160) => {
44+
return wally.wally_addr_segwit_from_bytes(Buffer.concat([version, Buffer.from(hash160)]),'tb',0);
45+
}).then((addr) => {
46+
console.log('bech32: addr: ', addr)
47+
});
48+
});
49+
});
50+
});
51+
52+
// Multisig Address
53+
wally.bip32_key_from_seed(Buffer.from('00000000000000000000000000000000', 'hex'), 0x0488ADE4, 0)
54+
.then(function(s) {
55+
56+
//Derive child pubkey from parent xpub in bytes
57+
var _pubkey1 = wally.bip32_pubkey_from_parent(s, 1, 0);
58+
var _pubkey2 = wally.bip32_pubkey_from_parent(s, 2, 0);
59+
60+
return Promise.all([_pubkey1, _pubkey2]);
61+
62+
}).then((xpubkeys) => {
63+
const pubkey1 = xpubkeys[0].slice(45, 78);
64+
const pubkey2 = xpubkeys[1].slice(45, 78);
65+
const byt_pubkeys = Buffer.concat([pubkey1, pubkey2]);
66+
67+
// build redeem script
68+
return wally.wally_scriptpubkey_multisig_from_bytes(
69+
byt_pubkeys,
70+
2,
71+
0,
72+
(byt_pubkeys.byteLength / EC_PUBLIC_KEY_LEN) * 34 + 3);
73+
}).then((redeem_script) => {
74+
console.log(Buffer.from(redeem_script).toString('hex'));
75+
76+
// hash redeem script
77+
return wally.wally_hash160(redeem_script);
78+
79+
}).then((script_hash) => {
80+
const prefix = Buffer.from(VERSION_PREFIX_LIQUID, 'hex');
81+
82+
// base58 encode with adding checksum
83+
return wally.wally_base58_from_bytes(Buffer.concat([prefix, script_hash]), 1);
84+
85+
}).then((addr) => {
86+
console.log('multisig addr: ', addr);
87+
});
88+
89+
wally.bip32_key_from_seed(Buffer.from('00000000000000000000000000000000', 'hex'), 0x0488ADE4, 0)
90+
.then(function(s) {
91+
92+
return wally.bip32_pubkey_from_parent(s, 0, 1);
93+
94+
}).then((xpubkey) => {
95+
const pubkey = xpubkey.slice(45, 78);
96+
97+
return wally.wally_hash160(pubkey);
98+
99+
}).then((script) => {
100+
const prefix = Buffer.from('eb', 'hex');
101+
102+
return wally.wally_base58_from_bytes(Buffer.concat([prefix, script]), 1);
103+
104+
}).then((addresses) => {
105+
console.log(addresses);
25106
});

src/wrap_js/makewrappers/templates/nan.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "../include/wally_elements.h"
1212
#include "../include/wally_script.h"
1313
#include <vector>
14-
14+
#include <iostream>
1515
namespace {
1616
1717
static struct wally_operations w_ops;
@@ -287,6 +287,18 @@ def _generate_nan(funcname, f):
287287
postprocessing.extend([
288288
'LocalObject res = AllocateBuffer(res_ptr, out_size, res_size, ret);'
289289
])
290+
elif arg == 'out_bytes_sized_script':
291+
output_args.extend([
292+
'const uint32_t res_size = GetUInt32(info, %s, ret);' % i,
293+
'unsigned char *res_ptr = Allocate(res_size, ret);',
294+
'size_t out_size;',
295+
])
296+
args.append('res_ptr')
297+
args.append('res_size')
298+
args.append('&out_size')
299+
postprocessing.extend([
300+
'LocalObject res = AllocateBuffer(res_ptr, out_size, res_size, ret);'
301+
])
290302
elif arg == 'out_bytes_fixedsized':
291303
output_args.extend([
292304
'const uint32_t res_size%s = GetUInt32(info, %s, ret);' % (i, i),

src/wrap_js/makewrappers/wrap.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def __init__(self, arguments, out_size=None, wally_name=None, nodejs_append_allo
8080
'uint32_t[flags]', 'out_bytes_sized'
8181
], out_size='Math.ceil(_arguments[2].length / 16) * 16 + 16')),
8282

83+
# Script:
84+
('wally_scriptpubkey_multisig_from_bytes', F([
85+
'const_bytes[bytes]', 'uint32_t[threshold]', 'uint32_t[flags]',
86+
'out_bytes_sized'
87+
], out_size='Math.ceil(_arguments[0].length / 33) * 34 + 3')),
88+
8389
# Scrypt:
8490
('wally_scrypt', F([
8591
'const_bytes[passwd]', 'const_bytes[salt]',
@@ -141,6 +147,10 @@ def __init__(self, arguments, out_size=None, wally_name=None, nodejs_append_allo
141147
'bip32_in', 'out_bytes_fixedsized'
142148
], out_size='32')),
143149

150+
('wally_ec_public_key_from_private_key', F([
151+
'const_bytes[key]', 'out_bytes_fixedsized'
152+
], out_size='33')),
153+
144154
('wally_format_bitcoin_message', F([
145155
'const_bytes[message]', 'uint32_t[flags]',
146156
'out_bytes_sized'

src/wrap_js/test/test_script.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var wally = require('../wally');
2+
var test = require('tape');
3+
4+
const EC_PUBLIC_KEY_LEN = 33;
5+
var pubkeys = [
6+
'02ad4199d0c53b564b39798c4c064a6e6093abbb71d56cc153abf75a02f85c8e99',
7+
'03afeefeba0806711b6d3fc7c8b0b6a3eff5ea2ecf938aea1b6a093898097875f3'
8+
];
9+
10+
test('Script', function(t) {
11+
t.plan(1);
12+
13+
var pubkey_bytes = Buffer.from(pubkeys[0] + pubkeys[1], 'hex');
14+
var redeem_script = '522102ad4199d0c53b564b39798c4c064a6e6093abbb71d56cc153abf75a02f85c8e992103afeefeba0806711b6d3fc7c8b0b6a3eff5ea2ecf938aea1b6a093898097875f352ae';
15+
16+
wally.wally_scriptpubkey_multisig_from_bytes(
17+
pubkey_bytes,
18+
2,
19+
0,
20+
(pubkey_bytes.byteLength / EC_PUBLIC_KEY_LEN) * 34 + 3
21+
).then((res) => {
22+
t.equal(Buffer.from(res).toString('hex'), redeem_script);
23+
});
24+
});

0 commit comments

Comments
 (0)