Problem while compiling BitcoinJS for browser

0 votes

I'm trying to compile BitcoinJS library to include it in browser with

<script src="js/bitcoinjs.js"></script>

I'm trying all day but I couldn't. What I do is to follow the instructions

npm -g install bitcoinjs-lib browserify
browserify bitcoinjs-lib -s bitcoin -o bitcoinjs.js

Compilation is successful (errors don't occur). When I try to use it in my webpage

function NewRandomWallet() {

    var keyPair = bitcoin.ECPair.makeRandom()

    // Print your private key (in WIF format)
    // => Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct

    // Print your public key address
    // => 14bZ7YWde4KdRb5YN7GYkToz3EHVCvRxkF


I get followed errors in the console of Chrome:

Unexpected token ...

//because of 3 points ... in oneOf(...types) and tuple(...types) in the bitcoinjs.js file

If I remove these points I get a key and address, my code is working.

Next problem is when I try to create a transaction:

var tx = new bitcoin.TransactionBuilder()

// Add the input (who is paying) of the form [previous transaction hash, index of the output to use]
tx.addInput("aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31", 0)

// Add the output (who to pay to) of the form [payee's address, amount in satoshis]
tx.addOutput("1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK", 15000)

// Initialize a private key using WIF
var keyPair = bitcoin.ECPair.fromWIF("L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy")

// Sign the first input with the new key
tx.sign(0, keyPair)

// Print transaction serialized as hex
// => 0100000001313eb630b128102b60241ca895f1d0ffca21 ...

I get a new error

types.every is not a function

it's pointing to this part of the code in bitcoinjs.js

    function tuple(value, strict) {
      return types.every((type, i) => typeforce(type, value[i], strict));
Aug 30, 2018 in Blockchain by slayer
• 29,360 points

1 answer to this question.

0 votes

The problem iss that you are using browserify wrong way.

cmd> cd:testdir
cmd> npm install bitcoinjs-lib
cmd> npm -g install browserify
cmd> browserify foobar.js -o bitcoinjs.js

foobar.js contains:

Bitcoin = require('bitcoinjs-lib');

to get minified version after use:

cmd> uglifyjs bitcoinjs.js -c -m -r 'Array,BigInteger,Boolean,Buffer,ECPair,Function,Number,Point,Script' -o bitcoinjs.min.js

if you want to get only minified version, use:

cmd> browserify -r bitcoinjs-lib -s Bitcoin | uglifyjs > bitcoinjs.min.js

Now, if you want to generate a wallet, you can do it as:

function NewRandomWallet() {
    var keyPair = Bitcoin.ECPair.makeRandom();
    // Print your private key (in WIF format)
    // => Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct
    // Print your public key address
    // => 14bZ7YWde4KdRb5YN7GYkToz3EHVCvRxkF        

You could add a function to generate a wallet from string before minify the library:

ECPair.makeFromString = function (aStr) {
var hash = Bitcoin.crypto.sha256(aStr)
var d = BigInteger.fromBuffer(hash)
return new ECPair(d)
answered Aug 30, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer
0 votes
1 answer

Can’t find value for key while using CouchDB for Fabric.

In the output you have mentioned: {   "id": ...READ MORE

answered Jul 31, 2018 in Blockchain by slayer
• 29,360 points
+1 vote
2 answers

Challenge while setting up Hyperledger Fabric 1.0 in Ubuntu 16.04

The rocksdb error you're seeing wouldn't be ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points

edited Jun 8, 2020 by Sirajul 999 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
0 votes
1 answer

How do I add multiple recipients for transactions via Blockchain API?

Convert the recipes into JSON objects. x = ...READ MORE

answered Jul 6, 2018 in Blockchain by Perry
• 17,100 points
0 votes
1 answer

Bitcoinjs browser compile creates an empty file

Try the following: Create index.js with the ...READ MORE

answered Aug 30, 2018 in Blockchain by digger
• 26,740 points
+1 vote
1 answer

What problem does pow(proof of work) solves in blockchain?

Proof of Work: When a miner gets a ...READ MORE

answered Aug 6, 2018 in Blockchain by digger
• 26,740 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP