public static ConstructionMetadataRequest FromJson(JObject json)
 {
     return(new ConstructionMetadataRequest(NetworkIdentifier.FromJson(json["network_identifier"]),
                                            json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null,
                                            json.ContainsProperty("options") ? Metadata.FromJson(json["options"]) : null,
                                            json.ContainsProperty("public_keys") ? (json["public_keys"] as JArray).Select(p => PublicKey.FromJson(p)).ToArray() : null));
 }
Example #2
0
        private JObject ProcessRequest(JObject request)
        {
            if (!request.ContainsProperty("id"))
            {
                return(null);
            }
            if (!request.ContainsProperty("method") || !request.ContainsProperty("params") || !(request["params"] is JArray))
            {
                return(CreateErrorResponse(request["id"], -32600, "Invalid Request"));
            }
            JObject result = null;

            try
            {
                result = Process(request["method"].AsString(), (JArray)request["params"]);
            }
            catch (Exception ex)
            {
#if DEBUG
                return(CreateErrorResponse(request["id"], ex.HResult, ex.Message, ex.StackTrace));
#else
                return(CreateErrorResponse(request["id"], ex.HResult, ex.Message));
#endif
            }
            JObject response = CreateResponse(request["id"]);
            response["result"] = result;
            return(response);
        }
Example #3
0
 public static AccountBalanceRequest FromJson(JObject json)
 {
     return(new AccountBalanceRequest(NetworkIdentifier.FromJson(json["network_identifier"]),
                                      AccountIdentifier.FromJson(json["account_identifier"]),
                                      json.ContainsProperty("block_identifier") ? PartialBlockIdentifier.FromJson(json["block_identifier"]) : null,
                                      json.ContainsProperty("currencies") ? (json["currencies"] as JArray).Select(p => Currency.FromJson(p)).ToArray() : null));
 }
Example #4
0
 public static ConstructionPreprocessRequest FromJson(JObject json)
 {
     return(new ConstructionPreprocessRequest(NetworkIdentifier.FromJson(json["network_identifier"]),
                                              (json["operations"] as JArray).Select(p => Operation.FromJson(p)).ToArray(),
                                              json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null,
                                              json.ContainsProperty("max_fee") ? (json["max_fee"] as JArray).ToList().Select(p => Amount.FromJson(p)).ToArray() : null,
                                              json.ContainsProperty("suggested_fee_multiplier") ? json["suggested_fee_multiplier"].AsNumber() : null));
 }
Example #5
0
        private JObject ProcessRequest(HttpContext context, JObject request)
        {
            if (!request.ContainsProperty("id"))
            {
                return(null);
            }
            if (!request.ContainsProperty("method") || !request.ContainsProperty("params") || !(request["params"] is JArray))
            {
                return(CreateErrorResponse(request["id"], -32600, "Invalid Request"));
            }
            JObject result = null;

            try
            {
                string method  = request["method"].AsString();
                JArray _params = (JArray)request["params"];
                foreach (IRpcPlugin plugin in Plugin.RpcPlugins)
                {
                    plugin.PreProcess(context, method, _params);
                }
                foreach (IRpcPlugin plugin in Plugin.RpcPlugins)
                {
                    result = plugin.OnProcess(context, method, _params);
                    if (result != null)
                    {
                        break;
                    }
                }
                if (result == null)
                {
                    result = Process(method, _params);
                }
                foreach (IRpcPlugin plugin in Plugin.RpcPlugins)
                {
                    plugin.PostProcess(context, method, _params, result);
                }
            }
            catch (FormatException)
            {
                return(CreateErrorResponse(request["id"], -32602, "Invalid params"));
            }
            catch (IndexOutOfRangeException)
            {
                return(CreateErrorResponse(request["id"], -32602, "Invalid params"));
            }
            catch (Exception ex)
            {
#if DEBUG
                return(CreateErrorResponse(request["id"], ex.HResult, ex.Message, ex.StackTrace));
#else
                return(CreateErrorResponse(request["id"], ex.HResult, ex.Message));
#endif
            }
            JObject response = CreateResponse(request["id"]);
            response["result"] = result;
            return(response);
        }
 public static PartialBlockIdentifier FromJson(JObject json)
 {
     if (json is null)
     {
         return(null);
     }
     return(new PartialBlockIdentifier(json.ContainsProperty("index") ? (long?)json["index"].AsNumber() : null,
                                       json.ContainsProperty("hash") ? json["hash"].AsString() : null));
 }
