I tested Wanchain

Nicolas Cantu
Mar 21 · 9 min read

I followed the following example:

The environment I used

I am using VirtualBox with Windows:

Configuration:

  • OS type : Linux - Debian 64 Bit
  • Disk type : VDI 700 Go (dynamic)
  • Memory : 8 Go
  • Video memory: 16Mo
  • CD Rom: debian-testing-amd64-netinst.iso

Result:

Note:

If you need to extend the VDI size (Windows):

> “D:\Program Files\Oracle\VirtualBox\VBoxManage.exe” modifyhd “D:\Users\Chain Accelerator\VirtualBox VMs\wanchain\wanchain.vdi” — resize 700000"

My Debian testing (buster)

Install Debian:

  • without root password (sudo actived)
  • Default Debian interface
  • No other requirement

Debian first run:

chainacc@wanchain:~$ cd ~chainacc@wanchain:~$ sudo apt-get updatechainacc@wanchain:~$ sudo apt-get upgradechainacc@wanchain:~$ sudo apt-get install -y vim emacs ntpdatechainacc@wanchain:~$ sudo ntpdate-debianchainacc@wanchain:~$ cd ~

Installing Wanchain prerequisites

chainacc@wanchain:~$ cd ~chainacc@wanchain:~$ sudo apt-get install -y build-essential wget git golang npm gconf2 libappindicator1chainacc@wanchain:~$ cd ~

Working with Wanchain

My Wanchain blockchain install

chainacc@wanchain:~$ cd ~chainacc@wanchain:~$ git clone https://github.com/wanchain/go-wanchain.gitchainacc@wanchain:~$ cd go-wanchainchainacc@wanchain:~/go-wanchain$ make gwanchainacc@wanchain:~$ cd ~

My Wanchain testnet setup

chainacc@wanchain:~$ cd ~chainacc@wanchain:~$ wget https://wanchain.org/download/linwallet3chainacc@wanchain:~$ sudo dpkg -i WanWalletGui-linux64–3.0.26.debchainacc@wanchain:~$ sudo apt — fix-broken installchainacc@wanchain:~$ wanwanwalletgui (sync full, at the end : exit app)chainacc@wanchain:~$ cd ~

My Wanchain testnet run

I want to run it on localhost “rpcaddr 0.0.0.0” and on port 18545 “rpcport 18545

chainacc@wanchain:~$ cd ~chainacc@wanchain:~$ .config/WanWalletGui/binaries/Gwan/unpacked/gwan — testnet — verbosity 4 — maxpeers 500 — maxpendpeers 100 — gasprice 180000000000 — txpool.nolocals — rpc — rpcaddr 0.0.0.0 — rpcport 18545 — rpcapi “debug,eth,personal,net,admin,wan,txpool”chainacc@wanchain:~$ cd ~

Wanchain testnet faucet

Create a Wanchain address

chainacc@wanchain:~$ cd ~chainacc@wanchain:~$ cd ~/.config/WanWalletGui/binaries/Gwan/unpacked/gwan — testnet account new

Passphrase:
Repeat passphrase:
Address: {3fEa542162F4C8ec83Be74664A9Cbd61716F676A}

  • Copy the Wanchain address
~/.config/WanWalletGui/binaries/Gwan/unpacked/gwan — testnet account update 2c9b4DaBaD6Cc1d0eC23705b31ea97bd50635A72

unlocking account 2c9b4DaBaD6Cc1d0eC23705b31ea97bd50635A72 | INFO [01–31|21:54:48] Unlocked account address=0x2c9b4DaBaD6Cc1d0eC23705b31ea97bd50635A72

  • Copy the Wanchain legacy address

Get some WAN for testing from the address copied:

Working with Bitcoin

chainacc@wanchain:~$ cd ~chainacc@wanchain:~$ wget https://bitcoin.org/bin/bitcoin-core-0.17.1/bitcoin-0.17.1-x86_64-linux-gnu.tar.gzchainacc@wanchain:~$ tar xvf bitcoin-0.17.1-x86_64-linux-gnu.tar.gzchainacc@wanchain:~$ cd bitcoin-0.17.1/binchainacc@wanchain:~/bitcoin-0.17.1/bin$ ./bitcoind — testnet

