Esempio n. 1
0
        public byte[] CreatePrivateKeyScript(Transaction tx, int inputIndex, byte hashType, ECPrivateKeyParameters privateKey, ECPublicKeyParameters publicKey)
        {
            //TODO
            var sha256       = new SHA256Managed();
            var scriptEngine = new ScriptEngine(this.logger);

            var publicAddress   = CreatePublicAddress(publicKey);
            var publicKeyScript = CreatePublicKeyScript(publicAddress);
            var txSignature     = scriptEngine.TxSignature(publicKeyScript.ToImmutableArray(), tx, inputIndex, hashType);
            var txSignatureHash = sha256.ComputeDoubleHash(txSignature);

            //Debug.WriteLine("Signing Tx:       {0}".Format2(txSignature.ToHexDataString()));
            //Debug.WriteLine("Signing Tx  Hash: {0}".Format2(txSignatureHash.ToHexDataString()));

            var signer = new ECDsaSigner();

            signer.Init(forSigning: true, parameters: privateKey);
            var signature = signer.GenerateSignature(txSignatureHash);
            var r         = signature[0];
            var s         = signature[1];

            byte[] sigEncoded;
            using (var stream = new MemoryStream())
            {
                using (var asn1Stream = new Asn1OutputStream(stream))
                {
                    asn1Stream.WriteObject(new DerSequence(new DerInteger(r), new DerInteger(s)));
                }

                sigEncoded = stream.ToArray().Concat(hashType);
            }

            //Debug.WriteLine("Sig R:       {0}".Format2(r.ToHexNumberStringUnsigned()));
            //Debug.WriteLine("Sig S:       {0}".Format2(s.ToHexNumberStringUnsigned()));
            //Debug.WriteLine("Sig Encoded: {0}".Format2(sigEncoded.ToHexDataString()));

            using (var privateKeyScript = new ScriptBuilder())
            {
                privateKeyScript.WritePushData(sigEncoded);
                privateKeyScript.WritePushData(publicAddress);
                //Debug.WriteLine("Private Script: {0}".Format2(privateKeyScript.GetScript().ToHexDataString()));

                return(privateKeyScript.GetScript());
            }
        }
Esempio n. 2
0
        public void TestWritePushData65535()
        {
            var data = random.NextBytes(65535);
            var expected = new byte[] { 0x4D }.Concat(0xFF).Concat(0xFF).Concat(data);

            var actual = new ScriptBuilder();
            actual.WritePushData(data);

            CollectionAssert.AreEqual(expected, actual.GetScript());
        }
Esempio n. 3
0
        public void TestWritePushData0()
        {
            var data = new byte[0];
            var expected = new byte[] { 0x00 };

            var actual = new ScriptBuilder();
            actual.WritePushData(data);

            CollectionAssert.AreEqual(expected, actual.GetScript());
        }
Esempio n. 4
0
        public byte[] CreateOutput(byte[] publicKey)
        {
            using (var outputScript = new ScriptBuilder())
            {
                outputScript.WritePushData(publicKey);
                outputScript.WriteOp(ScriptOp.OP_CHECKSIG);

                return(outputScript.GetScript());
            }
        }
Esempio n. 5
0
        public byte[] CreateOutputFromPublicKeyHash(byte[] publicKeyHash)
        {
            using (var outputScript = new ScriptBuilder())
            {
                outputScript.WriteOp(ScriptOp.OP_DUP);
                outputScript.WriteOp(ScriptOp.OP_HASH160);
                outputScript.WritePushData(publicKeyHash);
                outputScript.WriteOp(ScriptOp.OP_EQUALVERIFY);
                outputScript.WriteOp(ScriptOp.OP_CHECKSIG);

                return(outputScript.GetScript());
            }
        }
        public byte[] CreateOutputFromPublicKeyHash(byte[] publicKeyHash)
        {
            using (var outputScript = new ScriptBuilder())
            {
                outputScript.WriteOp(ScriptOp.OP_DUP);
                outputScript.WriteOp(ScriptOp.OP_HASH160);
                outputScript.WritePushData(publicKeyHash);
                outputScript.WriteOp(ScriptOp.OP_EQUALVERIFY);
                outputScript.WriteOp(ScriptOp.OP_CHECKSIG);

                return outputScript.GetScript();
            }
        }
