Exemple #1
0
        private void copyPublicHexQRMenuItem_Click(object sender, EventArgs e)
        {
            string toencode = txtPubHex.Text.Replace(" ", "");
            Bitmap b        = QR.EncodeQRCode(toencode);

            if (b == null)
            {
                MessageBox.Show("Enter or create a valid public key first.");
                return;
            }
            Clipboard.SetText(toencode);
            Clipboard.SetImage(b);
        }
Exemple #2
0
        private void copyAddressQRMenuItem_Click(object sender, EventArgs e)
        {
            string toencode = txtBtcAddr.Text;
            Bitmap b        = QR.EncodeQRCode(toencode);

            if (b == null)
            {
                MessageBox.Show("Enter or create a valid address first.");
                return;
            }
            Clipboard.SetText(toencode);
            Clipboard.SetImage(b);
        }
        protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
        {
            base.OnPrintPage(e);
            int printHeight;
            int printWidth;
            int leftMargin;
            int rightMargin;

            //Set print area size and margins
            {
                printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
                printWidth  = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
                leftMargin  = base.DefaultPageSettings.Margins.Left;
                //X
                rightMargin = base.DefaultPageSettings.Margins.Top;
                //Y
            }


            for (int i = 0; i < 16; i++)
            {
                int eachheight = 120;

                switch (PrintMode)
                {
                case PrintModes.PubPrivQR:
                    if (i >= 8)
                    {
                        i = 999;
                    }
                    eachheight = 120;
                    break;

                case PrintModes.PsyBanknote:
                    if (i >= 3)
                    {
                        i = 999;
                    }
                    eachheight = 365;
                    break;
                }

                if (i == 999)
                {
                    break;
                }



                if (PrintMode == PrintModes.PubPrivQR && i >= 8)
                {
                    break;
                }

                if (PrintMode == PrintModes.PsyBanknote && i >= NotesPerPage)
                {
                    break;
                }

                if (keys.Count == 0)
                {
                    break;
                }

                int thiscodeX = 50;
                int thiscodeY = 50 + eachheight * i;
                if (i >= 8)
                {
                    thiscodeX = 450;
                    thiscodeY = 50 + eachheight * (i - 8);
                }

                //    T-------------------------------|
                //    |                               |
                //    |                               |
                //    |                               |
                //    |                               |
                //    |                               |
                //    |-------------------------------|
                //
                //    T = thiscodeX,thiscodeY
                //


                // Load the Ubuntu font directly from a file so it doesn't need to be installed on the system.
                if (UbuntuFontLoaded == false)
                {
                    UbuntuFontLoaded = true;
                    try {
                        System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
                        pfc.AddFontFile("Ubuntu-R.ttf");
                    } catch { }
                }

                ubuntufont = new Font("Ubuntu", 6);
                ubuntumid  = new Font("Ubuntu", 9);
                ubuntubig  = new Font("Ubuntu", 17);


                KeyCollectionItem k = (KeyCollectionItem)keys[0];
                keys.RemoveAt(0);

                string privkey = k.PrivateKey;
                if (PreferUnencryptedPrivateKeys)
                {
                    if (k.EncryptedKeyPair != null && k.EncryptedKeyPair.IsUnencryptedPrivateKeyAvailable())
                    {
                        privkey = k.EncryptedKeyPair.GetUnencryptedPrivateKey().PrivateKey;
                    }
                }

                Bitmap b = QR.EncodeQRCode(privkey);

                if (PrintMode == PrintModes.PsyBanknote)
                {
                    if (BitcoinNote == null)
                    {
                        BitcoinNote = Image.FromFile(ImageFilename);
                    }

                    float desiredScale = 550F;

                    float scalefactor = (desiredScale / 650.0F);
                    float leftOffset  = (float)printWidth - desiredScale;


                    // draw the note
                    e.Graphics.DrawImage(BitcoinNote,
                                         leftOffset + scalefactor * (float)thiscodeX,
                                         scalefactor * (float)thiscodeY, (float)650F * scalefactor,
                                         (float)650F * scalefactor * (float)BitcoinNote.Height / (float)BitcoinNote.Width);

                    // draw the private QR
                    e.Graphics.DrawImage(b, leftOffset + scalefactor * (float)(thiscodeX + 472),
                                         scalefactor * (float)(thiscodeY + 140),
                                         scalefactor * 145F,
                                         scalefactor * 147F);

                    // draw the public QR
                    Bitmap b2 = QR.EncodeQRCode(k.GetAddressBase58());
                    e.Graphics.DrawImage(b2,
                                         leftOffset + scalefactor * (float)(thiscodeX + 39),
                                         scalefactor * (float)(thiscodeY + 90), scalefactor * 128F, scalefactor * 128F);

                    // write bitcoin address
                    StringFormat sf = new StringFormat();
                    //sf.FormatFlags |= StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft;

                    e.Graphics.RotateTransform(-90F);
                    e.Graphics.DrawString("Bitcoin Address\r\n" + k.GetAddressBase58(), ubuntumid, Brushes.Black,
                                          -scalefactor * (float)(thiscodeY + 338),
                                          leftOffset + scalefactor * (float)(thiscodeX + 170),

                                          sf);

                    // write private key
                    string whattoprint;
                    if (privkey.Length > 30)
                    {
                        whattoprint = privkey.Substring(0, 25) + "\r\n" + privkey.Substring(25);
                    }
                    else
                    {
                        whattoprint = "\r\n" + privkey;
                    }
                    float xpos = 444;
                    if (privkey.StartsWith("6"))
                    {
                        whattoprint = "Password Required\r\n" + whattoprint;
                        xpos       -= 10;
                    }

                    e.Graphics.DrawString(whattoprint, ubuntufont, Brushes.Black,
                                          -scalefactor * (float)(thiscodeY + 290),
                                          leftOffset + scalefactor * (float)(thiscodeX + xpos),
                                          sf);


                    e.Graphics.RotateTransform(90F);

                    // write denomination, if any
                    if ((Denomination ?? "") != "")
                    {
                        e.Graphics.DrawString(Denomination, ubuntubig, Brushes.Black,
                                              leftOffset + scalefactor * (float)(thiscodeX + 330),
                                              scalefactor * (float)(thiscodeY + 310)

                                              );
                    }

                    if (PrintMiniKeysWith1DBarcode && k.Address is MiniKeyPair)
                    {
                        Bitmap barcode1d = Barcode128b.GetBarcode(k.PrivateKey);
                        float  aspect1d  = (float)barcode1d.Width / (float)barcode1d.Height;
                        e.Graphics.DrawImage(barcode1d, leftOffset + scalefactor * (float)(thiscodeX + 231F),
                                             scalefactor * (float)(thiscodeY + 293),
                                             scalefactor * 420F,
                                             scalefactor * 50F);
                    }
                }
                else if (PrintMode == PrintModes.PrivQR)
                {
                    // ----------------------------------------------------------------
                    // Paper wallet with only private key QR code.  Fits 16 to a page.
                    // ----------------------------------------------------------------
                    e.Graphics.DrawImage(b, thiscodeX, thiscodeY, 100, 100);

                    e.Graphics.DrawString("Bitcoin address: " + k.GetAddressBase58(), fontsmall, Brushes.Black, thiscodeX + 110, thiscodeY);

                    string whattowrite;
                    if (privkey.Length > 30)
                    {
                        whattowrite = privkey.Substring(0, 25) + "\r\n" + privkey.Substring(25);
                    }
                    else
                    {
                        whattowrite = "\r\n" + privkey;
                    }
                    if (privkey.StartsWith("6"))
                    {
                        whattowrite = whattowrite + "\r\nPassword Required";
                    }


                    e.Graphics.DrawString(whattowrite, font, Brushes.Black, thiscodeX + 110, thiscodeY + 15);

                    if ((Denomination ?? "") != "")
                    {
                        e.Graphics.DrawString(Denomination + " BTC", fontbig, Brushes.Black, thiscodeX + 110, thiscodeY + 75);
                    }
                }
                else if (PrintMode == PrintModes.PubPrivQR)
                {
                    // ----------------------------------------------------------------
                    // Paper wallet with public and private QR codes.  Fits 8 to a page.
                    // ----------------------------------------------------------------

                    e.Graphics.DrawImage(b, thiscodeX + 600, thiscodeY, 100, 100);
                    QRCodeEncoder qr2 = new QRCodeEncoder();
                    qr2.QRCodeVersion = 3;

                    Bitmap b2 = qr2.Encode(k.GetAddressBase58());
                    e.Graphics.DrawImage(b2, thiscodeX, thiscodeY, 100, 100);

                    e.Graphics.DrawString("Bitcoin address:\r\n" + k.GetAddressBase58(), font, Brushes.Black, thiscodeX + 110, thiscodeY);

                    StringFormat sf = new StringFormat();
                    sf.Alignment = StringAlignment.Far; // right justify
                    string whattoprint = privkey;
                    if (privkey.StartsWith("6"))
                    {
                        whattoprint = whattoprint + "\r\nPassword Required";
                    }

                    e.Graphics.DrawString("Private key:\r\n" + whattoprint, font, Brushes.Black, thiscodeX + 597, thiscodeY + 65, sf);
                }
            }

            e.HasMorePages = keys.Count > 0;
        }
        protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
        {
            base.OnPrintPage(e);
            int printHeight;
            int printWidth;
            int leftMargin;
            int rightMargin;

            //Set print area size and margins
            {
                printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
                printWidth  = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
                leftMargin  = base.DefaultPageSettings.Margins.Left;
                //X
                rightMargin = base.DefaultPageSettings.Margins.Top;
                //Y
            }


            for (int i = 0; i < 8; i++)
            {
                int eachheight = 120;
                if (keys.Count == 0)
                {
                    break;
                }

                KeyCollectionItem kci     = keys[0];
                string            address = kci.GetAddressBase58();

                string privkey  = kci.PrivateKey;
                string confcode = "";
                if (kci.EncryptedKeyPair != null && kci.EncryptedKeyPair is Bip38KeyPair)
                {
                    confcode = ((Bip38KeyPair)kci.EncryptedKeyPair).GetConfirmationCode() ?? "";
                    //if (confcode != "") confcode = "Confirmation code:\r\n" + confcode;
                }

                keys.RemoveAt(0);

                int thiscodeX = 0; //  50;
                int thiscodeY = 50 + eachheight * i;

                // ----------------------------------------------------------------
                // Coin insert with public and private QR codes.  Fits 8 to a page.
                // ----------------------------------------------------------------


                float CircleDiameterInches = (7F / 16F); // 7/16"

                // draw the private key circle
                using (Pen blackpen = new Pen(Color.Black)) {
                    blackpen.Width = (1F / 72F);

                    e.Graphics.DrawEllipse(blackpen, thiscodeX + 30F, thiscodeY + 10F, CircleDiameterInches * 100F, CircleDiameterInches * 100F);

                    // Over 30 characters? do a folding insert at 95% diameter away
                    if (privkey.Length > 30)
                    {
                        e.Graphics.DrawEllipse(blackpen, thiscodeX + 30F, thiscodeY + 10F + (CircleDiameterInches * 95F), CircleDiameterInches * 100F, CircleDiameterInches * 100F);
                        e.Graphics.FillEllipse(Brushes.White, thiscodeX + 30F, thiscodeY + 10F + (CircleDiameterInches * 95F), CircleDiameterInches * 100F, CircleDiameterInches * 100F);
                    }
                    e.Graphics.FillEllipse(Brushes.White, thiscodeX + 30F, thiscodeY + 10F, CircleDiameterInches * 100F, CircleDiameterInches * 100F);
                }



                int[]  charsPerLine = new int[] { 4, 7, 8, 7, 4, 0, 4, 7, 8, 7, 4 };
                string privkeyleft  = privkey;
                // if it's going to take two circles, add hyphens
                if (privkeyleft.Length > 30)
                {
                    privkeyleft = privkeyleft.Substring(0, 29) + "--" + privkeyleft.Substring(29);
                }
                string privkeytoprint = "";
                for (int c = 0; c < 11; c++)
                {
                    if (charsPerLine[c] == 0)
                    {
                        privkeytoprint += "\r\n";
                    }
                    else
                    {
                        if (privkeyleft.Length > charsPerLine[c])
                        {
                            privkeytoprint += privkeyleft.Substring(0, charsPerLine[c]) + "\r\n";
                            privkeyleft     = privkeyleft.Substring(charsPerLine[c]);
                        }
                        else
                        {
                            privkeytoprint += privkeyleft + "\r\n";
                            privkeyleft     = "";
                        }
                    }
                }
                using (StringFormat sfcenter = new StringFormat()) {
                    sfcenter.Alignment = StringAlignment.Center;
                    e.Graphics.DrawString(privkeytoprint, fontsmall, Brushes.Black, thiscodeX + 30F + (CircleDiameterInches * 100F / 2F), thiscodeY + 14F, sfcenter);
                }



                // draw the address QR code
                using (Bitmap b2 = QR.EncodeQRCode(address)) {
                    e.Graphics.DrawImage(b2, thiscodeX + 100, thiscodeY, 100, 100);
                }

                e.Graphics.DrawString("Bitcoin address:\r\n" + address, font, Brushes.Black, thiscodeX + 210, thiscodeY);

                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Far; // right justify

                if (confcode != "")
                {
                    // Print the confirmation QR code
                    using (Bitmap b = QR.EncodeQRCode(confcode)) {
                        e.Graphics.DrawImage(b, thiscodeX + 600, thiscodeY, 100, 100);

                        string whattoprint = "Confirmation code:\r\n" + confcode.Substring(0, 38) + "\r\n" + confcode.Substring(38);

                        e.Graphics.DrawString(whattoprint, font, Brushes.Black, thiscodeX + 597, thiscodeY + 55, sf);
                    }
                }
            }
            if (keys.Count != 0)
            {
                e.HasMorePages = true;
            }
        }
        protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
        {
            baseOnPrintPage(e);
            int   printHeight;
            int   printWidth;
            int   leftMargin;
            int   rightMargin;
            Int32 lines;
            Int32 chars;

            //Set print area size and margins
            {
                printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
                printWidth  = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
                leftMargin  = base.DefaultPageSettings.Margins.Left;
                //X
                rightMargin = base.DefaultPageSettings.Margins.Top;
                //Y
            }

            int startwidth  = 0;
            int startheight = 50;

            for (int i = 0; i < 96; i++)
            {
                int eachheight = 60, eachwidth = 130;
                if (keys.Count == 0)
                {
                    break;
                }

                KeyCollectionItem kci     = keys[0];
                string            address = kci.GetAddressBase58();

                string privkey = kci.PrivateKey;

                keys.RemoveAt(0);

                int thiscodeX = startwidth + eachwidth * (i / 16);
                int thiscodeY = startheight + eachheight * (i % 16);

                // ----------------------------------------------------------------
                // Coin insert with public and private QR codes.  Fits 8 to a page.
                // ----------------------------------------------------------------


                float CircleDiameterInches = (7F / 16F); // 7/16"

                // draw the private key circle
                using (Pen blackpen = new Pen(Color.Black)) {
                    // print some alignment marks for use in laser cutting
                    if (i == 0)
                    {
                        e.Graphics.FillRectangle(Brushes.Black, startwidth + eachwidth * 3F, startheight, 0.01F, 0.01F);
                        e.Graphics.FillRectangle(Brushes.Black, startwidth + eachwidth * 3F, (float)startheight + (float)eachheight * 8.5F, 0.01F, 0.01F);
                        e.Graphics.FillRectangle(Brushes.Black, startwidth + eachwidth * 3F, (float)startheight + (float)eachheight * 17F, 0.01F, 0.01F);
                    }


                    blackpen.Width = (1F / 72F);

                    e.Graphics.DrawEllipse(blackpen, thiscodeX + 30F, thiscodeY + 10F, CircleDiameterInches * 100F, CircleDiameterInches * 100F);

                    // Over 30 characters? do a folding insert at 95% diameter away
                    if (privkey.Length > 30)
                    {
                        e.Graphics.DrawEllipse(blackpen, thiscodeX + 30F, thiscodeY + 10F + (CircleDiameterInches * 95F), CircleDiameterInches * 100F, CircleDiameterInches * 100F);
                        e.Graphics.FillEllipse(Brushes.White, thiscodeX + 30F, thiscodeY + 10F + (CircleDiameterInches * 95F), CircleDiameterInches * 100F, CircleDiameterInches * 100F);
                    }
                    e.Graphics.FillEllipse(Brushes.White, thiscodeX + 30F, thiscodeY + 10F, CircleDiameterInches * 100F, CircleDiameterInches * 100F);
                }



                int[]  charsPerLine = new int[] { 4, 7, 8, 7, 4, 0, 4, 7, 8, 7, 4 };
                string privkeyleft  = privkey;
                // if it's going to take two circles, add hyphens
                if (privkeyleft.Length > 30)
                {
                    privkeyleft = privkeyleft.Substring(0, 29) + "--" + privkeyleft.Substring(29);
                }
                string privkeytoprint = "";
                for (int c = 0; c < 11; c++)
                {
                    if (charsPerLine[c] == 0)
                    {
                        privkeytoprint += "\r\n";
                    }
                    else
                    {
                        if (privkeyleft.Length > charsPerLine[c])
                        {
                            privkeytoprint += privkeyleft.Substring(0, charsPerLine[c]) + "\r\n";
                            privkeyleft     = privkeyleft.Substring(charsPerLine[c]);
                        }
                        else
                        {
                            privkeytoprint += privkeyleft + "\r\n";
                            privkeyleft     = "";
                        }
                    }
                }
                using (StringFormat sfcenter = new StringFormat()) {
                    sfcenter.Alignment = StringAlignment.Center;
                    e.Graphics.DrawString(privkeytoprint, fontsmall, Brushes.Black, thiscodeX + 30F + (CircleDiameterInches * 100F / 2F), thiscodeY + 14F, sfcenter);
                }

                // draw the address QR code
                using (Bitmap b2 = QR.EncodeQRCode(address)) {
                    e.Graphics.DrawImage(b2, thiscodeX + 80, thiscodeY + 10, 50, 50);
                }

                e.Graphics.RotateTransform(-90F);
                // transform 90 degrees changes our coordinate space so we can do sideways text.
                // must swap xy and value supplied as x parameter must be negative
                // instead of             it's now
                //        -Y                  +X
                //        |                   |
                //  -X-------+X        -Y----------+Y
                //        | PRINT             | PRINT
                //        +Y                  -X


                using (StringFormat sfright = new StringFormat()) {
                    sfright.Alignment = StringAlignment.Far;
                    e.Graphics.DrawString(address.Substring(0, 12) + "\r\n" + address.Substring(12, 12) + "\r\n" + address.Substring(24), fontsmall, Brushes.Black,
                                          -(float)(thiscodeY + 10),
                                          (float)(thiscodeX + 130), sfright);
                }
                // get out of sideways mode
                e.Graphics.RotateTransform(90F);
            }
            if (keys.Count != 0)
            {
                e.HasMorePages = true;
            }
        }