bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.7k stars 2.11k forks source link

create testnet Transaction 1-1 #1589

Closed xzbnm closed 4 years ago

xzbnm commented 4 years ago

Hello ,i want to Create a 1-to-1 testnet Transaction

when like this

            const alice = bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet)

            const psbt = new bitcoin.Psbt({ network: bitcoin.networks.testnet });

            const unspentOutput = {
                    "txid": "645eaeb30d55c68613632e2c7b13a69f9b7cf013482fe95a97e7d00f04e4c00b", //latest tx id
                    "vout": 1,
                    "address": "2NC3CVsW7xDYvvRr5WN3cTz3YPcbirVUEPS", //my Address1
                    "label": "payment",
                    "amount": 0.01739502,
            }

            let rawTransaction = "020000000001015f4bb2668e794d67fe947f5950e5a192481fbe348fe7cfac17e96b60005e5d8b00000000171600148a8eb6306572263dd5aef5fcae51925f4405d706feffffff028aeb14000000000017a914ceadf393e3525451ed56ac12421d922c983a7bcd87102700000000000017a914ce24279876e838178b0817245114826a05ddb81f870247304402202b92f21acee1fe6e4a97636696c9ff614d2b43f84dda3fa2275fa2d6ce890b8a022058736ead596a9fc965685545092ace2ff9fb1a5f6e6ffa1802cdf3310ccba280012102894d273b5c3b4da14833a4fc22d62216614a2cfcd4b1061354d96f87f60bb5d6cba71a00"

            const   test = bitcoin.payments.p2sh({
                        redeem: bitcoin.payments.p2wpkh({
                            pubkey: alice.publicKey,
                            network: bitcoin.networks.testnet,
                        }),
                         network: bitcoin.networks.testnet,
            })

            psbt.addInput({
                hash: unspentOutput.txid,
                index: unspentOutput.vout,
                nonWitnessUtxo: Buffer.from(rawTransaction, 'hex'),
                redeemScript:Buffer.from( test.redeem.output, 'hex')
            })

            psbt.addOutput({
                address: this.toAddress,  , //my Address2
                value: 24502,
            })

            psbt.addOutput({
                address: this.publickeyBTC, //my Address1
                value: 1600000
            })

            psbt.signInput(0, alice)
            psbt.validateSignaturesOfInput(0)
            psbt.finalizeAllInputs()
            console.log(psbt.toHex())
            // create Transaction this Hex 
            // 70736274ff01007302000000010bc0e4040fd0e7975ae92f4813f07c9b9fa6137b2c2e631386c6550db3ae5e640100000000ffffffff02b65f00000000000017a914bc8b80fab97ca0833e982b87fcb25dee1ae87c4e87006a18000000000017a914ce24279876e838178b0817245114826a05ddb81f8700000000000100f7020000000001015f4bb2668e794d67fe947f5950e5a192481fbe348fe7cfac17e96b60005e5d8b00000000171600148a8eb6306572263dd5aef5fcae51925f4405d706feffffff028aeb14000000000017a914ceadf393e3525451ed56ac12421d922c983a7bcd87102700000000000017a914ce24279876e838178b0817245114826a05ddb81f870247304402202b92f21acee1fe6e4a97636696c9ff614d2b43f84dda3fa2275fa2d6ce890b8a022058736ead596a9fc965685545092ace2ff9fb1a5f6e6ffa1802cdf3310ccba280012102894d273b5c3b4da14833a4fc22d62216614a2cfcd4b1061354d96f87f60bb5d6cba71a00010717160014c0baada91e7a1ec27e777fe6c665c337fec168ca01086c02483045022100ae6457f48b062851f7e9b70a1691ee62434e1e524e930135bfa6307d1b35eb4d0220070112ef4face1cb1f044c5be2cb9dc9981dd5a602c6d9d06e0b4c7e1b8684ec012102148b1627898c099aa836f992992ab9b110cad637a2b6d57310ad24ea02fb3d18000000

in Decoded Transaction I get this error

{
    "error": "sequence too long"
}

and my btc address 2NC3CVsW7xDYvvRr5WN3cTz3YPcbirVUEPS

junderw commented 4 years ago

extract the transaction

psbt.extractTransaction().toHex()

xzbnm commented 4 years ago

i try and get this error "Error: Outputs are spending more than Inputs"

junderw commented 4 years ago

I looked at rawTransaction's 2nd output (index 1) and it's only worth 0.0001..... not 0.01739502

junderw commented 4 years ago

index 0 is worth 0.01371018

xzbnm commented 4 years ago

in this address 2NC3CVsW7xDYvvRr5WN3cTz3YPcbirVUEPS recived 0.0001 + 0.0001 + 0.01719502 btc = 3 Transaction

and for send all amount in the new address : i just to get 3 latest txid and rawTransaction and used it in the new Transaction ?

junderw commented 4 years ago

You have to do addInput 3 times with each transaction, each txid, each output index.

xzbnm commented 4 years ago

So that's going to be tough. I wish there was a simpler way

xzbnm commented 4 years ago

If I wanted to send amount btc again, and how I would not use a transaction(txid) twice ?

