bitcoinjs / bitcoinjs-lib

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

Invalid transaction builder output section #1166

Closed smdmitry closed 5 years ago

smdmitry commented 5 years ago

I am using bitcoinjs-lib@4.0.1 with Vue,js in browser (Chrome, Firefox) and trying to build transaction.

The following sample code: create a 1-to-1 Transaction

Returns this transaction hex: 01000000019d344070eac3fe6e394a16d06d7704a7d5c0a10eb2a2c16bc98842b7cc20d561000000006d00483045022100c766a1ed287200afceaa6895e24c7e75369500be34ecf2a0c6f584e643a28e46022050a43b8fb6726397422521646df07197d4360ea09c6d66c9d6e389d51701cbfb010021029f50f51d63b345039a290c94bffd3180c99ed659ff6ea6b1242bca47eb93b59fffffffff01e02e0000000000001a0000001406afd46bcdfd22ef94ac122aa11f241244a37ecc000000000000

Which has invalid outputs format. And is not equal to sample transaction builder result.

Is this a bug in bitcoinjs-lib code, or something wrong with my environment?

junderw commented 5 years ago

Show us your code.

looking at your output it says:

e02e000000000000 // sending 0.00012 btc
1a // scriptPubkey is 26 bytes long
0000001406afd46bcdfd22ef94ac122aa11f241244a37ecc0000
OP_0 OP_0 OP_0 <PUSH: 06afd46bcdfd22ef94ac122aa11f241244a37ecc> OP_0 OP_0

Also your input has OP_0 before and after the signature, but then pushes a public key instead of a multisig redeemscript. (we use OP_0 as placeholders for incomplete multisig transactions) but you said you were trying to do a 1-to-1 transaction.

So show us the code, as I have no clue how it could get THIS messed up.

smdmitry commented 5 years ago

The code is exactly like in tests.

<template>
</template>
<script>
    let bitcoin = require('bitcoinjs-lib');
    export default {
        name: 'Test',
        props: [],
        data () {
            return {}
        },
        computed: {
        },
        created() {
            const alice = bitcoin.ECPair.fromWIF('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy');
            const txb = new bitcoin.TransactionBuilder();

            txb.setVersion(1);
            txb.addInput('61d520ccb74288c96bc1a2b20ea1c0d5a704776dd0164a396efec3ea7040349d', 0);
            txb.addOutput('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP', 12000);

            txb.sign(0, alice);

            console.log(txb.build().toHex());
        },
        methods: {
        }
    }
</script>
dabura667 commented 5 years ago

Ok, definitely a problem with how Vue,js packages everything.

smdmitry commented 5 years ago

It appears that you broke something from version 3.3.2. 4.0.1 version does not run on runkit: https://npm.runkit.com/bitcoinjs-lib But 3.3.2 version runs ok and produces expected transaction.

dabura667 commented 5 years ago

you broke something

write a test that breaks and we'll look into it.

we support node v6 v8 v9 v10 all the tests pass.

dcousens commented 5 years ago

@smdmitry if you could please provide a method for us to reproduce this error, that would help us resolve your problem for the benefit of everyone :)

junderw commented 5 years ago

4.0.1 version does not run on runkit

This is a problem with npm, they don't have tiny-secp256k1 installed. I reported it. This is not a problem with this library.

dcousens commented 5 years ago

@junderw what is it doing instead though?

.sign should explode. .build should explode.

Why isn't it exploding with exceptions, instead making this malformed abomination of a transaction.

Repro please!

junderw commented 5 years ago

@dcousens it's not doing anything. It's exploding. So nothing is showing up in console.log.

I think OP is saying "runkit is broken" as a separate issue besides this Vue.JS problem.

I have never used vue.js........ so I don't know how to reproduce the error.

djstanton commented 5 years ago

hi all, i'm posted here code with invalid transaction hex, I hope this helps.

dcousens commented 5 years ago

Do we close? Or is there something we can action here?

junderw commented 5 years ago

@smdmitry the runkit issue is unrelated to your issue, and is easily resolved by telling npm to install tiny-secp256k1

Please tell us how to use vue.js? How to use the code example you gave us?

Give us a link to a project and a list of steps on how to reproduce the error.

dcousens commented 5 years ago

Will re open if needed.

djstanton commented 5 years ago

@dcousens @junderw, thanks, problem solved. Bitcoin-ops package package uses a json file. Our webpack config used file-loader.

{ test: /\.(png|woff|woff2|eot|ttf|jpg|gif|svg|json)$/, loader: 'file-loader', options: { name: '[name].[ext]?[hash]' } }

and in this line var OPS = require('./index.json') file-loader returned path to file, not its contents. In the future, if we need to use json files, there will be problems again. Maybe all the same the OPS constans are to be moved to map.js, then people will not depend on their loaders?

dcousens commented 5 years ago

@djstanton requiring JSON files is common... if you don't support it, you will probably be burnt by other dependencies. I'd resolve the cause not the symptom. Personally.

djstanton commented 5 years ago

@dcousens on this time we removed json from flie-loader, thanks.