Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count == 0)
            {
                return;
            }
            ContractParameter parameter = (ContractParameter)listView1.SelectedItems[0].Tag;

            parameter.SetValue(textBox2.Text);
            listView1.SelectedItems[0].SubItems["value"].Text = parameter.ToString();
            textBox1.Text = listView1.SelectedItems[0].SubItems["value"].Text;
            textBox2.Clear();
        }
Example #2
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (listView1.SelectedIndices.Count == 0) return;
     ContractParameter parameter = (ContractParameter)listView1.SelectedItems[0].Tag;
     try
     {
         parameter.SetValue(textBox2.Text);
         listView1.SelectedItems[0].SubItems["value"].Text = parameter.ToString();
         textBox1.Text = listView1.SelectedItems[0].SubItems["value"].Text;
         textBox2.Clear();
     }
     catch(Exception err)
     {
         MessageBox.Show(err.Message);
     }
 }
        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
            {
                return;
            }
            if (listBox2.SelectedIndex < 0)
            {
                return;
            }
            ContractParameter parameter = (ContractParameter)listBox2.SelectedItem;

            parameter.SetValue(textBox2.Text);
            listBox2.Items[listBox2.SelectedIndex] = parameter;
            textBox1.Text   = textBox2.Text;
            button4.Visible = context.Completed;
        }
Example #4
0
        public async Task <object> InvokeContract(InvokeContractParameterModel para)
        {
            if (CurrentWallet == null)
            {
                return(Error(ErrorCode.WalletNotOpen));
            }
            if (para.ContractHash == null || para.Method.IsNull())
            {
                return(Error(ErrorCode.ParameterIsNull));
            }
            using var snapshot = Blockchain.Singleton.GetSnapshot();
            var contract = snapshot.Contracts.TryGet(para.ContractHash);

            if (contract == null)
            {
                return(Error(ErrorCode.UnknownContract));
            }
            ContractParameter[] contractParameters = para.Parameters?.Select(p =>
            {
                var parameterValue = new ContractParameter(p.Type);
                parameterValue.SetValue(p.Value);
                return(parameterValue);
            }).ToArray();

            var signers = new List <Cosigner>();

            if (para.Cosigners.NotEmpty())
            {
                signers.AddRange(para.Cosigners.Select(s => new Cosigner()
                {
                    Account = s.Account, Scopes = s.Scopes
                }));
            }

            Transaction tx = null;

            using ScriptBuilder sb = new ScriptBuilder();
            sb.EmitAppCall(para.ContractHash, para.Method, contractParameters);

            try
            {
                tx = CurrentWallet.InitTransaction(sb.ToArray(), signers.ToArray());
            }
            catch (InvalidOperationException)
            {
                return(Error(ErrorCode.EngineFault));
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Insufficient GAS"))
                {
                    return(Error(ErrorCode.GasNotEnough));
                }
                throw;
            }

            var(signSuccess, context) = CurrentWallet.TrySignTx(tx);
            if (!signSuccess)
            {
                return(Error(ErrorCode.SignFail, context.SafeSerialize()));
            }
            var result = new InvokeResultModel();

            using ApplicationEngine engine = ApplicationEngine.Run(tx.Script, tx, testMode: true);
            result.VmState     = engine.State;
            result.GasConsumed = new BigDecimal(tx.SystemFee, NativeContract.GAS.Decimals);
            result.ResultStack = engine.ResultStack.Select(p => JStackItem.FromJson(p.ToParameter().ToJson())).ToList();
            if (engine.State.HasFlag(VMState.FAULT))
            {
                return(Error(ErrorCode.EngineFault));
            }
            if (!para.SendTx)
            {
                return(result);
            }
            await tx.Broadcast();

            result.TxId = tx.Hash;
            return(result);
        }
