def InvokeWithTokenVerificationScript(wallet, tx, token, fee=Fixed8.Zero()): wallet_tx = wallet.MakeTransaction(tx=tx, fee=fee, use_standard=True) if wallet_tx: token_contract_state = Blockchain.Default().GetContract( token.ScriptHash.ToString()) print("token contract %s " % token_contract_state) tx.Attributes = [ TransactionAttribute(usage=TransactionAttributeUsage.Script, data=token.ScriptHash.Data) ] reedeem_script = token_contract_state.Code.Script.hex() # there has to be at least 1 param, and the first # one needs to be a signature param param_list = bytearray(b'\x00\x00') verification_contract = Contract.Create( reedeem_script, param_list, wallet.GetDefaultContract().PublicKeyHash) context = ContractParametersContext(wallet_tx) wallet.Sign(context) context.Add(verification_contract, 0, 0) if context.Completed: wallet_tx.scripts = context.GetScripts() relayed = False # print("full wallet tx: %s " % json.dumps(wallet_tx.ToJson(), indent=4)) # toarray = Helper.ToArray(wallet_tx) # print("to arary %s " % toarray) relayed = NodeLeader.Instance().Relay(wallet_tx) if relayed: print("Relayed Tx: %s " % wallet_tx.Hash.ToString()) # if it was relayed, we save tx wallet.SaveTransaction(wallet_tx) return wallet_tx else: print("Could not relay tx %s " % wallet_tx.Hash.ToString()) else: print("Incomplete signature") else: print("Insufficient funds") return False
def InvokeWithdrawTx(wallet, tx, contract_hash): # print("withdraw tx 1 %s " % json.dumps(tx.ToJson(), indent=4)) requestor_contract = wallet.GetDefaultContract() tx.Attributes = [ TransactionAttribute(usage=TransactionAttributeUsage.Script, data=Crypto.ToScriptHash( requestor_contract.Script).Data) ] withdraw_contract_state = Blockchain.Default().GetContract( contract_hash.encode('utf-8')) withdraw_verification = None if withdraw_contract_state is not None: reedeem_script = withdraw_contract_state.Code.Script.hex() # there has to be at least 1 param, and the first # one needs to be a signature param param_list = bytearray(b'\x00') # if there's more than one param # we set the first parameter to be the signature param if len(withdraw_contract_state.Code.ParameterList) > 1: param_list = bytearray(withdraw_contract_state.Code.ParameterList) param_list[0] = 0 verification_contract = Contract.Create( reedeem_script, param_list, requestor_contract.PublicKeyHash) address = verification_contract.Address withdraw_verification = verification_contract context = ContractParametersContext(tx) wallet.Sign(context) context.Add(withdraw_verification, 0, 0) if context.Completed: tx.scripts = context.GetScripts() print("withdraw tx %s " % json.dumps(tx.ToJson(), indent=4)) wallet.SaveTransaction(tx) relayed = NodeLeader.Instance().Relay(tx) if relayed: print("Relayed Withdrawal Tx: %s " % tx.Hash.ToString()) return True else: print("Could not relay witdrawal tx %s " % tx.Hash.ToString()) else: print("Incomplete signature")
def InvokeWithTokenVerificationScript(wallet, tx, token, fee=Fixed8.Zero(), invoke_attrs=None): try: wallet_tx = wallet.MakeTransaction(tx=tx, fee=fee, use_standard=True) except ValueError: print("Insufficient funds") return False if wallet_tx: token_contract_state = Blockchain.Default().GetContract(token.ScriptHash.ToString()) print("token contract %s " % token_contract_state) tx.Attributes = [ TransactionAttribute(usage=TransactionAttributeUsage.Script, data=token.ScriptHash.Data) ] if invoke_attrs: tx.Attributes += invoke_attrs reedeem_script = token_contract_state.Code.Script.hex() # there has to be at least 1 param, and the first # one needs to be a signature param param_list = bytearray(b'\x00\x00') verification_contract = Contract.Create(reedeem_script, param_list, wallet.GetDefaultContract().PublicKeyHash) context = ContractParametersContext(wallet_tx) wallet.Sign(context) context.Add(verification_contract, 0, 0) if context.Completed: wallet_tx.scripts = context.GetScripts() nodemgr = NodeManager() relayed = nodemgr.relay(wallet_tx) if relayed: print("Relayed Tx: %s " % wallet_tx.Hash.ToString()) # if it was relayed, we save tx wallet.SaveTransaction(wallet_tx) return wallet_tx else: print("Could not relay tx %s " % wallet_tx.Hash.ToString()) else: print("Incomplete signature") else: print("Insufficient funds") return False
def PerformWithdrawTx(wallet, tx, contract_hash): requestor_contract = wallet.GetDefaultContract() withdraw_contract_state = Blockchain.Default().GetContract( contract_hash.encode('utf-8')) reedeem_script = withdraw_contract_state.Code.Script.hex() # there has to be at least 1 param, and the first # one needs to be a signature param param_list = bytearray(b'\x00') # if there's more than one param # we set the first parameter to be the signature param if len(withdraw_contract_state.Code.ParameterList) > 1: param_list = bytearray(withdraw_contract_state.Code.ParameterList) param_list[0] = 0 verification_contract = Contract.Create(reedeem_script, param_list, requestor_contract.PublicKeyHash) context = ContractParametersContext(tx) context.Add(verification_contract, 0, bytearray(0)) if context.Completed: tx.scripts = context.GetScripts() print("withdraw tx %s " % json.dumps(tx.ToJson(), indent=4)) relayed = NodeLeader.Instance().Relay(tx) if relayed: # wallet.SaveTransaction(tx) # dont save this tx print("Relayed Withdrawal Tx: %s " % tx.Hash.ToString()) return tx else: print("Could not relay witdrawal tx %s " % tx.Hash.ToString()) else: print("Incomplete signature") return False