bitcore
bitcore copied to clipboard
Assertion failed for demo
https://github.com/bitpay/bitcore/blob/master/packages/bitcore-lib/docs/examples.md#create-a-transaction https://github.com/bitpay/bitcore/blob/master/packages/bitcore-lib/docs/examples.md#spend-from-a-2-of-2-multisig-p2sh-address
var privateKey = new bitcore.PrivateKey('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy'); var utxo = { "txId" : "115e8f72f39fad874cfab0deed11a80f24f967a84079fb56ddf53ea02e308986", "outputIndex" : 0, "address" : "17XBj6iFEsf8kzDMGQk5ghZipxX49VXuaV", "script" : "76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac", "satoshis" : 50000 };
var transaction = new bitcore.Transaction() .from(utxo) .to('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15000) .sign(privateKey);
get.vue?1d8e:243 error Error: Assertion failed at assert (bn.js?399f:6:1) at BN.toBuffer (bn.js?399f:529:1) at Signature.toBuffer.Signature.toDER (signature.js?47cf:169:1) at PublicKeyHashInput.addSignature (publickeyhash.js?7937:132:1) at Transaction.applySignature (transaction.js?97ea:1220:1) at eval (transaction.js?97ea:1189:1) at arrayEach (lodash.js?2ef0:530:1) at Function.forEach (lodash.js?2ef0:9410:1) at Transaction.sign (transaction.js?97ea:1188:1) at eval (get.vue?1d8e:237:1)
Hi,
I have the same issue. The reason is the bn.js library which is used by the elliptic library. The (old) bn.js version which is used overrides the "Buffer" class. The following patch fixes the issue for me.
diff --git a/node_modules/elliptic/node_modules/bn.js/lib/bn.js b/node_modules/elliptic/node_modules/bn.js/lib/bn.js
index 3a4371e..a094403 100644
--- a/node_modules/elliptic/node_modules/bn.js/lib/bn.js
+++ b/node_modules/elliptic/node_modules/bn.js/lib/bn.js
@@ -48,12 +48,14 @@
BN.BN = BN;
BN.wordSize = 26;
- var Buffer;
+ var BNBuffer;
try {
- if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') {
- Buffer = window.Buffer;
+ if (typeof Buffer !== 'undefined') {
+ BNBuffer = Buffer;
+ } else if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') {
+ BNBuffer = window.Buffer;
} else {
- Buffer = require('buffer').Buffer;
+ BNBuffer = require('buffer').Buffer;
}
} catch (e) {
}
@@ -526,8 +528,8 @@
};
BN.prototype.toBuffer = function toBuffer (endian, length) {
- assert(typeof Buffer !== 'undefined');
- return this.toArrayLike(Buffer, endian, length);
+ assert(typeof BNBuffer !== 'undefined');
+ return this.toArrayLike(BNBuffer, endian, length);
};
BN.prototype.toArray = function toArray (endian, length) {
How does one apply this patch?
this is just the git diff, so remove the lines that have a - and replace them with the lines that have a + https://www.npmjs.com/package/patch-package if you are collaborating with a team
@RobertW123 Thanks mate. Could you explain how to apply that patch?
I was also stuck on the same issue. Here is some information I found. It looks like there are a couple of upstream pull requests to update elliptic to use bn.js 5:
- https://github.com/indutny/elliptic/pull/210
- https://github.com/indutny/elliptic/pull/246
As well there is some further discussion here: https://github.com/indutny/elliptic/issues/191#issuecomment-772007899
It appears the second pull request https://github.com/indutny/elliptic/pull/246 fixes the issue for me. To apply it, I thought it would make more sense to apply via the package manager npm rather than manually hotpatching the modules.
I wanted to fork this repo and simply update the package.json
file to point to a newer version of bn.js and to the elliptic pull request above, but it seems there is a long-standing issue with npm that prevents installing subdirectories from a fork.
So I forked this repo, removed all the monorepo stuff, and left only a copy of bitcore-lib with the hotfix applied: bplunkert/bitcore-lib#elliptic_bn_js_hotfix
I do not want to maintain this fork, so I'm hoping that elliptic merges https://github.com/indutny/elliptic/pull/246 soon. In the meantime, you can apply the fix like this by replacing your bitcore-lib line in package.json
:
"dependencies": {
"bitcore-lib": "bplunkert/bitcore-lib#elliptic_bn_js_hotfix",
"bn.js": "=5.1.3",
thanks! @bplunkert
Just installing bn.js fixed the issue for me. Your hotfix worked as well! I've been stuck on this issue and some other issues with Bitcore for days now lol
npm i bn.js
fixed it for me.