public Task ExecuteResultAsync(ActionContext context)
        {
            var bytes = _Data.ToBytes();

            context.HttpContext.Response.StatusCode = 200;
            context.HttpContext.Response.Body.Write(bytes, 0, bytes.Length);
            return(Task.CompletedTask);
        }
Exemple #2
0
        byte[] NBitcoinSerialize(object obj)
        {
            IBitcoinSerializable serializable = obj as IBitcoinSerializable;

            if (serializable != null)
            {
                return(serializable.ToBytes());
            }
            throw new NotSupportedException();
        }
        private async Task <T> SendAsync <T>(HttpMethod method, IBitcoinSerializable body, string relativePath, params object[] parameters) where T : IBitcoinSerializable, new()
        {
            var uri     = GetFullUri(relativePath, parameters);
            var message = new HttpRequestMessage(method, uri);

            if (body != null)
            {
                message.Content = new ByteArrayContent(body.ToBytes());
            }

            var result = await Client.SendAsync(message).ConfigureAwait(false);

            if (!result.IsSuccessStatusCode)
            {
                string error = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                if (!string.IsNullOrEmpty(error))
                {
                    throw new HttpRequestException(result.StatusCode + ": " + error);
                }
            }
            if (result.StatusCode == HttpStatusCode.NotFound)
            {
                return(default(T));
            }
            if (result.Content?.Headers?.ContentLength > MaxContentLength)
            {
                throw new IOException("Content is too big");
            }

            result.EnsureSuccessStatusCode();
            if (typeof(T) == typeof(byte[]))
            {
                return((T)(object)await result.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
            }
            var str = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (typeof(T) == typeof(string))
            {
                return((T)(object)str);
            }

            var bytes = await result.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

            if (bytes.Length == 0)
            {
                return(default(T));
            }
            var stream = new BitcoinStream(new MemoryStream(bytes), false);

            var data = new T();

            stream.ReadWrite <T>(ref data);
            return(data);
        }
Exemple #4
0
        private static HttpResponseMessage Response(IBitcoinSerializable obj)
        {
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(obj.ToBytes())
            };

            result.Content.Headers.ContentType =
                new MediaTypeHeaderValue("application/octet-stream");
            return(result);
        }
        internal static byte[] NBitcoinSerialize(object obj)
        {
            IBitcoinSerializable serializable = obj as IBitcoinSerializable;

            if (serializable != null)
            {
                return(serializable.ToBytes());
            }
            uint256 u = obj as uint256;

            if (u != null)
            {
                return(u.ToBytes());
            }
            throw new NotSupportedException();
        }
        /// <summary>
        /// Serializes object to a binary data format.
        /// </summary>
        /// <param name="obj">Object to be serialized.</param>
        /// <returns>Binary data representing the serialized object.</returns>
        internal byte[] Serializer(object obj)
        {
            IBitcoinSerializable serializable = obj as IBitcoinSerializable;

            if (serializable != null)
            {
                return(serializable.ToBytes(network: this.Network));
            }

            uint256 u256 = obj as uint256;

            if (u256 != null)
            {
                return(u256.ToBytes());
            }

            uint160 u160 = obj as uint160;

            if (u160 != null)
            {
                return(u160.ToBytes());
            }

            uint?u32 = obj as uint?;

            if (u32 != null)
            {
                return(u32.ToBytes());
            }

            object[] arr = obj as object[];
            if (arr != null)
            {
                byte[][] serializedItems = new byte[arr.Length][];
                int      itemIndex       = 0;
                foreach (object arrayObject in arr)
                {
                    byte[] serializedObject = this.Serializer(arrayObject);
                    serializedItems[itemIndex] = serializedObject;
                    itemIndex++;
                }
                return(ConcatArrays(serializedItems));
            }

            throw new NotSupportedException();
        }
        /// <summary>
        /// Serializes object to a binary data format.
        /// </summary>
        /// <param name="obj">Object to be serialized.</param>
        /// <returns>Binary data representing the serialized object.</returns>
        internal static byte[] NBitcoinSerialize(object obj)
        {
            IBitcoinSerializable serializable = obj as IBitcoinSerializable;

            if (serializable != null)
            {
                return(serializable.ToBytes());
            }

            uint256 u256 = obj as uint256;

            if (u256 != null)
            {
                return(u256.ToBytes());
            }
            uint160 u160 = obj as uint160;

            if (u160 != null)
            {
                return(u160.ToBytes());
            }
            uint?u32 = obj as uint?;

            if (u32 != null)
            {
                return(u32.ToBytes());
            }

            object[] a = obj as object[];
            if (a != null)
            {
                var result = (from x in a select NBitcoinSerialize(x)).ToArray().SelectMany(x => x).ToArray();
                return(result);
            }

            throw new NotSupportedException();
        }
Exemple #8
0
 public static string ToHex(this IBitcoinSerializable me)
 {
     return(ByteHelpers.ToHex(me.ToBytes()));
 }
Exemple #9
0
 public static int GetSerializedSize(this IBitcoinSerializable serializable, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION)
 {
     return(serializable.ToBytes(version).Length);
 }
Exemple #10
0
 public void Put(string key, IBitcoinSerializable obj)
 {
     PutBytes(key, obj == null ? null : obj.ToBytes());
 }
Exemple #11
0
 protected BitcoinExtKeyBase(IBitcoinSerializable key, Network network)
     : base(key.ToBytes(), network)
 {
 }
 public Task PutAsync(string key, IBitcoinSerializable obj)
 {
     return(PutBytes(key, obj == null ? null : obj.ToBytes(this.Network.Consensus.ConsensusFactory)));
 }
 internal static void WriteBuffer(System.IO.Stream data, IBitcoinSerializable serializable)
 {
     WriteBuffer(data, serializable.ToBytes());
 }
 private static string ToString(IBitcoinSerializable spend)
 {
     return(Encoders.Hex.EncodeData(spend.ToBytes()));
 }
Exemple #15
0
 public Task PutAsync(string key, IBitcoinSerializable obj)
 {
     return(PutBytes(key, obj == null ? null : obj.ToBytes(options: this.TransactionOptions)));
 }
Exemple #16
0
 public Task PutAsync(string key, IBitcoinSerializable obj)
 {
     return(PutBytes(key, obj == null ? null : obj.ToBytes()));
 }
Exemple #17
0
	    protected BitcoinExtKeyBase(IBitcoinSerializable key, Network network)
			: base(key.ToBytes(), network)
		{
		}
		private static string ToString(IBitcoinSerializable spend)
		{
			return Encoders.Hex.EncodeData(spend.ToBytes());
		}
 internal static void WriteBuffer(System.IO.Stream data, IBitcoinSerializable serializable)
 {
     WriteBuffer(data, serializable.ToBytes());
 }