def test_bytom_htlc(): with pytest.raises(ConnectionError, match=r".*http://localhost:9888*."): htlc = HTLC(network="mainnet").init( secret_hash=sha256("Hello Meheret!".encode()).hex(), recipient_public= "ac13c0bb1445423a641754182d53f0677cd4351a0e743e6f10b35122c3d7ea01", sender_public= "91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2", sequence=100) with pytest.raises(ValueError, match=r".*initialization htlc first"): htlc_bytecode = HTLC(network="mainnet").bytecode() assert htlc_bytecode == "01642091ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b644" \ "8f22e220ac13c0bb1445423a641754182d53f0677cd4351a0e743e6f10b35122" \ "c3d7ea01203a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e" \ "9e45820eeb741f547a6416000000557aa888537a7cae7cac631f000000537acd" \ "9f6972ae7cac00c0" with pytest.raises(ValueError, match=r".*initialization htlc first"): htlc_opcode = HTLC(network="mainnet").opcode() assert htlc_opcode == "0x64 0x91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448" \ "f22e2 0xac13c0bb1445423a641754182d53f0677cd4351a0e743e6f10b35122c3" \ "d7ea01 0x3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e4" \ "5820eeb DEPTH 0x547a6416000000557aa888537a7cae7cac631f000000537acd" \ "9f6972ae7cac FALSE CHECKPREDICATE"
def test_bytom_htlc(): htlc = HTLC(network="mainnet").init( secret_hash=sha256("Hello Meheret!".encode()).hex(), recipient_public= "91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2", sender_public= "3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e", sequence=1000, use_script=False) assert htlc.bytecode() == "02e803203e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e2091ff" \ "7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2203a26da82ead15a80" \ "533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb741f547a6416000000557aa888537a" \ "7cae7cac631f000000537acd9f6972ae7cac00c0" assert htlc.opcode() == "0xe803 0x3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e 0x91ff" \ "7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2 0x3a26da82ead15a805" \ "33a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb DEPTH 0x547a6416000000557aa88853" \ "7a7cae7cac631f000000537acd9f6972ae7cac FALSE CHECKPREDICATE" assert htlc.hash( ) == "a5addb7eb5649d10031304ece72d9ccc9dc424abcda895d904a309727424e40e" assert htlc.address( ) == "bm1q5kkakl44vjw3qqcnqnkwwtvuejwugf9tek5ftkgy5vyhyapyus8qgcttcs" assert HTLC().from_bytecode(bytecode=htlc.bytecode())
def htlc(secret_hash, recipient_public, sender_public, sequence, network): try: click.echo( HTLC(network=network).init(secret_hash=secret_hash, recipient_public=recipient_public, sender_public=sender_public, sequence=sequence).bytecode()) except Exception as exception: click.echo(click.style("Error: {}").format(str(exception)), err=True) sys.exit()
def fund(sender_guid, amount, asset, bytecode, network): try: click.echo( FundTransaction(network=network).build_transaction( wallet=Wallet(network=network).from_guid(guid=sender_guid), htlc=HTLC(network=network).from_bytecode(bytecode=bytecode), amount=int(amount), asset=asset).unsigned_raw()) except Exception as exception: click.echo(click.style("Error: {}").format(str(exception)), err=True) sys.exit()
def test_bytom_htlc_init_validation(): with pytest.raises(TypeError, match="secret hash must be string format"): HTLC(network="mainnet").init(int(), str(), str(), int()) with pytest.raises(ValueError, match="invalid secret hash, length must be 64."): HTLC(network="mainnet").init(str(), str(), str(), int()) with pytest.raises(TypeError, match="recipient public key must be string format"): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", float(), str(), int()) with pytest.raises( ValueError, match="invalid recipient public key, length must be 64."): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", str(), str(), int()) with pytest.raises(TypeError, match="sender public key must be string format"): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", "91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2", bool(), int()) with pytest.raises(ValueError, match="invalid sender public key, length must be 64."): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", "91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2", str(), int()) with pytest.raises(TypeError, match="sequence must be integer format"): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", "91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2", "ac13c0bb1445423a641754182d53f0677cd4351a0e743e6f10b35122c3d7ea01", str())
def test_bytom_fund_transaction(): unsigned_fund_transaction = FundTransaction(network=network) unsigned_fund_transaction.build_transaction( wallet=sender_wallet, htlc=HTLC(network=network).init( secret_hash=sha256("Hello Meheret!".encode()).hex(), recipient_public=recipient_wallet.public_key(), sender_public=sender_wallet.public_key(), sequence=1000), amount=amount, asset=asset) assert unsigned_fund_transaction.type() == "bytom_fund_unsigned" assert unsigned_fund_transaction.unsigned_raw( ) == "eyJmZWUiOiAxMDAwMDAwMCwgImd1aWQiOiAiNTcxNzg0YTgtMDk0NS00ZDc4LWI5NzMtYWFjNGIwOWQ2NDM5IiwgInVuc2lnbmVkX2RhdGFzIjogW3siZGF0YXMiOiBbImI1NTgxODRiZDJjNWNmMWQ3YWY5NTIyN2Y4OTk2Nzc3ZDQ2ZDQxMDY5YTgyZjc4YzExYzYxODBhNTMyZWViODUiXSwgInB1YmxpY19rZXkiOiAiM2UwYTM3N2FlNGFmYTAzMWQ0NTUxNTk5ZDliYjdkNWIyN2Y0NzM2ZDc3Zjc4Y2FjNGQ0NzZmMGZmYmE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signed_fund_transaction.signatures() == [] signed_fund_transaction = unsigned_fund_transaction.sign(solver=FundSolver( xprivate_key=sender_wallet.xprivate_key())) assert unsigned_fund_transaction.fee() == signed_fund_transaction.fee( ) == 10000000 assert unsigned_fund_transaction.hash() == signed_fund_transaction.hash() == \ "89c954404bdb3e6123ed8a413fe3bd526bf7b552936d80fd8c37c2d7eae6d0c0" assert unsigned_fund_transaction.raw() == signed_fund_transaction.raw() == \ "070100010160015e02e0b1ad2107222cfc13ab6c3365d366b5d13b11e6dc7e1e0cc4ae5676cdee44fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff09888d8030101160014887ee66d84a82f2d86824a45bb51fdea03c92f492201203e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e020146ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff904e01220020a5addb7eb5649d10031304ece72d9ccc9dc424abcda895d904a309727424e40e00013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09da5d30301160014887ee66d84a82f2d86824a45bb51fdea03c92f4900" # assert unsigned_fund_transaction.json() == signed_fund_transaction.json() == \ # {'tx_id': '89c954404bdb3e6123ed8a413fe3bd526bf7b552936d80fd8c37c2d7eae6d0c0', 'version': 1, 'size': 275, 'time_range': 0, 'inputs': [{'type': 'spend', 'asset_id': 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'asset_definition': {}, 'amount': 989990000, 'control_program': '0014887ee66d84a82f2d86824a45bb51fdea03c92f49', 'address': 'bm1q3plwvmvy4qhjmp5zffzmk50aagpujt6f5je85p', 'spent_output_id': '94f85b2b634c4d42bfc2ca883e74145a508f482dfc10adb407be59df3069aff7', 'input_id': '8a3b64f6cbf76f7fab17d53dd17ba2fdd4402e143b0384a62e3a371f54f77681', 'witness_arguments': ['3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e'], 'sign_data': 'b558184bd2c5cf1d7af95227f8996777d46d41069a82f78c11c6180a532eeb85'}], 'outputs': [{'type': 'control', 'id': '72f09f149c9c8c9d1ee8122472a8b0bd2e0c897d51f21daa7480428f1b61fc3c', 'position': 0, 'asset_id': 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'asset_definition': {}, 'amount': 10000, 'control_program': '0020a5addb7eb5649d10031304ece72d9ccc9dc424abcda895d904a309727424e40e', 'address': 'bm1q5kkakl44vjw3qqcnqnkwwtvuejwugf9tek5ftkgy5vyhyapyus8qgcttcs'}, {'type': 'control', 'id': 'f95b95b531da409f4d80e959ba9351d592b0bcc3e6b0f79cfaea03143c520ed9', 'position': 1, 'asset_id': 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'asset_definition': {}, 'amount': 979980000, 'control_program': '0014887ee66d84a82f2d86824a45bb51fdea03c92f49', 'address': 'bm1q3plwvmvy4qhjmp5zffzmk50aagpujt6f5je85p'}], 'fee': 10000000} assert unsigned_fund_transaction.unsigned_datas() == signed_fund_transaction.unsigned_datas() == \ [{'datas': ['b558184bd2c5cf1d7af95227f8996777d46d41069a82f78c11c6180a532eeb85'], 'public_key': '3e0a377ae4afa031d4551599d9bb7d5b27f4736d77f78cac4d476f0ffba5ae3e', 'network': 'mainnet', 'path': 'm/44/153/1/0/1'}] assert signed_fund_transaction.type() == "bytom_fund_signed" assert signed_fund_transaction.signatures() == [[ 'e0af0886e126924fa1637314e38669512d2759e60b6cf69699616d8da5b0a316b7f662fed558c8adc65f288e937fc1e273d23587fbaea62994d1ad1fa795f103' ]]
# Recipient wallet information's recipient_public_key = recipient_bytom_wallet.public_key() print("Recipient Public Key:", recipient_public_key) recipient_program = recipient_bytom_wallet.program() print("Recipient Program:", recipient_program) recipient_address = recipient_bytom_wallet.address() print("Recipient Address:", recipient_address) # recipient_balance = recipient_bytom_wallet.balance() # print("Recipient Balance:", recipient_balance) print("=" * 10, "Hash Time Lock Contract (HTLC) between Sender and Recipient") # Initialization Hash Time Lock Contract (HTLC). bytom_htlc = HTLC(network="mainnet").init( secret_hash=sha256("Hello Meheret!".encode()).hex(), recipient_public=recipient_public_key, sender_public=sender_public_key, sequence=100 ) htlc_bytecode = bytom_htlc.bytecode() print("HTLC Bytecode:", htlc_bytecode) htlc_opcode = bytom_htlc.opcode() print("HTLC OP_Code:", htlc_opcode) print("=" * 10, "Unsigned Fund Transaction") # Initialization fund transaction unsigned_fund_transaction = FundTransaction(network="mainnet") # Building fund transaction unsigned_fund_transaction.build_transaction( wallet=sender_bytom_wallet,
def test_bytom_htlc_exception(): with pytest.raises(ConnectionError, match=r".*http://localhost:9888*."): HTLC(network="mainnet").init( secret_hash=sha256("Hello Meheret!".encode()).hex(), recipient_public= "ac13c0bb1445423a641754182d53f0677cd4351a0e743e6f10b35122c3d7ea01", sender_public= "91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2", sequence=100, use_script=True) with pytest.raises(TypeError, match="secret hash must be string format"): HTLC(network="mainnet").init(int(), str(), str(), int()) with pytest.raises(ValueError, match="invalid secret hash, length must be 64"): HTLC(network="mainnet").init(str(), str(), str(), int()) with pytest.raises(ValueError, match="htlc script is none, initialization htlc first"): HTLC(network="mainnet").bytecode() with pytest.raises(ValueError, match="htlc script is none, initialization htlc first"): HTLC(network="mainnet").opcode() with pytest.raises(ValueError, match="htlc script is none, initialization htlc first"): HTLC(network="mainnet").hash() with pytest.raises(ValueError, match="htlc script is none, initialization htlc first"): HTLC(network="mainnet").address() with pytest.raises(TypeError, match="recipient public key must be string format"): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", float(), str(), int()) with pytest.raises( ValueError, match="invalid recipient public key, length must be 64"): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", str(), str(), int()) with pytest.raises(TypeError, match="sender public key must be string format"): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", "91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2", bool(), int()) with pytest.raises(ValueError, match="invalid sender public key, length must be 64"): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", "91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2", str(), int()) with pytest.raises(TypeError, match="sequence must be integer format"): HTLC(network="mainnet").init( "3a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb", "91ff7f525ff40874c4f47f0cab42e46e3bf53adad59adef9558ad1b6448f22e2", "ac13c0bb1445423a641754182d53f0677cd4351a0e743e6f10b35122c3d7ea01", str())
# Getting recipient wallet information's recipient_public_key = recipient_wallet.public_key() print("Recipient Public Key:", recipient_public_key) recipient_program = recipient_wallet.program() print("Recipient Program:", recipient_program) recipient_address = recipient_wallet.address() print("Recipient Address:", recipient_address) # recipient_balance = recipient_wallet.balance() # print("Recipient Balance:", recipient_balance) print("=" * 10, "Hash Time Lock Contract (HTLC) between Sender and Recipient") # Initializing Hash Time Lock Contract (HTLC) htlc = HTLC(network=NETWORK).init(secret_hash=sha256( "Hello Meheret!".encode()).hex(), recipient_public=recipient_public_key, sender_public=sender_public_key, sequence=1000) htlc_bytecode = htlc.bytecode() print("HTLC Bytecode:", htlc_bytecode) htlc_opcode = htlc.opcode() print("HTLC OP_Code:", htlc_opcode) htlc_hash = htlc.hash() print("HTLC Hash:", htlc_hash) htlc_address = htlc.address() print("HTLC Address:", htlc_address) print("=" * 10, "Unsigned Fund Transaction") # Initializing fund transaction