def test_num2hexstring(self): self.assertEqual(num2hexstring(0), '00') self.assertEqual(num2hexstring(255), 'ff') self.assertEqual(num2hexstring(256, size=2, little_endian=True), '0001') self.assertEqual(num2hexstring(2222, size=2, little_endian=True), 'ae08')
def create_offer_hash(neo_address, offer_asset_hash, offer_asset_amt, want_asset_hash, want_asset_amt, txn_uuid): reverse_user_hash = reverse_hex(neo_get_scripthash_from_address(neo_address)) reverse_offer_asset_hash = reverse_hex(offer_asset_hash) reverse_offer_amount = num2hexstring(number=offer_asset_amt, size=8, little_endian=True) reverse_want_asset_hash = reverse_hex(want_asset_hash) reverse_want_amount = num2hexstring(number=want_asset_amt, size=8, little_endian=True) nonce_hex = txn_uuid.encode('utf-8').hex() offer_key_bytes = reverse_user_hash + reverse_offer_asset_hash + reverse_want_asset_hash + reverse_offer_amount +\ reverse_want_amount + nonce_hex offer_hash = reverse_hex(Crypto.Hash256(binascii.a2b_hex(offer_key_bytes)).hex()) return offer_hash
def serialize_transaction_attribute(attr): if len(attr['data']) > max_transaction_attribute_size: raise ValueError('Transaction attribute data is larger than the Maximum allowed attribute size.') out = num2hexstring(attr['usage']) if attr['usage'] == 0x81: out += num2hexstring(len(attr['data']) / 2) elif attr['usage'] == 0x90 or attr['usage'] >= 0xf0: out += num2varint(len(attr['data']) / 2) if attr['usage'] == 0x02 or attr['usage'] == 0x03: out += attr['data'][2:64] else: out += attr['data'] return out
def serialize_transaction(transaction, signed=True): serialized_txn = '' serialized_txn += str(num2hexstring(transaction['type'])) serialized_txn += str(num2hexstring(transaction['version'])) serialized_txn += str(serialize_exclusive[transaction['type']](transaction)) serialized_txn += str(num2varint(len(transaction['attributes']))) for attribute in transaction['attributes']: serialized_txn += serialize_transaction_attribute(attribute) serialized_txn += str(num2varint(len(transaction['inputs']))) for txn_input in transaction['inputs']: serialized_txn += serialize_transaction_input(txn_input) serialized_txn += str(num2varint(len(transaction['outputs']))) for txn_output in transaction['outputs']: serialized_txn += serialize_transaction_output(txn_output) if signed and transaction['scripts'] and len(transaction['scripts']) > 0: serialized_txn += str(num2varint(len(transaction['scripts']))) for script in transaction['scripts']: serialized_txn += serialize_witness(script) return serialized_txn
def serialize_transaction_input(txn_input): return reverse_hex(txn_input['prevHash']) + reverse_hex(num2hexstring(txn_input['prevIndex'], 2))