def test_num2varint(self): self.assertEqual(num2varint(0), '00') self.assertEqual(num2varint(252), 'fc') self.assertEqual(num2varint(253), 'fdfd00') self.assertEqual(num2varint(255), 'fdff00') self.assertEqual(num2varint(256), 'fd0001') self.assertEqual(num2varint(2222), 'fdae08') self.assertEqual(num2varint(111111), 'fe07b20100') self.assertEqual(num2varint(11111111111), 'ffc719469602000000')
def serialize_claim_exclusive(transaction): if transaction['type'] != 0x02: raise ValueError( 'The transaction type {} does not match the claim exclusive method.'.format(transaction['type'])) out = num2varint(len(transaction['claims'])) for claim in transaction['claims']: out += serialize_transaction_input(claim) return out
def serialize_invocation_exclusive(transaction): if transaction['type'] != 0xd1: raise ValueError( 'The transaction type {} does not match the invocation exclusive method.'.format(transaction['type'])) out = num2varint(int(len(transaction['script'])/2)) out += transaction['script'] if transaction['version'] >= 1: out += num2fixed8(transaction['gas']) 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_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_witness(witness): invocation_len = num2varint(len(witness['invocationScript']) / 2) verification_len = num2varint(len(witness['verificationScript']) / 2) return invocation_len + witness['invocationScript'] + verification_len + witness['verificationScript']
def encode_message(message): message_hex = binascii.hexlify(stringify_message(message).encode('utf-8')).decode() message_hex_length = num2varint(len(message_hex) // 2) return '010001f0' + message_hex_length + message_hex + '0000'