public void GetSigOpCount()
        {
            // Test CScript::GetSigOpCount()
            Script s1 = new Script();
            Assert.Equal(s1.GetSigOpCount(false), 0U);
            Assert.Equal(s1.GetSigOpCount(true), 0U);

            uint160 dummy = new uint160(0);
            s1 = s1 + OpcodeType.OP_1 + dummy.ToBytes() + dummy.ToBytes() + OpcodeType.OP_2 + OpcodeType.OP_CHECKMULTISIG;
            Assert.Equal(s1.GetSigOpCount(true), 2U);
            s1 = s1 + OpcodeType.OP_IF + OpcodeType.OP_CHECKSIG + OpcodeType.OP_ENDIF;
            Assert.Equal(s1.GetSigOpCount(true), 3U);
            Assert.Equal(s1.GetSigOpCount(false), 21U);

            var payToScript = new PayToScriptHashTemplate();
            Script p2sh = payToScript.GenerateScriptPubKey(s1);
            Script scriptSig = payToScript.GenerateScriptSig(new[] { (Op)OpcodeType.OP_0 }, s1);
            Assert.Equal(p2sh.GetSigOpCount(scriptSig), 3U);

            var multiSig = new PayToMultiSigTemplate();
            PubKey[] keys = Enumerable.Range(0, 3).Select(_ => new Key(true).PubKey).ToArray();

            Script s2 = multiSig.GenerateScriptPubKey(1, keys);
            Assert.Equal(s2.GetSigOpCount(true), 3U);
            Assert.Equal(s2.GetSigOpCount(false), 20U);

            p2sh = payToScript.GenerateScriptPubKey(s2);
            Assert.Equal(p2sh.GetSigOpCount(true), 0U);
            Assert.Equal(p2sh.GetSigOpCount(false), 0U);
            Script scriptSig2 = new Script();
            scriptSig2 = scriptSig2 + OpcodeType.OP_1 + dummy.ToBytes() + dummy.ToBytes() + s2.ToRawScript();
            Assert.Equal(p2sh.GetSigOpCount(scriptSig2), 3U);
        }
        private uint GetSigOpCount(Script script, Network network, Script scriptSig)
        {
            if (!script.IsScriptType(ScriptType.P2SH))
            {
                return(script.GetSigOpCount(true));
            }
            // This is a pay-to-script-hash scriptPubKey;
            // get the last item that the scriptSig
            // pushes onto the stack:
            bool validSig = new PayToScriptHashTemplate().CheckScriptSig(network, scriptSig, script);

            return(!validSig ? 0 : this.GetSigOpCount(new Script(scriptSig.ToOps().Last().PushData), true, network));
            // ... and return its opcount:
        }
Exemple #3
0
        public uint GetSigOpCount(Script scriptSig)
        {
            if (!IsPayToScriptHash)
            {
                return(GetSigOpCount(true));
            }

            // This is a pay-to-script-hash scriptPubKey;
            // get the last item that the scriptSig
            // pushes onto the stack:
            var validSig = new PayToScriptHashTemplate().CheckScriptSig(scriptSig, this);

            return(!validSig ? 0 : new Script(scriptSig.ToOps().Last().PushData).GetSigOpCount(true));
            // ... and return its opcount:
        }
Exemple #4
0
        public void CanCompressScript()
        {
            var payToHashTemplate   = new PayToPubkeyHashTemplate();
            var payToScriptTemplate = new PayToScriptHashTemplate();
            var payToPubKeyTemplate = new PayToPubkeyTemplate();


            var key = new Key(true);

            //Pay to pubkey hash (encoded as 21 bytes)
            var script = payToHashTemplate.GenerateScriptPubKey(key.PubKey.ID);

            AssertCompressed(script, 21);
            script = payToHashTemplate.GenerateScriptPubKey(key.PubKey.Decompress().ID);
            AssertCompressed(script, 21);

            //Pay to script hash (encoded as 21 bytes)
            script = payToScriptTemplate.GenerateScriptPubKey(script);
            AssertCompressed(script, 21);

            //Pay to pubkey starting with 0x02, 0x03 or 0x04 (encoded as 33 bytes)
            script = payToPubKeyTemplate.GenerateScriptPubKey(key.PubKey);
            script = AssertCompressed(script, 33);
            var readenKey = payToPubKeyTemplate.ExtractScriptPubKeyParameters(script);

            AssertEx.CollectionEquals(readenKey.ToBytes(), key.PubKey.ToBytes());

            script    = payToPubKeyTemplate.GenerateScriptPubKey(key.PubKey.Decompress());
            script    = AssertCompressed(script, 33);
            readenKey = payToPubKeyTemplate.ExtractScriptPubKeyParameters(script);
            AssertEx.CollectionEquals(readenKey.ToBytes(), key.PubKey.Decompress().ToBytes());


            //Other scripts up to 121 bytes require 1 byte + script length.
            script = new Script(Enumerable.Range(0, 60).Select(_ => (Op)OpcodeType.OP_RETURN).ToArray());
            AssertCompressed(script, 61);
            script = new Script(Enumerable.Range(0, 120).Select(_ => (Op)OpcodeType.OP_RETURN).ToArray());
            AssertCompressed(script, 121);

            //Above that, scripts up to 16505 bytes require 2 bytes + script length.
            script = new Script(Enumerable.Range(0, 122).Select(_ => (Op)OpcodeType.OP_RETURN).ToArray());
            AssertCompressed(script, 124);
        }
