예제 #1
0
        public static PaymentRequest Load(Stream source)
        {
            var result = new PaymentRequest();
            var req    = Serializer.Deserialize <Proto.PaymentRequest>(source);

            result.PKIType = ToPKIType(req.pki_type);
            if (req.pki_data != null && req.pki_data.Length != 0)
            {
                var  certs = Serializer.Deserialize <Proto.X509Certificates>(new MemoryStream(req.pki_data));
                bool first = true;
                foreach (var cert in certs.certificate)
                {
                    if (first)
                    {
                        first = false;
                        result.MerchantCertificate = new X509Certificate2(cert);
                    }
                    else
                    {
                        result.AdditionalCertificates.Add(new X509Certificate2(cert));
                    }
                }
            }
            result._PaymentDetails = PaymentDetails.Load(req.serialized_payment_details);
            result.Signature       = req.signature;
            result.OriginalData    = req;
            return(result);
        }
예제 #2
0
        public static PaymentRequest Load(Stream source, out byte[] signed)
        {
            RecorderStream record          = new RecorderStream(source);
            PaymentRequest req             = new PaymentRequest();
            var            reader          = new ProtobufReaderWriter(record);
            bool           signatureLoaded = false;
            int            key;
            bool           firstCert = true;

            while (reader.TryReadKey(out key))
            {
                switch (key)
                {
                case 1:
                    req.DetailsVersion = (uint)reader.ReadULong();
                    break;

                case 2:
                    req.PKIType = ToPKIType(reader.ReadString());
                    break;

                case 3:
                    var bytes = reader.ReadBytes();
                    ProtobufReaderWriter certs = new ProtobufReaderWriter(new MemoryStream(bytes));
                    int k;
                    while (certs.TryReadKey(out k))
                    {
                        if (firstCert)
                        {
                            req.MerchantCertificate = certs.ReadBytes();
                            firstCert = false;
                        }
                        else
                        {
                            req.AdditionalCertificates.Add(certs.ReadBytes());
                        }
                    }
                    break;

                case 4:
                    req._PaymentDetails = PaymentDetails.Load(reader.ReadBytes());
                    break;

                case 5:
                    record.Activated = false;
                    req.Signature    = reader.ReadBytes();
                    signatureLoaded  = req.Signature.Length != 0;
                    record.Activated = true;
                    record.RecordBytes(new byte[0]);
                    break;

                default:
                    break;
                }
            }
            signed = record.ToBytes();
            return(req);
        }
예제 #3
0
        private static PaymentDetails Load(Stream source)
        {
            var reader = new Protobuf.ProtobufReaderWriter(source);
            var result = new PaymentDetails();
            int key;

            while (reader.TryReadKey(out key))
            {
                switch (key)
                {
                case 1:
                    var network = reader.ReadString();
                    result.Network = network.Equals("main", StringComparison.OrdinalIgnoreCase) ? Network.Main :
                                     network.Equals("test", StringComparison.OrdinalIgnoreCase) ? Network.TestNet :
                                     network.Equals("regtest", StringComparison.OrdinalIgnoreCase) ? Network.RegTest : null;
                    if (result.Network == null)
                    {
                        throw new NotSupportedException("Invalid network");
                    }
                    break;

                case 2:
                    result.Outputs.Add(PaymentOutput.Load(reader.ReadBytes()));
                    break;

                case 3:
                    result.Time = Utils.UnixTimeToDateTime(reader.ReadULong());
                    break;

                case 4:
                    result.Expires = Utils.UnixTimeToDateTime(reader.ReadULong());
                    break;

                case 5:
                    result.Memo = reader.ReadString();
                    break;

                case 6:
                    result.PaymentUrl = new Uri(reader.ReadString());
                    break;

                case 7:
                    result.MerchantData = reader.ReadBytes();
                    break;

                default:
                    break;
                }
            }
            return(result);
        }
예제 #4
0
        private static PaymentDetails Load(Stream source)
        {
            var result  = new PaymentDetails();
            var details = PaymentRequest.Serializer.Deserialize <Proto.PaymentDetails>(source);

            result.Network = details.network == "main" ? Network.Main :
                             details.network == "test" ? Network.TestNet : null;
            if (result.Network == null)
            {
                throw new NotSupportedException("Invalid network");
            }
            result.Time         = Utils.UnixTimeToDateTime(details.time);
            result.Expires      = Utils.UnixTimeToDateTime(details.expires);
            result.Memo         = details.memoSpecified ? details.memo : null;
            result.MerchantData = details.merchant_dataSpecified ? details.merchant_data : null;
            result.PaymentUrl   = details.payment_urlSpecified ? new Uri(details.payment_url, UriKind.Absolute) : null;
            foreach (var output in details.outputs)
            {
                result.Outputs.Add(new PaymentOutput(output));
            }
            result.OriginalData = details;
            return(result);
        }