def _encode_manager_common(w: bytearray, sequence_length, operation, to_contract=False): IMPLICIT_ADDRESS_LENGTH = 21 SMART_CONTRACT_ADDRESS_LENGTH = 22 # 5 = tag and sequence_length (1 byte + 4 bytes) argument_length = sequence_length + 5 helpers.write_bool(w, True) write_uint8(w, helpers.DO_ENTRYPOINT_TAG) write_uint32_be(w, argument_length) write_uint8(w, helpers.MICHELSON_SEQUENCE_TAG) write_uint32_be(w, sequence_length) helpers.write_instruction(w, "DROP") helpers.write_instruction(w, "NIL") helpers.write_instruction(w, "operation") helpers.write_instruction(w, operation) if to_contract is True: helpers.write_instruction(w, "address") else: helpers.write_instruction(w, "key_hash") if operation == "PUSH": write_bytes_unchecked(w, bytes([10])) # byte sequence if to_contract is True: write_uint32_be(w, SMART_CONTRACT_ADDRESS_LENGTH) else: write_uint32_be(w, IMPLICIT_ADDRESS_LENGTH)
def write_header(hasher: Writer, header: EosTxHeader) -> None: write_uint32_le(hasher, header.expiration) write_uint16_le(hasher, header.ref_block_num) write_uint32_le(hasher, header.ref_block_prefix) write_variant32(hasher, header.max_net_usage_words) write_uint8(hasher, header.max_cpu_usage_ms) write_variant32(hasher, header.delay_sec)
def generate_proof( node: bip32.HDNode, script_type: EnumTypeInputScriptType, multisig: Optional[MultisigRedeemScriptType], coin: CoinInfo, user_confirmed: bool, ownership_ids: List[bytes], script_pubkey: bytes, commitment_data: bytes, ) -> Tuple[bytes, bytes]: flags = 0 if user_confirmed: flags |= _FLAG_USER_CONFIRMED proof = empty_bytearray(4 + 1 + 1 + len(ownership_ids) * _OWNERSHIP_ID_LEN) write_bytes_fixed(proof, _VERSION_MAGIC, 4) write_uint8(proof, flags) write_bitcoin_varint(proof, len(ownership_ids)) for ownership_id in ownership_ids: write_bytes_fixed(proof, ownership_id, _OWNERSHIP_ID_LEN) sighash = hashlib.sha256(proof) sighash.update(script_pubkey) sighash.update(commitment_data) signature = common.ecdsa_sign(node, sighash.digest()) public_key = node.public_key() write_bip322_signature_proof(proof, script_type, multisig, coin, public_key, signature) return proof, signature
def _encode_proposal(w: bytearray, proposal): write_uint8(w, helpers.OP_TAG_PROPOSALS) write_bytes_fixed(w, proposal.source, helpers.TAGGED_PUBKEY_HASH_SIZE) write_uint32_be(w, proposal.period) write_uint32_be(w, len(proposal.proposals) * helpers.PROPOSAL_HASH_SIZE) for proposal_hash in proposal.proposals: write_bytes_fixed(w, proposal_hash, helpers.PROPOSAL_HASH_SIZE)
def _encode_ballot(w: bytearray, ballot): ballot_tag = 6 write_uint8(w, ballot_tag) write_bytes_unchecked(w, ballot.source) write_uint32_be(w, ballot.period) write_bytes_unchecked(w, ballot.proposal) write_uint8(w, ballot.ballot)
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 _encode_common(w: bytearray, operation, str_operation): operation_tags = {"reveal": 7, "transaction": 8, "origination": 9, "delegation": 10} write_uint8(w, operation_tags[str_operation]) _encode_contract_id(w, operation.source) _encode_zarith(w, operation.fee) _encode_zarith(w, operation.counter) _encode_zarith(w, operation.gas_limit) _encode_zarith(w, operation.storage_limit)
def _encode_proposal(w: bytearray, proposal): proposal_tag = 5 write_uint8(w, proposal_tag) write_bytes_unchecked(w, proposal.source) write_uint32_be(w, proposal.period) write_uint32_be(w, len(proposal.proposals) * PROPOSAL_LENGTH) for proposal_hash in proposal.proposals: write_bytes_unchecked(w, proposal_hash)
def _encode_zarith(w: bytearray, num): while True: byte = num & 127 num = num >> 7 if num == 0: write_uint8(w, byte) break write_uint8(w, 128 | byte)
def write_tx_input_check(w: Writer, i: TxInput) -> None: write_bytes_fixed(w, i.prev_hash, TX_HASH_SIZE) write_uint32(w, i.prev_index) write_uint32(w, i.script_type) write_uint8(w, input_is_external_unverified(i)) write_uint32(w, len(i.address_n)) for n in i.address_n: write_uint32(w, n) write_uint32(w, i.sequence) write_uint64(w, i.amount or 0) write_bytes_prefixed(w, i.script_pubkey or b"")
def _encode_common(w: bytearray, operation, str_operation): operation_tags = { "reveal": 107, "transaction": 108, "origination": 109, "delegation": 110, } write_uint8(w, operation_tags[str_operation]) write_bytes_unchecked(w, operation.source) _encode_zarith(w, operation.fee) _encode_zarith(w, operation.counter) _encode_zarith(w, operation.gas_limit) _encode_zarith(w, operation.storage_limit)
def _encode_common(w: bytearray, operation, str_operation): operation_tags = { "reveal": helpers.OP_TAG_REVEAL, "transaction": helpers.OP_TAG_TRANSACTION, "origination": helpers.OP_TAG_ORIGINATION, "delegation": helpers.OP_TAG_DELEGATION, } write_uint8(w, operation_tags[str_operation]) write_bytes_fixed(w, operation.source, helpers.TAGGED_PUBKEY_HASH_SIZE) _encode_zarith(w, operation.fee) _encode_zarith(w, operation.counter) _encode_zarith(w, operation.gas_limit) _encode_zarith(w, operation.storage_limit)
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 generate_proof( node: bip32.HDNode, script_type: InputScriptType, multisig: MultisigRedeemScriptType | None, coin: CoinInfo, user_confirmed: bool, ownership_ids: list[bytes], script_pubkey: bytes, commitment_data: bytes, ) -> tuple[bytes, bytes]: flags = 0 if user_confirmed: flags |= _FLAG_USER_CONFIRMED proof = utils.empty_bytearray(4 + 1 + 1 + len(ownership_ids) * _OWNERSHIP_ID_LEN) write_bytes_fixed(proof, _VERSION_MAGIC, 4) write_uint8(proof, flags) write_bitcoin_varint(proof, len(ownership_ids)) for ownership_id in ownership_ids: write_bytes_fixed(proof, ownership_id, _OWNERSHIP_ID_LEN) sighash = hashlib.sha256(proof) sighash.update(script_pubkey) sighash.update(commitment_data) if script_type in ( InputScriptType.SPENDADDRESS, InputScriptType.SPENDMULTISIG, InputScriptType.SPENDWITNESS, InputScriptType.SPENDP2SHWITNESS, ): signature = common.ecdsa_sign(node, sighash.digest()) elif script_type == InputScriptType.SPENDTAPROOT: signature = common.bip340_sign(node, sighash.digest()) else: raise wire.DataError("Unsupported script type.") public_key = node.public_key() write_bip322_signature_proof(proof, script_type, multisig, coin, public_key, signature) return proof, signature
def _encode_manager_common(w: bytearray, sequence_length, operation, to_contract=False): # 5 = tag and sequence_length (1 byte + 4 bytes) argument_length = sequence_length + 5 helpers.write_bool(w, True) write_uint8(w, helpers.DO_ENTRYPOINT_TAG) write_uint32_be(w, argument_length) write_uint8(w, helpers.MICHELSON_SEQUENCE_TAG) write_uint32_be(w, sequence_length) helpers.write_instruction(w, "DROP") helpers.write_instruction(w, "NIL") helpers.write_instruction(w, "operation") helpers.write_instruction(w, operation) if to_contract is True: helpers.write_instruction(w, "address") else: helpers.write_instruction(w, "key_hash") if operation == "PUSH": write_uint8(w, 10) # byte sequence if to_contract is True: write_uint32_be(w, helpers.CONTRACT_ID_SIZE) else: write_uint32_be(w, helpers.TAGGED_PUBKEY_HASH_SIZE)
def _encode_natural(w: bytearray, num): # encode a natural integer with its signed bit on position 7 # as we do not expect negative numbers in a transfer operation the bit is never set natural_tag = 0 write_uint8(w, natural_tag) byte = num & 63 modified = num >> 6 if modified == 0: write_uint8(w, byte) else: write_uint8(w, 128 | byte) _encode_zarith(w, modified)
def _encode_contract_id(w: bytearray, contract_id): write_uint8(w, contract_id.tag) write_bytes(w, contract_id.hash)
def write_tx_input_decred(w: Writer, i: TxInputType) -> None: write_bytes_reversed(w, i.prev_hash, TX_HASH_SIZE) write_uint32(w, i.prev_index or 0) write_uint8(w, i.decred_tree or 0) write_uint32(w, i.sequence)
def write_action_delegate(w: Writer, msg: EosActionDelegate) -> None: write_uint64_le(w, msg.sender) write_uint64_le(w, msg.receiver) write_asset(w, msg.net_quantity) write_asset(w, msg.cpu_quantity) write_uint8(w, 1 if msg.transfer else 0)
def write_bool(w: bytearray, boolean: bool): if boolean: write_uint8(w, 255) else: write_uint8(w, 0)
def write_tx_input_decred(w, i: TxInputType): write_bytes_reversed(w, i.prev_hash) write_uint32(w, i.prev_index or 0) write_uint8(w, i.decred_tree or 0) write_uint32(w, i.sequence)
def _encode_bool(w: bytearray, boolean): if boolean: write_uint8(w, 255) else: write_uint8(w, 0)
def _encode_contract_id(w: bytearray, contract_id): write_uint8(w, contract_id.tag) write_bytes_fixed(w, contract_id.hash, helpers.CONTRACT_ID_SIZE - 1)
def _encode_ballot(w: bytearray, ballot): write_uint8(w, helpers.OP_TAG_BALLOT) write_bytes_fixed(w, ballot.source, helpers.TAGGED_PUBKEY_HASH_SIZE) write_uint32_be(w, ballot.period) write_bytes_fixed(w, ballot.proposal, helpers.PROPOSAL_HASH_SIZE) write_uint8(w, ballot.ballot)
def write_bool(w: Writer, boolean: bool) -> None: if boolean: write_uint8(w, 255) else: write_uint8(w, 0)