Skip to content

Commit eabb934

Browse files
committed
Migrate example to test
1 parent 7a5dd99 commit eabb934

File tree

3 files changed

+81
-95
lines changed

3 files changed

+81
-95
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ endif
283283
$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_bip39.js
284284
$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_hash.js
285285
$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_scrypt.js
286+
$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_script.js
286287
endif
287288
endif # SHARED_BUILD_ENABLED
288289
endif # RUN_TESTS

src/wrap_js/example.js

Lines changed: 6 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
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');
1+
var wally = require('./wally');
52

63
wally.wally_sha256(Buffer.from('test', 'ascii')).then(function(uint8Array) {
74
console.log(Buffer.from(uint8Array).toString('hex'))
@@ -13,94 +10,14 @@ wally.wally_base58_from_bytes(Buffer.from('xyz', 'ascii'), 0).then(function(s) {
1310
});
1411
});
1512

13+
var seed = Buffer.from('00000000000000000000000000000000', 'hex');
1614
wally.bip32_key_from_seed(seed, 0x0488ADE4, 0).then(function(s) {
1715
wally.wally_base58_from_bytes(s, 1).then(function (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));
16+
console.log('privkey:', s);
2317
});
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);
18+
wally.bip32_pubkey_from_parent(s, 1, 0).then(function (pub) {
19+
wally.wally_base58_from_bytes(pub, 1).then(function (s) {
20+
console.log('pubkey:', s);
2821
});
2922
});
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);
10623
});

src/wrap_js/test/test_bip32.js

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
var wally = require('../wally');
22
var test = require('tape');
3+
const EC_PUBLIC_KEY_LEN = 33;
4+
var seed = Buffer.from('00000000000000000000000000000000', 'hex');
35

46
test('BIP32 from seed + derivation', function(t) {
5-
t.plan(2);
6-
var zeroes = [];
7-
for (var i = 0; i < 16; ++i) {
8-
zeroes.push(0);
9-
}
10-
wally.bip32_key_from_seed(Buffer.from(zeroes), 0x0488ADE4, 0).then(function(s) {
7+
t.plan(7);
8+
wally.bip32_key_from_seed(Buffer.from(seed), 0x0488ADE4, 0).then(function(s) {
119
wally.wally_base58_from_bytes(s, 1).then(function (s) {
1210
t.equal(
1311
s,
@@ -16,6 +14,7 @@ test('BIP32 from seed + derivation', function(t) {
1614
'privkey'
1715
);
1816
});
17+
1918
wally.bip32_pubkey_from_parent(s, 1, 0).then(function (pub) {
2019
wally.wally_base58_from_bytes(pub, 1).then(function (s) {
2120
t.equal(
@@ -26,5 +25,74 @@ test('BIP32 from seed + derivation', function(t) {
2625
);
2726
});
2827
});
28+
29+
wally.wally_ec_public_key_from_private_key(s.slice(46, 78)).then(function(master_pubkey) {
30+
t.equal(
31+
Buffer.from(master_pubkey).toString('hex'),
32+
'02be99138b48b430a8ee40bf8b56c8ebc584c363774010a9bfe549a87126e61746',
33+
'M/0'
34+
);
35+
});
36+
37+
wally.bip32_privkey_from_parent(s, 0, 0).then(function (xpriv_0_0) {
38+
wally.wally_base58_from_bytes(xpriv_0_0, 1).then(function (base58_xpriv) {
39+
t.equal(
40+
base58_xpriv,
41+
'xprv9u4S6Taa3k3GxnaHfWzboKwLPPPHpDyDHdLGqDArBejguBuv6GkerLy6MtAeFfo9RDfZy22FWEc1ExEShuRGZJpgVgeVu5KZ5obWbV2R3D2',
42+
'xpriv m/0/0'
43+
);
44+
});
45+
});
46+
47+
wally.bip32_pubkey_from_parent(s, 0, 0).then(function (xpub_0_0) {
48+
wally.wally_base58_from_bytes(xpub_0_0, 1).then(function (base58_xpub) {
49+
t.equal(
50+
base58_xpub,
51+
'xpub683nVy7Tt7baBGekmYXcATt4wRDnDgh4erFsdbaTjzGfmzF4dp4uQ9HaDCdvSqctrsbxZey5wozKyyy2J3zhDDHU3UhW4uCFQp6bESv8ewQ',
52+
'xpub M/0/0'
53+
);
54+
});
55+
56+
wally.bip32_pubkey_from_parent(xpub_0_0, 1, 1).then(function (xpub_0_0_1) {
57+
wally.wally_base58_from_bytes(xpub_0_0_1, 1).then(function (base58_xpub) {
58+
t.equal(
59+
base58_xpub,
60+
'xpub6An6e2ai6kSDnnxJ3876JwfeigdQu9YNudcP7ayT828xDFzFQkP9oBoBNdvj7xDrDQd9TQDpzkLhM5L71rFDTmxMuzSvXwZKnLx56Es6MEg',
61+
'xpub M/0/0/1'
62+
);
63+
});
64+
65+
var version = Buffer.from('0014', 'hex');
66+
wally.wally_hash160(xpub_0_0_1.slice(45, 78)).then((hash160) => {
67+
return wally.wally_addr_segwit_from_bytes(Buffer.concat([version, Buffer.from(hash160)]),'tb',0);
68+
}).then((addr) => {
69+
t.equal(
70+
addr,
71+
'tb1q6pqwl5tyaluz8qtqky7x2cmxzqdr6gsyld4hrn',
72+
'bech32: addr'
73+
);
74+
});
75+
});
76+
});
77+
});
78+
});
79+
80+
test('BIP32 from seed to address', function(t) {
81+
t.plan(1);
82+
83+
wally.bip32_key_from_seed(seed, 0x0488ADE4, 0).then(function(s) {
84+
return wally.bip32_pubkey_from_parent(s, 0, 1);
85+
}).then((xpubkey) => {
86+
const pubkey = xpubkey.slice(45, 78);
87+
return wally.wally_hash160(pubkey);
88+
}).then((script) => {
89+
const prefix = Buffer.from('eb', 'hex');
90+
return wally.wally_base58_from_bytes(Buffer.concat([prefix, script]), 1);
91+
}).then((address) => {
92+
t.equal(
93+
address,
94+
'2dmvtD27wpRyLK79FsAidyS33uUogsYNC4U',
95+
'address'
96+
);
2997
});
3098
});

0 commit comments

Comments
 (0)