def __anchors_to_binary(self): binary = b'' for anchor in self.anchors: binary += struct.pack(">H", len(crypto.str2bytes(anchor))) binary += crypto.str2bytes(anchor) return binary
def __to_binary_v3(self): return (self.TYPE.to_bytes(1, 'big') + b'\3' + crypto.str2bytes(self.chain_id) + struct.pack(">Q", self.timestamp) + crypto.key_type_id(self.sender_key_type) + base58.b58decode(self.sender_public_key) + struct.pack(">Q", self.tx_fee) + base58.b58decode(self.recipient) + struct.pack(">i", self.association_type) + struct.pack(">H", len(crypto.str2bytes(self.anchor))) + crypto.str2bytes(self.anchor))
def __to_binary_v3(self): return (self.TYPE.to_bytes(1, 'big') + b'\3' + crypto.str2bytes(self.chain_id) + struct.pack(">Q", self.timestamp) + crypto.key_type_id(self.sender_key_type) + base58.b58decode(self.sender_public_key) + struct.pack(">Q", self.tx_fee) + struct.pack(">H", len(self.transfers)) + self.__transfers_to_binary() + struct.pack(">H", len(self.attachment)) + crypto.str2bytes(self.attachment))
def __value_to_binary(self): if self.type == 'integer': return b'\0' + struct.pack(">Q", self.value) elif self.type == 'boolean': return b'\1' + (b'\1' if self.value else b'\0') elif self.type == 'binary': byte_val = crypto.str2bytes(self.value) return b'\2' + struct.pack(">H", len(byte_val)) + byte_val elif self.type == 'string': byte_val = crypto.str2bytes(self.value) return b'\3' + struct.pack(">H", len(byte_val)) + byte_val else: raise Exception('Data Type not supported')
def __to_binary_v1(self): return (self.TYPE.to_bytes(1, 'big') + b'\1' + crypto.str2bytes(crypto.get_network(self.sender)) + base58.b58decode(self.sender_public_key) + base58.b58decode(self.recipient) + struct.pack(">Q", self.timestamp) + struct.pack(">Q", self.tx_fee))
def __to_binary_v2(self): return (self.TYPE.to_bytes(1, 'big') + b'\02' + crypto.str2bytes(self.chain_id) + base58.b58decode(self.sender_public_key) + struct.pack(">Q", self.tx_fee) + struct.pack(">Q", self.timestamp) + base58.b58decode(self.lease_id))
def __to_binary_v1(self): if self.anchor: return (b'\x10' + b'\1' + crypto.str2bytes(self.chain_id) + base58.b58decode(self.sender_public_key) + base58.b58decode(self.recipient) + struct.pack(">i", self.association_type) + b'\1' + struct.pack(">H", len(crypto.str2bytes(self.anchor))) + crypto.str2bytes(self.anchor) + struct.pack(">Q", self.timestamp) + struct.pack(">Q", self.tx_fee)) else: return (b'\x10' + b'\1' + crypto.str2bytes(self.chain_id) + base58.b58decode(self.sender_public_key) + base58.b58decode(self.recipient) + struct.pack(">i", self.association_type) + b'\0' + struct.pack(">Q", self.timestamp) + struct.pack(">Q", self.tx_fee))
def __to_binary_v2(self): return (self.TYPE.to_bytes(1, 'big') + b'\2' + base58.b58decode(self.sender_public_key) + struct.pack(">Q", self.timestamp) + struct.pack(">Q", self.amount) + struct.pack(">Q", self.tx_fee) + base58.b58decode(self.recipient) + struct.pack(">H", len(self.attachment)) + crypto.str2bytes(self.attachment))
def __to_binary_v3(self): data_binary = self.__data_to_binary() return (self.TYPE.to_bytes(1, 'big') + b'\3' + crypto.str2bytes(self.chain_id) + struct.pack(">Q", self.timestamp) + crypto.key_type_id(self.sender_key_type) + base58.b58decode(self.sender_public_key) + struct.pack(">Q", self.tx_fee) + struct.pack(">H", len(self.data)) + data_binary)
def __to_binary_v1(self): if self.anchor: return (self.TYPE.to_bytes(1, 'big') + b'\1' + crypto.str2bytes(crypto.get_network(self.sender)) + base58.b58decode(self.sender_public_key) + base58.b58decode(self.recipient) + struct.pack(">i", self.association_type) + b'\1' + struct.pack(">H", len(crypto.str2bytes(self.anchor))) + crypto.str2bytes(self.anchor) + struct.pack(">Q", self.timestamp) + struct.pack(">Q", self.tx_fee)) else: return (self.TYPE.to_bytes(1, 'big') + b'\1' + crypto.str2bytes(crypto.get_network(self.sender)) + base58.b58decode(self.sender_public_key) + base58.b58decode(self.recipient) + struct.pack(">i", self.association_type) + b'\0' + struct.pack(">Q", self.timestamp) + struct.pack(">Q", self.tx_fee))
def __to_binary_v3(self): decoded_script = base64.b64decode(self.script[7:]) if self.script != "" else b'' return (self.TYPE.to_bytes(1, 'big') + b'\3' + crypto.str2bytes(self.chain_id) + struct.pack(">Q", self.timestamp) + crypto.key_type_id(self.sender_key_type) + base58.b58decode(self.sender_public_key) + struct.pack(">Q", self.tx_fee) + struct.pack(">H", len(decoded_script)) + decoded_script)
def create_sign_keys(self, seed, nonce=0): if self.seed_method == 'brainwallet': seed_hash = crypto.hash_chain(struct.pack(">L", nonce) + crypto.str2bytes(seed)) account_seed_hash = crypto.sha256(seed_hash) elif self.seed_method == 'bip39' or self.seed_method.startswith('bip39:'): raise Exception('Seed method under construction') #account_seed_hash = self.mnemo.to_seed(seed) else: raise Exception('Unsupported seed method') private_key = SigningKey(account_seed_hash[:32]) public_key = private_key.verify_key return private_key, public_key, self.key_type
def __to_binary_v1(self): if self.script != "": decoded_script = base64.b64decode(self.script[7:]) binary_script = b'\1' + struct.pack(">H", len(decoded_script)) + decoded_script else: binary_script = b'\0' return (self.TYPE.to_bytes(1, 'big') + b'\1' + crypto.str2bytes(crypto.get_network(self.sender)) + base58.b58decode(self.sender_public_key) + binary_script + struct.pack(">Q", self.tx_fee) + struct.pack(">Q", self.timestamp))
def to_json(self): return (crypto.merge_dicts( { "id": self.id if self.id else "", "type": self.TYPE, "version": self.version, "sender": self.sender, "senderKeyType": self.sender_key_type, "senderPublicKey": self.sender_public_key, "fee": self.tx_fee, "timestamp": self.timestamp, "anchors": list(map(lambda anchor: base58.b58encode(crypto.str2bytes(anchor)), self.anchors)), "proofs": self.proofs, "height": self.height if self.height else "" }, self._sponsor_json()))
def to_json(self): return (crypto.merge_dicts( { "id": self.id if self.id else "", "type": self.TYPE, "version": self.version, "sender": self.sender, "senderKeyType": self.sender_key_type, "senderPublicKey": self.sender_public_key, "recipient": self.recipient, "associationType": self.association_type, "hash": base58.b58encode(crypto.str2bytes(self.anchor)), "timestamp": self.timestamp, "fee": self.tx_fee, "proofs": self.proofs, "height": self.height if self.height else "" }, self._sponsor_json()))
def create_address(self, public_key): unhashed_address = chr(1) + str(self.chain_id) + crypto.hash_chain(public_key.__bytes__())[0:20] address_hash = crypto.hash_chain(crypto.str2bytes(unhashed_address))[0:4] return base58.b58encode(crypto.str2bytes(unhashed_address + address_hash))
def testhash_chain(self): assert crypto.hash_chain( crypto.str2bytes('T©È T\÷?<ÑÙ½êYV¤u< '))[0:4] == 'Z*gó'
def testConversion(self): assert crypto.str2bytes('test') == b'test' assert crypto.bytes2str(b'test') == 'test' assert crypto.str2list('test') == ['t', 'e', 's', 't']
def to_binary(self): key_bytes = crypto.str2bytes(self.key) return (struct.pack(">H", len(key_bytes)) + key_bytes + self.__value_to_binary())