Bitcoin testnet faucet

chainacc@wanchain:~/bitcoin-0.17.1/bin$ ./bitcoin-cli -testnet getnewaddress ‘’ legacy

n3JCvbHzyDJYiuvQtdo3Re14mcik5Q74P6

  • Copy the BTC address
chainacc@wanchain:~/bitcoin-0.17.1/bin$ ./bitcoin-cli -testnet dumpprivkey n3JCvbHzyDJYiuvQtdo3Re14mcik5Q74P6

cRmnGdm2NFi24NzC3ZEa3Ha8CDmeyGGwDfi4zhLW8rhjn94NpcNH

Copy the WIF of the redeemer bitcoin address

chainacc@wanchain:~/bitcoin-0.17.1/bin$ cd ~

Get some BTC for testing from the address copied:

Bitcoin testnet RPC Configuration

chainacc@wanchain:~/bitcoin-0.17.1/bin$ cd ~/.bitcoin/testnet3chainacc@wanchain:~/.bitcoin/testnet3 $ vim bitcoin.conf

Insert this config :

testnet=1server=1rpcuser=wanchainAccrpcpassword=w@nch@1n@ccrpcport=18332txindex=1

Bitcoin testnet node run

chainacc@wanchain:~/bitcoin-0.17.1/bin$ ./bitcoind -conf=~/.bitcoin/testnet3 -daemon

My Wanchain project install

chainacc@wanchain:~$ cd ~chainacc@wanchain:~$ git clone https://github.com/wanchain/wanxchainacc@wanchain:~$ cd wanxchainacc@wanchain:~/wanx$ npm updatechainacc@wanchain:~/wanx$ npm cache verifychainacc@wanchain:~/wanx$ npm installchainacc@wanchain:~/wanx$ npm install node-bitcoin-rpc wanchainjs-txchainacc@wanchain:~/wanx$ npm install keythereum

Problem here :

Resolved by update v8.11.2 to v11.9.0 :

chainacc@wanchain:~/wanx$ wget https://nodejs.org/dist/v11.9.0/node-v11.9.0-linux-x64.tar.xzchainacc@wanchain:~/wanx$ sudo tar -C /usr/local — strip-components 1 -xJf node-v11.9.0-linux-x64.tar.xzchainacc@wanchain:~/wanx$ npm install keythereum

After :

chainacc@wanchain:~/wanx$ cd exampleschainacc@wanchain:~/wanx/examples$ cp btc2wbtc-complete-manual.js btc2wbtc-test.jschainacc@wanchain:~$ cd ~

My Wanchain project

chainacc@wanchain:~$ cd ~/wanx/exampleschainacc@wanchain:~/wanx/examples$ vim btc2wbtc-test.js
  • Line 17–18: Checked localhost and port const
