Exemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            Block block = new Block
            {
                PrevBlock    = UInt256.Zero,
                Timestamp    = DateTime.Now.ToTimestamp(),
                Height       = 0,
                Nonce        = 2083236893, //向比特币致敬
                NextMiner    = Contract.CreateMultiSigContract(Blockchain.GetMinSignatureCount(Blockchain.StandbyMiners.Length), Blockchain.StandbyMiners).ScriptHash,
                Transactions = new Transaction[]
                {
                    new GenerationTransaction
                    {
                        Nonce   = 0,
                        Inputs  = new TransactionInput[0],
                        Outputs = new TransactionOutput[0],
                        Scripts = { }
                    },
                    textBox3.Text.HexToBytes().AsSerializable <RegisterTransaction>()
                }
            };

            block.RebuildMerkleRoot();
            SignatureContext context = new SignatureContext(block.Header);

            InformationBox.Show(context.ToString(), "创世区块头签名上下文");
        }
Exemplo n.º 2
0
 private void 资产分发IToolStripMenuItem_Click(object sender, EventArgs e)
 {
     using (IssueDialog dialog = new IssueDialog())
     {
         if (dialog.ShowDialog() != DialogResult.OK)
         {
             return;
         }
         IssueTransaction tx = dialog.GetTransaction();
         if (tx == null)
         {
             return;
         }
         //TODO: 检查是否符合规则,如是否超过总量、分发方式是否符合约定等;
         SignatureContext context = new SignatureContext(tx);
         Program.CurrentWallet.Sign(context);
         if (context.Completed)
         {
             context.Signable.Scripts = context.GetScripts();
             InformationBox.Show(context.Signable.ToArray().ToHexString(), "分发交易构造完成,并已完整签名,可以广播。");
         }
         else
         {
             InformationBox.Show(context.ToString(), "分发交易构造完成,但签名信息还不完整。");
         }
     }
 }
Exemplo n.º 3
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("必须输入一段含有待签名数据的JSON对象。");
                return;
            }
            SignatureContext context = SignatureContext.Parse(textBox1.Text);

            if (!Program.CurrentWallet.Sign(context))
            {
                MessageBox.Show("没有足够的私钥对数据进行签名。");
                return;
            }
            if (context.Completed)
            {
                context.Signable.Scripts = context.GetScripts();
                textBox2.Text            = context.Signable.ToArray().ToHexString();
                MessageBox.Show("签名完成,该对象的签名信息已经完整,可以广播。");
            }
            else
            {
                textBox2.Text = context.ToString();
                MessageBox.Show("签名完成,但该对象的签名信息还不完整。");
            }
        }
Exemplo n.º 4
0
        public static void SignAndShowInformation(Transaction tx)
        {
            if (tx == null)
            {
                MessageBox.Show(Strings.InsufficientFunds);
                return;
            }
            SignatureContext context;

            try
            {
                context = new SignatureContext(tx);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show(Strings.UnsynchronizedBlock);
                return;
            }
            Program.CurrentWallet.Sign(context);
            if (context.Completed)
            {
                context.Verifiable.Scripts = context.GetScripts();
                Program.CurrentWallet.SaveTransaction(tx);
                Program.LocalNode.Relay(tx);
                InformationBox.Show(tx.Hash.ToString(), Strings.SendTxSucceedMessage, Strings.SendTxSucceedTitle);
            }
            else
            {
                InformationBox.Show(context.ToString(), Strings.IncompletedSignatureMessage, Strings.IncompletedSignatureTitle);
            }
        }
