Skip to content
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

Assertion failed for demo #3357

Open
stefaniepei opened this issue Apr 26, 2022 · 7 comments
Open

Assertion failed for demo #3357

stefaniepei opened this issue Apr 26, 2022 · 7 comments

Comments

@stefaniepei
Copy link

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)

@RobertW123
Copy link

RobertW123 commented May 10, 2022

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) {

@christroutner
Copy link

How does one apply this patch?

@MaxSpiro
Copy link

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

@Douglasacost
Copy link

@RobertW123 Thanks mate. Could you explain how to apply that patch?

@bplunkert
Copy link

bplunkert commented Nov 9, 2022

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:

As well there is some further discussion here:
indutny/elliptic#191 (comment)

It appears the second pull request indutny/elliptic#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 indutny/elliptic#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",

@THEmmanuel
Copy link

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

@THEmmanuel
Copy link

npm i bn.js fixed it for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants