Hash Time Lock Contract (HTLC)

Vapor Hash Time Lock Contract (HTLC).

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

Vapor Hash Time Lock Contract (HTLC).

Parameters

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

Returns

HTLC – Vapor HTLC instance.

Note

Vapor 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.vapor.htlc.HTLC

Build Vapor Hash Time Lock Contract (HTLC).

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

  • recipient_public_key (str) – Vapor recipient public key.

  • sender_public_key (str) – Vapor sender public key.

  • endblock (int) – Vapor expiration block height.

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

Returns

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

>>> from swap.providers.vapor.htlc import HTLC
>>> from swap.providers.vapor.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=1000), use_script=False)
<swap.providers.vapor.htlc.HTLC object at 0x0409DAF0>
from_bytecode(bytecode: str) swap.providers.vapor.htlc.HTLC

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

Parameters

bytecode (str) – Vapor bytecode.

Returns

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

>>> from swap.providers.vapor.htlc import HTLC
>>> htlc: HTLC = HTLC(network="testnet")
>>> bytecode: str = "042918320720fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212203e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e203a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb741f547a6416000000557aa888537a7cae7cac631f000000537acd9f6972ae7cac00c0"
>>> htlc.from_bytecode(bytecode=bytecode)
<swap.providers.vapor.htlc.HTLC object at 0x0409DAF0>
bytecode() str

Get Vapor Hash Time Lock Contract (HTLC) bytecode.

Returns

str – Vapor HTLC bytecode.

>>> from swap.providers.vapor.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=120723497)
>>> htlc.bytecode()
"042918320720fe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212203e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e203a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb741f547a6416000000557aa888537a7cae7cac631f000000537acd9f6972ae7cac00c0"
opcode() Optional[str]

Get Vapor Hash Time Lock Contract (HTLC) OP_Code.

Returns

str – Vapor HTLC opcode.

>>> from swap.providers.vapor.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=120723497)
>>> htlc.opcode()
"0x29183207 0xfe6b3fd4458291b19605d92837ae1060cc0237e68022b2eb9faf01a118226212 0x3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e 0x3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb DEPTH 0x547a6416000000557aa888537a7cae7cac631f000000537acd9f6972ae7cac FALSE CHECKPREDICATE"
hash() str

Get Vapor Hash Time Lock Contract (HTLC) hash.

Returns

str – Vapor HTLC hash.

>>> from swap.providers.vapor.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=120723497)
>>> htlc.hash()
"34a3db50301b941b8ed43dcfdbd3381df1b739fa64ab77e4264f703a45e0be31"
contract_address() str

Get Vapor Hash Time Lock Contract (HTLC) address.

Returns

str – Vapor HTLC address.

>>> from swap.providers.vapor.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=120723497)
>>> htlc.contract_address()
"vp1qxj3ak5psrw2phrk58h8ah5ecrhcmww06vj4h0epxfacr530qhccs4pczgc"
balance(asset: Union[str, swap.providers.vapor.assets.AssetNamespace] = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', unit: str = 'NEU') Union[int, float]

Get Vapor HTLC balance.

Parameters
  • asset (str, vapor.assets.AssetNamespace, vapor.assets.AssetNamespace) – Vapor asset id, defaults to BTM.

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

Returns

int, float – Vapor HTLC balance.

>>> from swap.providers.vapor.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=120723497)
>>> htlc.balance(asset="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", unit="BTM")
0.1
utxos(asset: Union[str, swap.providers.vapor.assets.AssetNamespace] = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', limit: int = 15) list

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

Parameters
  • asset (str, vapor.assets.AssetNamespace) – Vapor asset id, defaults to BTM.

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

Returns

list – Vapor unspent transaction outputs.

>>> from swap.providers.vapor.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=120723497)
>>> htlc.utxos(asset="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
[{'hash': '144dd8355cae0d9aea6ca3fb1ff685fb7b455b1f9cb0c5992c9035844c664ad1', 'asset': 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'amount': 10000000}]