Exemple #5
0
        public void CanParseAndGeneratePayToScript()
        {
            var template = new PayToScriptHashTemplate();
            var redeem   = "1 0364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27c 0364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27d 2 OP_CHECKMULTISIG";

            var scriptPubkey = "OP_HASH160 b5b88dd9befc9236915fcdbb7fd50052df50c855 OP_EQUAL";

            var scriptSig = "3044022064f45a382a15d3eb5e7fe72076eec4ef0f56fde1adfd710866e729b9e5f3383d02202720a895914c69ab49359087364f06d337a2138305fbc19e20d18da78415ea9301 51210364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27c210364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27d52ae";

            var pubParams = template.ExtractScriptPubKeyParameters(new Script(scriptPubkey));

            Assert.Equal("b5b88dd9befc9236915fcdbb7fd50052df50c855", pubParams.ToString());
            Assert.Equal(scriptPubkey, template.GenerateScriptPubKey(pubParams).ToString());

            var sigParams = template.ExtractScriptSigParameters(new Script(scriptSig));

            Assert.Equal("3044022064f45a382a15d3eb5e7fe72076eec4ef0f56fde1adfd710866e729b9e5f3383d02202720a895914c69ab49359087364f06d337a2138305fbc19e20d18da78415ea9301", Encoders.Hex.EncodeData(sigParams.Signatures[0].ToBytes()));
            Assert.Equal(redeem, sigParams.RedeemScript.ToString());
            Assert.Equal(scriptSig, template.GenerateScriptSig(sigParams.Signatures, sigParams.RedeemScript).ToString());
        }
        public void GetSigOpCount()
        {
            // Test CScript::GetSigOpCount()
            Script s1 = new Script();

            Assert.Equal(s1.GetSigOpCount(false), 0U);
            Assert.Equal(s1.GetSigOpCount(true), 0U);

            uint160 dummy = new uint160(0);

            s1 = s1 + OpcodeType.OP_1 + dummy.ToBytes() + dummy.ToBytes() + OpcodeType.OP_2 + OpcodeType.OP_CHECKMULTISIG;
            Assert.Equal(s1.GetSigOpCount(true), 2U);
            s1 = s1 + OpcodeType.OP_IF + OpcodeType.OP_CHECKSIG + OpcodeType.OP_ENDIF;
            Assert.Equal(s1.GetSigOpCount(true), 3U);
            Assert.Equal(s1.GetSigOpCount(false), 21U);

            var    payToScript = new PayToScriptHashTemplate();
            Script p2sh        = payToScript.GenerateScriptPubKey(s1);
            Script scriptSig   = payToScript.GenerateScriptSig(new[] { (Op)OpcodeType.OP_0 }, s1);

            Assert.Equal(p2sh.GetSigOpCount(scriptSig), 3U);

            var multiSig = new PayToMultiSigTemplate();

            PubKey[] keys = Enumerable.Range(0, 3).Select(_ => new Key(true).PubKey).ToArray();

            Script s2 = multiSig.GenerateScriptPubKey(1, keys);

            Assert.Equal(s2.GetSigOpCount(true), 3U);
            Assert.Equal(s2.GetSigOpCount(false), 20U);

            p2sh = payToScript.GenerateScriptPubKey(s2);
            Assert.Equal(p2sh.GetSigOpCount(true), 0U);
            Assert.Equal(p2sh.GetSigOpCount(false), 0U);
            Script scriptSig2 = new Script();

            scriptSig2 = scriptSig2 + OpcodeType.OP_1 + dummy.ToBytes() + dummy.ToBytes() + s2.ToRawScript();
            Assert.Equal(p2sh.GetSigOpCount(scriptSig2), 3U);
        }
Exemple #7
0
        public void CanCompressScript()
        {
            var payToHashTemplate = new PayToPubkeyHashTemplate();
            var payToScriptTemplate = new PayToScriptHashTemplate();
            var payToPubKeyTemplate = new PayToPubkeyTemplate();

            var key = new Key(true);

            //Pay to pubkey hash (encoded as 21 bytes)
            var script = payToHashTemplate.GenerateScriptPubKey(key.PubKey.ID);
            AssertCompressed(script, 21);
            script = payToHashTemplate.GenerateScriptPubKey(key.PubKey.Decompress().ID);
            AssertCompressed(script, 21);

            //Pay to script hash (encoded as 21 bytes)
            script = payToScriptTemplate.GenerateScriptPubKey(script);
            AssertCompressed(script, 21);

            //Pay to pubkey starting with 0x02, 0x03 or 0x04 (encoded as 33 bytes)
            script = payToPubKeyTemplate.GenerateScriptPubKey(key.PubKey);
            script = AssertCompressed(script, 33);
            var readenKey = payToPubKeyTemplate.ExtractScriptPubKeyParameters(script);
            AssertEx.CollectionEquals(readenKey.ToBytes(), key.PubKey.ToBytes());

            script = payToPubKeyTemplate.GenerateScriptPubKey(key.PubKey.Decompress());
            script = AssertCompressed(script, 33);
            readenKey = payToPubKeyTemplate.ExtractScriptPubKeyParameters(script);
            AssertEx.CollectionEquals(readenKey.ToBytes(), key.PubKey.Decompress().ToBytes());

            //Other scripts up to 121 bytes require 1 byte + script length.
            script = new Script(Enumerable.Range(0, 60).Select(_ => (Op)OpcodeType.OP_RETURN).ToArray());
            AssertCompressed(script, 61);
            script = new Script(Enumerable.Range(0, 120).Select(_ => (Op)OpcodeType.OP_RETURN).ToArray());
            AssertCompressed(script, 121);

            //Above that, scripts up to 16505 bytes require 2 bytes + script length.
            script = new Script(Enumerable.Range(0, 122).Select(_ => (Op)OpcodeType.OP_RETURN).ToArray());
            AssertCompressed(script, 124);
        }