async def sign_tx(ctx, msg, keychain): await paths.validate_path(ctx, helpers.validate_full_path, keychain, msg.address_n, CURVE) node = keychain.derive(msg.address_n, CURVE) tx = msg.tx if tx.protocol != PROTOCOL: raise wire.DataError("Invalid protocol") if tx.api > SUPPORT_API_VER: raise wire.DataError( "Not support to sign this transaction. Please upgrade firmware!") if tx.opc != OPC_TX: raise wire.DataError("Invalid operation code") if not tx.fee: raise wire.DataError("Invalid fee") if not tx.feeScale: raise wire.DataError("Invalid fee scale") if not tx.timestamp: raise wire.DataError("Invalid timestamp") sk = helpers.modify_private_key(node.private_key()) pk = curve25519.publickey(sk) pk_base58 = base58.encode(pk) if tx.senderPublicKey != pk_base58: raise wire.DataError( "Public key mismatch. Please confirm you used correct Trezor device." ) if tx.transactionType == PAYMENT_TX_TYPE: await layout.require_confirm_payment_tx(ctx, tx.recipient, tx.amount) to_sign_bytes = encode_payment_tx_to_bytes(tx) elif tx.transactionType == LEASE_TX_TYPE: await layout.require_confirm_lease_tx(ctx, tx.recipient, tx.amount) to_sign_bytes = encode_lease_tx_to_bytes(tx) elif tx.transactionType == LEASE_CANCEL_TX_TYPE: await layout.require_confirm_cancel_lease_tx(ctx, tx.txId) to_sign_bytes = encode_cancel_lease_tx_to_bytes(tx) else: raise wire.DataError("Transaction type unsupported") signature = generate_content_signature(to_sign_bytes, sk) signature_base58 = base58.encode(signature) return VsysSignedTx(signature=signature_base58, api=SIGN_API_VER, protocol=PROTOCOL, opc=OPC_SIGN)
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 derive_address_and_node(root_node, path: list): validate_derivation_path(path) derived_node = root_node.clone() # this means empty derivation path m/44'/1815' if len(path) == 2: address_payload = None address_attributes = {} else: if len(path) == 5: p = [path[2], path[4]] else: p = [path[2]] for indice in p: derived_node.derive_cardano(indice) hd_passphrase = _derive_hd_passphrase(root_node) address_payload = _encrypt_derivation_path(p, hd_passphrase) address_attributes = {1: cbor.encode(address_payload)} address_root = _get_address_root(derived_node, address_payload) address_type = 0 address_data = [address_root, address_attributes, address_type] address_data_encoded = cbor.encode(address_data) address = base58.encode( cbor.encode([ cbor.Tagged(24, address_data_encoded), crc.crc32(address_data_encoded) ])) return (address, derived_node)
def encode_human_readable(address_bytes: bytes) -> str: address_type = get_type(address_bytes) if address_type == CardanoAddressType.BYRON: return base58.encode(address_bytes) elif address_type in ADDRESS_TYPES_SHELLEY: hrp = _get_bech32_hrp(address_type, _get_network_id(address_bytes)) return bech32.encode(hrp, address_bytes) else: raise ValueError
async def get_address(ctx, msg, keychain): await paths.validate_path(ctx, keychain, msg.address_n) node = keychain.derive(msg.address_n) pubkey = node.public_key()[1:] # skip ed25519 pubkey marker address = base58.encode(pubkey) if msg.show_display: desc = address_n_to_str(msg.address_n) await show_address(ctx, address=address, address_qr=address, desc=desc) return SolanaAddress(address=address)
def derive_human_readable_address( keychain: seed.Keychain, parameters: CardanoAddressParametersType, protocol_magic: int, network_id: int, ) -> str: address = derive_address_bytes(keychain, parameters, protocol_magic, network_id) address_type = _get_address_type(address) if address_type == CardanoAddressType.BYRON: return base58.encode(address) elif address_type in ADDRESS_TYPES_SHELLEY: hrp = _get_bech32_hrp_for_address(_get_address_type(address), network_id) return bech32.encode(hrp, address) else: raise ValueError
def derive_address_and_node(keychain, path: list): node = keychain.derive(path) address_payload = None address_attributes = {} address_root = _get_address_root(node, address_payload) address_type = 0 address_data = [address_root, address_attributes, address_type] address_data_encoded = cbor.encode(address_data) address = base58.encode( cbor.encode( [cbor.Tagged(24, address_data_encoded), crc.crc32(address_data_encoded)] ) ) return (address, node)
async def get_public_key(ctx, msg, keychain): await paths.validate_path(ctx, helpers.validate_full_path, keychain, msg.address_n, CURVE) node = keychain.derive(msg.address_n, CURVE) pk = seed.remove_ed25519_prefix(node.public_key()) pk_base58 = base58.encode(pk) chain_id = helpers.get_chain_id(msg.address_n) address = helpers.get_address_from_public_key(pk_base58, chain_id) if msg.show_display: await _show_vsys_pubkey(ctx, pk_base58) return VsysPublicKey(api=ACCOUNT_API_VER, public_key=pk_base58, address=address, protocol=PROTOCOL, opc=OPC_ACCOUNT)
async def sign_tx_hash(ctx, msg, keychain): await paths.validate_path(ctx, keychain, msg.address_n) hash = bytes(msg.hash) # Confirm text = Text("Sign transaction hash", ui.ICON_SEND, ui.GREEN) text.mono(base58.encode(hash)) await require_confirm(ctx, text, ButtonRequestType.SignTx) # Get key node = keychain.derive(msg.address_n) seckey = node.private_key() pubkey = node.public_key()[1:] # skip ed25519 pubkey marker # Sign hash signature = ed25519.sign(seckey, hash) return SolanaSignedTx(signature=signature)
def derive_address_and_node(root_node, path: list): derived_node = root_node.clone() address_payload = None address_attributes = {} for indice in path: derived_node.derive_cardano(indice) address_root = _get_address_root(derived_node, address_payload) address_type = 0 address_data = [address_root, address_attributes, address_type] address_data_encoded = cbor.encode(address_data) address = base58.encode( cbor.encode([ cbor.Tagged(24, address_data_encoded), crc.crc32(address_data_encoded) ])) return (address, derived_node)
def base58_encode(prefix: str, sig_prefix: str, data: bytes) -> str: b58 = base58.encode(data + base58.ripemd160_32(data + sig_prefix.encode())) if sig_prefix: return prefix + sig_prefix + "_" + b58 else: return prefix + b58
def encode(data: bytes) -> str: """ Convert bytes to base58 encoded string. """ return base58.encode(data, alphabet=_ripple_alphabet)
def _encode_address_raw(address_data_encoded): return base58.encode( cbor.encode([ cbor.Tagged(24, address_data_encoded), crc.crc32(address_data_encoded) ]))
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