Пример #1
0
 public Script GenerateScriptPubKey(CoinWitPubKeyAddress address)
 {
     if (address == null)
     {
         throw new ArgumentNullException("address");
     }
     return(GenerateScriptPubKey(address.Hash));
 }
        public static T Parse <T>(string str, Network expectedNetwork) where T : ICryptoCurrencyString
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            var networks = new[] { expectedNetwork };
            var maybeb58 = true;

            if (maybeb58)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    if (!Base58Encoder.pszBase58Chars.Contains(str[i]))
                    {
                        maybeb58 = false;
                        break;
                    }
                }
            }
            if (maybeb58)
            {
                try
                {
                    Encoders.Base58Check.DecodeData(str);
                }
                catch (FormatException)
                {
                    maybeb58 = false;
                }
                if (maybeb58)
                {
                    foreach (var candidate in GetCandidates(networks, str))
                    {
                        bool rightNetwork = (candidate.Network == expectedNetwork);
                        bool rightType    = candidate is T;
                        if (rightNetwork && rightType)
                        {
                            return((T)(object)candidate);
                        }
                    }
                    throw new FormatException("Invalid base58 string");
                }
            }

            foreach (var network in networks)
            {
                int i = BEACH32_WITNESS_PUBKEY_ADDRESS;
                foreach (var encoder in network.bech32Encoders)
                {
                    if (encoder == null)
                    {
                        continue;
                    }
                    var type = (Bech32Type)i;
                    try
                    {
                        byte   witVersion;
                        var    bytes     = encoder.Decode(str, out witVersion);
                        object candidate = null;

                        if (witVersion == 0 && bytes.Length == 20 && type == Network.BEACH32_WITNESS_PUBKEY_ADDRESS)
                        {
                            candidate = new CoinWitPubKeyAddress(str, network);
                        }
                        if (witVersion == 0 && bytes.Length == 32 && type == Network.BEACH32_WITNESS_SCRIPT_ADDRESS)
                        {
                            candidate = new CoinWitScriptAddress(str, network);
                        }

                        if (candidate is T)
                        {
                            return((T)(object)candidate);
                        }
                    }
                    catch (Bech32FormatException) { throw; }
                    catch (FormatException) { continue; }
                    i++;
                }
            }

            throw new FormatException("Invalid string");
        }