Example #7
0
 public static AccountIdentifier FromJson(JObject json)
 {
     if (json is null)
     {
         return(null);
     }
     return(new AccountIdentifier(json["address"].AsString(),
                                  json.ContainsProperty("sub_account") ? SubAccountIdentifier.FromJson(json["sub_account"]) : null,
                                  json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null));
 }
Example #8
0
        public void ToJson()
        {
            SetupWitnessWithValues(uut, 2, 3, out _, out _);

            JObject json = uut.ToJson();

            Assert.IsTrue(json.ContainsProperty("invocation"));
            Assert.IsTrue(json.ContainsProperty("verification"));
            Assert.AreEqual(json["invocation"].AsString(), "ICA=");
            Assert.AreEqual(json["verification"].AsString(), "ICAg");
        }
Example #9
0
 public static Operation FromJson(JObject json)
 {
     return(new Operation(OperationIdentifier.FromJson(json["operation_identifier"]),
                          json["type"].AsString(),
                          json["status"].AsString(),
                          json.ContainsProperty("related_operations") ? (json["related_operations"] as JArray).Select(p => OperationIdentifier.FromJson(p)).ToArray() : null,
                          json.ContainsProperty("account") ? AccountIdentifier.FromJson(json["account"]) : null,
                          json.ContainsProperty("amount") ? Amount.FromJson(json["amount"]) : null,
                          json.ContainsProperty("coin_change") ? CoinChange.FromJson(json["coin_change"]) : null,
                          json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null));
 }
Example #10
0
        public void ToJson()
        {
            byte[] invocationScript;
            byte[] verificationScript;
            setupWitnessWithValues(uut, 2, 3, out invocationScript, out verificationScript);

            JObject json = uut.ToJson();

            Assert.IsTrue(json.ContainsProperty("invocation"));
            Assert.IsTrue(json.ContainsProperty("verification"));
            Assert.AreEqual(json["invocation"].AsString(), "2020");
            Assert.AreEqual(json["verification"].AsString(), "202020");
        }
Example #11
0
        private void button3_Click(object sender, EventArgs e)
        {
            SignatureContext context;
            JObject          json1 = JObject.Parse(textBox2.Text);

            if (json1.ContainsProperty("hex"))
            {
                context = SignatureContext.FromJson(json1);
            }
            else
            {
                ContractTransaction tx1 = JsonToRequest(json1);
                ContractTransaction tx2 = JsonToRequest(JObject.Parse(textBox3.Text));
                context = new SignatureContext(new ContractTransaction
                {
                    Attributes = new TransactionAttribute[0],
                    Inputs     = tx1.Inputs.Concat(tx2.Inputs).ToArray(),
                    Outputs    = tx1.Outputs.Concat(tx2.Outputs).ToArray()
                });
            }
            Program.CurrentWallet.Sign(context);
            if (context.Completed)
            {
                context.Signable.Scripts = context.GetScripts();
                ContractTransaction tx = (ContractTransaction)context.Signable;
                Program.CurrentWallet.SendTransaction(tx);
                Program.LocalNode.Relay(tx);
                InformationBox.Show(tx.Hash.ToString(), "交易已发送,这是交易编号(TXID):", "交易成功");
            }
            else
            {
                InformationBox.Show(context.ToString(), "交易构造完成,请将以下信息发送给对方进行签名:", "签名不完整");
            }
        }
Example #12
0
        private void button3_Click(object sender, EventArgs e)
        {
            SignatureContext context;
            JObject          json1 = JObject.Parse(textBox2.Text);

            if (json1.ContainsProperty("hex"))
            {
                context = SignatureContext.FromJson(json1);
            }
            else
            {
                ContractTransaction tx1 = JsonToRequest(json1);
                ContractTransaction tx2 = JsonToRequest(JObject.Parse(textBox3.Text));
                context = new SignatureContext(new ContractTransaction
                {
                    Attributes = new TransactionAttribute[0],
                    Inputs     = tx1.Inputs.Concat(tx2.Inputs).ToArray(),
                    Outputs    = tx1.Outputs.Concat(tx2.Outputs).ToArray()
                });
            }
            Program.CurrentWallet.Sign(context);
            if (context.Completed)
            {
                context.Verifiable.Scripts = context.GetScripts();
                ContractTransaction tx = (ContractTransaction)context.Verifiable;
                Program.CurrentWallet.SaveTransaction(tx);
                Program.LocalNode.Relay(tx);
                InformationBox.Show(tx.Hash.ToString(), Strings.TradeSuccessMessage, Strings.TradeSuccessCaption);
            }
            else
            {
                InformationBox.Show(context.ToString(), Strings.TradeNeedSignatureMessage, Strings.TradeNeedSignatureCaption);
            }
        }
