Hash Time Lock Contract (HTLC)¶
Bitcoin Hash Time Lock Contract (HTLC).
-
class
swap.providers.bitcoin.htlc.
HTLC
(network: str = 'mainnet')¶ 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, sequence: int = 1000) → 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.
sequence (int) – Bitcoin sequence number of expiration block, defaults to 1000.
- Returns
HTLC – Bitcoin Hash Time Lock Contract (HTLC) instance.
>>> from swap.providers.bitcoin.htlc import HTLC >>> from swap.utils import sha256 >>> htlc = HTLC(network="testnet") >>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_address="mgokpSJoX7npmAK1Zj8ze1926CLxYDt1iF", sender_address="mkFWGt4hT11XS8dJKzzRFsTrqjjAwZfQAC", sequence=1000) <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(network="testnet") >>> opcode = "OP_IF OP_HASH256 821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e0158 OP_EQUALVERIFY OP_DUP OP_HASH160 0e259e08f2ec9fc99a92b6f66fdfcb3c7914fd68 OP_EQUALVERIFY OP_CHECKSIG OP_ELSE e803 OP_CHECKSEQUENCEVERIFY OP_DROP OP_DUP OP_HASH160 33ecab3d67f0e2bde43e52f41ec1ecbdc73f11f8 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(network="testnet") >>> bytecode = "63aa20821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e01588876a9140e259e08f2ec9fc99a92b6f66fdfcb3c7914fd6888ac6702e803b27576a91433ecab3d67f0e2bde43e52f41ec1ecbdc73f11f888ac68" >>> 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(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgokpSJoX7npmAK1Zj8ze1926CLxYDt1iF", "mkFWGt4hT11XS8dJKzzRFsTrqjjAwZfQAC", 1000) >>> htlc.bytecode() "63aa20821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e01588876a9140e259e08f2ec9fc99a92b6f66fdfcb3c7914fd6888ac6702e803b27576a91433ecab3d67f0e2bde43e52f41ec1ecbdc73f11f888ac68"
-
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(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgokpSJoX7npmAK1Zj8ze1926CLxYDt1iF", "mkFWGt4hT11XS8dJKzzRFsTrqjjAwZfQAC", 1000) >>> htlc.opcode() "OP_IF OP_HASH256 821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e0158 OP_EQUALVERIFY OP_DUP OP_HASH160 0e259e08f2ec9fc99a92b6f66fdfcb3c7914fd68 OP_EQUALVERIFY OP_CHECKSIG OP_ELSE e803 OP_CHECKSEQUENCEVERIFY OP_DROP OP_DUP OP_HASH160 33ecab3d67f0e2bde43e52f41ec1ecbdc73f11f8 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(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgokpSJoX7npmAK1Zj8ze1926CLxYDt1iF", "mkFWGt4hT11XS8dJKzzRFsTrqjjAwZfQAC", 1000) >>> htlc.hash() "a9149418feed4647e156d6663db3e0cef7c050d0386787"
-
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(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgokpSJoX7npmAK1Zj8ze1926CLxYDt1iF", "mkFWGt4hT11XS8dJKzzRFsTrqjjAwZfQAC", 1000) >>> htlc.address() "2N6kHwQy6Ph5EdKNgzGrcW2WhGHKGfmP5ae"
-
balance
() → int¶ Get Bitcoin HTLC balance.
- Returns
int – Bitcoin HTLC balance (SATOSHI amount).
>>> from swap.providers.bitcoin.htlc import HTLC >>> from swap.utils import sha256 >>> htlc = HTLC(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgokpSJoX7npmAK1Zj8ze1926CLxYDt1iF", "mkFWGt4hT11XS8dJKzzRFsTrqjjAwZfQAC", 1000) >>> htlc.balance() 10000
-
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(network="testnet") >>> htlc.build_htlc(sha256("Hello Meheret!"), "mgokpSJoX7npmAK1Zj8ze1926CLxYDt1iF", "mkFWGt4hT11XS8dJKzzRFsTrqjjAwZfQAC", 1000) >>> htlc.utxos() [{'index': 0, 'hash': '9825b68e57c8a924285828dde37869c2eca3bb3784b171353962f0d7e7577da1', 'output_index': 0, 'amount': 10000, 'script': 'a9149418feed4647e156d6663db3e0cef7c050d0386787'}]