public static TransactionOutput Create(Fixed8 value, string address) { if (address == null) throw new ArgumentNullException(nameof(address)); bool p2sh; UInt160 hash = Wallet.ToScriptHash(address, out p2sh); using (ScriptBuilder sb = new ScriptBuilder()) { if (p2sh) { sb.Add(ScriptOp.OP_HASH160); sb.Push(hash.ToArray()); sb.Add(ScriptOp.OP_EQUAL); } else { sb.Add(ScriptOp.OP_DUP); sb.Add(ScriptOp.OP_HASH160); sb.Push(hash.ToArray()); sb.Add(ScriptOp.OP_EQUALVERIFY); sb.Add(ScriptOp.OP_CHECKSIG); } return new TransactionOutput { Value = value, Script = sb.ToArray() }; } }
public Transaction SignTransaction(UnsignedTransaction utx) { const HashType hashType = HashType.SIGHASH_ALL; Transaction tx = new Transaction { Version = utx.Version, Inputs = new TransactionInput[utx.Inputs.Length], Outputs = utx.Outputs, LockTime = utx.LockTime }; for (uint i = 0; i < utx.Inputs.Length; i++) { tx.Inputs[i] = new TransactionInput { PrevHash = utx.Inputs[i].TxId, PrevIndex = utx.Inputs[i].Index, Sequence = uint.MaxValue }; if (utx.Inputs[i].ScriptHash == null) return null; WalletEntry entry = FindEntry(utx.Inputs[i].ScriptHash); if (entry == null) return null; BigInteger[] signature; using (entry.Decrypt()) { ECDsa signer = new ECDsa(entry.PrivateKey, ECCurve.Secp256k1); signature = signer.GenerateSignature(utx.GetHashForSigning(hashType, i)); } byte[] sigEncoded; using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms)) { byte[] r = signature[0].ToByteArray().Reverse().ToArray(); byte[] s = signature[1].ToByteArray().Reverse().ToArray(); writer.Write((byte)0x30); writer.Write((byte)(2 + r.Length + 2 + s.Length)); writer.Write((byte)0x02); writer.Write((byte)r.Length); writer.Write(r); writer.Write((byte)0x02); writer.Write((byte)s.Length); writer.Write(s); writer.Write((byte)hashType); writer.Flush(); sigEncoded = ms.ToArray(); } using (ScriptBuilder sb = new ScriptBuilder()) { sb.Push(sigEncoded); sb.Push(entry.PublicKey.EncodePoint(entry.Compressed)); tx.Inputs[i].Script = sb.ToArray(); } } return tx; }