Exemplo n.º 1
0
        internal bool VerifyType(string address, PubkeyScriptType scrType, out byte[] result)
        {
            result = null;
            try
            {
                switch (scrType)
                {
                case PubkeyScriptType.P2PKH:
                    byte[] decoded = b58Encoder.DecodeWithCheckSum(address);
                    if (decoded[0] != versionByte_P2pkh_MainNet &&
                        decoded[0] != versionByte_P2pkh_TestNet &&
                        decoded[0] != versionByte_P2pkh_RegTest &&
                        decoded.Length != hashFunc.HashByteSize)
                    {
                        return(false);
                    }
                    result = decoded.SubArray(1);
                    return(true);

                case PubkeyScriptType.P2SH:
                    decoded = b58Encoder.DecodeWithCheckSum(address);
                    if (decoded[0] != versionByte_P2sh_MainNet &&
                        decoded[0] != versionByte_P2sh_TestNet &&
                        decoded[0] != versionByte_P2sh_RegTest &&
                        decoded.Length != hashFunc.HashByteSize)
                    {
                        return(false);
                    }
                    result = decoded.SubArray(1);
                    return(true);

                case PubkeyScriptType.P2WPKH:
                    decoded = b32Encoder.Decode(address, out byte witVer, out string hrp);
                    if (witVer != 0 ||
                        decoded.Length != hashFunc.HashByteSize)
                    {
                        return(false);
                    }
                    result = decoded;
                    return(true);

                case PubkeyScriptType.P2WSH:
                    decoded = b32Encoder.Decode(address, out witVer, out hrp);
                    if (witVer != 0 ||
                        decoded.Length != witHashFunc.HashByteSize)
                    {
                        return(false);
                    }
                    result = decoded;
                    return(true);

                default:
                    return(false);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        public void DecodeTest(byte[] expBa, byte expWitVer, string expHrp, string bechStr)
        {
            byte[] actualBa = encoder.Decode(bechStr, out byte actualWitVer, out string actualHrp);

            Assert.Equal(expBa, actualBa);
            Assert.Equal(expWitVer, actualWitVer);
            Assert.Equal(expHrp, actualHrp);
        }
Exemplo n.º 3
0
 public void TestValidChecksum()
 {
     foreach (var t in ValidCheckSum)
     {
         Bech32.Decode(t);
     }
 }
Exemplo n.º 4
0
 public void TestInvalidChecksum()
 {
     foreach (var(t, i) in InvalidChecksum.Select((value, i) => (value, i)))
     {
         Console.WriteLine($"i: {i}");
         Assert.Throws <Exception>(() => Bech32.Decode(t));
     }
 }
        /// <summary>
        /// Converts the special bech-32 encoded private key back to its byte array representation.
        /// </summary>
        /// <param name="bech">Special bech-32 encoded private key</param>
        /// <param name="scrT">Corresponding script type</param>
        /// <param name="hrp">Human readable part</param>
        /// <param name="creationDate">The time this key was created (can help speed up re-scanning)</param>
        /// <returns>Private key as an array of bytes</returns>
        public byte[] Decode(string bech, out ScriptType scrT, out string hrp, out DateTime creationDate)
        {
            var data = encoder.Decode(bech, out byte ver, out hrp);

            scrT         = (ScriptType)ver;
            creationDate = UnixTimeStamp.EpochToTime(data[32] |
                                                     (long)data[33] << 8 |
                                                     (long)data[34] << 16 |
                                                     (long)data[35] << 24 |
                                                     (long)data[36] << 32);
            return(data.SubArray(0, 32));
        }
Exemplo n.º 6
0
        public AddressType GetAddressType(string address)
        {
            if (string.IsNullOrWhiteSpace(address))
            {
                return(AddressType.Invalid);
            }

            try
            {
                byte[] decoded = Base58.DecodeWithChecksum(address);
                if (decoded.Length == Hash160.HashByteSize + 1)
                {
                    if (decoded[0] == versionByte_P2pkh_MainNet ||
                        decoded[0] == versionByte_P2pkh_TestNet ||
                        decoded[0] == versionByte_P2pkh_RegTest)
                    {
                        return(AddressType.P2PKH);
                    }
                    else if (decoded[0] == versionByte_P2sh_MainNet ||
                             decoded[0] == versionByte_P2sh_TestNet ||
                             decoded[0] == versionByte_P2sh_RegTest)
                    {
                        return(AddressType.P2SH);
                    }
                }

                return(AddressType.Invalid);
            }
            catch (Exception) { }

            try
            {
                byte[] decoded = Bech32.Decode(address, Bech32.Mode.B32, out byte witVer, out string hrp);

                if (witVer == 0 &&
                    hrp == hrp_MainNet || hrp == hrp_TestNet || hrp == hrp_RegTest)
                {
                    if (decoded.Length == Hash160.HashByteSize)
                    {
                        return(AddressType.P2WPKH);
                    }
                    else if (decoded.Length == witHashFunc.BlockByteSize)
                    {
                        return(AddressType.P2WSH);
                    }
                }
            }
            catch (Exception) { }

            return(AddressType.Invalid);
        }
Exemplo n.º 7
0
        public void Decode_ExceptionTests(string bech, Bech32.Mode mode, byte expWitVer, string expHrp, string expErr)
        {
            byte      actWitVer = 255;
            string    actHrp    = ".";
            Exception ex        = Assert.Throws <FormatException>(() => Bech32.Decode(bech, mode, out actWitVer, out actHrp));

            Assert.Equal(expWitVer, actWitVer);
            Assert.Equal(expHrp, actHrp);
            Assert.Contains(expErr, ex.Message);

            bool b = Bech32.TryDecode(bech, mode, out byte[] result, out actWitVer, out actHrp);

            Assert.False(b);
            Assert.Null(result);
            Assert.Equal(0, actWitVer);
        }
Exemplo n.º 8
0
        public Bech32Address(string addr)
        {
            var data = Bech32.Decode(addr);

            WitnessVersion = data.Item2[0];
            Hrp            = data.Item1;

            //Decoded bytes contains version byte, remove this
            var pg = new byte[data.Item2.Length - 1];

            Array.Copy(data.Item2, 1, pg, 0, pg.Length);

            //convert back to witness program
            WitnessProgram = ConvertBits(pg, 5, 8, false);
            Network        = GetNetworkFromHrp(Hrp);

            ValidateAddress();
        }
Exemplo n.º 9
0
        public void Bech32_Parse()
        {
            //from BIP 0173
            var main_p2wpkh = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4";
            var test_p2wpkh = "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx";
            var main_p2wsh  = "bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3";
            var test_p2wsh  = "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7";

            //check for exceptions
            Bech32.Decode(main_p2wpkh);
            Bech32.Decode(test_p2wpkh);
            Bech32.Decode(main_p2wsh);
            Bech32.Decode(test_p2wsh);

            //verify checksum fails                  ↓ this 'a' is supposed to be 'r'
            var check_fail = "bc1qw508d6qejxtdg4y5r3aarvary0c5xw7kv8f3t4";

            Assert.ThrowsAny <Exception>(() => Bech32.Decode(check_fail));
        }
Exemplo n.º 10
0
 private static bool ValidateBech32Address(string address)
 {
     return(Bech32.Decode(address) != null);
 }