Exemplo n.º 1
0
        public string CheckBase58Address(string address)
        {
            if (!Base58.IsValid(address))
            {
                return("The given address contains invalid base-58 characters.");
            }
            if (!Base58.IsValidWithChecksum(address))
            {
                return("The given address has an invalid checksum.");
            }

            byte[] addrBa = Base58.DecodeWithChecksum(address);

            if (addrBa[0] != ConstantsFO.P2pkhAddrFirstByte && addrBa[0] != ConstantsFO.P2shAddrFirstByte)
            {
                return("The given address starts with an invalid byte.");
            }
            if (addrBa.Length != 21)
            {
                return("The given address byte length is invalid.");
            }

            return($"The given address is a valid base-58 encoded address used for " +
                   $"{(addrBa[0] == ConstantsFO.P2pkhAddrFirstByte ? "P2PKH" : "P2SH")} scripts.");
        }
Exemplo n.º 2
0
        public string CheckPrivateKey(string key)
        {
            if (!Base58.IsValid(key))
            {
                return("The given key contains invalid base-58 characters.");
            }
            if (!Base58.IsValidWithChecksum(key))
            {
                return("The given key has an invalid checksum.");
            }

            byte[] keyBa = Base58.DecodeWithChecksum(key);
            if (keyBa[0] != ConstantsFO.PrivKeyFirstByte)
            {
                return($"Invalid first key byte (actual={keyBa[0]}, expected={ConstantsFO.PrivKeyFirstByte}).");
            }

            if (keyBa.Length == 33)
            {
                if (!IsPrivateKeyInRange(keyBa.SubArray(1)))
                {
                    return("Invalid key integer value (outside of the range defined by secp256k1 curve).");
                }

                return("The given key is a valid uncompressed private key.");
            }
            else if (keyBa.Length == 34)
            {
                if (keyBa[^ 1] != ConstantsFO.PrivKeyCompLastByte)
Exemplo n.º 3
0
        public bool CheckBase58Bip38(string bip38, out string message)
        {
            if (!Base58.IsValid(bip38))
            {
                message = "The given BIP-38 string contains invalid base-58 characters.";
                return(false);
            }
            if (!Base58.IsValidWithChecksum(bip38))
            {
                message = "The given BIP-38 string has an invalid checksum.";
                return(false);
            }

            byte[] data = Base58.DecodeWithChecksum(bip38);
            if (data.Length != ConstantsFO.Bip38ByteLen)
            {
                message = "The given BIP-38 string has an invalid byte length.";
                return(false);
            }
            if (data[0] != 1 || (data[1] != 0x42 && data[1] != 0x43))
            {
                message = "The given BIP-38 string has invalid starting bytes.";
                return(false);
            }

            message = "The given BIP-38 string is valid.";
            return(true);
        }
Exemplo n.º 4
0
 public void Validate(string address)
 {
     if (string.IsNullOrEmpty(address))
     {
         AddError(nameof(address), "Address can not be empty!");
     }
     else if (address.StartsWith("1") || address.StartsWith("3"))
     {
         Base58 b58enc = new Base58();
         if (b58enc.IsValid(address))
         {
             RemoveError(nameof(address), "");
         }
         else
         {
             AddError(nameof(address), "Invalid Base58 encoded address!");
         }
     }
     else if (address.StartsWith("bc"))
     {
         Bech32 b32enc = new Bech32();
         if (b32enc.IsValid(address))
         {
             RemoveError(nameof(address), "");
         }
         else
         {
             AddError(nameof(address), "Invalid Bech32 encoded address!");
         }
     }
     else
     {
         AddError(nameof(address), "Invalid address format!");
     }
 }
Exemplo n.º 5
0
        public string CheckBase58Bip38(string bip38)
        {
            if (!b58Enc.HasValidChars(bip38))
            {
                return("The given BIP-38 string contains invalid base-58 characters.");
            }
            if (!b58Enc.IsValid(bip38))
            {
                return("The given BIP-38 string has an invalid checksum.");
            }

            byte[] data = b58Enc.DecodeWithCheckSum(bip38);
            if (data.Length != ConstantsFO.Bip38ByteLen)
            {
                return("The given BIP-38 string has an invalid byte length.");
            }
            if (data[0] != 1 || (data[1] != 0x42 && data[1] != 0x43))
            {
                return("The given BIP-38 string has invalid starting bytes.");
            }

            return("The given BIP-38 string is valid.");
        }
Exemplo n.º 6
0
 [InlineData("1BvBMSOYstWetqTFn5Au4m4GFg7xJaNVN2", false)] // Invalid char
 public void IsValidTest(string s, bool expected)
 {
     Assert.Equal(expected, encoder.IsValid(s));
 }
Exemplo n.º 7
0
 public void HasValidTest(string s, bool expected)
 {
     Assert.Equal(expected, Base58.IsValid(s));
 }
Exemplo n.º 8
0
        internal bool TryGetType(string address, out PubkeyScriptType scrType, out byte[] hash)
        {
            scrType = PubkeyScriptType.Unknown;
            if (string.IsNullOrWhiteSpace(address))
            {
                hash = null;
                return(false);
            }

            try
            {
                if (b58Encoder.IsValid(address))
                {
                    byte[] decoded = b58Encoder.DecodeWithCheckSum(address);

                    if (decoded.Length != hashFunc.HashByteSize + 1)
                    {
                        hash = null;
                        return(false);
                    }
                    else if (decoded[0] != versionByte_P2pkh_MainNet ||
                             decoded[0] != versionByte_P2pkh_TestNet ||
                             decoded[0] != versionByte_P2pkh_RegTest)
                    {
                        scrType = PubkeyScriptType.P2PKH;
                        hash    = decoded.SubArray(1);
                        return(true);
                    }
                    else if (decoded[0] != versionByte_P2sh_MainNet ||
                             decoded[0] != versionByte_P2sh_TestNet ||
                             decoded[0] != versionByte_P2sh_RegTest)
                    {
                        scrType = PubkeyScriptType.P2SH;
                        hash    = decoded.SubArray(1);
                        return(true);
                    }
                    else
                    {
                        hash = null;
                        return(false);
                    }
                }
                else if (b32Encoder.IsValid(address))
                {
                    byte[] decoded = b32Encoder.Decode(address, out byte witVer, out string hrp);

                    if (witVer != 0)
                    {
                        hash = null;
                        return(false);
                    }
                    else if (decoded.Length == hashFunc.HashByteSize &&
                             hrp == hrp_MainNet ||
                             hrp == hrp_TestNet ||
                             hrp == hrp_RegTest)
                    {
                        scrType = PubkeyScriptType.P2WPKH;
                        hash    = decoded;
                        return(true);
                    }
                    else if (decoded.Length == witHashFunc.HashByteSize &&
                             hrp == hrp_MainNet ||
                             hrp == hrp_TestNet ||
                             hrp == hrp_RegTest)
                    {
                        scrType = PubkeyScriptType.P2WPKH;
                        hash    = decoded;
                        return(true);
                    }
                    else
                    {
                        hash = null;
                        return(false);
                    }
                }
            }
            catch (Exception) { }

            hash = null;
            return(false);
        }