Exemple #1
0
 public Script GenerateScriptPubKey(BitcoinWitScriptAddress address)
 {
     if (address == null)
     {
         throw new ArgumentNullException("address");
     }
     return(GenerateScriptPubKey(address.Hash));
 }
        public static T Parse <T>(string str, Network expectedNetwork) where T : IBitcoinString
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }

            IEnumerable <Network> networks = expectedNetwork == null?GetNetworks() : new[] { expectedNetwork };
            bool maybeb58 = true;

            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 (IBase58Data candidate in GetCandidates(networks, str))
                    {
                        bool rightNetwork = expectedNetwork == null || (candidate.Network == expectedNetwork);
                        bool rightType    = candidate is T;
                        if (rightNetwork && rightType)
                        {
                            return((T)(object)candidate);
                        }
                    }
                    throw new FormatException("Invalid base58 string");
                }
            }

            foreach (Network network in networks)
            {
                int i = -1;
                foreach (Bech32Encoder encoder in network.Bech32Encoders)
                {
                    i++;
                    if (encoder == null)
                    {
                        continue;
                    }
                    var type = (Bech32Type)i;
                    try
                    {
                        byte   witVersion;
                        byte[] bytes     = encoder.Decode(str, out witVersion);
                        object candidate = null;

                        if (witVersion == 0 && bytes.Length == 20 && type == Bech32Type.WITNESS_PUBKEY_ADDRESS)
                        {
                            candidate = new BitcoinWitPubKeyAddress(str, network);
                        }
                        if (witVersion == 0 && bytes.Length == 32 && type == Bech32Type.WITNESS_SCRIPT_ADDRESS)
                        {
                            candidate = new BitcoinWitScriptAddress(str, network);
                        }

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

            throw new FormatException("Invalid string");
        }
		public Script GenerateScriptPubKey(BitcoinWitScriptAddress address)
		{
			if(address == null)
				throw new ArgumentNullException("address");
			return GenerateScriptPubKey(address.Hash);
		}