Hash Time Lock Contract (HTLC)
Bitcoin Hash Time Lock Contract (HTLC).
- class swap.providers.bitcoin.htlc.HTLC(network: str = 'mainnet', contract_address: Optional[str] = None)
Bitcoin Hash Time Lock Contract (HTLC).
- Parameters
network (str) – Bitcoin network, defaults to mainnet.
- Returns
HTLC – Bitcoin HTLC instance.
Note
Bitcoin has only two networks,
mainnet
andtestnet
.- build_htlc(secret_hash: str, recipient_address: str, sender_address: str, endtime: int) swap.providers.bitcoin.htlc.HTLC
Build Bitcoin Hash Time Lock Contract (HTLC).
- Parameters
secret_hash (str) – secret sha-256 hash.
recipient_address (str) – Bitcoin recipient address.
sender_address (str) – Bitcoin sender address.
endtime (int) – Expiration block time (Seconds).
- Returns
HTLC – Bitcoin Hash Time Lock Contract (HTLC) instance.
>>> from swap.providers.bitcoin.htlc import HTLC >>> from swap.utils import sha256 >>> htlc: HTLC = HTLC(network="testnet") >>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_address="mgS3WMHp9nvdUPeDJxr5iCF2P5HuFZSR3V", sender_address="n1wgm6kkzMcNfAtJmes8YhpvtDzdNhDY5a", endtime=1624687630) <swap.providers.bitcoin.htlc.HTLC object at 0x0409DAF0>
- from_opcode(opcode: str) swap.providers.bitcoin.htlc.HTLC
Initiate Bitcoin Hash Time Lock Contract (HTLC) from opcode script.
- Parameters
opcode (str) – Bitcoin opcode script.
- Returns
HTLC – Bitcoin Hash Time Lock Contract (HTLC) instance.
>>> from swap.providers.bitcoin.htlc import HTLC >>> htlc: HTLC = HTLC(network="testnet") >>> opcode: str = "OP_IF OP_HASH256 821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e0158 OP_EQUALVERIFY OP_DUP OP_HASH160 0a0a6590e6ba4b48118d21b86812615219ece76b OP_EQUALVERIFY OP_CHECKSIG OP_ELSE 0ec4d660 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 e00ff2a640b7ce2d336860739169487a57f84b15 OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF" >>> htlc.from_opcode(opcode=opcode) <swap.providers.bitcoin.htlc.HTLC object at 0x0409DAF0>
- from_bytecode(bytecode: str) swap.providers.bitcoin.htlc.HTLC
Initialize Bitcoin Hash Time Lock Contract (HTLC) from bytecode.
- Parameters
bytecode (str) – Bitcoin bytecode.
- Returns
HTLC – Bitcoin Hash Time Lock Contract (HTLC) instance.
>>> from swap.providers.bitcoin.htlc import HTLC >>> htlc: HTLC = HTLC(network="testnet") >>> bytecode: str = "63aa20821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e01588876a9140a0a6590e6ba4b48118d21b86812615219ece76b88ac67040ec4d660b17576a914e00ff2a640b7ce2d336860739169487a57f84b1588ac68" >>> htlc.from_bytecode(bytecode=bytecode) <swap.providers.bitcoin.htlc.HTLC object at 0x0409DAF0>
- bytecode() str
Get Bitcoin Hash Time Lock Contract (HTLC) bytecode.
- Returns
str – Bitcoin HTLC bytecode.
>>> from swap.providers.bitcoin.htlc import HTLC >>> from swap.utils import sha256 >>> htlc: HTLC = HTLC(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgS3WMHp9nvdUPeDJxr5iCF2P5HuFZSR3V", "n1wgm6kkzMcNfAtJmes8YhpvtDzdNhDY5a", 1624687630) >>> htlc.bytecode() "63aa20821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e01588876a9140a0a6590e6ba4b48118d21b86812615219ece76b88ac67040ec4d660b17576a914e00ff2a640b7ce2d336860739169487a57f84b1588ac68"
- opcode() str
Get Bitcoin Hash Time Lock Contract (HTLC) OP_Code.
- Returns
str – Bitcoin HTLC opcode.
>>> from swap.providers.bitcoin.htlc import HTLC >>> from swap.utils import sha256 >>> htlc: HTLC = HTLC(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgS3WMHp9nvdUPeDJxr5iCF2P5HuFZSR3V", "n1wgm6kkzMcNfAtJmes8YhpvtDzdNhDY5a", 1624687630) >>> htlc.opcode() "OP_IF OP_HASH256 821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e0158 OP_EQUALVERIFY OP_DUP OP_HASH160 0a0a6590e6ba4b48118d21b86812615219ece76b OP_EQUALVERIFY OP_CHECKSIG OP_ELSE 0ec4d660 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 e00ff2a640b7ce2d336860739169487a57f84b15 OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF"
- hash() str
Get Bitcoin HTLC hash.
- Returns
str – Bitcoin Hash Time Lock Contract (HTLC) hash.
>>> from swap.providers.bitcoin.htlc import HTLC >>> from swap.utils import sha256 >>> htlc: HTLC = HTLC(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgS3WMHp9nvdUPeDJxr5iCF2P5HuFZSR3V", "n1wgm6kkzMcNfAtJmes8YhpvtDzdNhDY5a", 1624687630) >>> htlc.hash() "a914c8c77a9b43ee2bdf1a07c48699833d7668bf264c87"
- contract_address() str
Get Bitcoin Hash Time Lock Contract (HTLC) address.
- Returns
str – Bitcoin HTLC address.
>>> from swap.providers.bitcoin.htlc import HTLC >>> from swap.utils import sha256 >>> htlc: HTLC = HTLC(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgS3WMHp9nvdUPeDJxr5iCF2P5HuFZSR3V", "n1wgm6kkzMcNfAtJmes8YhpvtDzdNhDY5a", 1624687630) >>> htlc.contract_address() "2NBYr6gvh4ujsRwKKjDrrRr2vGonazzX6Z6"
- balance(unit: str = 'Satoshi') Union[int, float]
Get Bitcoin HTLC balance.
- Parameters
unit (str) – Bitcoin unit, default to Satoshi.
- Returns
int, float – Bitcoin wallet balance.
>>> from swap.providers.bitcoin.htlc import HTLC >>> from swap.utils import sha256 >>> htlc: HTLC = HTLC(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgS3WMHp9nvdUPeDJxr5iCF2P5HuFZSR3V", "n1wgm6kkzMcNfAtJmes8YhpvtDzdNhDY5a", 1624687630) >>> htlc.balance(unit="BTC") 0.001
- utxos(limit: int = 15) list
Get Bitcoin HTLC unspent transaction output (UTXO’s).
- Parameters
limit (int) – Limit of UTXO’s, default is 15.
- Returns
list – Bitcoin unspent transaction outputs.
>>> from swap.providers.bitcoin.htlc import HTLC >>> from swap.utils import sha256 >>> htlc: HTLC = HTLC(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgS3WMHp9nvdUPeDJxr5iCF2P5HuFZSR3V", "n1wgm6kkzMcNfAtJmes8YhpvtDzdNhDY5a", 1624687630) >>> htlc.utxos() [{'index': 0, 'hash': 'a211d21110756b266925fee2fbf2dc81529beef5e410311b38578dc3a076fb31', 'output_index': 0, 'amount': 100000, 'script': 'a914c8c77a9b43ee2bdf1a07c48699833d7668bf264c87'}]