Esempio n. 1
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 inner = BitcoinAddress.Create(BitcoinColoredAddress.GetWrappedBase58(base58, network));
                        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");
        }
Esempio n. 2
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. 3
0
        static IEnumerable <IBase58Data> GetCandidates(IEnumerable <Network> networks, string base58)
        {
            if (base58 == null)
            {
                throw new ArgumentNullException("base58");
            }
            foreach (var network in networks)
            {
                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;
                        }
                        try
                        {
                            var inner = network.CreateBase58Data(wrappedType.Value, wrapped);
                            if (inner.Network != network)
                            {
                                continue;
                            }
                        }
                        catch (FormatException)
                        {
                        }
                    }

                    IBase58Data data = null;
                    try
                    {
                        data = network.CreateBase58Data(type.Value, base58);
                    }
                    catch (FormatException)
                    {
                    }

                    if (data != null)
                    {
                        yield return(data);
                    }
                }
            }
        }
Esempio n. 4
0
 public static T ToNetwork <T>(this T obj, Network network) where T : IBitcoinString
 {
     if (network == null)
     {
         throw new ArgumentNullException(nameof(network));
     }
     if (obj == null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     if (obj.Network == network)
     {
         return(obj);
     }
     if (obj is IBase58Data)
     {
         var b58 = (IBase58Data)obj;
         if (b58.Type != Base58Type.COLORED_ADDRESS)
         {
             byte[] version   = network.GetVersionBytes(b58.Type, true);
             var    enc       = network.NetworkStringParser.GetBase58CheckEncoder();
             var    inner     = enc.DecodeData(b58.ToString()).Skip(version.Length).ToArray();
             var    newBase58 = enc.EncodeData(version.Concat(inner).ToArray());
             return(Network.Parse <T>(newBase58, network));
         }
         else
         {
             var colored = BitcoinColoredAddress.GetWrappedBase58(obj.ToString(), obj.Network);
             var address = Network.Parse <BitcoinAddress>(colored, obj.Network).ToNetwork(network);
             return((T)(object)address.ToColoredAddress());
         }
     }
     else if (obj is IBech32Data)
     {
         var  b32     = (IBech32Data)obj;
         var  encoder = b32.Network.GetBech32Encoder(b32.Type, true);
         byte wit;
         var  data = encoder.Decode(b32.ToString(), out wit);
         encoder = network.GetBech32Encoder(b32.Type, true);
         var str = encoder.Encode(wit, data);
         return((T)(object)Network.Parse <T>(str, network));
     }
     else
     {
         throw new NotSupportedException();
     }
 }