private bool ReadScript(Script script) { try { var data = TxNullDataTemplate.Instance.ExtractScriptPubKeyParameters(script); if(data == null) return false; BitcoinStream stream = new BitcoinStream(data); ushort marker = 0; stream.ReadWrite(ref marker); if(marker != Tag) return false; stream.ReadWrite(ref _Version); if(_Version != 1) return false; ulong quantityCount = 0; stream.ReadWriteAsVarInt(ref quantityCount); Quantities = new ulong[quantityCount]; for(ulong i = 0 ; i < quantityCount ; i++) { Quantities[i] = ReadLEB128(stream); if(Quantities[i] > MAX_QUANTITY) return false; } stream.ReadWriteAsVarString(ref _Metadata); return true; } catch(Exception) { return false; } }
private bool ReadData(byte[] data) { try { BitcoinStream stream = new BitcoinStream(data); ushort marker = 0; stream.ReadWrite(ref marker); if(marker != Tag) return false; stream.ReadWrite(ref _Version); if(_Version != 1) return false; ulong quantityCount = 0; stream.ReadWriteAsVarInt(ref quantityCount); Quantities = new ulong[quantityCount]; for(ulong i = 0 ; i < quantityCount ; i++) { Quantities[i] = ReadLEB128(stream); if(Quantities[i] > MAX_QUANTITY) return false; } stream.ReadWriteAsVarString(ref _Metadata); if(stream.Inner.Position != data.Length) return false; return true; } catch(Exception) { return false; } }
// serialization implementation #region IBitcoinSerializable Members public void ReadWrite(BitcoinStream stream) { stream.ReadWrite(ref _TransactionCount); stream.ReadWrite(ref _Hashes); byte[] vBytes = null; if(!stream.Serializing) { stream.ReadWriteAsVarString(ref vBytes); BitWriter writer = new BitWriter(); for(int p = 0 ; p < vBytes.Length * 8 ; p++) writer.Write((vBytes[p / 8] & (1 << (p % 8))) != 0); _Flags = writer.ToBitArray(); } else { vBytes = new byte[(_Flags.Length + 7) / 8]; for(int p = 0 ; p < _Flags.Length ; p++) vBytes[p / 8] |= (byte)(ToByte(_Flags.Get(p)) << (p % 8)); stream.ReadWriteAsVarString(ref vBytes); } }
public byte[] ToBytes() { MemoryStream ms = new MemoryStream(); BitcoinStream stream = new BitcoinStream(ms, true); stream.ReadWrite(Tag); stream.ReadWrite(ref _Version); var quantityCount = (uint)this.Quantities.Length; stream.ReadWriteAsVarInt(ref quantityCount); for(int i = 0 ; i < quantityCount ; i++) { if(Quantities[i] > MAX_QUANTITY) throw new ArgumentOutOfRangeException("Quantity should not exceed " + Quantities[i]); WriteLEB128(Quantities[i], stream); } stream.ReadWriteAsVarString(ref _Metadata); return ms.ToArray(); }
public void ReadWrite(BitcoinStream stream) { stream.ReadWriteAsVarString(ref _Data); }
public void ReadWrite(BitcoinStream stream) { stream.ReadWrite(ref nTransactions); stream.ReadWrite(ref vHash); byte[] vBytes = null; if(!stream.Serializing) { stream.ReadWriteAsVarString(ref vBytes); PartialMerkleTree us = this; //Might need copy us.vBits = us.vBits.Take(vBytes.Length * 8).ToList(); for(int p = 0 ; p < us.vBits.Count ; p++) us.vBits[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0; us.fBad = false; } else { vBytes = new byte[(vBits.Count + 7) / 8]; for(int p = 0 ; p < vBits.Count ; p++) vBytes[p / 8] |= (byte)(ToByte(vBits[p]) << (p % 8)); stream.ReadWriteAsVarString(ref vBytes); } }
public void ReadWrite(BitcoinStream stream) { stream.ReadWriteAsVarString(ref vData); stream.ReadWrite(ref nHashFuncs); stream.ReadWrite(ref nTweak); stream.ReadWrite(ref nFlags); }
public void ReadWrite(BitcoinStream stream) { stream.ReadWriteAsVarString(ref this.signature); }
public void ReadWrite(BitcoinStream stream) { var b = _Block == null ? new uint256(0).ToBytes() : _Block.ToBytes(); stream.ReadWrite(ref b); if(!stream.Serializing) _Block = new uint256(b) == 0 ? null : new uint256(b); stream.ReadWrite(ref _Reason); stream.ReadWrite(ref _Spendable); var change = BalanceChange.Satoshi.ToByteArray(); stream.ReadWriteAsVarString(ref change); if(!stream.Serializing) _BalanceChange = new Money(new BigInteger(change)); }
public void ReadWrite(BitcoinStream stream) { stream.ReadWriteAsVarString(ref _EncryptedSolution); stream.ReadWrite(ref _KeyHash); }
public virtual void ReadWrite(BitcoinStream stream) { var version = stream.ProtocolVersion ?? 0; if (stream.Type == SerializationType.Disk) { stream.ReadWrite(ref version); } if ( stream.Type == SerializationType.Disk || (stream.ProtocolCapabilities.SupportTimeAddress && stream.Type != SerializationType.Hash)) { // The only time we serialize a CAddress object without nTime is in // the initial VERSION messages which contain two CAddress records. // At that point, the serialization version is INIT_PROTO_VERSION. // After the version handshake, serialization version is >= // MIN_PEER_PROTO_VERSION and all ADDR messages are serialized with // nTime. stream.ReadWrite(ref nTime); } if ((version & AddrV2Format) != 0) { stream.ReadWriteAsVarInt(ref services); } else { stream.ReadWrite(ref services); } var useV2Format = stream.ProtocolVersion is {} protcocolVersion&& (protcocolVersion & AddrV2Format) != 0; if (useV2Format) { if (stream.Serializing) { var bip155net = (byte)GetBIP155Network(); stream.ReadWrite(bip155net); stream.ReadWriteAsVarString(ref addr); } else { stream.ReadWrite(ref network); stream.ReadWriteAsVarString(ref addr); if (SetNetFromBIP155Network((BIP155Network)network, addr.Length)) { if ((BIP155Network)network == BIP155Network.IPv6) { if (HasPrefix(addr, IPV4_IN_IPV6_PREFIX) || HasPrefix(addr, TORV2_IN_IPV6_PREFIX)) { // set to invalid because IPv4 and Torv2 should not be embeded in IPv6. addr = IPV6_NONE; network = (byte)Network.IPv6; } } } else { // set to invalid because IPv4 and Torv2 should not be embeded in IPv6. addr = IPV6_NONE; network = (byte)Network.IPv6; } } } else { if (stream.Serializing) { var localAddr = IsAddrV1Compatible ? SerializeV1Array() : IPV6_NONE; stream.ReadWrite(ref localAddr); } else { var localAddr = new byte[ADDR_IPV6_SIZE]; stream.ReadWrite(ref localAddr); SetLegacyIpv6(localAddr); } } using (stream.BigEndianScope()) { stream.ReadWrite(ref port); } }
internal void ReadWrite(BitcoinStream stream) { for (int i = 0; i < _Inputs.Count; i++) { if (stream.Serializing) { var bytes = ((ElementsTxIn)_Inputs[i]).IssuanceAmountRangeProof; stream.ReadWriteAsVarString(ref bytes); bytes = ((ElementsTxIn)_Inputs[i]).InflationKeysRangeProof; stream.ReadWriteAsVarString(ref bytes); bytes = (_Inputs[i].WitScript ?? WitScript.Empty).ToBytes(); stream.ReadWrite(ref bytes); bytes = (((ElementsTxIn)_Inputs[i]).PeginWitScript ?? WitScript.Empty).ToBytes(); stream.ReadWrite(ref bytes); } else { byte[] bytes = null; stream.ReadWriteAsVarString(ref bytes); ((ElementsTxIn)_Inputs[i]).IssuanceAmountRangeProof = bytes; bytes = null; stream.ReadWriteAsVarString(ref bytes); ((ElementsTxIn)_Inputs[i]).InflationKeysRangeProof = bytes; ((ElementsTxIn)_Inputs[i]).WitScript = WitScript.Load(stream); ((ElementsTxIn)_Inputs[i]).PeginWitScript = WitScript.Load(stream); } } for (int i = 0; i < _Outputs.Count; i++) { if (stream.Serializing) { var bytes = ((ElementsTxOut)_Outputs[i]).SurjectionProof; stream.ReadWriteAsVarString(ref bytes); bytes = ((ElementsTxOut)_Outputs[i]).RangeProof; stream.ReadWriteAsVarString(ref bytes); } else { byte[] bytes = null; stream.ReadWriteAsVarString(ref bytes); ((ElementsTxOut)_Outputs[i]).SurjectionProof = bytes; bytes = null; stream.ReadWriteAsVarString(ref bytes); ((ElementsTxOut)_Outputs[i]).RangeProof = bytes; } } if (IsNull()) { throw new FormatException("Superfluous witness record"); } }
public Script GetScript() { MemoryStream ms = new MemoryStream(); BitcoinStream stream = new BitcoinStream(ms, true); stream.ReadWrite(Tag); stream.ReadWrite(ref _Version); var quantityCount = (uint)this.Quantities.Length; stream.ReadWriteAsVarInt(ref quantityCount); for(int i = 0 ; i < quantityCount ; i++) { if(Quantities[i] > MAX_QUANTITY) throw new ArgumentOutOfRangeException("Quantity should not exceed " + Quantities[i]); WriteLEB128(Quantities[i], stream); } stream.ReadWriteAsVarString(ref _Metadata); return TxNullDataTemplate.Instance.GenerateScriptPubKey(ms.ToArray()); }
public override void ReadWriteCore(BitcoinStream stream) { stream.ReadWriteAsVarString(ref this.data); }
public override void ReadWriteCore(BitcoinStream stream) { stream.ReadWriteAsVarString(ref _Data); }
public void ReadWrite(BitcoinStream stream) { stream.ReadWrite(ref _Puzzle); stream.ReadWriteAsVarString(ref _Promise); }