def DeserializeFrom(reader): ttype = reader.ReadByte() tx = None from neo.Core.TX.RegisterTransaction import RegisterTransaction from neo.Core.TX.IssueTransaction import IssueTransaction from neo.Core.TX.ClaimTransaction import ClaimTransaction from neo.Core.TX.MinerTransaction import MinerTransaction if ttype == int.from_bytes(TransactionType.RegisterTransaction, 'little'): tx = RegisterTransaction() elif ttype == int.from_bytes(TransactionType.MinerTransaction, 'little'): tx = MinerTransaction() elif ttype == int.from_bytes(TransactionType.IssueTransaction, 'little'): tx = IssueTransaction() elif ttype == int.from_bytes(TransactionType.ClaimTransaction, 'little'): tx = ClaimTransaction() tx.DeserializeUnsignedWithoutType(reader) try: witness = Witness() witness.Deserialize(reader) tx.scripts = [witness] except Exception as e: pass tx.OnDeserialized() return tx
def FromTrimmedData(byts): """ Deserialize a block from raw bytes. Args: byts: Returns: Block: """ block = Block() block.__is_trimmed = True ms = StreamManager.GetStream(byts) reader = BinaryReader(ms) block.DeserializeUnsigned(reader) reader.ReadByte() witness = Witness() witness.Deserialize(reader) block.Script = witness bc = GetBlockchain() tx_list = [] for tx_hash in reader.ReadHashes(): tx = bc.GetTransaction(tx_hash)[0] if not tx: raise Exception("Could not find transaction!\n Are you running code against a valid Blockchain instance?\n Tests that accesses transactions or size of a block but inherit from NeoTestCase instead of BlockchainFixtureTestCase will not work.") tx_list.append(tx) block.Transactions = tx_list StreamManager.ReleaseStream(ms) return block
def FromTrimmedData(byts): """ Deserialize a block from raw bytes. Args: byts: Returns: Block: """ block = Block() block.__is_trimmed = True ms = StreamManager.GetStream(byts) reader = BinaryReader(ms) block.DeserializeUnsigned(reader) reader.ReadByte() witness = Witness() witness.Deserialize(reader) block.Script = witness block.Transactions = reader.ReadHashes() StreamManager.ReleaseStream(ms) return block
def FromTrimmedData(byts, index=None, transaction_method=None): """ Deserialize a block from raw bytes. Args: byts: index: UNUSED transaction_method: UNUSED Returns: Block: """ block = Block() block.__is_trimmed = True ms = StreamManager.GetStream(byts) reader = BinaryReader(ms) block.DeserializeUnsigned(reader) reader.ReadByte() witness = Witness() witness.Deserialize(reader) block.witness = witness block.Transactions = reader.ReadHashes() StreamManager.ReleaseStream(ms) return block
def FromTrimmedData(data, index): """ Deserialize into a Header object from the provided data. Args: data (bytes): index: UNUSED Returns: Header: """ header = Header() ms = StreamManager.GetStream(data) reader = BinaryReader(ms) header.DeserializeUnsigned(reader) reader.ReadByte() witness = Witness() witness.Deserialize(reader) header.Script = witness StreamManager.ReleaseStream(ms) return header
def Deserialize(self, reader): self.DeserializeUnsigned(reader) byt = reader.ReadByte() if int(byt) != 1: raise Exception('Incorrect format') witness = Witness() witness.Deserialize(reader) self.Script = witness
def DeserializeFrom(reader): """ Deserialize full object. Args: reader (neo.IO.BinaryReader): Returns: Transaction: """ ttype = reader.ReadByte() tx = None from neo.Core.TX.RegisterTransaction import RegisterTransaction from neo.Core.TX.IssueTransaction import IssueTransaction from neo.Core.TX.ClaimTransaction import ClaimTransaction from neo.Core.TX.MinerTransaction import MinerTransaction from neo.Core.TX.PublishTransaction import PublishTransaction from neo.Core.TX.InvocationTransaction import InvocationTransaction from neo.Core.TX.EnrollmentTransaction import EnrollmentTransaction from neo.Core.TX.StateTransaction import StateTransaction if ttype == int.from_bytes(TransactionType.RegisterTransaction, 'little'): tx = RegisterTransaction() elif ttype == int.from_bytes(TransactionType.MinerTransaction, 'little'): tx = MinerTransaction() elif ttype == int.from_bytes(TransactionType.IssueTransaction, 'little'): tx = IssueTransaction() elif ttype == int.from_bytes(TransactionType.ClaimTransaction, 'little'): tx = ClaimTransaction() elif ttype == int.from_bytes(TransactionType.PublishTransaction, 'little'): tx = PublishTransaction() elif ttype == int.from_bytes(TransactionType.InvocationTransaction, 'little'): tx = InvocationTransaction() elif ttype == int.from_bytes(TransactionType.EnrollmentTransaction, 'little'): tx = EnrollmentTransaction() elif ttype == int.from_bytes(TransactionType.StateTransaction, 'little'): tx = StateTransaction() else: tx = Transaction() tx.Type = ttype tx.DeserializeUnsignedWithoutType(reader) tx.scripts = [] byt = reader.ReadVarInt() if byt > 0: for i in range(0, byt): witness = Witness() witness.Deserialize(reader) tx.scripts.append(witness) tx.OnDeserialized() return tx
def DeserializeFrom(self, reader): """ Deserialize full object. Args: reader (neo.IO.BinaryReader): """ ttype = reader.ReadByte() if ttype == int.from_bytes(TransactionType.RegisterTransaction, 'little'): self.Type = TransactionType.RegisterTransaction elif ttype == int.from_bytes(TransactionType.MinerTransaction, 'little'): self.Type = TransactionType.MinerTransaction elif ttype == int.from_bytes(TransactionType.IssueTransaction, 'little'): self.Type = TransactionType.IssueTransaction elif ttype == int.from_bytes(TransactionType.ClaimTransaction, 'little'): self.Type = TransactionType.ClaimTransaction elif ttype == int.from_bytes(TransactionType.PublishTransaction, 'little'): self.Type = TransactionType.PublishTransaction elif ttype == int.from_bytes(TransactionType.InvocationTransaction, 'little'): self.Type = TransactionType.InvocationTransaction elif ttype == int.from_bytes(TransactionType.EnrollmentTransaction, 'little'): self.Type = TransactionType.EnrollmentTransaction elif ttype == int.from_bytes(TransactionType.StateTransaction, 'little'): self.Type = TransactionType.StateTransaction else: self.Type = ttype self.DeserializeUnsignedWithoutType(reader) self.scripts = [] byt = reader.ReadVarInt() if byt > 0: for i in range(0, byt): witness = Witness() witness.Deserialize(reader) self.scripts.append(witness) self.OnDeserialized()
def Deserialize(self, reader): """ Deserialize full object. Args: reader (neo.IO.BinaryReader): """ self.DeserializeUnsigned(reader) byt = reader.ReadByte() if int(byt) != 1: raise Exception('Incorrect format') witness = Witness() witness.Deserialize(reader) self.Script = witness
def GetScripts(self): if not self.Completed: raise Exception("Signature Context not complete") scripts = [] for i in range(0, len(self.ScriptHashes)): item = self.ContextItems[self.ScriptHashes[i].ToBytes()] print("GETTING SCRIPTS, item is %s " % item) sb = ScriptBuilder() plist = list(item.ContractParameters) plist.reverse() for p in plist: sb.push(p.Value) vscript = bytearray(0) if item.Script is not None: vscript = item.Script witness = Witness( invocation_script=sb.ToArray(), verification_script=vscript ) scripts.append(witness) return scripts
def FromTrimmedData(data, index): header = Header() ms = MemoryStream(data) reader = BinaryReader(ms) header.DeserializeUnsigned(reader) reader.ReadByte() witness = Witness() witness.Deserialize(reader) header.Script = witness return header
def GenesisBlock(): """ Create the GenesisBlock. Returns: BLock: """ prev_hash = UInt256(data=bytearray(32)) timestamp = int( datetime(2016, 7, 15, 15, 8, 21, tzinfo=pytz.utc).timestamp()) index = 0 consensus_data = 2083236893 # 向比特币致敬 ( Pay Tribute To Bitcoin ) next_consensus = Blockchain.GetConsensusAddress( Blockchain.StandbyValidators()) script = Witness(bytearray(0), bytearray(PUSHT)) mt = MinerTransaction() mt.Nonce = 2083236893 output = TransactionOutput( Blockchain.SystemShare().Hash, Blockchain.SystemShare().Amount, Crypto.ToScriptHash( Contract.CreateMultiSigRedeemScript( int(len(Blockchain.StandbyValidators()) / 2) + 1, Blockchain.StandbyValidators()))) it = IssueTransaction([], [output], [], [script]) return Block( prev_hash, timestamp, index, consensus_data, next_consensus, script, [mt, Blockchain.SystemShare(), Blockchain.SystemCoin(), it], True)
def test_issue_tx(self): miner_tx = MinerTransaction() miner_tx.Nonce = 2083236893 share_tx = GetSystemShare() coin_tx = GetSystemCoin() script = Contract.CreateMultiSigRedeemScript(int(len(Blockchain.StandbyValidators()) / 2) + 1, Blockchain.StandbyValidators()) if Settings.MAGIC == 1953787457: self.assertEqual(script, self.contractraw) out = Crypto.ToScriptHash(script) output = TransactionOutput( share_tx.Hash, Blockchain.SystemShare().Amount, out ) script = Witness( bytearray(0), bytearray(PUSHT)) issue_tx = IssueTransaction([],[output],[], [script]) self.assertEqual(issue_tx.GetHashData(), self.issuetx_rraw) self.assertEqual(issue_tx.Hash.ToBytes(), self.gen_issue_tx_id)
def FromTrimmedData(data, index): header = Header() ms = StreamManager.GetStream(data) reader = BinaryReader(ms) header.DeserializeUnsigned(reader) reader.ReadByte() witness = Witness() witness.Deserialize(reader) header.Script = witness StreamManager.ReleaseStream(ms) return header
def FromTrimmedData(byts, index, transaction_method=None): block = Block() block.__is_trimmed = True ms = StreamManager.GetStream(byts) reader = BinaryReader(ms) block.DeserializeUnsigned(reader) reader.ReadByte() witness = Witness() witness.Deserialize(reader) block.witness = witness block.Transactions = reader.ReadHashes() StreamManager.ReleaseStream(ms) return block
def deploy(from_addr, bytecode, contract_params, return_type, details): response = requests.post('http://127.0.0.1:20332', json={ 'jsonrpc': '2.0', 'id': 1, 'method': 'mw_construct_deploy_tx', 'params': [{ 'from_addr': from_addr, 'bin': bytecode, 'contract_params': contract_params, 'return_type': return_type, 'details': details, }] }).json() if 'error' in response: print(response['error']['message']) return context = response['result']['context'] binary_tx = response['result']['tx'] contract_hash = response['result']['hash'] tx = ContractTransaction.DeserializeFromBufer( binascii.unhexlify(binary_tx)) scripts = requests.post('http://127.0.0.1:5000/neo_sign/', json={ 'binary_tx': binary_tx }).json() tx.scripts = [ Witness( x['invocation'].encode(), x['verification'].encode(), ) for x in scripts ] ms = StreamManager.GetStream() writer = BinaryWriter(ms) tx.Serialize(writer) ms.flush() signed_tx = ms.ToArray() response = requests.post('http://127.0.0.1:20332', json={ 'jsonrpc': '2.0', 'id': 1, 'method': 'sendrawtransaction', 'params': [ signed_tx.decode(), ] }).json() print('contract hash:', contract_hash) print(response)
def sign_neo_transaction(tx, binary_tx, address): scripts = requests.post( 'http://{}/neo_sign/'.format(SIGNER), json={'binary_tx': binary_tx, 'address': address} ).json() tx.scripts = [Witness( x['invocation'].encode(), x['verification'].encode(), ) for x in scripts] return tx
def send(addr_from, addr_to, asset, amount): response = requests.post('http://127.0.0.1:20332', json={ 'jsonrpc': '2.0', 'id': 1, 'method': 'mw_construct_send_tx', 'params': { 'from': addr_from, 'to': addr_to, 'asset': asset, 'amount': amount, } }).json() print(response) context = response['result']['context'] binary_tx = response['result']['tx'] tx = ContractTransaction.DeserializeFromBufer( binascii.unhexlify(binary_tx)) scripts = requests.post('http://127.0.0.1:5000/neo_sign/', json={ 'binary_tx': binary_tx, 'address': addr_from }).json() print('scripts', scripts) tx.scripts = [ Witness( x['invocation'].encode(), x['verification'].encode(), ) for x in scripts ] print(scripts) ms = StreamManager.GetStream() writer = BinaryWriter(ms) tx.Serialize(writer) ms.flush() signed_tx = ms.ToArray() print(tx.ToJson()) # print('does not send: return') ; return response = requests.post('http://127.0.0.1:20332', json={ 'jsonrpc': '2.0', 'id': 1, 'method': 'sendrawtransaction', 'params': [ signed_tx.decode(), ] }).json() print(response)
def GetScripts(self): if not self.Completed: raise Exception("Signature Context not complete") scripts = [] for i in range(0, len(self.ScriptHashes)): item = self.ContextItems[self.ScriptHashes[i].ToBytes()] sb = ScriptBuilder() plist = list(item.ContractParameters) plist.reverse() for p in plist: if type(p.Value) is list: pa = p.Value pa.reverse() listlength = len(pa) for listitem in pa: sb.push(listitem) sb.push(listlength) sb.Emit(OpCode.PACK) else: sb.push(p.Value) vscript = bytearray(0) if item.Script is not None: if type(item.Script) is str: item.Script = item.Script.encode('utf-8') vscript = item.Script # logger.info("SCRIPT IS %s " % item.Script) witness = Witness( # invocation_script='40fdb984faf0a400b6894c1ce5b317cf894ba3eb89b899cefda2ac307b278418b943534ad298884f9200dc4b7e1dc244db16c62a44a830a860060ec11d3e6e9717', invocation_script=sb.ToArray(), verification_script=vscript) scripts.append(witness) return scripts
def GetScripts(self): if not self.Completed: raise Exception("Signature Context not complete") scripts = [] for i in range(0, len(self.ScriptHashes)): item = self.ContextItems[self.ScriptHashes[i].ToBytes()] sb = ScriptBuilder() plist = list(item.ContractParameters) plist.reverse() for p in plist: if type(p.Value) is list: pa = p.Value pa.reverse() listlength = len(pa) for listitem in pa: sb.push(listitem) sb.push(listlength) sb.Emit(OpCode.PACK) else: sb.push(p.Value) vscript = bytearray(0) if item.Script is not None: if type(item.Script) is str: item.Script = item.Script.encode('utf-8') vscript = item.Script # logger.info("SCRIPT IS %s " % item.Script) witness = Witness( invocation_script=sb.ToArray(), verification_script=vscript ) scripts.append(witness) return scripts
def GetScripts(self): if not self.Completed: raise Exception("Signature Context not complete") scripts = [] for i in range(0, len(self.Parameters)): sb = ScriptBuilder() plist = list(self.Parameters[i]) plist.reverse() for p in plist: sb.push(p) witness = Witness(invocation_script=sb.ToArray(), verification_script=self.Verifications[i]) scripts.append(witness) return scripts