/// <summary> /// Decode - Decodes a raw bitcoin transaction /// </summary> private void Decode() { // tx version - uint32 TXVersion = ReadUInt(); // if the transaction has witness, next bytes will be 0001 if (ByteData[Offset] == 0 && ByteData[Offset + 1] == 1) { Offset += 2; HasWitness = true; } // get the number of inputs - vin length Inputs = new TXInput[ReadVarLenInt()]; // read all the inputs for (var i = 0; i < Inputs.Length; ++i) { Inputs[i] = new TXInput { Hash = ReadSlice(32), Index = ReadUInt(), Script = ReadSlice( (int)ReadVarLenInt()), // script length maximum is 520 bytes, so casting to int should be fine Sequence = ReadUInt() } } ; // get the number of inputs - vout length Outputs = new TXOutput[ReadVarLenInt()]; // read all the outputs for (var i = 0; i < Outputs.Length; ++i) { Outputs[i] = new TXOutput { Value = ReadUInt64(), Script = ReadSlice( (int)ReadVarLenInt()) // script length maximum is 520 bytes, so casting to int should be fine } } ; // if this is a segwit transaction, read in the witnesses if (HasWitness) { foreach (var input in Inputs) { input.Witness = ReadVector(); } } LockTime = ReadUInt(); // strict validation - we should be at the end of the transaction LengthMatch = Offset == ByteData.Length; } } }
/// <summary> /// Decode - Decodes a raw bitcoin transaction /// </summary> private Transaction DecodeTX() { var origOffset = Offset; // tx version - uint32 var txVersion = ReadUInt(); var hasWitness = false; // if the transaction has witness, next bytes will be 0001 if (ByteData[Offset] == 0 && ByteData[Offset + 1] == 1) { Offset += 2; hasWitness = true; } // get the number of inputs - vin length var inputs = new TXInput[ReadVarLenInt()]; // read all the inputs for (var i = 0; i < inputs.Length; ++i) { inputs[i] = new TXInput { Hash = ReadSlice(32), Index = ReadUInt(), Script = ReadSlice( (int)ReadVarLenInt()), // script length maximum is 520 bytes, so casting to int should be fine Sequence = ReadUInt() } } ; // get the number of inputs - vout length var outputs = new TXOutput[ReadVarLenInt()]; // read all the outputs for (var i = 0; i < outputs.Length; ++i) { outputs[i] = new TXOutput { Value = ReadUInt64(), Script = ReadSlice( (int)ReadVarLenInt()) // script length maximum is 520 bytes, so casting to int should be fine } } ; // if this is a segwit transaction, read in the witnesses if (hasWitness) { foreach (var input in inputs) { input.Witness = ReadVector(); } } // transaction lock_time var lockTime = ReadUInt(); // hash the entire transaction for the TXID var txLength = Offset - origOffset; Offset = origOffset; var byteData = ReadSlice(txLength); return(new Transaction(byteData, BlockHash, txVersion, hasWitness, inputs, outputs, lockTime) { FirstSeen = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), LastUpdated = DateTimeOffset.UtcNow.ToUnixTimeSeconds() }); } } }