BitcoinHTLC

swapchain documentationBitcoinHTLC

Handle creation, redemption and refunding of HTLCs on the bitcoin blockchain

Hierarchy

  • BitcoinHTLC

Index

Constructors

Properties

Methods

Constructors

constructor

+ new BitcoinHTLC(network: string, sender: ECPairInterface, receiver: ECPairInterface, priority: number, BitcoinAPIConstructor: BitcoinAPIConstructor): BitcoinHTLC

Defined in pkg/bitcoin/htlc/btcHTLC.ts:54

Creates an instance of BitcoinHTLC.

memberof BitcoinHTLC

Parameters:

NameTypeDefaultDescription
networkstring"testnet"mainnet, testnet, or regtest.
senderECPairInterface-Sender keypair.
receiverECPairInterface-Receiver keypair.
prioritynumber-The transaction priority (0 = high, 1 = medium, 2 = low)
BitcoinAPIConstructorBitcoinAPIConstructor-Bitcoin API to use for communication with the blockchain.

Returns: BitcoinHTLC

Properties

Private amountAfterFees

amountAfterFees: number

Defined in pkg/bitcoin/htlc/btcHTLC.ts:53


bitcoinAPI

bitcoinAPI: BitcoinAPI

Defined in pkg/bitcoin/htlc/btcHTLC.ts:54


Private fundingTxBlockHeight

fundingTxBlockHeight: number | undefined

Defined in pkg/bitcoin/htlc/btcHTLC.ts:51


Private network

network: Network

Defined in pkg/bitcoin/htlc/btcHTLC.ts:47


Private preimage

preimage: string

Defined in pkg/bitcoin/htlc/btcHTLC.ts:52


Private priority

priority: number

Defined in pkg/bitcoin/htlc/btcHTLC.ts:50


Private receiver

receiver: ECPairInterface

Defined in pkg/bitcoin/htlc/btcHTLC.ts:49


Private sender

sender: ECPairInterface

Defined in pkg/bitcoin/htlc/btcHTLC.ts:48

Methods

calculateFee

calculateFee(): Promise‹object›

Defined in pkg/bitcoin/htlc/btcHTLC.ts:301

Get wanted and max fee.

memberof BitcoinHTLC

Returns: Promise‹object›

A fee object with a max fee, which is the maximum fee the redeemer of the HTLC should accept and a want fee, which is the desired fee according to the selected priority.


create

create(config: HTLCConfigBTC): Promise‹string›

Defined in pkg/bitcoin/htlc/btcHTLC.ts:354

Creates an HTLC.

memberof BitcoinHTLC

Parameters:

NameTypeDescription
configHTLCConfigBTCConfiguration object for the HTLC.

Returns: Promise‹string›

The hex for the refund transaction.


Private getFinalScriptsRedeem

getFinalScriptsRedeem(inputIndex: number, input: PsbtInput, script: Buffer, isSegwit: boolean, isP2SH: boolean, isP2WSH: boolean): object

Defined in pkg/bitcoin/htlc/btcHTLC.ts:173

Finalize an HTLC redeem transaction using PSBT.

Parameters:

NameTypeDescription
inputIndexnumberThe index in the input array.
inputPsbtInputThe array of transaction inputs.
scriptBufferP2WSH redeemscript.
isSegwitbooleanIncluded only for bitcoinjs-lib compatible reasons.
isP2SHbooleanIncluded only for bitcoinjs-lib compatible reasons.
isP2WSHbooleanIncluded only for bitcoinjs-lib compatible reasons.

Returns: object

A function to finalize and serialize the scripts.

  • finalScriptSig: Buffer | undefined

  • finalScriptWitness: Buffer | undefined


Private getFinalScriptsRefund

getFinalScriptsRefund(inputIndex: number, input: PsbtInput, script: Buffer, isSegwit: boolean, isP2SH: boolean, isP2WSH: boolean): object

Defined in pkg/bitcoin/htlc/btcHTLC.ts:131

Finalize an HTLC refund transaction using PSBT.

Parameters:

NameTypeDescription
inputIndexnumberThe index in the input array.
inputPsbtInputThe array of transaction inputs.
scriptBufferP2WSH redeemscript.
isSegwitbooleanIncluded only for bitcoinjs-lib compatible reasons.
isP2SHbooleanIncluded only for bitcoinjs-lib compatible reasons.
isP2WSHbooleanIncluded only for bitcoinjs-lib compatible reasons.

Returns: object

A function to finalize and serialize the scripts.

  • finalScriptSig: Buffer | undefined

  • finalScriptWitness: Buffer | undefined


getFundingTxBlockHeight

getFundingTxBlockHeight(): number | undefined

Defined in pkg/bitcoin/htlc/btcHTLC.ts:88

Get block height of funding transaction

memberof BitcoinHTLC

Returns: number | undefined

Blockheight of funding transaction or undefined if block is not mined/broadcasted yet.


getP2WSH

getP2WSH(hash: Buffer, sequence: number): Payment

Defined in pkg/bitcoin/htlc/btcHTLC.ts:230

Calculate the p2wsh to send our money to before creating the timelock refund operation.

memberof BitcoinHTLC

Parameters:

NameTypeDescription
hashBufferSHA256 hash of the secret.
sequencenumberHow many blocks need to be mined before refund is possible.

Returns: Payment

The pay-to-scripthash object.


Private getRedeemHex

getRedeemHex(transactionID: string, p2wsh: Payment): Promise‹string›

Defined in pkg/bitcoin/htlc/btcHTLC.ts:443

Redeems the HTLC.

memberof BitcoinHTLC

Parameters:

NameTypeDescription
transactionIDstringThe transaction id from the funding transaction.
p2wshPaymentPay-to-witness-script-hash object.

Returns: Promise‹string›

A redeem hex.


Private getRefundHex

getRefundHex(transactionID: string, sequence: number, p2wsh: Payment): Promise‹string›

Defined in pkg/bitcoin/htlc/btcHTLC.ts:406

Refunds the HTLC to sender.

memberof BitcoinHTLC

Parameters:

NameTypeDescription
transactionIDstringThe transaction id from the funding transaction.
sequencenumberThe timelock as number of blocks.
p2wshPaymentPay-to-witness-script-hash object.

Returns: Promise‹string›

A refund hex.


Private getWitnessPublicKeyHash

getWitnessPublicKeyHash(keyPair: ECPairInterface): Payment

Defined in pkg/bitcoin/htlc/btcHTLC.ts:216

Helperfunction to get the public key from an ECPair

memberof BitcoinHTLC

Parameters:

NameTypeDescription
keyPairECPairInterfaceA keyPair initialized as ECPair.

Returns: Payment

A p2wpkh object.


redeem

redeem(p2wsh: Payment, amount: number, secret: Secret): Promise‹void›

Defined in pkg/bitcoin/htlc/btcHTLC.ts:325

Redeems an HTLC.

memberof BitcoinHTLC

Parameters:

NameTypeDescription
p2wshPaymentThe Pay-to-witness-script-hash object to redeem.
amountnumberThe amount of satoshi to exchange.
secretSecretThe secret object with the correct preimage inside.

Returns: Promise‹void›


Private redeemScript

redeemScript(hash: Buffer, sequence: number): Buffer

Defined in pkg/bitcoin/htlc/btcHTLC.ts:101

Create the redeem script in bitcoin's scripting language.

memberof BitcoinHTLC

Parameters:

NameTypeDescription
hashBufferSHA256 hash of the secret.
sequencenumberHow many blocks need to be mined before refund is possible.

Returns: Buffer

A bitcoin script.


Private sendToP2WSHAddress

sendToP2WSHAddress(p2wsh: Payment, transactionID: string, amount: number): Promise‹string›

Defined in pkg/bitcoin/htlc/btcHTLC.ts:249

Send funds to a pay2witnessScripthash address to be used in the timelock refund operation.

memberof BitcoinHTLC

Parameters:

NameTypeDescription
p2wshPaymentPay-to-witness-script-hash object.
transactionIDstringA Transaction id with unspent funds.
amountnumberThe amount of satoshi to exchange.

Returns: Promise‹string›

The hex-coded transaction. This needs to be pushed to the chain using this.pushTX