- Get outbound fee
- Send lock tx on Wanchain with outbound fee
- Wait for storeman response on Wanchain
- Get txid and lockTime from response
- Send redeem tx on Bitcoin
to
- the redeeming Bitcoin addressfrom
- the sending Wanchain addressvalue
- the value to be transferred (in satoshis)storeman
- the storeman (wan/btc) accounts to useredeemKey
- the tx redeem key, including x and xHash
to
- the redeeming Bitcoin address (legacy type only)payTo
- the Bitcoin address where to send funds (optional, defaults toto
; type legacy, P2SH, or bech32)value
- the value to be transferred (in satoshis, excluding the mining fee)storeman
- the storeman btc accountredeemKey
- the tx redeem key, including x and xHashtxid
- the txid of the tx that funds the P2SH lock address, as reported by the storeman lock confirmationlockTime
- the lockTime of the P2SH lock address, as reported by the storeman lock confirmationredeemScript
- the redeemScript of the P2SH lock addresspublicKey
- the public key of the redeemingto
addresssigHash
- the signature hash of the redeeming tx, signed externally with redeeming private keywif
- use in place ofpublicKey
andsigHash
; the private key of theto
address, in WIF format
from
- the sending Wanchain addressredeemKey
- the tx redeem key, including x and xHash
For outbound bitcoin transactions, the lock process is all on Wanchain, and the redeem process is just on Bitcoin.
Simple Usage: if the specified Wanchain account is open, then you can do the whole crosschain transaction all in one call. You will want to set up event handlers to watch for progress.
cctx.lock(opts);
cctx.on('info', info => {
...
cctx.on('error', err => {
...
cctx.on('complete', res => {
...
Advanced Usage: if you need to handle each step separately, like if some steps need to happen on the client and others on the server, or if you need to sign the transactions manually, you can use WanX to handle each step of the crosschain transaction.
// fine grain handling
Promise.resolve([]).then(() => {
return cctx.getOutboundFee(opts);
}).then(fee => {
opts.outboundFee = fee;
const lockTx = cctx.buildLockTx(opts);
return webwan.eth.sendTransaction(lockTx);
}).then(receipt => {
return cctx.listenLock(opts);
}).then(log => {
...
Once the storeman responds with the lock confirmation, you can then redeem the bitcoin from the lock address. First, you will need to get the lockTime from the storeman lock confirmation event. Then with the lockTime, rebuild the P2SH lock address to derive its redeemScript.
Rebuild P2SH lock address
opts.lockTime = lockTime;
// reconstruct P2SH
const contract = cctx.buildHashTimeLockContract(opts);
opts.redeemScript = contract.redeemScript;
With the lockTime and redeemScript added to opts
, build the redeem
transaction either by passing in the redeemer private key to the
buildRedeemTxFromWif
method, or by using hashForRedeemSig
to get the hash
for signature, then signing it and passing it along with the public key of the
redeemer address to the buildRedeemTx
method.
Build redeem using WIF
opts.wif = 'cNggJXP2mMNSHA1r9CRd1sv65uykZNyeH8wkH3ZPZVUL1nLfTxRM';
// build redeem tx
const signedTx = cctx.buildRedeemTxFromWif(opts);
Build redeem using sigHash
const bitcoin = require('bitcoinjs-lib');
...
const publicKey = '03e55a948b017ad25994cbe3e10842bffc8835054f56528fe2ed32b9e6ec853e4c';
// get hash for signature
const hashForSignature = cctx.hashForRedeemSig(opts);
// sign hash
const keyPair = bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet);
const sigHash = keyPair.sign(new Buffer.from(hashForSignature, 'hex'));
// build redeem tx
const signedTx = cctx.buildRedeemTx(Object.assign({}, opts, { sigHash, publicKey }));
Both the buildRedeemTx
and buildRedeemTxFromWif
methods return the signed
transaction in hex format, ready to be sent on to the network through a Bitcoin
node.