/// <summary>
 /// Constructs the object with string key, returning any intended exception as a string.
 /// </summary>
 private string constructWithKey(string key, bool compressed)
 {
     byte[] hex = Util.Base58CheckToByteArray(key);
     if (hex == null)
     {
         hex = Util.HexStringToBytes(key, true);
         if (hex == null)
         {
             // tolerate a minikey
             if (MiniKeyPair.IsValidMiniKey(key) > 0)
             {
                 PrivateKeyBytes = new MiniKeyPair(key).PrivateKeyBytes;
                 return(null);
             }
             else
             {
                 return("Invalid private key");
             }
         }
     }
     if (hex.Length == 32)
     {
         _privKey = new byte[32];
         Array.Copy(hex, 0, _privKey, 0, 32);
         IsCompressedPoint = compressed;
     }
     else if (hex.Length == 33 && hex[0] == 0x80)
     {
         // normal private key
         _privKey = new byte[32];
         Array.Copy(hex, 1, _privKey, 0, 32);
         IsCompressedPoint = false;
     }
     else if (hex.Length == 34 && hex[0] == 0x80 && hex[33] == 0x01)
     {
         // compressed private key
         _privKey = new byte[32];
         Array.Copy(hex, 1, _privKey, 0, 32);
         IsCompressedPoint = true;
     }
     else if (key.StartsWith("6"))
     {
         return("Key is encrypted, decrypt first.");
     }
     else
     {
         return("Not a recognized private key format");
     }
     return(validateRange());
 }
        public static bool PassphraseTooSimple(string passphrase)
        {
            int Lowercase = 0, Uppercase = 0, Numbers = 0, Symbols = 0, Spaces = 0;

            foreach (char c in passphrase.ToCharArray())
            {
                if (c >= 'a' && c <= 'z')
                {
                    Lowercase++;
                }
                else if (c >= 'A' && c <= 'Z')
                {
                    Uppercase++;
                }
                else if (c >= '0' && c <= '9')
                {
                    Numbers++;
                }
                else if (c == ' ')
                {
                    Spaces++;
                }
                else
                {
                    Symbols++;
                }
            }

            // let mini private keys through - they won't contain words, they are nonsense characters, so their entropy is a bit better per character
            if (MiniKeyPair.IsValidMiniKey(passphrase) != 1)
            {
                return(false);
            }

            if (passphrase.Length < 30 && (Lowercase < 10 || Uppercase < 3 || Numbers < 2 || Symbols < 2))
            {
                return(true);
            }

            return(false);
        }
Пример #3
0
        /// <summary>
        /// Interprets a string to automatically detect a type of Bitcoin-related object.
        /// compressed and addresstype are only considered when the object doesn't define these itself.
        /// </summary>
        public static object Interpret(string what, bool compressed = false, byte addressType = 36)     //GRS
        {
            if (what == null)
            {
                return(null);
            }

            what = what.Trim();

            // Is the string interpretable as base58?
            byte[] hex = Util.Base58CheckToByteArray(what);

            if (hex != null)
            {
                try {
                    switch (hex.Length)
                    {
                    case 21:
                        // It's an address of some sort.
                        return(new AddressBase(what));

                    case 31:
                    case 32:
                    case 33:
                    case 34:
                        // Unencrypted private key
                        return(new KeyPair(what));

                    case 36:
                        // these pairs aren't decided by length alone,
                        // but the constructors will throw an exception if they
                        // don't contain valid key material.
                        return(new ShaPassphraseKeyPair(what));

                    case 39:
                        return(new Bip38KeyPair(what));

                    case 49:
                        return(new Bip38Intermediate(what, Bip38Intermediate.Interpretation.IntermediateCode));

                    case 51:
                        return(new Bip38Confirmation(what));
                    }
                } catch {}
                // If a constructor didn't like something, then don't return anything.
            }


            hex = Util.HexStringToBytes(what, true);
            if (hex != null)
            {
                try {
                    switch (hex.Length)
                    {
                    case 33:
                    case 65:
                        return(new PublicKey(hex));

                    case 20:
                        return(new AddressBase(hex, addressType));

                    case 21:     // hash160
                        return(new AddressBase(hex));

                    case 30:
                    case 31:
                    case 32:
                        return(new KeyPair(hex, compressed: compressed, addressType: addressType));
                    }
                } catch { }
            }

            if (MiniKeyPair.IsValidMiniKey(what) == 1)
            {
                return(new MiniKeyPair(what));
            }

            return(null);
        }
Пример #4
0
 /// <summary>
 /// Constructs the object with string key, returning any intended exception as a string.
 /// </summary>
 private string constructWithKey(string key, bool compressed)
 {
     byte[] hex = Util.Base58CheckToByteArray(key);
     if (hex == null) {
         hex = Util.HexStringToBytes(key, true);
         if (hex == null) {
             // tolerate a minikey
             if (MiniKeyPair.IsValidMiniKey(key) > 0) {
                 PrivateKeyBytes = new MiniKeyPair(key, AddressType).PrivateKeyBytes;
                 return null;
             } else {
                 return "Invalid private key";
             }
         }
     }
     if (hex.Length == 32) {
         _privKey = new byte[32];
         Array.Copy(hex, 0, _privKey, 0, 32);
         IsCompressedPoint = compressed;
     } else if (hex.Length == 33 && hex[0] == Bitcoin.AddressType.ToPrivateKeyPrefix(AddressType)) {
         // normal private key
         _privKey = new byte[32];
         Array.Copy(hex, 1, _privKey, 0, 32);
         IsCompressedPoint = false;
     } else if (hex.Length == 34 && hex[0] == Bitcoin.AddressType.ToPrivateKeyPrefix(AddressType) && hex[33] == 0x01) {
         // compressed private key
         _privKey = new byte[32];
         Array.Copy(hex, 1, _privKey, 0, 32);
         IsCompressedPoint = true;
     } else if (key.StartsWith("6")) {
         return "Key is encrypted, decrypt first.";
     } else {
         return "Not a recognized private key format";
     }
     return validateRange();
 }