- Generate new Bitcoin P2SH lock address
- Send bitcoin to the lock address
- Send lock tx on Wanchain with lockTime and txid of funding tx
- Wait for storeman response on Wanchain
- Send redeem tx on Wanchain
- Wait for storeman response on Wanchain
to
- the receiving Wanchain accountfrom
- the revoker Bitcoin address (legacy type only)value
- the value to be transferred (in satoshis)storeman
- the storeman (wan/btc) accounts to useredeemKey
- the tx redeem key, including x and xHashtxid
- the id of the BTC tx funding the P2SH lock addresslockTime
- the lockTime of the P2SH lock address
to
- the receiving Wanchain accountredeemKey
- the tx redeem key, including x and xHash
from
- the revoker Bitcoin addresspayTo
- the Bitcoin address where to send funds (optional, defaults tofrom
; 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 addresslockTime
- the lockTime of the P2SH lock addressredeemScript
- 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 thefrom
address, in WIF format
All inbound transactions must start by generating a new P2SH lock address and sending funds to it.
// create lock address
const contract = cctx.buildHashTimeLockContract(opts);
// send btc to it
sendBtc(contract.address, opts.value);
Once the bitcoin transaction is sent and the txid and lockTime are added to the opts, you can continue with either the simple version or advance version.
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.send(opts);
cctx.on('info', info => {
...
Alternatively, you can do the lock and redeem steps separately.
// do lock
cctx.lock(opts);
cctx.on('info', info => {
...
// later, and even maybe elsewhere, do redeem
cctx.redeem(opts);
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, you can manually handle each step of the crosschain transaction.
// fine grain handling
Promise.resolve([]).then(() => {
return cctx.sendLock(opts);
}).then(receipt => {
return cctx.listenLock(opts);
}).then(log => {
...
Once the lockTime expires, you can generate a revoke transaction on Bitcoin,
either by passing in the revoker private key to the buildRevokeTxFromWif
method, or by using hashForRevokeSig
to get the hash for signature, then
signing it and passing it along with the public key of the revoker address to
the buildRevokeTx
method.
Build revoke using WIF
opts.wif = 'cNggJXP2mMNSHA1r9CRd1sv65uykZNyeH8wkH3ZPZVUL1nLfTxRM';
// build revoke tx
const signedTx = cctx.buildRevokeTxFromWif(opts);
Build revoke using sigHash
const bitcoin = require('bitcoinjs-lib');
...
// get hash for signature
const hashForSignature = cctx.hashForRevokeSig(opts);
// sign hash
const keyPair = bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet);
const sigHash = keyPair.sign(new Buffer.from(sigHash, 'hex'));
// build revoke tx
const signedTx = cctx.buildRevokeTx(Object.assign({}, opts, { sigHash }));
Both the buildRevokeTx
and buildRevokeTxFromWif
methods return the signed
transaction in hex format, ready to be sent on to the network through a Bitcoin
node.