Esempio n. 1
0
        /// <summary>
        /// The create a pay to pub key hash transaction.
        /// </summary>
        public virtual Transaction CreatePubKeyHashTransaction(CoinParameters parameters, CreateRawTransaction rawTransaction)
        {
            var transaction = new Transaction
            {
                Version   = parameters.TransactionVersion,
                Locktime  = 0,
                Timestamp = (int)DateTime.UtcNow.UnixTimeStampFromDateTime()
            };

            // create the inputs
            transaction.Inputs = rawTransaction.Inputs
                                 .Select(input => new TransactionInput
            {
                Outpoint = new TransactionOutPoint
                {
                    Hash  = input.TransactionId,
                    Index = input.Output
                },
                ScriptBytes = Enumerable.Empty <byte>().ToArray(),

                Sequence = 0xffffffff     // uint.MaxValue
            }).ToList();

            // create the output
            transaction.Outputs = rawTransaction.Outputs
                                  .Select((output, index) => new TransactionOutput
            {
                Index       = index,
                Value       = this.Serializer.ValueToNum(output.Value),
                ScriptBytes = ScriptBuilder.CreateOutputScript(Address.Create(parameters, output.Key)).GetProgram()
            }).ToList();

            return(transaction);
        }
Esempio n. 2
0
        public void Given_a_publickey_When_parsed_Then_should_match_script_address()
        {
            // pay to pubkey
            var toKey     = new BitcoinPrivateKey(parameters, "KzXzfn1AKhA9XaBtr2enevcsw1w3NXWXUDgKKpqa8QyVi1cKYKdS").Key;
            var toAddress = toKey.ToPublicKey().ToAddress(parameters);

            Assert.AreEqual(toAddress.ToString(), ScriptBuilder.CreateOutputScript(toKey).GetToAddress(parameters, true).ToString());

            // pay to pubkey hash
            Assert.AreEqual(toAddress.ToString(), ScriptBuilder.CreateOutputScript(toAddress).GetToAddress(parameters, true).ToString());

            // pay to script hash
            ////Script p2ShScript = ScriptBuilder.CreateP2SHOutputScript(new byte[20]);
            ////Address scriptAddress = Address.Create(parameters, p2ShScript.GetPubKeyHash());
            ////Assert.AreEqual(scriptAddress, p2ShScript.GetToAddress(parameters, true));
        }
Esempio n. 3
0
        public override Transaction CreatePubKeyHashTransaction(CoinParameters parameters, CreateRawTransaction rawTransaction)
        {
            var transaction = base.CreatePubKeyHashTransaction(parameters, rawTransaction);

            var changePointer = transaction.Inputs.SingleOrDefault(p => p.Outpoint.Index == -1);

            if (changePointer == null)
            {
                return(transaction);
            }

            var rawChangeOutput         = rawTransaction.Outputs.Single(p => p.Key == changePointer.Outpoint.Hash);
            var changeOutputScriptBytes = ScriptBuilder
                                          .CreateOutputScript(Address.Create(parameters, rawChangeOutput.Key)).GetProgram();

            changePointer.Outpoint.Index =
                transaction.Outputs.Single(p => p.ScriptBytes == changeOutputScriptBytes).Index;
            changePointer.Outpoint.Hash = "0";

            return(transaction);
        }
Esempio n. 4
0
        public void Given_a_invalid_publickey_When_parsed_Then_should_fail()
        {
            var key = new BitcoinPrivateKey(parameters, "KzXzfn1AKhA9XaBtr2enevcsw1w3NXWXUDgKKpqa8QyVi1cKYKdS").Key;

            ScriptBuilder.CreateOutputScript(key).GetToAddress(parameters, false);
        }
Esempio n. 5
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Creating output script for 'bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a'...");
            // input cash address and produce an output script
            var decoded = CashAddress.DecodeCashAddress("bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a");

            // so far, cash addresses have two defined types
            // let's print the readable output script for the address
            switch (decoded.Type)
            {
            // our demo happens to be a P2PKH cash address...
            case ScriptType.P2PKH:
                // use ByteHexConverter to convert raw byte data for output script to readable hex
                Console.WriteLine("Output script: " +
                                  "OP_DUP OP_HASH160 " + ByteHexConverter.ByteArrayToHex(decoded.Hash) + " OP_EQUALVERIFY OP_CHECKSIG");
                break;

            // if it was a P2SH cash address...
            case ScriptType.P2SH:
                // use ByteHexConverter to convert raw byte data for output script to readable hex
                Console.WriteLine("Output script: " +
                                  "OP_HASH160 " + ByteHexConverter.ByteArrayToHex(decoded.Hash) + " OP_CHECKSIG");
                break;

            // whoa... another type?
            default:
                Console.WriteLine("This shouldn't happen! That's an unknown cash address type!");
                break;
            }

            // let's use script builder now
            var outputScript = ScriptBuilder.CreateOutputScript(decoded.Type, decoded.Hash);

            // same thing if we go straight from a Cash Address
            outputScript = ScriptBuilder.CreateOutputScript("bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a");
            Console.WriteLine("Output script from ScriptBuilder: " + outputScript);
            Console.WriteLine("Output script RAW from ScriptBuilder: " + ByteHexConverter.ByteArrayToHex(outputScript.ScriptBytes));

            // What about if we want to create an OP_RETURN output?
            // ASCII encode "My Bitcoin OP_RETURN!" and create an output script
            var opReturn = ScriptBuilder.CreateOpReturn(Encoding.ASCII.GetBytes("My Bitcoin OP_RETURN!"));

            Console.WriteLine("OP_RETURN script from ScriptBuilder: " + opReturn);
            Console.WriteLine("OP_RETURN script RAW from ScriptBuilder: " + ByteHexConverter.ByteArrayToHex(opReturn.ScriptBytes));


            // encode a hash160 from an output script as a cash address (demo script is P2PKH)
            Console.WriteLine("Encoding output script 'OP_DUP OP_HASH160 76a04053bda0a88bda5177b86a15c3b29f559873 OP_EQUALVERIFY OP_CHECKSIG'...");
            // use ByteHexConverter to convert the readable hex to raw byte data (as it would actually be encoded in an output script)
            var encoded = CashAddress.EncodeCashAddress(AddressPrefix.bitcoincash, ScriptType.P2PKH,
                                                        ByteHexConverter.StringToByteArray("76a04053bda0a88bda5177b86a15c3b29f559873"));

            Console.WriteLine("Cash Address: " + encoded);


            // let's try decoding a raw transaction!
            var txHex =
                "020000000113b15104613103365466d9c1773a2c60c3dec7ab6ea41f7f2824f6b00556bd98370000006b483045022100bda8b53dcffbcbf3c005b7c55a923cd04eb3d3abd7632dd260f97d15cc2982ed02202dc15d4a9ad826f4b3a0781693050fe8c1cdeb919903ba11385f0b5e83c1ea5641210384dd3ad997f2e10980e755236b474f986c519599946027876cdeb4eb5a30a09fffffffff0110270000000000001976a91476a04053bda0a88bda5177b86a15c3b29f55987388ac00000000";

            var tx = new Transaction(ByteHexConverter.StringToByteArray(txHex));

            Console.WriteLine("Decoded transaction. TXID: " + tx.TXIDHex + ". Inputs: " + tx.Inputs.Length + ". Outputs: " + tx.Outputs.Length + ". Output Scripts:");
            foreach (var output in tx.Outputs)
            {
                Console.WriteLine(output);
            }

            // wait
            Console.ReadKey();
        }