TX_NULL_DATA,
};
+ /// <summary>
+ /// Signature hash types/flags
+ /// </summary>
+ public enum sigflag
+ {
+ SIGHASH_ALL = 1,
+ SIGHASH_NONE = 2,
+ SIGHASH_SINGLE = 3,
+ SIGHASH_ANYONECANPAY = 0x80,
+ };
+
public static class ScriptCode
{
+ public static string GetTxnOutputType(txnouttype t)
+ {
+ switch (t)
+ {
+ case txnouttype.TX_NONSTANDARD: return "nonstandard";
+ case txnouttype.TX_PUBKEY: return "pubkey";
+ case txnouttype.TX_PUBKEYHASH: return "pubkeyhash";
+ case txnouttype.TX_SCRIPTHASH: return "scripthash";
+ case txnouttype.TX_MULTISIG: return "multisig";
+ case txnouttype.TX_NULL_DATA: return "nulldata";
+ }
+ return string.Empty;
+ }
/// <summary>
/// Get the name of supplied opcode
return -1;
}
-
+ /// <summary>
+ /// Is it a standart type of scriptPubKey?
+ /// </summary>
+ /// <param name="scriptPubKey">CScript instance</param>
+ /// <param name="whichType">utut type</param>
+ /// <returns>Checking result</returns>
public static bool IsStandard(CScript scriptPubKey, out txnouttype whichType)
{
IList<IEnumerable<byte>> solutions = new List<IEnumerable<byte>>();
if (whichType == txnouttype.TX_MULTISIG)
{
+ // Additional verification of OP_CHECKMULTISIG arguments
byte m = solutions.First().First();
byte n = solutions.Last().First();
// There are shortcuts for pay-to-script-hash and pay-to-pubkey-hash, which are more constrained than the other types:
// It is always OP_HASH160 20 [20 byte hash] OP_EQUAL
- if (scriptPubKey.IsPayToScriptHash())
+ if (scriptPubKey.IsPayToScriptHash)
{
typeRet = txnouttype.TX_SCRIPTHASH;
// Take 20 bytes with offset of 2 bytes
- IEnumerable<byte> hashBytes = scriptPubKey.Enumerable.Skip(2).Take(20);
+ IEnumerable<byte> hashBytes = scriptPubKey.Bytes.Skip(2).Take(20);
solutions.Add(hashBytes);
return true;
}
// It is always OP_DUP OP_HASH160 20 [20 byte hash] OP_EQUALVERIFY OP_CHECKSIG
- if (scriptPubKey.IsPayToPubKeyHash())
+ if (scriptPubKey.IsPayToPubKeyHash)
{
typeRet = txnouttype.TX_PUBKEYHASH;
// Take 20 bytes with offset of 3 bytes
- IEnumerable<byte> hashBytes = scriptPubKey.Enumerable.Skip(3).Take(20);
+ IEnumerable<byte> hashBytes = scriptPubKey.Bytes.Skip(3).Take(20);
solutions.Add(hashBytes);
return true;
foreach (Tuple<txnouttype, IEnumerable<byte>> templateTuple in templateTuples)
{
- CScript script2 = new CScript(templateTuple.Item2);
CScript script1 = scriptPubKey;
+ CScript script2 = new CScript(templateTuple.Item2);
opcodetype opcode1, opcode2;
IEnumerable<byte> args1, args2;
- byte last1 = script1.Enumerable.Last();
- byte last2 = script2.Enumerable.Last();
+ byte last1 = script1.Bytes.Last();
+ byte last2 = script2.Bytes.Last();
while (true)
{
- if (wl1.GetItem() == last1 && wl2.GetItem() == last2)
+ if (wl1.GetCurrentItem() == last1 && wl2.GetCurrentItem() == last2)
{
// Found a match
typeRet = templateTuple.Item1;
break;
}
}
- else if (opcode1 != opcode2 || args1.SequenceEqual(args2))
+ else if (opcode1 != opcode2 || !args1.SequenceEqual(args2))
{
// Others must match exactly
break;
return false;
}
+
+ public static Hash256 SignatureHash(CScript scriptCode, CTransaction txTo, int nIn, int nHashType)
+ {
+ if (nIn >= txTo.vin.Length)
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.AppendFormat("ERROR: SignatureHash() : nIn={0} out of range\n", nIn);
+ throw new ArgumentOutOfRangeException("nIn", sb.ToString());
+ }
+
+ CTransaction txTmp = new CTransaction(txTo);
+
+ // In case concatenating two scripts ends up with two codeseparators,
+ // or an extra one at the end, this prevents all those possible incompatibilities.
+ scriptCode.RemovePattern(new byte[] { (byte)opcodetype.OP_CODESEPARATOR });
+
+ // Blank out other inputs' signatures
+ for (int i = 0; i < txTmp.vin.Length; i++)
+ {
+ txTmp.vin[i].scriptSig = new CScript();
+ }
+ txTmp.vin[nIn].scriptSig = scriptCode;
+
+ // Blank out some of the outputs
+ if ((nHashType & 0x1f) == (int)sigflag.SIGHASH_NONE)
+ {
+ // Wildcard payee
+ txTmp.vout = new CTxOut[0];
+
+ // Let the others update at will
+ for (int i = 0; i < txTmp.vin.Length; i++)
+ {
+ if (i != nIn)
+ {
+ txTmp.vin[i].nSequence = 0;
+ }
+ }
+ }
+ else if ((nHashType & 0x1f) == (int)sigflag.SIGHASH_SINGLE)
+ {
+ // Only lock-in the txout payee at same index as txin
+ int nOut = nIn;
+ if (nOut >= txTmp.vout.Length)
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.AppendFormat("ERROR: SignatureHash() : nOut={0} out of range\n", nOut);
+ throw new ArgumentOutOfRangeException("nOut", sb.ToString());
+ }
+ Array.Resize(ref txTmp.vout, nOut + 1);
+
+ for (int i = 0; i < nOut; i++)
+ {
+ txTmp.vout[i] = new CTxOut();
+ }
+
+ // Let the others update at will
+ for (int i = 0; i < txTmp.vin.Length; i++)
+ {
+ if (i != nIn)
+ {
+ txTmp.vin[i].nSequence = 0;
+ }
+ }
+ }
+
+ // Blank out other inputs completely, not recommended for open transactions
+ if ((nHashType & (int)sigflag.SIGHASH_ANYONECANPAY) != 0)
+ {
+ txTmp.vin[0] = txTmp.vin[nIn];
+ Array.Resize(ref txTmp.vin, 1);
+ }
+
+ // Serialize and hash
+ List<byte> b = new List<byte>();
+ b.AddRange(txTmp.Bytes);
+ b.AddRange(BitConverter.GetBytes(nHashType));
+
+ return Hash256.Compute256(b);
+ }
+
};
}