npm install --save etherex/btc-swap
var BtcSwap = require('btc-swap');
var client = new BtcSwap({
address: "0x4491959fe1772faa7332464b0e7f1aa9aa2d8446", // Address of the BtcSwap contract
host: "localhost:8545", // Ethereum node
from: "0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826", // Ethereum account
testnet: true, // Use BTC testnet, defaults to true
debug: true
});
For any method with a failure
callback, any error will fire that callback
before aborting the execution of that method.
Returns a ticket object specified by ticketId
to the success
callback.
Create a ticket for btcAddress
of numEther
for btcTotal
. The success
callback gets fired when the Ethereum transaction is sent and returns the
transaction hash. The completed
callback gets fired when the transaction
is mined, returns the same transaction hash as first parameter and the created
ticket object as second parameter.
Reserve a ticket with ID ticketId
, with the BTC transaction hash txHash
and computed nonce powNonce
(see computePoW()
). The success
callback
gets fired when the Ethereum transaction is sent, returns the ticket ID as
first parameter and the Ethereum transaction hash as second parameter. The
completed
callback gets fired when the transaction is mined and returns the
reserved ticket object.
feeWei, success, completed, failure)Claim a ticket with ID
ticketId, with the signed BTC transaction hex
txHex, the BTC transaction hash
txHash, BTC transaction index
txIndex, BTC merkle siblings
merkleSiblingand BTC block hash
txBlockHash.
feeWeican be obtained by calling
getFeeAmount(txBlockHash)and will be the transaction's value if it's above zero. The
successcallback gets fired when the Ethereum transaction is sent and returns the Ethereum transaction hash. The
completed`
callback gets fired when the transaction is mined and returns the ticket ID.
Cancel a ticket with ticketId
, if the ticket is still reservable and by
the creator of that ticket only. The success
callback gets fired when the
Ethereum transaction is sent and returns the ticket ID as first parameter and
the Ethereum transaction hash as second parameter. The completed
callback gets
fired when the transaction is mined and returns the ticket ID that was just
canceled.
Returns open ticket IDs. Use with lookupTicket()
to load all currently open
tickets.
Sets a global watch filter for all ticket events, which calls ticketEvent
that
returns new
, reserved
or removed
as first parameter, and the ticket ID
as second parameter. Use this to watch tickets created, reserved, claimed or
canceled by other users. Take note that this filter will also trigger on the
same events as the other methods' completed
callbacks.
Compute a nonce for a ticket with ID ticketId
, with the BTC transaction hash
txHash
. The success
callback gets fired when a nonce is found.
Verify a nonce for a ticket with ID ticketId
, with the BTC transaction hash
txHash
and previously computed nonce
. The success
callback gets fired
when the nonce is found to be valid with a success message, same with the
failure
callback when the nonce is invalid.
Generate an intermediate wallet to be used for reserving and claiming a ticket.
The success
callback gets fired when the wallet is successfully generated and
returns a wallet object with the address and WIF key.
Import a previously generated wallet using the WIF key from wif
. The success
callback gets fired on successful importation of the wallet and returns a
wallet object with the address and the same WIF key.
Create a signed BTC transaction to get a transaction hash for reserveTicket()
to be later broadcast with propagateTransaction()
(see below), using a
wallet object wallet
(as returned by generateWallet()
), paid to the BTC
address recipient
for the amount of amountBtc
, including an ether fee of
etherFee
for a third-party claimer, and from the Ethereum address
etherAddress
. The success
callback gets fired when the transaction is
successfully created and returns an object with fee
for the actual BTC miner
fee after the etherAddress
and etherFee
have been encoded into the
transaction, the hash
of the BTC transaction and hex
of the raw transaction.
Broadcast a previously signed raw transaction txHex
to the Bitcoin network.
The success
callback gets fired when the transaction is successfully broadcast
and returns the BTC transaction hash.
Query the btcrelay contract for its last
stored BTC block hash. The success
callback gets fired on a successful call
and returns the hash of the latest block.
Query the btcrelay contract for its last
stored BTC block number. The success
callback gets fired on a successful call
and returns the block number of the latest block.
Get the fee from btcrelay to validate a
transaction in a given blockHash
. The success
callback gets fired on a
successful call and returns the fee amount in wei.
Query the blockr API for the raw block data of a block
with hash blockHash
, generate the BTC block header from that data, call the
storeBlockWithFee
method of btcrelay
and send an Ethereum transaction for that same method if successful, effectively
storing the block header. The success
callback gets fired when the transaction
is mined and returns the BTC block number for which the block header was stored.