public byte[] TxSignature(ImmutableArray<byte> scriptPubKey, Transaction tx, int inputIndex, byte hashType) { ///TODO Debug.Assert(inputIndex < tx.Inputs.Length); // Blank out other inputs' signatures var empty = ImmutableArray.Create<byte>(); var newInputs = new TxInput[tx.Inputs.Length]; for (var i = 0; i < tx.Inputs.Length; i++) { var oldInput = tx.Inputs[i]; var newInput = oldInput.With(scriptSignature: i == inputIndex ? scriptPubKey : empty); newInputs[i] = newInput; } //// Blank out some of the outputs //if ((hashType & 0x1F) == (int)ScriptHashType.SIGHASH_NONE) //{ // //TODO // Debug.Assert(false); // // Wildcard payee // // Let the others update at will //} //else if ((hashType & 0x1F) == (int)ScriptHashType.SIGHASH_SINGLE) //{ // //TODO // Debug.Assert(false); // // Only lock-in the txout payee at same index as txin // // Let the others update at will //} //// Blank out other inputs completely, not recommended for open transactions //if ((hashType & 0x80) == (int)ScriptHashType.SIGHASH_ANYONECANPAY) //{ // //TODO // Debug.Assert(false); //} // create simplified transaction var newTx = tx.With(Inputs: newInputs.ToImmutableArray()); // return wire-encoded simplified transaction with the 4-byte hashType tacked onto the end var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream)) { writer.WriteBytes(DataCalculator.EncodeTransaction(newTx)); writer.Write4Bytes(hashType); return stream.ToArray(); } }
private static ImmutableArray<byte> GetScriptFromInputPrevOutput(TxInput input, TxOutput prevOutput) { return input.ScriptSignature.AddRange(prevOutput.ScriptPublicKey); }
public static byte[] EncodeTxInput(TxInput txInput) { var stream = new MemoryStream(); EncodeTxInput(stream, txInput); return stream.ToArray(); }
public static void EncodeTxInput(Stream stream, TxInput txInput) { using (var writer = new BinaryWriter(stream, Encoding.ASCII, leaveOpen: true)) { writer.Write32Bytes(txInput.PreviousTxOutputKey.TxHash); writer.Write4Bytes(txInput.PreviousTxOutputKey.TxOutputIndex); writer.WriteVarBytes(txInput.ScriptSignature.ToArray()); writer.Write4Bytes(txInput.Sequence); } }