Hash Time Lock Contract (HTLC)

Bytom Hash Time Lock Contract (HTLC).

class swap.providers.bytom.htlc.HTLC(network: str = 'mainnet', contract_address: Optional[str] = None)

Bytom Hash Time Lock Contract (HTLC).

Parameters

network (str) – Bytom network, defaults to mainnet.

Returns

HTLC – Bytom HTLC instance.

Note

Bytom has only three networks, mainnet, solonet and testnet.

build_htlc(secret_hash: str, recipient_public_key: str, sender_public_key: str, endblock: int, use_script: bool = False) swap.providers.bytom.htlc.HTLC

Build Bytom Hash Time Lock Contract (HTLC).

Parameters
  • secret_hash (str) – secret sha-256 hash.

  • recipient_public_key (str) – Bytom recipient public key.

  • sender_public_key (str) – Bytom sender public key.

  • endblock (int) – Bytom expiration block height.

  • use_script (bool) – Initialize HTLC by using script, default to False.

Returns

HTLC – Bytom Hash Time Lock Contract (HTLC) instance.

>>> from swap.providers.bytom.htlc import HTLC
>>> from swap.providers.bytom.rpc import get_current_block_height
>>> from swap.utils import sha256
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_public_key="3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e", sender_public_key="fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212", endblock=get_current_block_height(plus=100), use_script=False)
<swap.providers.bytom.htlc.HTLC object at 0x0409DAF0>
from_bytecode(bytecode: str) swap.providers.bytom.htlc.HTLC

Initialize Bytom Hash Time Lock Contract (HTLC) from bytecode.

Parameters

bytecode (str) – Bytom bytecode.

Returns

HTLC – Bytom Hash Time Lock Contract (HTLC) instance.

>>> from swap.providers.bytom.htlc import HTLC
>>> htlc: HTLC = HTLC(network="testnet")
>>> bytecode: str = "03285d0a20fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212203e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e203a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb741f547a6416000000557aa888537a7cae7cac631f000000537acd9f6972ae7cac00c0"
>>> htlc.from_bytecode(bytecode=bytecode)
<swap.providers.bytom.htlc.HTLC object at 0x0409DAF0>
bytecode() str

Get Bytom Hash Time Lock Contract (HTLC) bytecode.

Returns

str – Bytom HTLC bytecode.

>>> from swap.providers.bytom.htlc import HTLC
>>> from swap.utils import sha256
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_public_key="3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e", sender_public_key="fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212", endblock=679208)
>>> htlc.bytecode()
"03285d0a20fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212203e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e203a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb741f547a6416000000557aa888537a7cae7cac631f000000537acd9f6972ae7cac00c0"
opcode() Optional[str]

Get Bytom Hash Time Lock Contract (HTLC) OP_Code.

Returns

str – Bytom HTLC opcode.

>>> from swap.providers.bytom.htlc import HTLC
>>> from swap.utils import sha256
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_public_key="3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e", sender_public_key="fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212", endblock=679208)
>>> htlc.opcode()
"0x285d0a 0xfe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212 0x3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e 0x3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb DEPTH 0x547a6416000000557aa888537a7cae7cac631f000000537acd9f6972ae7cac FALSE CHECKPREDICATE"
hash() str

Get Bytom Hash Time Lock Contract (HTLC) hash.

Returns

str – Bytom HTLC hash.

>>> from swap.providers.bytom.htlc import HTLC
>>> from swap.utils import sha256
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_public_key="3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e", sender_public_key="fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212", endblock=679208)
>>> htlc.hash()
"e7f4a9815f3a36c616c5666b97fb7fdacd3720c117d078c429494d1b617fe7d4"
contract_address() str

Get Bytom Hash Time Lock Contract (HTLC) address.

Returns

str – Bytom HTLC address.

>>> from swap.providers.bytom.htlc import HTLC
>>> from swap.utils import sha256
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_public_key="3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e", sender_public_key="fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212", endblock=679208)
>>> htlc.contract_address()
"bm1qul62nq2l8gmvv9k9ve4e07mlmtxnwgxpzlg833pff9x3kctlul2q727jyy"
balance(asset: Union[str, swap.providers.bytom.assets.AssetNamespace] = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', unit: str = 'NEU') Union[int, float]

Get Bytom HTLC balance.

Parameters
  • asset (str, bytom.assets.AssetNamespace, bytom.assets.AssetNamespace) – Bytom asset id, defaults to BTM.

  • unit (str) – Bytom unit, default to NEU.

Returns

int, float – Bytom HTLC balance.

>>> from swap.providers.bytom.htlc import HTLC
>>> from swap.utils import sha256
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_public_key="3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e", sender_public_key="fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212", endblock=679208)
>>> htlc.balance(asset="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", unit="BTM")
0.1
utxos(asset: Union[str, swap.providers.bytom.assets.AssetNamespace] = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', limit: int = 15) list

Get Bytom HTLC unspent transaction output (UTXO’s).

Parameters
  • asset (str, bytom.assets.AssetNamespace) – Bytom asset id, defaults to BTM.

  • limit (int) – Limit of UTXO’s, default is 15.

Returns

list – Bytom unspent transaction outputs.

>>> from swap.providers.bytom.htlc import HTLC
>>> from swap.utils import sha256
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_public_key="3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e", sender_public_key="fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212", endblock=679208)
>>> htlc.utxos(asset="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
[{'hash': '1aaf7df33c1d41bc6108c93d8b6da6af1d7f68632f54516408a03ff86494a1f0', 'asset': 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'amount': 10000000}]