如何为浏览器编译BitcoinJS

时间:2015-11-05 18:31:48

标签: compilation browserify bitcoin

我正在尝试编译BitcoinJS库,将其包含在浏览器中

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

我整天都在努力,但我不能。 我所做的是按照说明进行操作

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

编译成功(不会发生错误)。 当我尝试在我的网页中使用它时

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

}

我在Chrome控制台中遇到错误:

  

意外的令牌......

     

//因为bitcoinjs.js文件中的oneOf(... types)和tuple(... types)中的3分...

如果我删除这些点,我会得到一个密钥和地址,我的代码正常运行。 为什么会出现这些观点?

下一个问题是当我尝试创建一个事务时:

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 ...

我收到了新错误

  

types.every不是函数

它指向bitcoinjs.js中的这部分代码

    function tuple(value, strict) {
      return types.every((type, i) => typeforce(type, value[i], strict));
    }

有什么想法吗?库的代码是错误的还是编译错误的方式?

2 个答案:

答案 0 :(得分:0)

问题是我使用了浏览器错误的方法。 我在Windows操作系统的CMD中使用此命令编译了bitcoinjs库:

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

foobar.js包含:

Bitcoin = require('bitcoinjs-lib');

使用后获得缩小版本:

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

如果您只想获得缩小版,请使用:

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

现在,如果您想生成钱包,可以这样做:

function NewRandomWallet() {
    var keyPair = Bitcoin.ECPair.makeRandom();
    // Print your private key (in WIF format)
    $('#private_key').val(keyPair.toWIF());
    // => Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct
    // Print your public key address
    $('#address').val(keyPair.getAddress());
    // => 14bZ7YWde4KdRb5YN7GYkToz3EHVCvRxkF        
}

您可以在缩小库之前添加一个函数来从字符串生成钱包:

ECPair.makeFromString = function (aStr) {
var hash = Bitcoin.crypto.sha256(aStr)
var d = BigInteger.fromBuffer(hash)
return new ECPair(d)
}

答案 1 :(得分:0)

为方便起见,我刚刚将bitcoinjs-lib与编译后的最小分发文件夹分叉。

https://github.com/davidapple/bitcoinjs-lib-for-browsers

./node_modules/.bin/nodeunit test/*.test.js SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:373:25) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object.<anonymous> (/home/travis/build/restify/node-restify/node_modules/anumargak/node_modules/randexp/lib/randexp.js:4:16) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object.<anonymous> (/home/travis/build/restify/node-restify/node_modules/anumargak/src/util.js:3:15) at Module._compile (module.js:409:26) make: *** [test] Error 1 npmERR! Test failed. See above for more details.

bower install bitcoinjs-lib-for-browsers#4.0.2 --save