junderw commented 4 years ago

A "utxo" (unspent transaction output) is like a dollar bill.

If you give me your dollar bill, you don't cut it in half, you either give it to me or not.

If you want to pay me 5 dollars but only have a 10 dollar bill, you give me your 10 bill and I give you a 5 bill in return. That is called "change", right?

In Bitcoin, change is an output you send back to yourself.

So addInput(txid1, index1, 10 dollar...) then addOutput(them, 5 dollar) + addOutput(me, 5 dollar)

The total amount of inputs minus the total amount of outputs is called the "miner fee". So if I spend 10 dollar, give you 5, and send 4 to myself as change, then the person who mined the block with my transaction gets 1 dollar.

After you have "spent" txid1:index1 you can not spend txid1:index1, BUT if txid1 transaction had 2 outputs sent to you, you can use the other txid1:index0(etc) if it is your utxo.

But usually, once you send your transaction, that transaction's txid + the index of your change output (which order you did the addOutput) is a new utxo for your wallet.

Thanks to this system, Bitcoin is extremely stable, and stateless.

Other coins such as Ethereum and Ripple have a huge state that makes it near impossible to run your own node and verify transactions without trusting a 3rd party.

xzbnm commented 4 years ago

That " s right. Thank you very much for the time you left me. btc is amazing.

xzbnm commented 4 years ago

i try agan and get error "error": "sequence too long"


         const alice = bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet)
            const psbt = new bitcoin.Psbt({ network: bitcoin.networks.testnet });
            const unspent1 = {
                    "txid": "645eaeb30d55c68613632e2c7b13a69f9b7cf013482fe95a97e7d00f04e4c00b",
                    "vout": 1,
                    "amount": 0.0001,
            }
            const unspent2 = {
                    "txid": "ae3a152f207d9b12a8b08879ceac952a40d26bafdd9d191a64112e508cfb90cf",
                    "vout": 1,
                    "amount": 0.0001,
            }
            const unspent3 = {
                    "txid": "b5622d2b777eef4f8c6c43e3c3165207483136f203a31e103aae28face73969c",
                    "vout": 1,
                    "amount": 0.01719502,
            }
            let raw1 = '020000000001015f4bb2668e794d67fe947f5950e5a192481fbe348fe7cfac17e96b60005e5d8b00000000171600148a8eb6306572263dd5aef5fcae51925f4405d706feffffff028aeb14000000000017a914ceadf393e3525451ed56ac12421d922c983a7bcd87102700000000000017a914ce24279876e838178b0817245114826a05ddb81f870247304402202b92f21acee1fe6e4a97636696c9ff614d2b43f84dda3fa2275fa2d6ce890b8a022058736ead596a9fc965685545092ace2ff9fb1a5f6e6ffa1802cdf3310ccba280012102894d273b5c3b4da14833a4fc22d62216614a2cfcd4b1061354d96f87f60bb5d6cba71a00'
            let raw2 = '02000000000101593a2cb124917f47f4d28c6c2e8f59a574af987846c648a65409bcf3f4484655010000001716001461354698b039b27f603f352b8b530c1008c2c946feffffff022e8ec8010000000017a914d81223f416311197a791b12e3cc3eb9b249af1b187102700000000000017a914ce24279876e838178b0817245114826a05ddb81f8702473044022043bc6882bfe200a536d8cdae34df5a1ce860d0d74d7a3f56e502fd6842d16eb502201c9c720cee9a5d5a8ca51be3a0505f56d7c04ae8ed8fc774a390948655ea63fc01210397a6489524bc79931d828cccfd2e002d4d984c550bb3cd8ce09619a144e0e37ecaa71a00'
            let raw3 = '020000000001019d2cb1c42c10cba0d8b3186e9d128c708a4d5d4c42a6f4f342a896a081c9f191010000001716001483ad046c15058ce9f8226cb7abea5946a425eee0feffffff0261e8e75e0000000017a914063a3f8dde0a0c2f8c99017d29ed6b73e38a698f87ce3c1a000000000017a914ce24279876e838178b0817245114826a05ddb81f870247304402205b0eedde011e6452f0f4073ec7488db2e62a9ce2dacefc1bbae1a64e5b71495202203b9b58ad5a07a43ca30689e1465d008c253b79a8101fafd5a286d9d9cb0ded7b012102aa8016a97bdd5c82a61c1468b71d9ac7f95c9cff78d387e5f606b604286cde68caa71a00'
            const   test = bitcoin.payments.p2sh({
                        redeem: bitcoin.payments.p2wpkh({
                            pubkey: alice.publicKey,
                            network: bitcoin.networks.testnet,
                        }),
                         network: bitcoin.networks.testnet,
            })

            const redeemScript = test.redeem.output
            psbt.addInput({
                hash: unspent1.txid,
                index: unspent1.vout,
                nonWitnessUtxo: Buffer.from(raw1, 'hex'),
                redeemScript:Buffer.from( test.redeem.output, 'hex')
            })
            psbt.addInput({
                hash: unspent2.txid,
                index: unspent2.vout,
                nonWitnessUtxo: Buffer.from(raw2, 'hex'),
                redeemScript:Buffer.from( test.redeem.output, 'hex')
            })
            psbt.addInput({
                hash: unspent3.txid,
                index: unspent3.vout,
                nonWitnessUtxo: Buffer.from(raw3, 'hex'),
                redeemScript:Buffer.from( test.redeem.output, 'hex')
            })

            psbt.addOutput({
                address: this.toAddress,
                value: 29502,
            })

            psbt.addOutput({
                address: this.publickeyBTC,
                value: 1700000
            })

            psbt.signInput(0, alice)
            psbt.signInput(1, alice)
            psbt.signInput(2, alice)
            psbt.validateSignaturesOfInput(0)
            psbt.finalizeAllInputs()
            psbt.extractTransaction()
            console.log(psbt.toHex())