Esempio n. 7
0
        public byte[] CreatePublicKeyScript(byte[] publicKey)
        {
            var publicKeyHash = RIPEMD160Static.ComputeHash(SHA256Static.ComputeHash(publicKey));

            using (var publicKeyScript = new ScriptBuilder())
            {
                publicKeyScript.WriteOp(ScriptOp.OP_DUP);
                publicKeyScript.WriteOp(ScriptOp.OP_HASH160);
                publicKeyScript.WritePushData(publicKeyHash);
                publicKeyScript.WriteOp(ScriptOp.OP_EQUALVERIFY);
                publicKeyScript.WriteOp(ScriptOp.OP_CHECKSIG);

                //Debug.WriteLine("Public Script: {0}".Format2(publicKeyScript.GetScript().ToHexDataString()));

                return(publicKeyScript.GetScript());
            }
        }
Esempio n. 8
0
        public byte[] CreatePublicKeyScript(byte[] publicKey)
        {
            var publicKeyHash = RIPEMD160Static.ComputeHash(SHA256Static.ComputeHash(publicKey));

            using (var publicKeyScript = new ScriptBuilder())
            {
                publicKeyScript.WriteOp(ScriptOp.OP_DUP);
                publicKeyScript.WriteOp(ScriptOp.OP_HASH160);
                publicKeyScript.WritePushData(publicKeyHash);
                publicKeyScript.WriteOp(ScriptOp.OP_EQUALVERIFY);
                publicKeyScript.WriteOp(ScriptOp.OP_CHECKSIG);

                //Debug.WriteLine("Public Script: {0}".Format2(publicKeyScript.GetScript().ToHexDataString()));

                return publicKeyScript.GetScript();
            }
        }
Esempio n. 9
0
 public void TestWritePushData4294967296()
 {
     var data = random.NextBytes(4294967296);
     var actual = new ScriptBuilder();
     actual.WritePushData(data);
 }
Esempio n. 10
0
        public byte[] CreatePrivateKeyScript(Transaction tx, int inputIndex, byte hashType, ECPrivateKeyParameters privateKey, ECPublicKeyParameters publicKey)
        {
            //TODO
            var scriptEngine = new ScriptEngine();

            var publicAddress = CreatePublicAddress(publicKey);
            var publicKeyScript = CreatePublicKeyScript(publicAddress);
            var txSignature = scriptEngine.TxSignature(publicKeyScript, tx, inputIndex, hashType);
            var txSignatureHash = SHA256Static.ComputeDoubleHash(txSignature);

            //Debug.WriteLine("Signing Tx:       {0}".Format2(txSignature.ToHexDataString()));
            //Debug.WriteLine("Signing Tx  Hash: {0}".Format2(txSignatureHash.ToHexDataString()));

            var signer = new ECDsaSigner();
            signer.Init(forSigning: true, parameters: privateKey);
            var signature = signer.GenerateSignature(txSignatureHash);
            var r = signature[0];
            var s = signature[1];

            byte[] sigEncoded;
            using (var stream = new MemoryStream())
            {
                using (var asn1Stream = new Asn1OutputStream(stream))
                {
                    asn1Stream.WriteObject(new DerSequence(new DerInteger(r), new DerInteger(s)));
                }

                sigEncoded = stream.ToArray().Concat(hashType);
            }

            //Debug.WriteLine("Sig R:       {0}".Format2(r.ToHexNumberStringUnsigned()));
            //Debug.WriteLine("Sig S:       {0}".Format2(s.ToHexNumberStringUnsigned()));
            //Debug.WriteLine("Sig Encoded: {0}".Format2(sigEncoded.ToHexDataString()));

            using (var privateKeyScript = new ScriptBuilder())
            {
                privateKeyScript.WritePushData(sigEncoded);
                privateKeyScript.WritePushData(publicAddress);
                //Debug.WriteLine("Private Script: {0}".Format2(privateKeyScript.GetScript().ToHexDataString()));

                return privateKeyScript.GetScript();
            }
        }
Esempio n. 11
0
        private ImmutableArray<byte> GetPushInt64Script(long value)
        {
            var script = new ScriptBuilder();

            // push 0 onto the stack using OP_0
            if (value == 0)
            {
                script.WriteOp(ScriptOp.OP_0);
            }
            // push -1 onto the stack using OP_1NEGATE
            else if (value == -1)
            {
                script.WriteOp(ScriptOp.OP_1NEGATE);
            }
            // push 1-16 onto the stack using OP_1 to OP_16
            else if (value >= 1 && value <= 16)
            {
                script.WriteOp((ScriptOp)(value + ((long)ScriptOp.OP_1 - 1)));
            }
            else
            {
                var valueBytes = SerializeScriptValue(value);
                script.WritePushData(valueBytes);
            }

            return script.GetScript().ToImmutableArray();
        }