Exemplo n.º 5
0
        public static void SignAndShowInformation(Transaction tx)
        {
            if (tx == null)
            {
                MessageBox.Show("余额不足以支付系统费用。");
                return;
            }
            SignatureContext context;

            try
            {
                context = new SignatureContext(tx);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("钱包余额不足,或区块链未同步完成,无法发送该交易。");
                return;
            }
            Program.CurrentWallet.Sign(context);
            if (context.Completed)
            {
                context.Signable.Scripts = context.GetScripts();
                Program.LocalNode.Relay(tx);
                InformationBox.Show(tx.Hash.ToString(), "交易已发送,这是交易编号(TXID):", "交易成功");
            }
            else
            {
                InformationBox.Show(context.ToString(), "交易构造完成,但没有足够的签名:", "签名不完整");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 签名
        /// 注意事项
        /// 1.签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同。
        /// 2.签名用的url必须是调用JS接口页面的完整URL。
        /// </summary>
        /// <param name="noncestr">随机串</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="url">当前网页的URL(不包含#及其后面部分)</param>
        /// <returns></returns>
        public string Signature(string noncestr, long timestamp, string url)
        {
            var input = new SignatureContext
            {
                noncestr     = noncestr,
                timestamp    = timestamp,
                url          = url,
                jsapi_ticket = GetTicketString()
            };

            return(input.ToString().GetSHA1Hash());
        }
Exemplo n.º 7
0
 public static void ShowInformation(SignatureContext context)
 {
     if (context.Completed)
     {
         context.Signable.Scripts = context.GetScripts();
         Transaction tx = (Transaction)context.Signable;
         Program.LocalNode.Relay(tx);
         InformationBox.Show(tx.Hash.ToString(), "交易已发送,这是交易编号(TXID):", "交易成功");
     }
     else
     {
         InformationBox.Show(context.ToString(), "交易构造完成,但没有足够的签名:", "签名不完整");
     }
 }
Exemplo n.º 8
0
        private async Task ShowInformationAsync(SignatureContext context)
        {
            if (context.Completed)
            {
                context.Signable.Scripts = context.GetScripts();
                Transaction tx = (Transaction)context.Signable;
                await Program.LocalNode.RelayAsync(tx);

                InformationBox.Show(tx.Hash.ToString(), "交易已发送,这是交易编号(TXID):", "交易成功");
            }
            else
            {
                InformationBox.Show(context.ToString(), "交易构造完成,但没有足够的签名:", "签名不完整");
            }
        }
Exemplo n.º 9
0
        private void button1_Click(object sender, EventArgs e)
        {
            RegisterTransaction antshare = new RegisterTransaction
            {
                AssetType = AssetType.AntShare,
                Name      = "[{'lang':'zh-CN','name':'小蚁股'},{'lang':'en','name':'AntShare'}]",
                Amount    = Fixed8.FromDecimal(numericUpDown1.Value),
                Issuer    = Wallet.ToScriptHash(textBox1.Text),
                Admin     = Wallet.ToScriptHash(textBox2.Text),
                Inputs    = new TransactionInput[0],
                Outputs   = new TransactionOutput[0]
            };
            SignatureContext context = new SignatureContext(antshare);

            InformationBox.Show(context.ToString(), "小蚁股签名上下文");
        }
Exemplo n.º 10
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show(Strings.SigningFailedNoDataMessage);
                return;
            }
            SignatureContext context = SignatureContext.Parse(textBox1.Text);

            if (!Program.CurrentWallet.Sign(context))
            {
                MessageBox.Show(Strings.SigningFailedKeyNotFoundMessage);
                return;
            }
            textBox2.Text = context.ToString();
        }
Exemplo n.º 11
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("必须输入一段含有待签名数据的JSON对象。");
                return;
            }
            SignatureContext context = SignatureContext.Parse(textBox1.Text);

            if (!Program.CurrentWallet.Sign(context))
            {
                MessageBox.Show("没有足够的私钥对数据进行签名。");
                return;
            }
            textBox2.Text = context.ToString();
        }
Exemplo n.º 12
0
        private Transaction SignTransaction(Transaction tx)
        {
            if (tx == null)
            {
                Console.WriteLine($"no transaction specified");
                return(null);
            }
            SignatureContext context;

            try
            {
                context = new SignatureContext(tx);
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine($"unsynchronized block");

                return(null);
            }

            current_wallet.Sign(context);

            if (context.Completed)
            {
                context.Verifiable.Scripts = context.GetScripts();
                current_wallet.SaveTransaction(tx);

                bool relay_result = local_node.Relay(tx);

                if (relay_result)
                {
                    return(tx);
                }
                else
                {
                    Console.WriteLine($"Local Node could not relay transaction: {tx.Hash.ToString()}");
                }
            }
            else
            {
                Console.WriteLine($"Incomplete Signature: {context.ToString()}");
            }

            return(null);
        }
