public static void Sign(this TxIn txin, Transaction tx, TxOut prevOut, PrivateKey key, HashType hashType = HashType.SIGHASH_ALL)
        {
            SHA256 sha256 = new SHA256Managed();
            UInt32 txInIndex;
            for (txInIndex = 0; txInIndex < tx.inputs.Length; txInIndex++)
            {
                if (TxIn.ReferenceEquals(txin, tx.inputs[txInIndex]))
                    break;
            }
            if (txInIndex == tx.inputs.Length)
                throw new ArgumentException("Input not part of transaction.");

            // Only know how to sign if output does not contain OP_CODESEPERATOR for now.
            Script subScript = new Script(prevOut.scriptPubKey);
            Transaction txCopy = tx.CopyForSigning(txInIndex, subScript, hashType);

            Byte[] txHash = txCopy.ToBytes().Concat(new Byte[] { (Byte)hashType, 0x00, 0x00, 0x00 }).ToArray();
            txHash = sha256.ComputeHash(sha256.ComputeHash(txHash));

            Byte[] sig = key.Sign(txHash);
            sig = sig.Concat(new Byte[] { (Byte)hashType }).ToArray();

            Script s = new Script();

            if (subScript.IsPayToAddress())
            {
                s.elements.Add(new ScriptElement(sig));
                s.elements.Add(new ScriptElement(key.pubKey.ToBytes()));
            }
            else if (subScript.IsPayToPublicKey())
            {
                s.elements.Add(new ScriptElement(sig));
            }
            else
            {
                throw new ArgumentException("Unrecognized TxOut Script");
            }

            txin.scriptSig = s.ToBytes();
        }
 public static Address FromScript(Byte[] b)
 {
     Script s = new Script(b);
     if (s.IsPayToAddress())
         return new Address(s.elements[s.elements.Count - 3].data, PUBKEYHASH);
     if (s.IsPayToScriptHash())
         return new Address(s.elements[s.elements.Count - 2].data, SCRIPTHASH);
     if (s.IsPayToPublicKey())
         return new Address(s.elements[s.elements.Count - 2].data, PUBKEY);
     return null;
 }