private void ProcessInvokeWithWallet(JObject result, Cosigners cosigners = null) { Transaction tx = null; if (wallet != null && cosigners != null) { UInt160[] accounts = wallet.GetAccounts().Where(p => !p.Lock && !p.WatchOnly).Select(p => p.ScriptHash).ToArray(); Cosigner[] witnessCosigners = cosigners.GetCosigners().Where(p => accounts.Contains(p.Account)).ToArray(); if (witnessCosigners.Count() > 0) { tx = wallet.MakeTransaction(result["script"].AsString().HexToBytes(), null, witnessCosigners); ContractParametersContext context = new ContractParametersContext(tx); wallet.Sign(context); if (context.Completed) { tx.Witnesses = context.GetWitnesses(); } else { tx = null; } } } result["tx"] = tx?.ToArray().ToHexString(); }
public UInt160[] GetScriptHashesForVerifying(Snapshot snapshot) { var hashes = new HashSet <UInt160> { Sender }; hashes.UnionWith(Cosigners.Select(p => p.Account)); return(hashes.OrderBy(p => p).ToArray()); }
private JObject InvokeScript(JArray _params) { byte[] script = _params[0].AsString().HexToBytes(); Cosigners cosigners = _params.Count >= 2 ? new Cosigners(((JArray)_params[1]).Select(u => new Cosigner() { Account = UInt160.Parse(u["account"].AsString()), Scopes = (WitnessScope)Enum.Parse(typeof(WitnessScope), u["scopes"].AsString()) }).ToArray()) : null; return(GetInvokeResult(script, cosigners)); }
public Transaction Unwrap() { return(new Transaction { Version = Version, Nonce = Nonce, Sender = Sender, SystemFee = SystemFee, NetworkFee = NetworkFee, ValidUntilBlock = ValidUntilBlock, Attributes = Attributes.Select(p => p.Unwrap()).ToArray(), Cosigners = Cosigners.Select(p => p.Unwrap()).ToArray(), Script = Script, Witnesses = Witnesses.Select(p => p.Unwrap()).ToArray() }); }
public JObject ToJson() { JObject json = new JObject(); json["hash"] = Hash.ToString(); json["size"] = Size; json["version"] = Version; json["nonce"] = Nonce; json["sender"] = Sender.ToAddress(); json["sys_fee"] = SystemFee.ToString(); json["net_fee"] = NetworkFee.ToString(); json["valid_until_block"] = ValidUntilBlock; json["attributes"] = Attributes.Select(p => p.ToJson()).ToArray(); json["cosigners"] = Cosigners.Select(p => p.ToJson()).ToArray(); json["script"] = Convert.ToBase64String(Script); json["witnesses"] = Witnesses.Select(p => p.ToJson()).ToArray(); return(json); }
public JObject ToJson() { JObject json = new JObject(); json["hash"] = Hash.ToString(); json["size"] = Size; json["version"] = Version; json["nonce"] = Nonce; json["sender"] = Sender.ToAddress(); json["sys_fee"] = new BigDecimal(SystemFee, NativeContract.GAS.Decimals).ToString(); json["net_fee"] = new BigDecimal(NetworkFee, NativeContract.GAS.Decimals).ToString(); json["valid_until_block"] = ValidUntilBlock; json["attributes"] = Attributes.Select(p => p.ToJson()).ToArray(); json["cosigners"] = Cosigners.Select(p => p.ToJson()).ToArray(); json["script"] = Script.ToHexString(); json["witnesses"] = Witnesses.Select(p => p.ToJson()).ToArray(); return(json); }
private JObject InvokeFunction(JArray _params) { UInt160 script_hash = UInt160.Parse(_params[0].AsString()); string operation = _params[1].AsString(); ContractParameter[] args = _params.Count >= 3 ? ((JArray)_params[2]).Select(p => ContractParameter.FromJson(p)).ToArray() : new ContractParameter[0]; Cosigners cosigners = _params.Count >= 4 ? new Cosigners(((JArray)_params[3]).Select(u => new Cosigner() { Account = UInt160.Parse(u["account"].AsString()), Scopes = (WitnessScope)Enum.Parse(typeof(WitnessScope), u["scopes"].AsString()) }).ToArray()) : null; byte[] script; using (ScriptBuilder sb = new ScriptBuilder()) { script = sb.EmitAppCall(script_hash, operation, args).ToArray(); } return(GetInvokeResult(script, cosigners)); }
private JObject GetInvokeResult(byte[] script, Cosigners cosigners = null) { using ApplicationEngine engine = ApplicationEngine.Run(script, cosigners, gas: settings.MaxGasInvoke); JObject json = new JObject(); json["script"] = script.ToHexString(); json["state"] = engine.State; json["gasconsumed"] = engine.GasConsumed.ToString(); try { json["stack"] = new JArray(engine.ResultStack.Select(p => p.ToJson())); } catch (InvalidOperationException) { json["stack"] = "error: recursive reference"; } ProcessInvokeWithWallet(json, cosigners); return(json); }
public void DeserializeUnsigned(BinaryReader reader) { Version = reader.ReadByte(); if (Version > 0) { throw new FormatException(); } Nonce = reader.ReadUInt32(); Sender = reader.ReadSerializable <UInt160>(); SystemFee = reader.ReadInt64(); if (SystemFee < 0) { throw new FormatException(); } if (SystemFee % NativeContract.GAS.Factor != 0) { throw new FormatException(); } NetworkFee = reader.ReadInt64(); if (NetworkFee < 0) { throw new FormatException(); } if (SystemFee + NetworkFee < SystemFee) { throw new FormatException(); } ValidUntilBlock = reader.ReadUInt32(); Attributes = reader.ReadSerializableArray <TransactionAttribute>(MaxTransactionAttributes); Cosigners = reader.ReadSerializableArray <Cosigner>(MaxCosigners); if (Cosigners.Select(u => u.Account).Distinct().Count() != Cosigners.Length) { throw new FormatException(); } Script = reader.ReadVarBytes(ushort.MaxValue); if (Script.Length == 0) { throw new FormatException(); } }
/// <summary> /// Checks if an account is cosignatory of the multisig account. /// </summary> /// <param name="account">The account.</param> /// <returns><c>true</c> if the specified account has cosigners; otherwise, <c>false</c>.</returns> public bool HasCosigner(AccountInfo account) => Cosigners.Contains(account);