public static int GetSerializedSize(this IBitcoinSerializable serializable, ProtocolVersion version, SerializationType serializationType) { var ms = new MemoryStream(); BitcoinStream s = new BitcoinStream(ms, true); s.Type = serializationType; s.ReadWrite(serializable); return((int)ms.Length); }
/// <summary> /// Gets serialized size of <paramref name="data"/> in bytes. /// </summary> /// <param name="network">The blockchain network.</param> /// <param name="data">Data that we calculate serialized size of.</param> /// <param name="options">Serialization options.</param> /// <returns>Serialized size of <paramref name="data"/> in bytes.</returns> public static int GetSize(Network network, IBitcoinSerializable data, TransactionOptions options) { var bms = new BitcoinStream(Stream.Null, true); bms.TransactionOptions = options; bms.ConsensusFactory = network.Consensus.ConsensusFactory; data.ReadWrite(bms); return((int)bms.Counter.WrittenBytes); }
public static void ReadWrite(this IBitcoinSerializable serializable, Stream stream, bool serializing, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION) { BitcoinStream s = new BitcoinStream(stream, serializing) { ProtocolVersion = version }; serializable.ReadWrite(s); }
/// <summary> /// Gets serialized size of <paramref name="data"/> in bytes. /// </summary> /// <param name="data">Data that we calculate serialized size of.</param> /// <param name="options">Serialization options.</param> /// <returns>Serialized size of <paramref name="data"/> in bytes.</returns> private int GetSize(IBitcoinSerializable data, NetworkOptions options) { var bms = new BitcoinStream(Stream.Null, true); bms.TransactionOptions = options; bms.ConsensusFactory = this.network.Consensus.ConsensusFactory; data.ReadWrite(bms); return((int)bms.Counter.WrittenBytes); }
public override bool TryCreateNew(Type type, out IBitcoinSerializable result) { if (IsHeadersPayload(type)) { result = CreateHeadersPayload(); return(true); } return(base.TryCreateNew(type, out result)); }
public static void ReadWrite(this IBitcoinSerializable serializable, Stream stream, bool serializing, uint?version = null) { var s = new BitcoinStream(stream, serializing) { ProtocolVersion = version }; serializable.ReadWrite(s); }
byte[] NBitcoinSerialize(object obj) { IBitcoinSerializable serializable = obj as IBitcoinSerializable; if (serializable != null) { return(serializable.ToBytes()); } throw new NotSupportedException(); }
public static int GetSerializedSize(this IBitcoinSerializable serializable, ProtocolVersion version, SerializationType serializationType) { var bitcoinStream = new BitcoinStream(Stream.Null, true) { Type = serializationType }; bitcoinStream.ReadWrite(serializable); return((int)bitcoinStream.Counter.WrittenBytes); }
public static byte[] ToBytes(this IBitcoinSerializable serializable, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION) { MemoryStream ms = new MemoryStream(); serializable.ReadWrite(new BitcoinStream(ms, true) { ProtocolVersion = version }); return(ToArrayEfficient(ms)); }
public static void ReadWrite(this IBitcoinSerializable serializable, Stream stream, bool serializing, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION, Network network = null) { network = network ?? Network.Main; serializable.ReadWrite(new BitcoinStream(stream, serializing) { ProtocolVersion = version, ConsensusFactory = network.Consensus.ConsensusFactory }); }
public static byte[] ToBytes(this IBitcoinSerializable serializable, uint?version = null) { var ms = new MemoryStream(); serializable.ReadWrite(new BitcoinStream(ms, true) { ProtocolVersion = version }); return(ToArrayEfficient(ms)); }
public static int GetSerializedSize(this IBitcoinSerializable serializable, TransactionOptions options) { var bitcoinStream = new BitcoinStream(Stream.Null, true) { TransactionOptions = options }; serializable.ReadWrite(bitcoinStream); return((int)bitcoinStream.Counter.WrittenBytes); }
public static int GetSerializedSize(this IBitcoinSerializable serializable) { var bitcoinStream = new BitcoinStream(Stream.Null, true) { Type = SerializationType.Disk, }; bitcoinStream.ReadWrite(serializable); return((int)bitcoinStream.Counter.WrittenBytes); }
public static int GetSerializedSize(this IBitcoinSerializable serializable, ConsensusFactory consensusFactory, uint version, SerializationType serializationType) { var bitcoinStream = new BitcoinStream(Stream.Null, true, consensusFactory, version) { Type = serializationType }; bitcoinStream.ReadWrite(serializable); return((int)bitcoinStream.Counter.WrittenBytes); }
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); }
public static void FromBytes(this IBitcoinSerializable serializable, byte[] bytes, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION, NetworkOptions options = null) { var bms = new BitcoinStream(bytes) { ProtocolVersion = version, TransactionOptions = options }; serializable.ReadWrite(bms); }
/// <summary> /// Gets serialized size of <paramref name="data"/> in bytes. /// </summary> /// <param name="data">Data that we calculate serialized size of.</param> /// <param name="options">Serialization options.</param> /// <returns>Serialized size of <paramref name="data"/> in bytes.</returns> public static int GetSize(this IBitcoinSerializable data, TransactionOptions options, ConsensusFactory consensusFactory) { var bms = new BitcoinStream(Stream.Null, true, consensusFactory) { TransactionOptions = options, }; data.ReadWrite(bms); return((int)bms.Counter.WrittenBytes); }
public static int GetSerializedSize(this IBitcoinSerializable serializable, uint?version, SerializationType serializationType) { var s = new BitcoinStream(Stream.Null, true) { Type = serializationType, ProtocolVersion = version }; s.ReadWrite(serializable); return((int)s.Counter.BytesWritten); }
public static int GetSerializedSize(this IBitcoinSerializable serializable, TransactionOptions options) { var bitcoinStream = new BitcoinStream(Stream.Null, true) { ConsensusFactory = new DefaultConsensusFactory(), }; bitcoinStream.TransactionOptions = options; serializable.ReadWrite(bitcoinStream); return((int)bitcoinStream.Counter.WrittenBytes); }
public static byte[] ToBytes(this IBitcoinSerializable serializable, uint version) { using (var ms = new MemoryStream()) { var bms = new BitcoinStream(ms, true, version); serializable.ReadWrite(bms); return(ToArrayEfficient(ms)); } }
public void Put(string key, IBitcoinSerializable obj) { try { PutAsync(key, obj).Wait(); } catch (AggregateException aex) { ExceptionDispatchInfo.Capture(aex.InnerException).Throw(); } }
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); }
public static string ToHex(this IBitcoinSerializable serializable, SerializationType serializationType = SerializationType.Disk) { using (var memoryStream = new MemoryStream()) { BitcoinStream bitcoinStream = new BitcoinStream(memoryStream, true); bitcoinStream.Type = serializationType; bitcoinStream.ReadWrite(serializable); memoryStream.Seek(0, SeekOrigin.Begin); var bytes = memoryStream.ReadBytes((int)memoryStream.Length); return(DataEncoders.Encoders.Hex.EncodeData(bytes)); } }
public static void FromBytes(this IBitcoinSerializable serializable, byte[] bytes, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION, Network network = null) { network = network ?? Network.Main; var bms = new BitcoinStream(bytes) { ProtocolVersion = version, ConsensusFactory = network.Consensus.ConsensusFactory }; serializable.ReadWrite(bms); }
public static void FromBytes(this IBitcoinSerializable serializable, byte[] bytes, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION) { using (var ms = new MemoryStream(bytes)) { var bitcoinStream = new BitcoinStream(ms, false) { ProtocolVersion = version }; serializable.ReadWrite(bitcoinStream); } }
public static void ReadWrite(this IBitcoinSerializable serializable, byte[] bytes, ConsensusFactory consensusFactory) { if (consensusFactory == null) { throw new ArgumentException("{0} cannot be null", nameof(consensusFactory)); } using (var stream = new MemoryStream(bytes)) { serializable.ReadWrite(new BitcoinStream(stream, false, consensusFactory)); } }
public bool TryCreateNew <T>(out T result) where T : IBitcoinSerializable { result = default(T); IBitcoinSerializable r = null; var success = TryCreateNew(typeof(T), out r); if (success) { result = (T)r; } return(success); }
public static void ReadWrite(this IBitcoinSerializable serializable, Stream stream, bool serializing, ConsensusFactory consensusFactory, ProtocolVersion protocolVersion = ProtocolVersion.PROTOCOL_VERSION) { if (consensusFactory == null) { throw new ArgumentException("{0} cannot be null", nameof(consensusFactory)); } serializable.ReadWrite(new BitcoinStream(stream, serializing) { ProtocolVersion = protocolVersion, ConsensusFactory = consensusFactory }); }
public static void ReadWrite(this IBitcoinSerializable serializable, Stream stream, bool serializing, ConsensusFactory consensusFactory, uint?version = null) { BitcoinStream s = new BitcoinStream(stream, serializing) { ProtocolVersion = version }; if (consensusFactory != null) { s.ConsensusFactory = consensusFactory; } serializable.ReadWrite(s); }
public static byte[] ToBytes(this IBitcoinSerializable serializable, ConsensusFactory consensusFactory, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION) { using (var ms = new MemoryStream()) { var bms = new BitcoinStream(ms, true) { ProtocolVersion = version, ConsensusFactory = consensusFactory ?? Network.Main.Consensus.ConsensusFactory }; serializable.ReadWrite(bms); return(ToArrayEfficient(ms)); } }
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()); }
protected BitcoinExtKeyBase(IBitcoinSerializable key, Network network) : base(key.ToBytes(), network) { }