bitcoinjs / bitcoinjs-lib

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

Changing network for multisigOutput and ScriptHashOutput #618

Closed starsoccer closed 7 years ago

starsoccer commented 8 years ago

Unlike bitcoin.ECPair.makeRandom, bitcoin.script.multisigOutput and bitcoin.script.scriptHashOutput do not seem to support setting a different network. Is there a way to set a default network or any way to change the network typed used in both functions?

starsoccer commented 8 years ago

I have figured it out. For those having issues, you simply need to add the network type to fromoutputscript.

dcousens commented 8 years ago

add the network type to fromoutputscript.

This is address.fromOutputScript I assume? That is because an address and WIF data are the only data structures that capture network information in any way.

Scripts, private keys and transactions all don't care about the implied network. However, as you have noticed, ECPair requires a network for the simple fact of the toWIF and getAddress methods.

starsoccer commented 8 years ago

Yes, I later realized that the network type needed to simply be passed in bitcoin.address.fromOutputScript

On an unrelated note, I am working on creating a raw transaction with no signature using the below code but keep getting an error, "Transaction is not complete". It seems to require that I sign the transaction atleast once before I can get the ID or hex value of it. Is there anyway around that?I If I just uncomment //tx.sign(0, keyPairs[0]) it works perfectly though.

var bitcoin = require('bitcoinjs-lib');

var tx = new bitcoin.TransactionBuilder()
tx.addInput("TRANSACTIONIDHERE", 0);
tx.addOutput('ADDRESSHERE', 0.01 * 100000000)

//tx.sign(0, keyPairs[0])

var tx2 = tx.build()
console.log(tx2.getId()) // transaction id
console.log(tx2.toHex()) // push tx to network
dcousens commented 8 years ago

tx.buildIncomplete()?

It seems to require that I sign the transaction atleast once before I can get the ID or hex value of it.

The txId will change after signing, be wary of that.

starsoccer commented 8 years ago

Thanks, thats what I was looking for.

starsoccer commented 8 years ago

Sorry to post again here, but I couldnt find any example or reference code on how to check or verify a signature on a raw transaction, similar to a bitcoin message verification.

Basically I just want to plug in a raw transaction spending from a multisig as string(hex), and confirm if address/public key X has signed it or not. If its easier simply returning an array if address/public keys that have signed it could work as well.

dcousens commented 8 years ago

Ping @fanatid, thoughts on bitcoin.script.extract*?

dcousens commented 8 years ago

Alternatively @starsoccer, just import the transaction into TransactionBuilder and attempt to sign any of the multisig inputs. If it works, awesome! But that isn't very reassuring...

starsoccer commented 8 years ago

I am not really sure what you mean when you said match redeemscriptchunks. The only mention I can find of redeemscriptchunks was using processScript.

Would there be a way to compare an original raw transaction to this new signed transaction and be able to figure out from there what public key has signed it? I can live with not knowing who should be signing it as I would have the redeemscript stored.

To go a bit more into detail into exactly what I am trying to do, basically I will provide the user a raw transaction to sign which will be stored. Once signed I need to verify that its the correct rawtransactiom(user did not maliciously change it), and then verify the user used the correct public key to sign it. Once signed I would then broadcast it. The transaction creation code I have already via your help, and the broadcast code I have as well. So I just need to verify the rawtransaction hasnt changed, and then verify the user signed it correctly.

Preferably bitcoin.script.extract would return the input/outputs and/or the rawtransaction unsigned, if its possible to reverse engineer, and then return a list of public keys that have signed it or keys that should sign it and which have/havent.

starsoccer commented 7 years ago

@dcousens ssorry to reopen this case, but it looks like #681 might do exactly what I mentioned and it seems to be merged. Could you just clarify for me how I would go about using it as I am having a bit of trouble figuring it out.

For the sake of this example, lets just assume I am going to be importing the transaction as a string. I basically just want it to return a list of address or public keys that need to sign it preferably with a breakdown of who did/didnt sign it. Also if possible returning a break down of the inputs/outputs so I can confirm between the raw transaction given to the first party, and the first signature, that there were no alterations made.

dcousens commented 7 years ago

Closing, but re-open @starsoccer if you still need help.

Once signed I need to verify that its the correct rawtransactiom(user did not maliciously change it)

The easiest way to do that would just be to match the transactions ID is a match without any scriptSigs. If you want to investigate the redeemScripts... then you need to do exactly that.

starsoccer commented 7 years ago

Yes I think this can be closed. I ended up using a mix of 2 different functions to basically get the address that funds are being sent to along with the amount, and then another function to get the redeemscript from the rawtx and confirm it is the same redeemscript.