config = { wanchain: { url: ‘http://localhost:18545' },};
  • Line 23: Updated user and password btc node (rpc access)
const btcNode = [ ‘localhost’, 18332, ‘wanchainAcc’, ‘w@nch@1@cc’ ];
  • Line 36–38: Update the address and amount
  • Legacy BTC address : n3JCvbHzyDJYiuvQtdo3Re14mcik5Q74P6
  • Legacy Wanchain address : 0x2c9b4DaBaD6Cc1d0eC23705b31ea97bd50635A72
  • Amount in satoshis : 210000
const opts = {
from: ‘n3JCvbHzyDJYiuvQtdo3Re14mcik5Q74P6’,
to: ‘0x2c9b4DaBaD6Cc1d0eC23705b31ea97bd50635A72’,
value: ‘210000’,
storeman: {
wan: ‘0x9ebf2acd509e0d5f9653e755f26d9a3ddce3977c’,
btc: ‘0x83e5ca256c9ffd0ae019f98e4371e67ef5026d2d’,
},
  • Line 52: update the path to the keystore file (user => youruser)
const wanDatadir = ‘/home/chainacc/.wanchain/testnet/’;
  • Line 54: update the keystore password
const wanPrivateKey = keythereum.recover(‘w@np@ssw0rd’, wanKeyObject);

I run the script

chainacc@wanchain:~$ cd ~/wanx/exempleschainacc@wanchain:~/wanx/exemples$ node btc2wbtc-test.js

Problem here :

internal/modules/cjs/loader.js:611
throw err;
^
Error: Cannot find module ‘web3-requestManager’
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:609:15)
at Function.Module._load (internal/modules/cjs/loader.js:535:25)

Fixed by:

chainacc@wanchain:~/wanx/exemples$ cd ..chainacc@wanchain:~/wanx$ npm install web3chainacc@wanchain:~/wanx/exemples$ node btc2wbtc-test.js

New problem :

node btc2wbtc-test.js 
/home/chainacc/wanx/node_modules/keythereum/index.js:538
throw new Error(“could not find key file for address “ + address);
^
Error: could not find key file for address 2c9b4dabad6cc1d0ec23705b31ea97bd50635a72
at Object.importFromFile (/home/chainacc/wanx/node_modules/keythereum/index.js:538:15)

Fixed by:

  • check the wanchain dir in chainacc@wanchain:~/wanx/examples$ vim btc2wbtc-test.js
const wanDatadir = ‘/home/chainacc/.wanchain/testnet/’;
  • chainacc@wanchain:~/wanx/examples$ ls ~/.wanchain/testnet/keystore/

/home/chainacc/.wanchain/testnet/keystore/UTC — 2019–02–06T13–13–22.034149585Z — 2c9b4DaBaD6Cc1d0eC23705b31ea97bd50635A72

  • Renaming in lowercase the key : chainacc@wanchain:~/wanx/examples$ mv /home/chainacc/.wanchain/testnet/keystore/UTC — 2019–02–06T13–13–22.034149585Z — 2c9b4DaBaD6Cc1d0eC23705b31ea97bd50635A72 /home/chainacc/.wanchain/testnet/keystore/UTC — 2019–02–06T13–13–22.034149585Z — 2c9b4dabad6Cc1d0eC23705b31ea97bd50635a72
  • Updating : wanx/examples/btc2wbtc-test.js
// Define the transaction opts 
const opts = {
from: ‘n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3’,
to: ‘0x2c9b4dabad6cc1d0ec23705b31ea97bd50635a72’, // (lowercase)
value: ‘210000’,
storeman: {
wan: ‘0x9ebf2acd509e0d5f9653e755f26d9a3ddce3977c’,
btc: ‘0x83e5ca256c9ffd0ae019f98e4371e67ef5026d2d’,
},

New problem :

(node:5120) V8: /home/chainacc/wanx/node_modules/keythereum/lib/scrypt.js:2431 Linking failure in asm.js: Invalid heap sizeStarting btc inbound lock { from: ‘n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3’,
to: ‘0x2c9b4dabad6cc1d0ec23705b31ea97bd50635a72’,
value: ‘210000’,
storeman:
{ wan: ‘0x9ebf2acd509e0d5f9653e755f26d9a3ddce3977c’,
btc: ‘0x83e5ca256c9ffd0ae019f98e4371e67ef5026d2d’ },
redeemKey:
{ x:
‘3700d76795520e1be4ba75a532583d07453f157b77b125e9e4be76f80f4b41b5’,
xHash:
‘942e29100d8c518c9dad73eda3d93960492bc2f4d58ce397d7a1a54a9ee7e66f’ } }
Created new BTC contract { address: ‘2N9sGNxs91ATEkJZXJj7Ugw67zwTsgEsNGz’,
redeemScript:
‘63a820942e29100d8c518c9dad73eda3d93960492bc2f4d58ce397d7a1a54a9ee7e66f8876a91483e5ca256c9ffd0ae019f98e4371e67ef5026d2d67049bd3935cb17576a914d846ee271810245fc535cf8d5f53c8cf568ee8de6888ac’,
xHash:
‘942e29100d8c518c9dad73eda3d93960492bc2f4d58ce397d7a1a54a9ee7e66f’,
lockTime: 1553191835,
redeemer: ‘0x83e5ca256c9ffd0ae019f98e4371e67ef5026d2d’,
revoker: ‘0xd846ee271810245fc535cf8d5f53c8cf568ee8de’ }
Send amount 0.0021
Error: 401

And on the Bitcoin node this output: “2019–03–21T17:21:37Z ThreadRPCServer incorrect password attempt from 127.0.0.1:55144"

Fixed by:

  • Checking : ~/wanx/examples/btc2wbtc-test.js
const btcNode = [ ‘localhost’, 18332, ‘wanchainacc’, ‘w@nch@1n@cc’ ];
  • Checking : ~/.bitcoin/testnet3/bitcoin.conf
server=1
rpcport=18332
rpcuser=wanchainacc
rpcpassword=w@nch@1n@cc
rpcpport=18332
txindex=1

With fixing syntax (oups) it’s works better :)

But still a “Invalid heap size”:

chainacc@wanchain:~/wanx/examples$ node btc2wbtc-test.js
(node:8528) V8: /home/chainacc/wanx/node_modules/keythereum/lib/scrypt.js:2431 Linking failure in asm.js: Invalid heap size
Starting btc inbound lock { from: ‘n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3’,
to: ‘0x2c9b4dabad6cc1d0ec23705b31ea97bd50635a72’,
value: ‘210000’,
storeman:
{ wan: ‘0x9ebf2acd509e0d5f9653e755f26d9a3ddce3977c’,
btc: ‘0x83e5ca256c9ffd0ae019f98e4371e67ef5026d2d’ },
redeemKey:
{ x:
‘e1139479044f974fd2e97c1d1659f702ccdee7888fd1093386e967f678150699’,
xHash:
‘5799aa9e5008a673d28ca2741f91380919340ef85c1645287ad24786b2e785d6’ } }
Created new BTC contract { address: ‘2MyJmAfXjWqXbEXGxtHsUTrMnyWJuRM8v95’,
redeemScript:
‘63a8205799aa9e5008a673d28ca2741f91380919340ef85c1645287ad24786b2e785d68876a91483e5ca256c9ffd0ae019f98e4371e67ef5026d2d67046d4a945cb17576a914d846ee271810245fc535cf8d5f53c8cf568ee8de6888ac’,
xHash:
‘5799aa9e5008a673d28ca2741f91380919340ef85c1645287ad24786b2e785d6’,
lockTime: 1553222253,
redeemer: ‘0x83e5ca256c9ffd0ae019f98e4371e67ef5026d2d’,
revoker: ‘0xd846ee271810245fc535cf8d5f53c8cf568ee8de’ }
Send amount 0.0021
BTC tx sent 34c4479448f68aff164b2b09bde0684209a2f33878d99012614ecd26f5bdaa27

In the case the revoker is the btc address. The 0xd846ee.. is the base58 hash of the btc address

Result in details :

https://api.blockcypher.com/v1/btc/test3/addrs/n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3/full?limit=50

{
"address": "n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3",
"total_received": 8175444,
"total_sent": 210226, (+ fees)
"balance": 7965218,
"unconfirmed_balance": 0,
"final_balance": 7965218,
"n_tx": 2,
"unconfirmed_n_tx": 0,
"final_n_tx": 2,
"txs": [
{
"block_hash": "000000000001caaa8d75c7c228329ca0d5d213cff0fa92c71e91b4de97ff2af3",
"block_height": 1485513,
"block_index": 85,
"hash": "ed046b1d25ea4d43899bd3366522eae242f0b368f1f2486128e2db744fb96e9a",
"addresses": [
"2NEWHiYYpPAy1weH2aTibLv1JsJd141bBwb",
"n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3"
],
"total": 8175218,
"fees": 226,
"size": 223,
"preference": "low",
"relayed_by": "3.88.96.133:18333",
"confirmed": "2019-03-21T19:42:40Z",
"received": "2019-03-21T19:25:44.604Z",
"ver": 2,
"double_spend": false,
"vin_sz": 1,
"vout_sz": 2,
"confirmations": 1,
"confidence": 1,
"inputs": [
{
"prev_hash": "34c4479448f68aff164b2b09bde0684209a2f33878d99012614ecd26f5bdaa27",
"output_index": 1,
"script": "47304402204bc0a03694ae02f5c66ff52f51736b17a34d81c9d4ca25892cfea2e8b31e505b022060c082b32d06e16ee73b33fa5a3e3c7b5fb287c5a5b118fb91cb312c9b813651012103a4308b87ad26b12f8777b09e71e0a07a79d28fe7f337d8ad548b1a2a93658046",
"output_value": 8175444,
"sequence": 4294967294,
"addresses": [
"n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3"
],
"script_type": "pay-to-pubkey-hash",
"age": 1485509
}
],
"outputs": [
{
"value": 210000,
"script": "a914e933fe4f0a6a0c9f9ca368fb2902488ef92deddb87",
"addresses": [
"2NEWHiYYpPAy1weH2aTibLv1JsJd141bBwb"
],
"script_type": "pay-to-script-hash"
},
{
"value": 7965218,
"script": "76a914d846ee271810245fc535cf8d5f53c8cf568ee8de88ac",
"addresses": [
"n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3"
],
"script_type": "pay-to-pubkey-hash"
}
]
},
{
"block_hash": "000000000000005ca5c4f39371f8ef03010b1edc426bf4bf8a4b585279a8b965",
"block_height": 1485509,
"block_index": 27,
"hash": "34c4479448f68aff164b2b09bde0684209a2f33878d99012614ecd26f5bdaa27",
"addresses": [
"2My6867KrD36eS3jGpuQvNhgMeyWzWeuq3M",
"2MyJmAfXjWqXbEXGxtHsUTrMnyWJuRM8v95",
"n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3"
],
"total": 8385444,
"fees": 423,
"size": 140,
"preference": "low",
"relayed_by": "3.88.96.133:18333",
"confirmed": "2019-03-21T18:43:16Z",
"received": "2019-03-21T18:37:34.182Z",
"ver": 2,
"double_spend": false,
"vin_sz": 1,
"vout_sz": 2,
"confirmations": 5,
"confidence": 1,
"inputs": [
{
"prev_hash": "0b298896353e795e2399f36be3b9e714a346144204a418869169b9f82797aafe",
"output_index": 1,
"script": "16001417c75f762262104a9e4f68f29025a2bc2a8dcfdf",
"output_value": 8385867,
"sequence": 4294967294,
"addresses": [
"2My6867KrD36eS3jGpuQvNhgMeyWzWeuq3M"
],
"script_type": "pay-to-script-hash",
"age": 1454376,
"witness": [
"304402200a8fcb06626d0f6f7f1bb6e99153a0b1de4fc62223e3157d890f12c69aeb1d520220578655a5d58d7c876c5924bf6e8d121c2e9b3dea139afa1488ff08aa49d676b101",
"03a22f9a163ba741e1c058277ab19efa44f2b21792936531202e791b0ebf34aa2b"
]
}
],
"outputs": [
{
"value": 210000,
"script": "a914427c078cf598db175e63e06f99fc75e98e00945487",
"addresses": [
"2MyJmAfXjWqXbEXGxtHsUTrMnyWJuRM8v95"
],
"script_type": "pay-to-script-hash"
},
{
"value": 8175444,
"script": "76a914d846ee271810245fc535cf8d5f53c8cf568ee8de88ac",
"spent_by": "ed046b1d25ea4d43899bd3366522eae242f0b368f1f2486128e2db744fb96e9a",
"addresses": [
"n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3"
],
"script_type": "pay-to-pubkey-hash"
}
]
}
]
}
  • Find the wanchain account (to) at 18.217.171.41 (Wanchain testnet explorer) :

http://18.217.171.41/storeman/0x9ebf2acd509e0d5f9653e755f26d9a3ddce3977c

  • Find the BTC contract :

https://api.blockcypher.com/v1/btc/test3/addrs/2MyJmAfXjWqXbEXGxtHsUTrMnyWJuRM8v95/full?limit=50

{
"address": "2MyJmAfXjWqXbEXGxtHsUTrMnyWJuRM8v95",
"total_received": 210000,
"total_sent": 0,
"balance": 210000,
"unconfirmed_balance": 0,
"final_balance": 210000,
"n_tx": 1,
"unconfirmed_n_tx": 0,
"final_n_tx": 1,
"txs": [
{
"block_hash": "000000000000005ca5c4f39371f8ef03010b1edc426bf4bf8a4b585279a8b965",
"block_height": 1485509,
"block_index": 27,
"hash": "34c4479448f68aff164b2b09bde0684209a2f33878d99012614ecd26f5bdaa27",
"addresses": [
"2My6867KrD36eS3jGpuQvNhgMeyWzWeuq3M",
"2MyJmAfXjWqXbEXGxtHsUTrMnyWJuRM8v95",
"n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3"
],
"total": 8385444,
"fees": 423,
"size": 140,
"preference": "low",
"relayed_by": "3.88.96.133:18333",
"confirmed": "2019-03-21T18:43:16Z",
"received": "2019-03-21T18:37:34.182Z",
"ver": 2,
"double_spend": false,
"vin_sz": 1,
"vout_sz": 2,
"confirmations": 6,
"confidence": 1,
"inputs": [
{
"prev_hash": "0b298896353e795e2399f36be3b9e714a346144204a418869169b9f82797aafe",
"output_index": 1,
"script": "16001417c75f762262104a9e4f68f29025a2bc2a8dcfdf",
"output_value": 8385867,
"sequence": 4294967294,
"addresses": [
"2My6867KrD36eS3jGpuQvNhgMeyWzWeuq3M"
],
"script_type": "pay-to-script-hash",
"age": 1454376,
"witness": [
"304402200a8fcb06626d0f6f7f1bb6e99153a0b1de4fc62223e3157d890f12c69aeb1d520220578655a5d58d7c876c5924bf6e8d121c2e9b3dea139afa1488ff08aa49d676b101",
"03a22f9a163ba741e1c058277ab19efa44f2b21792936531202e791b0ebf34aa2b"
]
}
],
"outputs": [
{
"value": 210000,
"script": "a914427c078cf598db175e63e06f99fc75e98e00945487",
"addresses": [
"2MyJmAfXjWqXbEXGxtHsUTrMnyWJuRM8v95"
],
"script_type": "pay-to-script-hash"
},
{
"value": 8175444,
"script": "76a914d846ee271810245fc535cf8d5f53c8cf568ee8de88ac",
"spent_by": "ed046b1d25ea4d43899bd3366522eae242f0b368f1f2486128e2db744fb96e9a",
"addresses": [
"n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3"
],
"script_type": "pay-to-pubkey-hash"
}
]
}
]
}

What we have done ?

We “pass” a BTC to a WanchainBTC, so it’s a cross chain script:

  • Requirements :
const WanX = require(‘../’);
const Web3 = require(‘web3’);
const keythereum = require(‘keythereum’);
const WanTx = require(‘wanchainjs-tx’);
const moment = require(‘moment’);
const bitcoinRpc = require(‘node-bitcoin-rpc’);
const BigNumber = require(‘bignumber.js’);
const btcUtils = require(‘./btc-utils’);/**
* Requirements:
* — Bitcoin from address has enough to cover specified amount
* — Wanchain WAN balance covers the gas to redeem
*/
  • Rpc config :
const config = {
wanchain: { url: ‘http://localhost:18545' },
}
const wanx = new WanX(‘testnet’, config);
const btcNode = [ ‘localhost’, 18332, ‘wanchainacc’, ‘w@nch@1n@cc’ ];
bitcoinRpc.init(…btcNode);
bitcoinRpc.setTimeout(2000);
const web3wan = new Web3(new Web3.providers.HttpProvider(config.wanchain.url));
  • Transaction config :
// New crosschain transaction
// bitcoin, inbound
const cctx = wanx.newChain(‘btc’, true);
// Define the transaction opts
const opts = {
from: ‘n1EXDp3a1AxCfFsqPc2YAp7wUt1cbBsAm3’,
to: ‘0x2c9b4dabad6cc1d0ec23705b31ea97bd50635a72’,
value: ‘210000’,
storeman: {
wan: ‘0x9ebf2acd509e0d5f9653e755f26d9a3ddce3977c’,
btc: ‘0x83e5ca256c9ffd0ae019f98e4371e67ef5026d2d’,
},
// Generate a new redeemKey
redeemKey: wanx.newRedeemKey(‘sha256’),
// lockTime is optional
// lockTime: moment().add(8, ‘h’).unix(),
};
  • Key config :
// Get Wanchain private keys
const wanDatadir = ‘/home/chainacc/.wanchain/testnet/’;
const wanKeyObject = keythereum.importFromFile(opts.to, wanDatadir);
const wanPrivateKey = keythereum.recover(‘w@np@ssw0rd’, wanKeyObject);
// Do inbound BTC to WBTC transaction
Promise.resolve([]).then(() => {
console.log(‘Starting btc inbound lock’, opts);
  • Contract config :
// Create new P2SH lock address
const contract = cctx.buildHashTimeLockContract(opts);
console.log(‘Created new BTC contract’, contract);// Add the lockTime to opts
opts.lockTime = contract.lockTime;
// Convert BTC amount from satoshis to bitcoin
const sendAmount = (new BigNumber(opts.value)).div(100000000).toString();
console.log(‘Send amount’, sendAmount);
  • Sending BTC :
// Send BTC to P2SH lock address
return btcUtils.sendBtc(bitcoinRpc, contract.address, sendAmount, opts.from);
}).then(txid => {// Add txid to opts
opts.txid = txid;
console.log(‘BTC tx sent’, txid);// Get the tx count to determine next nonce
return web3wan.eth.getTransactionCount(opts.to);
}).then(txCount => {// Get the raw lock tx
const lockTx = cctx.buildLockTx(opts);
// Add nonce to tx
lockTx.nonce = web3wan.utils.toHex(txCount);
// Sign and serialize the tx
const transaction = new WanTx(lockTx);
transaction.sign(wanPrivateKey);
const serializedTx = transaction.serialize().toString(‘hex’);
// Send the lock transaction on Wanchain
return web3wan.eth.sendSignedTransaction(‘0x’ + serializedTx);
}).then(receipt => {
  • Lock des BTC :
console.log(‘Lock submitted and now pending on storeman’);
console.log(receipt);
// Scan for the lock confirmation from the storeman
return cctx.listenLock(opts, receipt.blockNumber);
}).then(log => {console.log(‘Lock confirmed by storeman’);
console.log(log);
// Get the tx count to determine next nonce
return web3wan.eth.getTransactionCount(opts.to);
}).then(txCount => {
  • Sending Redeem :
// Get the raw redeem tx
const redeemTx = cctx.buildRedeemTx(opts);
// Add nonce to tx
redeemTx.nonce = web3wan.utils.toHex(txCount);
// Sign and serialize the tx
const transaction = new WanTx(redeemTx);
transaction.sign(wanPrivateKey);
const serializedTx = transaction.serialize().toString(‘hex’);
// Send the lock transaction on Wanchain
return web3wan.eth.sendSignedTransaction(‘0x’ + serializedTx);
}).then(receipt => {console.log(‘Redeem submitted and now pending on storeman’);
console.log(receipt);
  • Check for confirmations
// Scan for the lock confirmation from the storeman
return cctx.listenRedeem(opts, receipt.blockNumber);
}).then(log => {console.log(‘Redeem confirmed by storeman’);
console.log(log);

Wanchain, It’s works !

console.log(‘COMPLETE!!!’);}).catch(err => {console.log(‘Error:’, err);});

In-house partners
Event partner corporates
Partner experts

Let us know which offer you are interested in and apply here => this form.

Don’t forget to follow us on social medias : twitter, telegram, slack

Chain Accelerator

Global network for blockchain startup acceleration

Nicolas Cantu

Written by

Chain Accelerator

Global network for blockchain startup acceleration