Esempio n. 1
0
        public static T ToNetwork <T>(this T base58, Network network) where T : Base58Data
        {
            if (network == null)
            {
                throw new ArgumentNullException("network");
            }
            if (base58.Network == network)
            {
                return(base58);
            }
            if (base58 == null)
            {
                throw new ArgumentNullException("base58");
            }
            var inner = base58.ToBytes();

            if (base58.Type != Base58Type.COLORED_ADDRESS)
            {
                byte[] version   = network.GetVersionBytes(base58.Type);
                var    newBase58 = Encoders.Base58Check.EncodeData(version.Concat(inner).ToArray());
                return(Network.CreateFromBase58Data <T>(newBase58, network));
            }
            else
            {
                var colored = BitcoinColoredAddress.GetWrappedBase58(base58.ToWif(), base58.Network);
                var address = Network.CreateFromBase58Data <BitcoinAddress>(colored).ToNetwork(network);
                return((T)(object)address.ToColoredAddress());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Find automatically the data type and the network to which belong the base58 data
        /// </summary>
        /// <param name="base58">base58 data</param>
        /// <exception cref="System.FormatException">Invalid base58 data</exception>
        public static Base58Data CreateFromBase58Data(string base58, Network expectedNetwork = null)
        {
            if (base58 == null)
            {
                throw new ArgumentNullException("base58");
            }
            bool invalidNetwork = false;

            foreach (var network in GetNetworks())
            {
                var type = network.GetBase58Type(base58);
                if (type.HasValue)
                {
                    if (type.Value == Base58Type.COLORED_ADDRESS)
                    {
                        var wrapped     = BitcoinColoredAddress.GetWrappedBase58(base58, network);
                        var wrappedType = network.GetBase58Type(wrapped);
                        if (wrappedType == null)
                        {
                            continue;
                        }
                        var inner = network.CreateBase58Data(wrappedType.Value, wrapped);
                        if (inner.Network != network)
                        {
                            continue;
                        }
                    }
                    if (expectedNetwork != null && network != expectedNetwork)
                    {
                        invalidNetwork = true;
                        continue;
                    }
                    return(network.CreateBase58Data(type.Value, base58));
                }
            }
            if (invalidNetwork)
            {
                throw new FormatException("Invalid network");
            }
            throw new FormatException("Invalid base58 data");
        }