Example #5
0
        public ContractParameter GetParameter()
        {
            UIElementCollection valueItems = stackValues.Children;

            switch ((string)((ComboBoxItem)cmbParamTypes.SelectedItem).Content)
            {
            case "Signature":
            {
                string value = ((TextBox)valueItems[0]).Text;

                try
                {
                    parameter.SetValue(value);
                }
                catch (FormatException fex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_FAIL_TYPE_SIGNATURE", iLang));
                }
                catch (ArgumentException aex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_UNKOWN_TYPE_SIGNATURE", iLang));
                }

                break;
            }

            case "Boolean":
            {
                string value = ((TextBox)valueItems[0]).Text;

                try
                {
                    parameter.SetValue(value);
                }
                catch (FormatException fex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_FAIL_TYPE_SIGNATURE", iLang));
                }
                catch (ArgumentException aex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_UNKOWN_TYPE_SIGNATURE", iLang));
                }
                break;
            }

            case "Integer":
            {
                string value = ((TextBox)valueItems[0]).Text;

                try
                {
                    parameter.SetValue(value);
                }
                catch (FormatException fex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_FAIL_TYPE_SIGNATURE", iLang));
                }
                catch (ArgumentException aex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_UNKOWN_TYPE_SIGNATURE", iLang));
                }
                break;
            }

            case "Hash160":
            {
                string value = ((TextBox)valueItems[0]).Text;

                try
                {
                    parameter.SetValue(value);
                }
                catch (FormatException fex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_FAIL_TYPE_SIGNATURE", iLang));
                }
                catch (ArgumentException aex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_UNKOWN_TYPE_SIGNATURE", iLang));
                }
                break;
            }

            case "Hash256":
            {
                string value = ((TextBox)valueItems[0]).Text;

                try
                {
                    parameter.SetValue(value);
                }
                catch (FormatException fex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_FAIL_TYPE_SIGNATURE", iLang));
                }
                catch (ArgumentException aex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_UNKOWN_TYPE_SIGNATURE", iLang));
                }
                break;
            }

            case "ByteArray":
            {
                string value = ((TextBox)valueItems[0]).Text;

                try
                {
                    parameter.SetValue(value);
                }
                catch (FormatException fex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_FAIL_TYPE_SIGNATURE", iLang));
                }
                catch (ArgumentException aex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_UNKOWN_TYPE_SIGNATURE", iLang));
                }
                break;
            }

            case "PublicKey":
            {
                string value = ((TextBox)valueItems[0]).Text;

                try
                {
                    parameter.SetValue(value);
                }
                catch (FormatException fex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_FAIL_TYPE_SIGNATURE", iLang));
                }
                catch (ArgumentException aex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_UNKOWN_TYPE_SIGNATURE", iLang));
                }
                break;
            }

            case "String":
            {
                string value = ((TextBox)valueItems[0]).Text;

                try
                {
                    parameter.SetValue(value);
                }
                catch (FormatException fex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_FAIL_TYPE_SIGNATURE", iLang));
                }
                catch (ArgumentException aex)
                {
                    StaticUtils.ShowMessageBox(StaticUtils.ErrorBrush, StringTable.GetInstance().GetString("STR_ERR_UNKOWN_TYPE_SIGNATURE", iLang));
                }
                break;
            }

            case "Array":
            {
                ((List <ContractParameter>)parameter.Value).Clear();
                foreach (StackPanel panItem in valueItems)
                {
                    string value = ((TextBox)panItem.Children[0]).Text;

                    ContractParameter param = new ContractParameter();

                    if (string.Equals(value, "true", StringComparison.OrdinalIgnoreCase))
                    {
                        param.Type  = ContractParameterType.Boolean;
                        param.Value = true;
                    }
                    else if (string.Equals(value, "false", StringComparison.OrdinalIgnoreCase))
                    {
                        param.Type  = ContractParameterType.Boolean;
                        param.Value = false;
                    }
                    else if (long.TryParse(value, out long num))
                    {
                        param.Type  = ContractParameterType.Integer;
                        param.Value = num;
                    }
                    else if (value.StartsWith("0x"))
                    {
                        if (UInt160.TryParse(value, out UInt160 i160))
                        {
                            param.Type  = ContractParameterType.Hash160;
                            param.Value = i160;
                        }
                        else if (UInt256.TryParse(value, out UInt256 i256))
                        {
                            param.Type  = ContractParameterType.Hash256;
                            param.Value = i256;
                        }
                        else if (BigInteger.TryParse(value.Substring(2), NumberStyles.AllowHexSpecifier, null, out BigInteger bi))
                        {
                            param.Type  = ContractParameterType.Integer;
                            param.Value = bi;
                        }
                        else
                        {
                            param.Type  = ContractParameterType.String;
                            param.Value = value;
                        }
                    }
                    else if (ECPoint.TryParse(value, ECCurve.Secp256r1, out ECPoint point))
                    {
                        param.Type  = ContractParameterType.PublicKey;
                        param.Value = point;
                    }
                    else
                    {
                        try
                        {
                            param.Value = value.HexToBytes();
                            param.Type  = ContractParameterType.ByteArray;
                        }
                        catch (FormatException)
                        {
                            param.Type  = ContractParameterType.String;
                            param.Value = value;
                        }
                    }

                    ((List <ContractParameter>)parameter.Value).Add(param);
                }
                break;
            }
            }

            return(parameter);
        }
