bitcoinjs-lib icon indicating copy to clipboard operation
bitcoinjs-lib copied to clipboard

Info with Angular

Open pf55351 opened this issue 4 years ago • 4 comments
trafficstars

hi could i use this lib with angular framework, because i tried but i have some errors.

pf55351 avatar Mar 21 '21 15:03 pf55351

In particular i have install the module with npm I use module in my service import * as bitcoin from 'bitcoinlib-js'

but when i use the simple method i receive this:

TypeError: Cannot read property 'call' of undefined at Hash.CipherBase (index.js:7) at new Hash (browser.js:9) at createHash (browser.js:29) at sha256 (crypto.js:23) at Object.hash160 (crypto.js:29) at Object.p2pkh (p2pkh.js:109) at AddressService. (address.service.ts:15)

pf55351 avatar Mar 21 '21 15:03 pf55351

Cryptographic operations could be a little bit of a pain in Angular. They are planning to work on it in the next release. There are quick fixes available for this kind of problem over the internet. One which I found and worked for me -

  1. In your project root folder, create a file called patch.js containing the following code:

` const fs = require('fs'); const f = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js';

fs.readFile(f, 'utf8', function (err,data) { if (err) { return console.log(err); } var result = data.replace(/node: false/g, 'node: {crypto: true, stream: true}');

fs.writeFile(f, result, 'utf8', function (err) { if (err) return console.log(err); }); }); `

  1. In your package.json , under scripts add the entry "postinstall": "node patch.js" , example:

"scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", "postinstall": "node patch.js" },

  1. Reinstall your project’s dependencies: $ rm -R node_modules && npm install

Let me know if it works for you.

abhishekS14 avatar Mar 22 '21 14:03 abhishekS14

Cryptographic operations could be a little bit of a pain in Angular. They are planning to work on it in the next release. There are quick fixes available for this kind of problem over the internet. One which I found and worked for me -

  1. In your project root folder, create a file called patch.js containing the following code:

` const fs = require('fs'); const f = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js';

fs.readFile(f, 'utf8', function (err,data) { if (err) { return console.log(err); } var result = data.replace(/node: false/g, 'node: {crypto: true, stream: true}');

fs.writeFile(f, result, 'utf8', function (err) { if (err) return console.log(err); }); }); `

  1. In your package.json , under scripts add the entry "postinstall": "node patch.js" , example:

"scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", "postinstall": "node patch.js" },

  1. Reinstall your project’s dependencies: $ rm -R node_modules && npm install

Let me know if it works for you.

Hi i red yet your solution on the web... but i'm using angular 11 and not found node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js'.... let me know how can i do... now i'm using browserify, but it's not a clean solution.

Thank you

pf55351 avatar Mar 22 '21 16:03 pf55351

We are using this library in an angular 11 project as well. The script that was posted has to be updated to work with ng11 (and maybe also ng10, not sure). You can find our script / project here: https://github.com/airgap-it/airgap-vault/blob/master/config/patch_crypto.js

AndreasGassmann avatar Apr 03 '21 00:04 AndreasGassmann