public void ReadWrite(CoinStream stream) { stream.ReadWriteAsVarString(ref vData); stream.ReadWrite(ref nHashFuncs); stream.ReadWrite(ref nTweak); stream.ReadWrite(ref nFlags); }
public void ReadWrite(CoinStream stream) { stream.ReadWrite(ref _PrefixByte); if (_PrefixByte < 0xFD) { _Value = _PrefixByte; } else if (_PrefixByte == 0xFD) { var value = (ushort)_Value; stream.ReadWrite(ref value); _Value = value; } else if (_PrefixByte == 0xFE) { var value = (uint)_Value; stream.ReadWrite(ref value); _Value = value; } else { var value = (ulong)_Value; stream.ReadWrite(ref value); _Value = value; } }
private static uint256 GetHash(CoinStream stream) { uint256 preimage = ((HashStream)stream.Inner).GetHash(); stream.Inner.Dispose(); return(preimage); }
// serialization implementation #region ICoinSerializable Members public void ReadWrite(CoinStream 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 override void ReadWrite(CoinStream stream) { base.ReadWrite(stream); if (Version > 1) { stream.ReadWrite(ref nTime); } }
public void ReadWrite(CoinStream stream) { stream.ReadWrite(ref vch); if (!stream.Serializing) { _ECKey = new ECKey(vch, true); } }
public override void ReadWrite(CoinStream stream) { stream.ReadWrite(ref nVersion); stream.ReadWrite(ref nTime); stream.ReadWrite <TxInList, TxIn>(ref vin); stream.ReadWrite <TxOutList, TxOut>(ref vout); vout.Transaction = this; stream.ReadWrite(ref nLockTime); }
internal uint256 GetHashOutputs() { uint256 hashOutputs; CoinStream ss = CreateHashWriter(HashVersion.Witness); foreach (TxOut txout in Outputs) { ss.ReadWrite(txout); } hashOutputs = GetHash(ss); return(hashOutputs); }
internal uint256 GetHashSequence() { uint256 hashSequence; CoinStream ss = CreateHashWriter(HashVersion.Witness); foreach (TxIn input in Inputs) { ss.ReadWrite((uint)input.Sequence); } hashSequence = GetHash(ss); return(hashSequence); }
internal uint256 GetHashPrevouts() { uint256 hashPrevouts; CoinStream ss = CreateHashWriter(HashVersion.Witness); foreach (TxIn input in Inputs) { ss.ReadWrite(input.PrevOut); } hashPrevouts = GetHash(ss); return(hashPrevouts); }
public void ReadWrite(CoinStream stream) { if (stream.Serializing) { var b = Value.ToBytes(); stream.ReadWrite(ref b); } else { byte[] b = new byte[WIDTH_BYTE]; stream.ReadWrite(ref b); _Value = new uint160(b); } }
public void ReadWrite(CoinStream stream) { var len = new VarInt((ulong)_Bytes.Length); stream.ReadWrite(ref len); if (!stream.Serializing) { if (len.ToLong() > (uint)stream.MaxArraySize) { throw new ArgumentOutOfRangeException("Array size not big"); } _Bytes = new byte[len.ToLong()]; } stream.ReadWrite(ref _Bytes); }
public void ReadWrite(CoinStream stream) { if (stream.Serializing) { ulong n = _Value; byte[] tmp = new byte[(_Size * 8 + 6) / 7]; int len = 0; while (true) { byte a = (byte)(n & 0x7F); byte b = (byte)(len != 0 ? 0x80 : 0x00); tmp[len] = (byte)(a | b); if (n <= 0x7F) { break; } n = (n >> 7) - 1; len++; } do { byte b = tmp[len]; stream.ReadWrite(ref b); } while(len-- != 0); } else { ulong n = 0; while (true) { byte chData = 0; stream.ReadWrite(ref chData); ulong a = (n << 7); byte b = (byte)(chData & 0x7F); n = (a | b); if ((chData & 0x80) != 0) { n++; } else { break; } } _Value = n; } }
private void PushDataToStream(byte[] data, Stream result) { var bitStream = new CoinStream(result, true); if (Code == OpcodeType.OP_0) { //OP_0 already pushed return; } if (OpcodeType.OP_1 <= Code && Code <= OpcodeType.OP_16) { //OP_1 to OP_16 already pushed return; } if (Code == OpcodeType.OP_1NEGATE) { //OP_1Negate already pushed return; } if (0x01 <= (byte)Code && (byte)Code <= 0x4b) { //Data length already pushed } else if (Code == OpcodeType.OP_PUSHDATA1) { bitStream.ReadWrite((byte)data.Length); } else if (Code == OpcodeType.OP_PUSHDATA2) { bitStream.ReadWrite((ushort)data.Length); } else if (Code == OpcodeType.OP_PUSHDATA4) { bitStream.ReadWrite((uint)data.Length); } else { throw new NotSupportedException("Data length should not be bigger than 0xFFFFFFFF"); } result.Write(data, 0, data.Length); }
public BlockHeader(byte[] data, IConsensusFactory consensusFactory) { if (data == null) { throw new ArgumentNullException(nameof(data)); } if (consensusFactory == null) { throw new ArgumentNullException(nameof(consensusFactory)); } CoinStream bs = new CoinStream(data) { ConsensusFactory = consensusFactory }; ReadWrite(bs); }
public BlockHeader(string hex, IConsensusFactory consensusFactory) { if (hex == null) { throw new ArgumentNullException(nameof(hex)); } if (consensusFactory == null) { throw new ArgumentNullException(nameof(consensusFactory)); } CoinStream bs = new CoinStream(Encoders.Hex.DecodeData(hex)) { ConsensusFactory = consensusFactory }; ReadWrite(bs); }
public byte[] ReadData(Stream stream) { uint len = 0; CoinStream bitStream = new CoinStream(stream, false); if (Code == 0) { return(new byte[0]); } if ((byte)OpcodeType.OP_1 <= (byte)Code && (byte)Code <= (byte)OpcodeType.OP_16) { return(new byte[] { (byte)(Code - OpcodeType.OP_1 + 1) }); } if (Code == OpcodeType.OP_1NEGATE) { return(new byte[] { 0x81 }); } try { if (0x01 <= (byte)Code && (byte)Code <= 0x4b) { len = (uint)Code; } else if (Code == OpcodeType.OP_PUSHDATA1) { len = bitStream.ReadWrite((byte)0); } else if (Code == OpcodeType.OP_PUSHDATA2) { len = bitStream.ReadWrite((ushort)0); } else if (Code == OpcodeType.OP_PUSHDATA4) { len = bitStream.ReadWrite((uint)0); } else { IsInvalid = true; return(new byte[0]); } byte[] data = null; if (len <= MAX_SCRIPT_ELEMENT_SIZE) //Most of the time { data = new byte[len]; var readen = stream.Read(data, 0, data.Length); if (readen != data.Length) { IsInvalid = true; return(new byte[0]); } } else //Mitigate against a big array allocation { List <byte> bytes = new List <byte>(); for (int i = 0; i < len; i++) { var b = stream.ReadByte(); if (b < 0) { IsInvalid = true; return(new byte[0]); } bytes.Add((byte)b); } data = bytes.ToArray(); } return(data); } catch (EndOfStreamException) { IsInvalid = true; return(new byte[0]); } }
public void ReadWrite(CoinStream stream) { stream.ReadWrite(ref vHave); }
//IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); ) public void ReadWrite(CoinStream stream) { stream.ReadWrite(ref hash); stream.ReadWrite(ref n); }
public uint256 GetSignatureHash(Script scriptCode, int nIn, SigHash nHashType, TxOut spentOutput, HashVersion sigversion, PrecomputedTransactionData precomputedTransactionData) { if (UsesForkId(nHashType)) { uint nForkHashType = (uint)nHashType; nForkHashType |= (uint)ForkID << 8; if (spentOutput?.Value == null || spentOutput.Value == TxOut.NullMoney) { throw new ArgumentException("The output being signed with the amount must be provided", nameof(spentOutput)); } uint256 hashPrevouts = uint256.Zero; uint256 hashSequence = uint256.Zero; uint256 hashOutputs = uint256.Zero; if ((nHashType & SigHash.AnyoneCanPay) == 0) { hashPrevouts = precomputedTransactionData == null? GetHashPrevouts() : precomputedTransactionData.HashPrevouts; } if ((nHashType & SigHash.AnyoneCanPay) == 0 && ((uint)nHashType & 0x1f) != (uint)SigHash.Single && ((uint)nHashType & 0x1f) != (uint)SigHash.None) { hashSequence = precomputedTransactionData == null? GetHashSequence() : precomputedTransactionData.HashSequence; } if (((uint)nHashType & 0x1f) != (uint)SigHash.Single && ((uint)nHashType & 0x1f) != (uint)SigHash.None) { hashOutputs = precomputedTransactionData == null? GetHashOutputs() : precomputedTransactionData.HashOutputs; } else if (((uint)nHashType & 0x1f) == (uint)SigHash.Single && nIn < Outputs.Count) { CoinStream ss = CreateHashWriter(sigversion); ss.ReadWrite(Outputs[nIn]); hashOutputs = GetHash(ss); } CoinStream sss = CreateHashWriter(sigversion); // Version sss.ReadWrite(Version); // Input prevouts/nSequence (none/all, depending on flags) sss.ReadWrite(hashPrevouts); sss.ReadWrite(hashSequence); // The input being signed (replacing the scriptSig with scriptCode + amount) // The prevout may already be contained in hashPrevout, and the nSequence // may already be contain in hashSequence. sss.ReadWrite(Inputs[nIn].PrevOut); sss.ReadWrite(scriptCode); sss.ReadWrite(spentOutput.Value.Satoshi); sss.ReadWrite((uint)Inputs[nIn].Sequence); // Outputs (none/one/all, depending on flags) sss.ReadWrite(hashOutputs); // Locktime sss.ReadWriteStruct(LockTime); // Sighash type sss.ReadWrite(nForkHashType); return(GetHash(sss)); } if (nIn >= Inputs.Count) { return(uint256.One); } SigHash hashType = nHashType & (SigHash)31; // Check for invalid use of SIGHASH_SINGLE if (hashType == SigHash.Single) { if (nIn >= Outputs.Count) { return(uint256.One); } } Script scriptCopy = scriptCode.Clone(); scriptCopy.FindAndDelete(OpcodeType.OP_CODESEPARATOR); if (!CustomTransaction.GetCustomTransaction(CurrencyID, out Transaction txCopy)) { txCopy = new Transaction(); } txCopy.FromBytes(this.ToBytes()); //Set all TxIn script to empty string foreach (TxIn txin in txCopy.Inputs) { txin.ScriptSig = new Script(); } //Copy subscript into the txin script you are checking txCopy.Inputs[nIn].ScriptSig = scriptCopy; if (hashType == SigHash.None) { //The output of txCopy is set to a vector of zero size. txCopy.Outputs.Clear(); //All other inputs aside from the current input in txCopy have their nSequence index set to zero foreach (TxIn input in txCopy.Inputs.Where((x, i) => i != nIn)) { input.Sequence = 0; } } else if (hashType == SigHash.Single) { //The output of txCopy is resized to the size of the current input index+1. txCopy.Outputs.RemoveRange(nIn + 1, txCopy.Outputs.Count - (nIn + 1)); //All other txCopy outputs aside from the output that is the same as the current input index are set to a blank script and a value of (long) -1. for (int i = 0; i < txCopy.Outputs.Count; i++) { if (i == nIn) { continue; } txCopy.Outputs[i] = new TxOut(); } //All other txCopy inputs aside from the current input are set to have an nSequence index of zero. foreach (TxIn input in txCopy.Inputs.Where((x, i) => i != nIn)) { input.Sequence = 0; } } if ((nHashType & SigHash.AnyoneCanPay) != 0) { //The txCopy input vector is resized to a length of one. TxIn script = txCopy.Inputs[nIn]; txCopy.Inputs.Clear(); txCopy.Inputs.Add(script); //The subScript (lead in by its length as a var-integer encoded!) is set as the first and only member of this vector. txCopy.Inputs[0].ScriptSig = scriptCopy; } //Serialize TxCopy, append 4 byte hashtypecode CoinStream stream = CreateHashWriter(sigversion); txCopy.ReadWrite(stream); //stream.ReadWrite(nForkHashType); return(GetHash(stream)); }
public void ReadWrite(CoinStream stream) { stream.ReadWrite(ref _value); }
public void ReadWrite(CoinStream stream) { stream.ReadWrite(ref header); stream.ReadWrite(ref _PartialMerkleTree); }