Example #6
0
        public void TestSetValue()
        {
            ContractParameter contractParameter1 = new ContractParameter(ContractParameterType.Signature);

            byte[] expectedArray1 = new byte[64];
            contractParameter1.SetValue(new byte[64].ToHexString());
            Assert.AreEqual(Encoding.Default.GetString(expectedArray1), Encoding.Default.GetString((byte[])contractParameter1.Value));
            Action action1 = () => contractParameter1.SetValue(new byte[50].ToHexString());

            action1.Should().Throw <FormatException>();

            ContractParameter contractParameter2 = new ContractParameter(ContractParameterType.Boolean);

            contractParameter2.SetValue("true");
            Assert.AreEqual(true, contractParameter2.Value);

            ContractParameter contractParameter3 = new ContractParameter(ContractParameterType.Integer);

            contractParameter3.SetValue("11");
            Assert.AreEqual(new BigInteger(11), contractParameter3.Value);

            ContractParameter contractParameter4 = new ContractParameter(ContractParameterType.Hash160);

            contractParameter4.SetValue("0x0000000000000000000000000000000000000001");
            Assert.AreEqual(UInt160.Parse("0x0000000000000000000000000000000000000001"), contractParameter4.Value);

            ContractParameter contractParameter5 = new ContractParameter(ContractParameterType.Hash256);

            contractParameter5.SetValue("0x0000000000000000000000000000000000000000000000000000000000000000");
            Assert.AreEqual(UInt256.Parse("0x0000000000000000000000000000000000000000000000000000000000000000"), contractParameter5.Value);

            ContractParameter contractParameter6 = new ContractParameter(ContractParameterType.ByteArray);

            contractParameter6.SetValue("2222");
            byte[] expectedArray6 = new byte[2];
            expectedArray6[0] = 0x22;
            expectedArray6[1] = 0x22;
            Assert.AreEqual(Encoding.Default.GetString(expectedArray6), Encoding.Default.GetString((byte[])contractParameter6.Value));

            ContractParameter contractParameter7 = new ContractParameter(ContractParameterType.PublicKey);
            Random            random7            = new Random();

            byte[] privateKey7 = new byte[32];
            for (int j = 0; j < privateKey7.Length; j++)
            {
                privateKey7[j] = (byte)random7.Next(256);
            }
            ECPoint publicKey7 = ECCurve.Secp256r1.G * privateKey7;

            contractParameter7.SetValue(publicKey7.ToString());
            Assert.AreEqual(true, publicKey7.Equals(contractParameter7.Value));

            ContractParameter contractParameter8 = new ContractParameter(ContractParameterType.String);

            contractParameter8.SetValue("AAA");
            Assert.AreEqual("AAA", contractParameter8.Value);

            ContractParameter contractParameter9 = new ContractParameter(ContractParameterType.Array);
            Action            action9            = () => contractParameter9.SetValue("AAA");

            action9.Should().Throw <ArgumentException>();
        }