-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bitcoin Cash TX redemption #831
Conversation
129dfe4
to
9cf4237
Compare
/** | ||
* Hash transaction for signing a specific input for Bitcoin Cash. | ||
*/ | ||
Transaction.prototype.hashForCashSignature = function (inIndex, prevOutScript, inAmount, hashType) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hashForCash
😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
finding humor in the little things, but will change it if need be :)
src/transaction_builder.js
Outdated
this.tx = new Transaction() | ||
} | ||
|
||
TransactionBuilder.prototype.enableBitcoinCash = function (setting) { | ||
if (typeof setting === 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/setting/enable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Same PR disclaimers as #826, please no political discussion 👍 . |
9cf4237
to
4a4b7fd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (but not to merge)
I think when I agree that merging can be held off until it becomes clear if it's worth having more code to maintain sitting around in the repo. I think it's good to have a PR that is reviewed and in a good enough state to be merged, it means others can use the branch without much worry and it's ready to go if there's a desire to merge it. |
txb.addInput(txid, vout, Transaction.DEFAULT_SEQUENCE, spk) | ||
txb.addOutput('mzDktdwPcWwqg8aZkPotx6aYi4mKvDD7ay', value) | ||
txb.enableBitcoinCash(true) | ||
txb.setVersion(2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, I just wanted to reproduce it's RPC output.
@afk11 I use your branch afk11:opt-in-bitcoincash-sighash with the mainnet bcc transaction. But when I broadcast it, returns "16: mandatory-script-verify-flag-failed (Script failed an OP_EQUALVERIFY operation)" here is my raw transaction: The code is just like your bcc testnet test code except the network. |
|
@panlilu post the code you used? |
Your scriptSig only contains one signature with no pubkey. |
@panlilu Yeah, you are missing your pubkey.
|
@dcousens @junderw Here is my code:
Result:
|
My project: https://github.com/panlilu/bcc-web-redemption |
|
@dcousens |
@panlilu pay-2-pubkeyhash is the default output type, you use it above. P2PKH: txb.addOutput('16EDc1dvditMcc9MEHRicnjgN7nZGe4A7G', value) You could probably fix your code by doing: txb.addInput(txid, vout) Instead of specifying P2PK. P2PK is paying DIRECTLY to the public key, no address involved. This is rare, and technically not as safe cryptographically. |
@panlilu the following document might help in any misunderstandings. |
@dcousens WOOOOW, It works! THANK YOU! |
Just noticed, this PR doesn't play well with multisig's right now, fixMultisigInput doesn't yet know about Bitcoin Cash..this might actually be quite hard since UTXO values aren't known in places like fromTransaction.. |
@afk11 We didn't have any problem, but then of course we had the satoshi values readily available from the txb.sign calling function. I think if you pass the satoshi value to txb.sign it works fine. |
sign can only be called once you have a TransactionBuilder instance. new TransactionBuilder is called inside fromTransaction, but it hasn't got the line for enableBitcoinCash yet. So if you had a partially signed tx, and wanted to use TransactionBuilder.fromTransaction(tx) it wouldn't correctly fix the signature order, and would then refuse to sign. |
@dcousens should I push this branch to bitcoinjs's origin? whatever about ourselves, other developers may feel better about using this remote instead of mine. |
Adds an extra parameter to fromTransaction, which tells the library to expect a value property to be added on each txin which uses bitcoin cash's sighashtype
@afk11 yes |
Pushed a commit which patches fromTransaction so it will work (or make an obvious error) if it isn't provided the value for a Bitcoin Cash input. If you use fromTransaction, and you parse incomplete multisig inputs, two things are required for this to work as before.
If this is not done, transaction_builder will not parse your input signatures, and you will be confused why it isn't signing. |
@@ -0,0 +1,37 @@ | |||
/* global describe, it */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating bitcoin cash raw transaction and broadcasting on network throws following error:
16: mandatory-script-verify-flag-failed (Script failed an OP_EQUALVERIFY operation). Code:-26
I am broadcasting through API call using URL https://cashexplorer.bitcoin.com/api/tx/send
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Show us the code you used to make the transaction. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong pubkeyhash I would say.. Did you sign with the wrong private key?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@junderw
code excerpt which works for BTC:
https://gist.github.com/skynxt/d395a115ae9bfbde6e9744b0e962beaa
code excerpt creating tx for BCH that shows error:
https://gist.github.com/skynxt/2e17f5ff088fc4f47d339d55aee14045
Another excerpt of BCH create tx which throws error too :
https://gist.github.com/skynxt/d395a115ae9bfbde6e9744b0e962beaa
@afk11 I am using correct private key for signing. Probably the above code might give you insight on how transactions are being created at my end.
Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You’re signing the output value not the prevOut value.
You need to pass sign the input value (previous Output value) not the output value
Why this is not merged? |
Because bitcoinjs-lib is for Bitcoin. |
var spk = bscript.pubKey.output.encode(pk) | ||
|
||
var txb = new TransactionBuilder(network) | ||
txb.addInput(txid, vout, Transaction.DEFAULT_SEQUENCE, spk) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is Transaction.DEFAULT_SEQUENCE, spk
necessary? I made a transaction without those parameters
Pretty much echo what @dabura667 said. Our company is getting by fine just rebasing this on top of our own fork, with modifications :) To explain why - there are several coins that make irreconcilable changes with bitcoin, and would only cause a maintenance burden on the developers here. Some coins change transaction serialization format, others use different hash functions, and bitcoin cash poaches BIP143 as it's replay protection. There should be zero expectation that bitcoinjs merges changes for every alt & hard-fork away from bitcoin. Each warrants its own dedicated repository with far better attention and test coverage than we can manage here. |
Does this PR still work? When I tried broadcasting a BCH tx I created with this branch on https://bitcoincash.blockexplorer.com I get the following error. An error occured: |
Did the same code work before Monday’s Hardfork? |
Today is the first time I tried this branch so I don't know wether it worked before Monday’s Hardfork. |
Sounds like you made an invalid signature somewhere.. could you have signed with the wrong txout/script? |
Your right. I was doing |
Please! |
Can we change |
Alternative to #826, with thanks to @junderw.
Avoids making Bitcoin Cash the default hashType, and exposes the functionality once the transaction_builder has been set to allow it. Adds a test case for P2PKH ALL|FORKID.
TransactionBuilder.enableBitcoinCash(setting)
Transaction.hashForCashSignature
Opening this so it's visible and people can use if needed, see #826 for discussions