Ejemplo n.º 1
0
        public BitcoinUrlBuilder(string uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            if (!uri.StartsWith("bitcoin:", StringComparison.OrdinalIgnoreCase))
            {
                throw new FormatException("Invalid scheme");
            }
            uri = uri.Remove(0, "bitcoin:".Length);
            if (uri.StartsWith("//"))
            {
                uri = uri.Remove(0, 2);
            }

            var    paramStart = uri.IndexOf('?');
            string address    = null;

            if (paramStart == -1)
            {
                address = uri;
            }
            else
            {
                address = uri.Substring(0, paramStart);
                uri     = uri.Remove(0, 1);             //remove ?
            }
            if (address != String.Empty)
            {
                Address = Network.CreateFromBase58Data <BitcoinAddress>(address);
            }
            uri = uri.Remove(0, address.Length);

            Dictionary <string, string> parameters;

            try
            {
                parameters = UriHelper.DecodeQueryParameters(uri);
            }
            catch (ArgumentException)
            {
                throw new FormatException("A URI parameter is duplicated");
            }
            if (parameters.ContainsKey("amount"))
            {
                Amount = Money.Parse(parameters["amount"]);
                parameters.Remove("amount");
            }
            if (parameters.ContainsKey("label"))
            {
                Label = parameters["label"];
                parameters.Remove("label");
            }
            if (parameters.ContainsKey("message"))
            {
                Message = parameters["message"];
                parameters.Remove("message");
            }
            if (parameters.ContainsKey("r"))
            {
                PaymentRequestUrl = new Uri(parameters["r"], UriKind.Absolute);
                parameters.Remove("r");
            }
            _UnknowParameters = parameters;
            var reqParam = parameters.Keys.FirstOrDefault(k => k.StartsWith("req-", StringComparison.OrdinalIgnoreCase));

            if (reqParam != null)
            {
                throw new FormatException("Non compatible required parameter " + reqParam);
            }
        }