//new tx
// 70736274ff0100c502000000030bc0e4040fd0e7975ae92f4813f07c9b9fa6137b2c2e631386c6550db3ae5e640100000000ffffffffcf90fb8c502e11641a199dddaf6bd2402a95acce7988b0a8129b7d202f153aae0100000000ffffffff9c9673cefa28ae3a101ea303f2363148075216c3e3436c8c4fef7e772b2d62b50100000000ffffffff023e7300000000000017a914bc8b80fab97ca0833e982b87fcb25dee1ae87c4e87a0f019000000000017a914ce24279876e838178b0817245114826a05ddb81f8700000000000100f7020000000001015f4bb2668e794d67fe947f5950e5a192481fbe348fe7cfac17e96b60005e5d8b00000000171600148a8eb6306572263dd5aef5fcae51925f4405d706feffffff028aeb14000000000017a914ceadf393e3525451ed56ac12421d922c983a7bcd87102700000000000017a914ce24279876e838178b0817245114826a05ddb81f870247304402202b92f21acee1fe6e4a97636696c9ff614d2b43f84dda3fa2275fa2d6ce890b8a022058736ead596a9fc965685545092ace2ff9fb1a5f6e6ffa1802cdf3310ccba280012102894d273b5c3b4da14833a4fc22d62216614a2cfcd4b1061354d96f87f60bb5d6cba71a00010717160014c0baada91e7a1ec27e777fe6c665c337fec168ca01086b0247304402206247007f0287957822f7d631c5f7125bab230a9631f26ee6538c63567917caf402204759968c10e41eebc72b86c885232543730bfbc153cb233b97e15dd5d2f2e5a2012102148b1627898c099aa836f992992ab9b110cad637a2b6d57310ad24ea02fb3d18000100f702000000000101593a2cb124917f47f4d28c6c2e8f59a574af987846c648a65409bcf3f4484655010000001716001461354698b039b27f603f352b8b530c1008c2c946feffffff022e8ec8010000000017a914d81223f416311197a791b12e3cc3eb9b249af1b187102700000000000017a914ce24279876e838178b0817245114826a05ddb81f8702473044022043bc6882bfe200a536d8cdae34df5a1ce860d0d74d7a3f56e502fd6842d16eb502201c9c720cee9a5d5a8ca51be3a0505f56d7c04ae8ed8fc774a390948655ea63fc01210397a6489524bc79931d828cccfd2e002d4d984c550bb3cd8ce09619a144e0e37ecaa71a00010717160014c0baada91e7a1ec27e777fe6c665c337fec168ca01086b0247304402204173aaf5a150a6acdf9aa4c7fa436c9bfc97cd396e3511a6bea0f654f1b76255022031272e2dc7c3e1edad0ced4ab9bca21a43c5a16690889e453df4390d696b3fbe012102148b1627898c099aa836f992992ab9b110cad637a2b6d57310ad24ea02fb3d18000100f7020000000001019d2cb1c42c10cba0d8b3186e9d128c708a4d5d4c42a6f4f342a896a081c9f191010000001716001483ad046c15058ce9f8226cb7abea5946a425eee0feffffff0261e8e75e0000000017a914063a3f8dde0a0c2f8c99017d29ed6b73e38a698f87ce3c1a000000000017a914ce24279876e838178b0817245114826a05ddb81f870247304402205b0eedde011e6452f0f4073ec7488db2e62a9ce2dacefc1bbae1a64e5b71495202203b9b58ad5a07a43ca30689e1465d008c253b79a8101fafd5a286d9d9cb0ded7b012102aa8016a97bdd5c82a61c1468b71d9ac7f95c9cff78d387e5f606b604286cde68caa71a00010717160014c0baada91e7a1ec27e777fe6c665c337fec168ca01086c02483045022100d47c82ab241cb53c0568ff23df2315c2d05725a67331ece108053509c691c19a022011ca6a6aa89bd579ff1888992d3e4fe53876230acdaa3a62415c3383f6ab7eb8012102148b1627898c099aa836f992992ab9b110cad637a2b6d57310ad24ea02fb3d18000000
`
xzbnm commented 4 years ago

ok .That's it. used console.log(psbt.extractTransaction().toHex()) instead of psbt.extractTransaction() console.log(psbt.toHex())