Example #1
0
        private void btnSignPrivkey_Click(object sender, EventArgs e)
        {
            PrivateKeyDialog pkd = new PrivateKeyDialog();
            DialogResult     res = pkd.ShowDialog();

            if (res == DialogResult.Cancel)
            {
                return;
            }

            List <PrivateKey> keys = new List <PrivateKey>();

            if (pkd.rbWIF.Checked)
            {
                foreach (TextBox tb in pkd.txtKeyList)
                {
                    keys.Add(PrivateKey.FromWIF(tb.Text));
                }
            }
            else if (pkd.rbHEX.Checked)
            {
                foreach (TextBox tb in pkd.txtKeyList)
                {
                    keys.Add(PrivateKey.FromHex(tb.Text));
                }
            }

            Transaction tx = new Transaction(HexString.ToByteArray(txtTx.Text));

            for (int i = 0; i < tx.inputs.Length; i++)
            {
                foreach (PrivateKey pk in keys)
                {
                    TxOut   prevOut = selectedUTXO[new TxOutId(tx.inputs[i].prevOut, tx.inputs[i].prevOutIndex)];
                    Address a       = Address.FromScript(prevOut.scriptPubKey);
                    Address b;
                    Script  redeemScript = null;
                    if (pkd.cbP2SH.Checked)
                    {
                        redeemScript = new Script(HexString.ToByteArray(pkd.txtRedeemScript.Text));
                        b            = new Address(redeemScript.ToBytes(), Address.SCRIPT);
                    }
                    else
                    {
                        b = pk.pubKey.address;
                    }
                    if (a.ToString() == b.ToString())
                    {
                        tx.inputs[i].Sign(tx, prevOut, pk, HashType.SIGHASH_ALL, redeemScript);
                    }
                }
            }
            txtTx.Text = HexString.FromByteArray(tx.ToBytes());
        }
Example #2
0
        public void FromPrivateKey()
        {
            var privateHex = "906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff";
            var publicHex  = "02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc";

            var privkey = PrivateKey.FromHex(privateHex);
            var pubkey  = privkey.CreatePublicKey();

            Assert.Equal(privateHex, privkey.ToHex());
            Assert.Equal(publicHex, pubkey.ToHex());
            Assert.True(privkey.VerifyPubKey(pubkey));
        }