Example #13
0
        private JObject ProcessRequest(HttpContext context, JObject request)
        {
            TR.Enter();
            if (!request.ContainsProperty("id"))
            {
                TR.Exit();
                return(null);
            }
            if (!request.ContainsProperty("method") || !request.ContainsProperty("params") || !(request["params"] is JArray))
            {
                return(TR.Exit(CreateErrorResponse(request["id"], -32600, "Invalid Request")));
            }
            JObject result = null;

            try
            {
                string method  = request["method"].AsString();
                JArray _params = (JArray)request["params"];
                foreach (RpcPlugin plugin in RpcPlugin.Instances)
                {
                    result = plugin.OnProcess(context, method, _params);
                    if (result != null)
                    {
                        break;
                    }
                }
                if (result == null)
                {
                    result = Process(method, _params);
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                return(TR.Exit(CreateErrorResponse(request["id"], ex.HResult, ex.Message, ex.StackTrace)));
#else
                return(CreateErrorResponse(request["id"], ex.HResult, ex.Message));
#endif
            }
            JObject response = CreateResponse(request["id"]);
            response["result"] = result;
            return(TR.Exit(response));
        }
 public static NetworkIdentifier FromJson(JObject json)
 {
     if (json is null)
     {
         return(null);
     }
     return(new NetworkIdentifier(json["blockchain"]?.AsString(),
                                  json["network"]?.AsString(),
                                  json.ContainsProperty("sub_network_identifier") ? SubNetworkIdentifier.FromJson(json["sub_network_identifier"]) : null));
 }
Example #15
0
        /// <summary>
        /// Initiate the ACL
        /// </summary>
        /// <param name="cfg">Config</param>
        public void Load(NetworkACLConfig cfg)
        {
            if (cfg == null)
            {
                return;
            }

            Type = cfg.Type;

            if (!string.IsNullOrEmpty(cfg.Path) && File.Exists(cfg.Path))
            {
                string  json = File.ReadAllText(cfg.Path);
                JObject jo   = JObject.Parse(json);

                if (!(jo is JArray array))
                {
                    return;
                }

                Entries = new Entry[array.Count];

                for (int x = 0, m = array.Count; x < m; x++)
                {
                    JObject j = array[x];
                    if (!j.ContainsProperty("value"))
                    {
                        continue;
                    }

                    string value = j.Properties["value"].AsString();

                    if (j.ContainsProperty("regex") && j.Properties["regex"].AsBooleanOrDefault(false))
                    {
                        Entries[x] = new RegexEntry(value);
                    }
                    else
                    {
                        Entries[x] = new Entry(value);
                    }
                }
            }
        }
Example #16
0
        private void button2_Click(object sender, EventArgs e)
        {
            IEnumerable <CoinReference>     inputs;
            IEnumerable <TransactionOutput> outputs;
            JObject json = JObject.Parse(textBox2.Text);

            if (json.ContainsProperty("hex"))
            {
                ContractTransaction tx_mine   = JsonToRequest(JObject.Parse(textBox3.Text));
                ContractTransaction tx_others = (ContractTransaction)ContractParametersContext.FromJson(json).Verifiable;
                inputs = tx_others.Inputs.Except(tx_mine.Inputs);
                List <TransactionOutput> outputs_others = new List <TransactionOutput>(tx_others.Outputs);
                foreach (TransactionOutput output_mine in tx_mine.Outputs)
                {
                    TransactionOutput output_others = outputs_others.FirstOrDefault(p => p.AssetId == output_mine.AssetId && p.Value == output_mine.Value && p.ScriptHash == output_mine.ScriptHash);
                    if (output_others == null)
                    {
                        MessageBox.Show(LanHelper.LocalLanguage("Validation failed, the counterparty falsified the transaction content!"), LanHelper.LocalLanguage("Failed"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    outputs_others.Remove(output_others);
                }
                outputs = outputs_others;
            }
            else
            {
                ContractTransaction tx_others = JsonToRequest(json);
                inputs  = tx_others.Inputs;
                outputs = tx_others.Outputs;
            }
            try
            {
                if (inputs.Select(p => Blockchain.Singleton.GetTransaction(p.PrevHash).Outputs[p.PrevIndex].ScriptHash).Distinct().Any(p => Program.CurrentWallet.Contains(p)))
                {
                    MessageBox.Show(LanHelper.LocalLanguage("Validation failed, the counterparty generated illegal transaction content!"), LanHelper.LocalLanguage("Failed"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch
            {
                MessageBox.Show(LanHelper.LocalLanguage("Validation failed, invalid transaction or unsynchronized blockchain, please try again when synchronized!"), LanHelper.LocalLanguage("Failed"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            outputs = outputs.Where(p => Program.CurrentWallet.Contains(p.ScriptHash));
            using (TradeVerificationDialog dialog = new TradeVerificationDialog(outputs))
            {
                button3.Enabled = dialog.ShowDialog() == DialogResult.OK;
            }
        }
Example #17
0
        private void button2_Click(object sender, EventArgs e)
        {
            IEnumerable <CoinReference>     inputs;
            IEnumerable <TransactionOutput> outputs;
            JObject json = JObject.Parse(textBox2.Text);

            if (json.ContainsProperty("hex"))
            {
                ContractTransaction tx_mine   = JsonToRequest(JObject.Parse(textBox3.Text));
                ContractTransaction tx_others = (ContractTransaction)SignatureContext.FromJson(json).Verifiable;
                inputs = tx_others.Inputs.Except(tx_mine.Inputs);
                List <TransactionOutput> outputs_others = new List <TransactionOutput>(tx_others.Outputs);
                foreach (TransactionOutput output_mine in tx_mine.Outputs)
                {
                    TransactionOutput output_others = outputs_others.FirstOrDefault(p => p.AssetId == output_mine.AssetId && p.Value == output_mine.Value && p.ScriptHash == output_mine.ScriptHash);
                    if (output_others == null)
                    {
                        MessageBox.Show(Strings.TradeFailedFakeDataMessage, Strings.Failed, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    outputs_others.Remove(output_others);
                }
                outputs = outputs_others;
            }
            else
            {
                ContractTransaction tx_others = JsonToRequest(json);
                inputs  = tx_others.Inputs;
                outputs = tx_others.Outputs;
            }
            try
            {
                if (inputs.Select(p => Blockchain.Default.GetTransaction(p.PrevHash).Outputs[p.PrevIndex].ScriptHash).Distinct().Any(p => Program.CurrentWallet.ContainsAddress(p)))
                {
                    MessageBox.Show(Strings.TradeFailedInvalidDataMessage, Strings.Failed, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch
            {
                MessageBox.Show(Strings.TradeFailedNoSyncMessage, Strings.Failed, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            outputs = outputs.Where(p => Program.CurrentWallet.ContainsAddress(p.ScriptHash));
            using (TradeVerificationDialog dialog = new TradeVerificationDialog(outputs))
            {
                button3.Enabled = dialog.ShowDialog() == DialogResult.OK;
            }
        }
Example #18
0
        public static RpcInvokeResult FromJson(JObject json)
        {
            RpcInvokeResult invokeScriptResult = new RpcInvokeResult
            {
                Script      = json["script"].AsString(),
                State       = json["state"].TryGetEnum <VM.VMState>(),
                GasConsumed = json["gasconsumed"].AsString()
            };

            if (json.ContainsProperty("exception"))
            {
                invokeScriptResult.Exception = json["exception"]?.AsString();
            }
            try
            {
                invokeScriptResult.Stack = ((JArray)json["stack"]).Select(p => Utility.StackItemFromJson(p)).ToArray();
            }
            catch { }
            invokeScriptResult.Tx = json["tx"]?.AsString();
            return(invokeScriptResult);
        }
Example #19
0
        private void button3_Click(object sender, EventArgs e)
        {
            ContractParametersContext context;
            JObject json1 = JObject.Parse(textBox2.Text);

            if (json1.ContainsProperty("hex"))
            {
                context = ContractParametersContext.FromJson(json1);
            }
            else
            {
                ContractTransaction tx1 = JsonToRequest(json1);
                ContractTransaction tx2 = JsonToRequest(JObject.Parse(textBox3.Text));
                context = new ContractParametersContext(new ContractTransaction
                {
                    Attributes = new TransactionAttribute[0],
                    Inputs     = tx1.Inputs.Concat(tx2.Inputs).ToArray(),
                    Outputs    = tx1.Outputs.Concat(tx2.Outputs).ToArray()
                });
            }
            Program.CurrentWallet.Sign(context);
            if (context.Completed)
            {
                ContractTransaction tx = (ContractTransaction)context.Verifiable;
                tx.Witnesses = context.GetWitnesses();
                Program.CurrentWallet.ApplyTransaction(tx);
                Program.OXSystem.LocalNode.Tell(new LocalNode.Relay {
                    Inventory = tx
                });
                InformationBox.Show(tx.Hash.ToString(), LanHelper.LocalLanguage("Transaction sent, this is the TXID:"), LanHelper.LocalLanguage("Trade Success"));
            }
            else
            {
                InformationBox.Show(context.ToString(), LanHelper.LocalLanguage("Transaction generated, please send the following information to the counterparty for signing:"), LanHelper.LocalLanguage("Need Signature"));
            }
        }
Example #20
0
 public static ConstructionDeriveRequest FromJson(JObject json)
 {
     return(new ConstructionDeriveRequest(NetworkIdentifier.FromJson(json["network_identifier"]),
                                          PublicKey.FromJson(json["public_key"]),
                                          json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null));
 }
Example #21
0
 public static ConstructionPayloadsRequest FromJson(JObject json)
 {
     return(new ConstructionPayloadsRequest(NetworkIdentifier.FromJson(json["network_identifier"]),
                                            (json["operations"] as JArray).Select(p => Operation.FromJson(p)).ToArray(),
                                            json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null));
 }
 public static NetworkRequest FromJson(JObject json)
 {
     return(new NetworkRequest(NetworkIdentifier.FromJson(json["network_identifier"]),
                               json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null));
 }
 public static SubNetworkIdentifier FromJson(JObject json)
 {
     return(new SubNetworkIdentifier(json["network"].AsString(),
                                     json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null));
 }
Example #24
0
 public static SigningPayload FromJson(JObject json)
 {
     return(new SigningPayload(json["hex_bytes"].AsString(),
                               json.ContainsProperty("account_identifier") ? AccountIdentifier.FromJson(json["account_identifier"]) : null,
                               json["signature_type"].ToSignatureType()));
 }
Example #25
0
        private static JObject ProcessRequest(JObject request)
        {
            if (!request.ContainsProperty("id")) return null;
            if (!request.ContainsProperty("method") || !request.ContainsProperty("params") || !(request["params"] is JArray))
            {
                return CreateErrorResponse(request["id"], -32600, "Invalid Request");
            }
            JObject result = null;
            try
            {
                result = InternalCall(request["method"].AsString(), (JArray)request["params"]);
            }
            catch (Exception ex)
            {
#if DEBUG
                return CreateErrorResponse(request["id"], ex.HResult, ex.Message, ex.StackTrace);
#else
                return CreateErrorResponse(request["id"], ex.HResult, ex.Message);
#endif
            }
            JObject response = CreateResponse(request["id"]);
            response["result"] = result;
            return response;
        }
 public static BalanceExemption FromJson(JObject json)
 {
     return(new BalanceExemption(json.ContainsProperty("sub_account_address") ? json["sub_account_address"].AsString() : null,
                                 json.ContainsProperty("currency") ? Currency.FromJson(json["currency"]) : null,
                                 json.ContainsProperty("exemption_type") ? json["exemption_type"].ToExemptionType() : ExemptionType.Unknown));
 }
 public static OperationIdentifier FromJson(JObject json)
 {
     return(new OperationIdentifier((long)json["index"].AsNumber(),
                                    json.ContainsProperty("network_index") ? (long?)json["network_index"].AsNumber() : null));
 }
Example #28
0
 public static Amount FromJson(JObject json)
 {
     return(new Amount(json["value"].AsString(),
                       Currency.FromJson(json["currency"]),
                       json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null));
 }
 public static AccountBalanceRequest FromJson(JObject json)
 {
     return(new AccountBalanceRequest(NetworkIdentifier.FromJson(json["network_identifier"]),
                                      AccountIdentifier.FromJson(json["account_identifier"]),
                                      json.ContainsProperty("block_identifier") ? PartialBlockIdentifier.FromJson(json["block_identifier"]) : null));
 }
Example #30
0
 public static SubAccountIdentifier FromJson(JObject json)
 {
     return(new SubAccountIdentifier(json["address"].AsString(),
                                     json.ContainsProperty("metadata") ? Metadata.FromJson(json["metadata"]) : null));
 }