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)
console.log(keyPair.toWIF())
// => Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct
// Print your public key address
console.log(keyPair.getAddress())
// => 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
console.log(tx.build().toHex())
// => 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));
}