def encode_payment_tx_to_bytes(tx): w = bytearray() write_uint8(w, tx.transactionType) write_uint64_be(w, helpers.convert_to_nano_sec(tx.timestamp)) write_uint64_be(w, tx.amount) write_uint64_be(w, tx.fee) write_uint16_be(w, tx.feeScale) write_bytes(w, base58.decode(tx.recipient)) try: attachment_bytes = base58.decode(tx.attachment) except Exception: attachment_bytes = bytes(tx.attachment, 'utf-8') write_uint16_be(w, len(attachment_bytes)) write_bytes(w, attachment_bytes) return w
def get_address_from_public_key(public_key: str, chain_id: str): addr_head_bytes = bytes([VSYS_ADDRESS_VERSION] + [ord(c) for c in chain_id]) addr_body_bytes = hash_chain(base58.decode(public_key))[:VSYS_ADDRESS_HASH_LENGTH] unhashed_address = addr_head_bytes + addr_body_bytes address_hash = hash_chain(unhashed_address)[:VSYS_CHECKSUM_LENGTH] address = base58.encode(unhashed_address + address_hash) return address
def encode_cancel_lease_tx_to_bytes(tx): w = bytearray() write_uint8(w, tx.transactionType) write_uint64_be(w, tx.fee) write_uint16_be(w, tx.feeScale) write_uint64_be(w, helpers.convert_to_nano_sec(tx.timestamp)) write_bytes(w, base58.decode(tx.txId)) return w
def get_address_bytes_unsafe(address: str) -> bytes: try: address_bytes = bech32.decode_unsafe(address) except ValueError: try: address_bytes = base58.decode(address) except ValueError: raise INVALID_ADDRESS return address_bytes
def get_bytes_unsafe(address: str) -> bytes: try: address_bytes = bech32.decode_unsafe(address) except ValueError: try: address_bytes = base58.decode(address) except ValueError: raise wire.ProcessError("Invalid address") return address_bytes
def serialise_tx(self): self._process_inputs() self._process_outputs() inputs_cbor = [] for i, output_index in enumerate(self.output_indexes): inputs_cbor.append( [ self.types[i], cbor.Tagged(24, cbor.encode([self.input_hashes[i], output_index])), ] ) inputs_cbor = cbor.IndefiniteLengthArray(inputs_cbor) outputs_cbor = [] for index, address in enumerate(self.output_addresses): outputs_cbor.append( [cbor.Raw(base58.decode(address)), self.outgoing_coins[index]] ) for index, address in enumerate(self.change_addresses): outputs_cbor.append( [cbor.Raw(base58.decode(address)), self.change_coins[index]] ) outputs_cbor = cbor.IndefiniteLengthArray(outputs_cbor) tx_aux_cbor = [inputs_cbor, outputs_cbor, self.attributes] tx_hash = hashlib.blake2b(data=cbor.encode(tx_aux_cbor), outlen=32).digest() witnesses = self._build_witnesses(tx_hash) tx_body = cbor.encode([tx_aux_cbor, witnesses]) self.fee = self.compute_fee( self.input_coins, self.outgoing_coins, self.change_coins ) return tx_body, tx_hash
def test_cancel_lease_signature(self): private_key = "8Yoy9QL2sqggrM22VvHcRmAVzRr5h23FuDeFohyAU27B" public_key = "2cLDxAPJNWGGWAyHUFEnyoznhkf4QCEkcQrL5g2oEBCY" private_key_bytes = base58.decode(private_key) public_key_bytes = base58.decode(public_key) msg = VsysTransaction( transactionType=LEASE_CANCEL_TX_TYPE, senderPublicKey=public_key, fee=10000000, feeScale=100, txId="8XC9UFcf1yWrFiWWtjiSnCqXhyuwzzw5SE7DrNuiE8gF", timestamp=1547722056762119200 ) to_sign_bytes = encode_cancel_lease_tx_to_bytes(msg) expect_to_sign = [0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0x96, 0x80, 0x00, 0x64, 0x15, 0x7a, 0x9d, 0x02, 0xac, 0x57, 0xd4, 0x20, 0x6f, 0xbd, 0xd5, 0xea, 0xe3, 0xe3, 0xa4, 0x8e, 0x7d, 0xe8, 0x14, 0x08, 0x4f, 0xf3, 0x4f, 0xd3, 0xf9, 0xaf, 0x97, 0x0e, 0xbf, 0x4a, 0x30, 0x82, 0xad, 0x32, 0xa5, 0x88, 0x09, 0x3c, 0xde, 0xb8] self.assertEqual(to_sign_bytes, bytearray(expect_to_sign)) signature = generate_content_signature(to_sign_bytes, private_key_bytes) self.assertTrue(verify_content_signature(to_sign_bytes, public_key_bytes, signature))
def test_lease_signature(self): private_key = "8Yoy9QL2sqggrM22VvHcRmAVzRr5h23FuDeFohyAU27B" public_key = "2cLDxAPJNWGGWAyHUFEnyoznhkf4QCEkcQrL5g2oEBCY" private_key_bytes = base58.decode(private_key) public_key_bytes = base58.decode(public_key) msg = VsysTransaction( transactionType=LEASE_TX_TYPE, senderPublicKey=public_key, amount=1000000000, fee=10000000, feeScale=100, recipient="AU6GsBinGPqW8zUuvmjgwpBNLfyyTU3p83Q", timestamp=1547722056762119200 ) to_sign_bytes = encode_lease_tx_to_bytes(msg) expect_to_sign = [0x03, 0x05, 0x54, 0x9c, 0x6d, 0xf7, 0xb3, 0x76, 0x77, 0x1b, 0x19, 0xff, 0x3b, 0xdb, 0x58, 0xd0, 0x4b, 0x49, 0x99, 0x91, 0x66, 0x3c, 0x47, 0x44, 0x4e, 0x42, 0x5f, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0x96, 0x80, 0x00, 0x64, 0x15, 0x7a, 0x9d, 0x02, 0xac, 0x57, 0xd4, 0x20] self.assertEqual(to_sign_bytes, bytearray(expect_to_sign)) signature = generate_content_signature(to_sign_bytes, private_key_bytes) self.assertTrue(verify_content_signature(to_sign_bytes, public_key_bytes, signature))
def validate_address(address: str, chain_id: str): try: addr_bytes = base58.decode(address) except: return False if len(addr_bytes) != VSYS_ADDRESS_LENGTH: return False # Wrong address length elif addr_bytes[0] != VSYS_ADDRESS_VERSION: return False # Wrong address version elif not chain_id or chr(addr_bytes[1]) != chain_id[0]: return False # Wrong chain id else: expected_checksum = addr_bytes[-VSYS_CHECKSUM_LENGTH:] actual_checksum = hash_chain(addr_bytes[:-VSYS_CHECKSUM_LENGTH])[:VSYS_CHECKSUM_LENGTH] return expected_checksum == actual_checksum
def is_safe_output_address(address) -> bool: """ Determines whether it is safe to include the address as-is as a tx output, preventing unintended side effects (e.g. CBOR injection) """ try: address_hex = base58.decode(address) address_unpacked = cbor.decode(address_hex) except ValueError as e: if __debug__: log.exception(__name__, e) return False if not isinstance(address_unpacked, list) or len(address_unpacked) != 2: return False address_data_encoded = address_unpacked[0] if not isinstance(address_data_encoded, bytes): return False return _encode_address_raw(address_data_encoded) == address
def decode(string: str) -> bytes: """ Convert base58 encoded string to bytes. """ return base58.decode(string, alphabet=_ripple_alphabet)
def get_public_key_from_private_key(private_key: str): private_key_bytes = base58.decode(private_key) public_key_bytes = curve25519.publickey(private_key_bytes) public_key = base58.encode(public_key_bytes) return public_key