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
36wally . 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} ) ;
0 commit comments