async def _write_coin_data(self): output = b"" output += VarInt(len(self.inputs)).encode() for coin in self.inputs: output += write_with_length(hash_from_address(coin.get('address'))) output += struct.pack("H", coin.get('assetsChainId')) output += struct.pack("H", coin.get('assetsId')) output += coin.get('amount').to_bytes(32, 'little') # output += struct.pack("Q", coin.get('amount')) output += write_with_length(bytes.fromhex(coin.get('nonce'))) output += bytes([coin.get('locked')]) output += VarInt(len(self.outputs)).encode() for coin in self.outputs: output += write_with_length(hash_from_address(coin.get('address'))) output += struct.pack("H", coin.get('assetsChainId')) output += struct.pack("H", coin.get('assetsId')) # output += struct.pack("Q", coin.get('amount')) output += coin.get('amount').to_bytes(32, 'little') if coin.get('lockTime') == -1: output += bytes.fromhex("ffffffffffffffffff") else: output += struct.pack("Q", coin.get('lockTime')) return output
def serialize(self): output = b"" if self.rawScript is not None: output += write_with_length(self.rawScript) elif self.fromHash is not None: output += write_with_length(self.fromHash + bytes([self.fromIndex])) elif self.address is not None: output += write_with_length(self.address) else: raise ValueError("Either fromHash and fromId should be set or address.") output += struct.pack("Q", self.na) output += writeUint48(self.lockTime) return output
async def serialize(self, for_hash=False, update_coins=True, update_data=True): if update_data and self.type in TX_TYPES_REGISTER: self.raw_tx_data = await self._write_data() if update_coins: self.raw_coin_data = await self._write_coin_data() output = b"" output += struct.pack("H", self.type) output += struct.pack("I", self.time) output += write_with_length(self.remark) output += write_with_length(self.raw_tx_data) output += write_with_length(self.raw_coin_data) if not for_hash: output += write_with_length(self.raw_signature) # output += write_with_length(await self.coin_data.serialize()) # output += self.scriptSig is not None and write_with_length(self.scriptSig) or PLACE_HOLDER return output
async def to_buffer(cls, md): output = hash_from_address(md['sender']) output += hash_from_address(md['contractAddress']) output += struct.pack("q", md['value']) output += struct.pack("I", md['codeLen']) output += write_with_length(unhexlify(md['code'])) output += struct.pack("q", md['gasLimit']) output += struct.pack("q", md['price']) output += bytes([len(md['args'])]) for arg in md['args']: output += bytes([len(arg)]) for argitem in arg: try: argitem = argitem.encode('utf-8') except UnicodeEncodeError: LOGGER.warning("Unicode encode error here, passing raw value.") output += write_with_length(argitem) return output