Exemplo n.º 13
0
        private void button2_Click(object sender, EventArgs e)
        {
            RegisterTransaction antshare = textBox3.Text.HexToBytes().AsSerializable <RegisterTransaction>();

            using (IssueDialog dialog = new IssueDialog(antshare))
            {
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                IssueTransaction tx = dialog.GetTransaction();
                if (tx.Outputs.Sum(p => p.Value) != antshare.Amount)
                {
                    MessageBox.Show("发行量不等于总量!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                SignatureContext context = new SignatureContext(tx);
                InformationBox.Show(context.ToString(), "小蚁股发行签名上下文:");
            }
        }
Exemplo n.º 14
0
        private void button1_Click(object sender, EventArgs e)
        {
            RegisterTransaction antshare = new RegisterTransaction
            {
                AssetType = AssetType.AntShare,
#if TESTNET
                Name = "[{'lang':'zh-CN','name':'小蚁股(测试)'},{'lang':'en','name':'AntShare(TestNet)'}]",
#else
                Name = "[{'lang':'zh-CN','name':'小蚁股'},{'lang':'en','name':'AntShare'}]",
#endif
                Amount     = Fixed8.FromDecimal(numericUpDown1.Value),
                Issuer     = ECPoint.Parse(textBox1.Text, ECCurve.Secp256r1),
                Admin      = Wallet.ToScriptHash(textBox2.Text),
                Attributes = new TransactionAttribute[0],
                Inputs     = new TransactionInput[0],
                Outputs    = new TransactionOutput[0]
            };
            SignatureContext context = new SignatureContext(antshare);

            InformationBox.Show(context.ToString(), "小蚁股签名上下文:");
        }
Exemplo n.º 15
0
        public static void SignAndShowInformation(Transaction tx)
        {
            if (tx == null)
            {
                MessageBox.Show(Strings.InsufficientFunds);
                return;
            }
            if (tx.Attributes.All(p => p.Usage != TransactionAttributeUsage.Vote) && tx.Outputs.Any(p => p.AssetId.Equals(Blockchain.AntShare.Hash)) && Settings.Default.Votes.Count > 0)
            {
                tx.Attributes = tx.Attributes.Concat(Settings.Default.Votes.OfType <string>().Select(p => new TransactionAttribute
                {
                    Usage = TransactionAttributeUsage.Vote,
                    Data  = UInt256.Parse(p).ToArray()
                })).ToArray();
            }
            SignatureContext context;

            try
            {
                context = new SignatureContext(tx);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show(Strings.UnsynchronizedBlock);
                return;
            }
            Program.CurrentWallet.Sign(context);
            if (context.Completed)
            {
                context.Signable.Scripts = context.GetScripts();
                Program.CurrentWallet.SaveTransaction(tx);
                Program.LocalNode.Relay(tx);
                InformationBox.Show(tx.Hash.ToString(), Strings.SendTxSucceedMessage, Strings.SendTxSucceedTitle);
            }
            else
            {
                InformationBox.Show(context.ToString(), Strings.IncompletedSignatureMessage, Strings.IncompletedSignatureTitle);
            }
        }
Exemplo n.º 16
0
        private bool OnSendCommand(string[] args)
        {
            if (args.Length < 4 || args.Length > 5)
            {
                Console.WriteLine("error");
                return(true);
            }
            if (Program.Wallet == null)
            {
                Console.WriteLine("You have to open the wallet first.");
                return(true);
            }
            using (SecureString password = ReadSecureString("password"))
            {
                if (password.Length == 0)
                {
                    Console.WriteLine("cancelled");
                    return(true);
                }
                if (!Program.Wallet.VerifyPassword(password))
                {
                    Console.WriteLine("Incorrect password");
                    return(true);
                }
            }
            UInt256 assetId;

            switch (args[1].ToLower())
            {
            case "neo":
            case "ans":
                assetId = Blockchain.SystemShare.Hash;
                break;

            case "gas":
            case "anc":
                assetId = Blockchain.SystemCoin.Hash;
                break;

            default:
                assetId = UInt256.Parse(args[1]);
                break;
            }
            UInt160             scriptHash = Wallet.ToScriptHash(args[2]);
            bool                isSendAll  = string.Equals(args[3], "all", StringComparison.OrdinalIgnoreCase);
            ContractTransaction tx;

            if (isSendAll)
            {
                Coin[] coins = Program.Wallet.FindUnspentCoins().Where(p => p.Output.AssetId.Equals(assetId)).ToArray();
                tx = new ContractTransaction
                {
                    Attributes = new TransactionAttribute[0],
                    Inputs     = coins.Select(p => p.Reference).ToArray(),
                    Outputs    = new[]
                    {
                        new TransactionOutput
                        {
                            AssetId    = assetId,
                            Value      = coins.Sum(p => p.Output.Value),
                            ScriptHash = scriptHash
                        }
                    }
                };
            }
            else
            {
                if (!Fixed8.TryParse(args[3], out Fixed8 amount))
                {
                    Console.WriteLine("Incorrect Amount Format");
                    return(true);
                }
                if (amount.GetData() % (long)Math.Pow(10, 8 - Blockchain.Default.GetAssetState(assetId).Precision) != 0)
                {
                    Console.WriteLine("Incorrect Amount Precision");
                    return(true);
                }
                Fixed8 fee = args.Length >= 5 ? Fixed8.Parse(args[4]) : Fixed8.Zero;
                tx = Program.Wallet.MakeTransaction(new ContractTransaction
                {
                    Outputs = new[]
                    {
                        new TransactionOutput
                        {
                            AssetId    = assetId,
                            Value      = amount,
                            ScriptHash = scriptHash
                        }
                    }
                }, fee: fee);
                if (tx == null)
                {
                    Console.WriteLine("Insufficient funds");
                    return(true);
                }
            }
            SignatureContext context = new SignatureContext(tx);

            Program.Wallet.Sign(context);
            if (context.Completed)
            {
                tx.Scripts = context.GetScripts();
                Program.Wallet.SaveTransaction(tx);
                LocalNode.Relay(tx);
                Console.WriteLine($"TXID: {tx.Hash}");
            }
            else
            {
                Console.WriteLine("SignatureContext:");
                Console.WriteLine(context.ToString());
            }
            return(true);
        }
Exemplo n.º 17
0
 private void button2_Click(object sender, EventArgs e)
 {
     InformationBox.Show(context.ToString(), "SignatureContext", "SignatureContext");
 }
        private bool OnSendCommand(string[] args)
        {
            if (Program.Wallet == null)
            {
                Console.WriteLine("You have to open the wallet first.");
                return(true);
            }
            if (args.Length < 4 || args.Length > 5)
            {
                Console.WriteLine("error");
                return(true);
            }
            UInt256 assetId;

            switch (args[1].ToLower())
            {
            case "ans":
                assetId = Blockchain.SystemShare.Hash;
                break;

            case "anc":
                assetId = Blockchain.SystemCoin.Hash;
                break;

            default:
                assetId = UInt256.Parse(args[1]);
                break;
            }
            UInt160 scriptHash = Wallet.ToScriptHash(args[2]);
            Fixed8  amount;

            if (!Fixed8.TryParse(args[3], out amount))
            {
                Console.WriteLine("Incorrect Amount Format");
                return(true);
            }
            if (amount.GetData() % (long)Math.Pow(10, 8 - Blockchain.Default.GetAssetState(assetId).Precision) != 0)
            {
                Console.WriteLine("Incorrect Amount Precision");
                return(true);
            }

            Fixed8 fee             = args.Length >= 5 ? Fixed8.Parse(args[4]) : Fixed8.Zero;
            ContractTransaction tx = Program.Wallet.MakeTransaction(new ContractTransaction
            {
                Outputs = new[]
                {
                    new TransactionOutput
                    {
                        AssetId    = assetId,
                        Value      = amount,
                        ScriptHash = scriptHash
                    }
                }
            }, fee: fee);

            if (tx == null)
            {
                Console.WriteLine("Insufficient funds");
                return(true);
            }
            using (SecureString password = ReadSecureString("password"))
            {
                if (password.Length == 0)
                {
                    Console.WriteLine("cancelled");
                    return(true);
                }
                if (!Program.Wallet.VerifyPassword(password))
                {
                    Console.WriteLine("Incorrect password");
                    return(true);
                }
            }
            SignatureContext context = new SignatureContext(tx);

            Program.Wallet.Sign(context);
            if (context.Completed)
            {
                tx.Scripts = context.GetScripts();
                Program.Wallet.SaveTransaction(tx);
                LocalNode.Relay(tx);
                Console.WriteLine($"TXID: {tx.Hash}");
            }
            else
            {
                Console.WriteLine("SignatureContext:");
                Console.WriteLine(context.ToString());
            }
            return(true);
        }