Bitcore是BitPay提供的一套工具。 其目标是为Bitcoin开发人员提供易于使用的工具。 几乎所有的Bitcore的代码都是用JavaScript编写的。 有一些专门为NodeJS编写的模块。 最后,Bitcore的“节点”模块包括Bitcoin Core的C ++代码。 有关详细信息,请参阅https://bitcore.io

Bitcore的功能列表

Bitcoin full node (bitcore-node)Block explorer (insight)Block, transaction, and wallet utilities (bitcore-lib)Communicating directly with Bitcoin’s P2P network (bitcore-p2p)Seed entropy mnemonic generation(种子熵助记符) (bitcore-mnemonic)Payment protocol (bitcore-payment-protocol)Message verification and signing (bitcore-message)Elliptic curve Integrated Encryption Scheme(椭圆曲线综合加密方案) (bitcore-ecies)Wallet service (bitcore-wallet-service)Wallet client (bitcore-wallet-client)Playground (bitcore-playground)Integrating services directly with Bitcoin Core (bitcore-node)

Bitcore库示例

先决条件

NodeJS >= 4.x 或者使用hosted online playground

如果使用NodeJS和节点REPL:

  1. $ npm install -g bitcore-lib bitcore-p2p
  2. $ NODE_PATH=$(npm list -g | head -1)/node_modules node

使用bitcore-lib的钱包示例

使用关联的私钥创建新的比特币地址:

  1. > bitcore = require('bitcore-lib')
  2. > privateKey = new bitcore.PrivateKey()
  3. > address = privateKey.toAddress().toString()

创建分层确定性私钥和地址:

  1. > hdPrivateKey = bitcore.HDPrivateKey()
  2. > hdPublicKey = bitcore.HDPublicKey(hdPrivateKey)
  3. > hdAddress = new bitcore.Address(hdPublicKey.publicKey).toString()

从UTXO创建和签署交易:

  1. > utxo = {
  2. txId: transaction id containing an unspent output,
  3. outputIndex: output indexi e.g. 0,
  4. address: addressOfUtxo,
  5. script: bitcore.Script.buildPublicKeyHashOut(addressOfUtxo).toString(),
  6. satoshis: amount sent to the address
  7. }
  8. > fee = 3000 //set appropriately for conditions on the network
  9. > tx = new bitcore.Transaction()
  10. .from(utxo)
  11. .to(address, 35000)
  12. .fee(fee)
  13. .enableRBF()
  14. .sign(privateKeyOfUtxo)

替换mempool中的最后一个交易(替换费):

  1. > rbfTx = new Transaction()
  2. .from(utxo)
  3. .to(address, 35000)
  4. .fee(fee*2)
  5. .enableRBF()
  6. .sign(privateKeyOfUtxo);
  7. > tx.serialize();
  8. > rbfTx.serialize();

将交易广播到比特币网络(注意:仅广播有效交易;请参阅https://bitnodes.21.co/nodes):将以下代码复制到名为broadcast.js的文件中。tx和rbfTx变量分别是tx.serialize()和rbfTx.serialize()的输出。为了更换费用,对等人必须支持bitcoind选项mempoolreplace并将其设置为1。运行文件节点broadcast.js:

  1. var p2p = require('bitcore-p2p');
  2. var bitcore = require('bitcore-lib');
  3. var tx = new bitcore.Transaction('output from serialize function');
  4. var rbfTx = new bitcore.Transaction('output from serialize function');
  5. var host = 'ip address'; //use valid peer listening on tcp 8333
  6. var peer = new p2p.Peer({host: host});
  7. var messages = new p2p.Messages();
  8. peer.on('ready', function() {
  9. var txs = [messages.Transaction(tx), messages.Transaction(rbfTx)];
  10. var index = 0;
  11. var interval = setInterval(function() {
  12. peer.sendMessage(txs[index++]);
  13. console.log('tx: ' + index + ' sent');
  14. if (index === txs.length) {
  15. clearInterval(interval);
  16. console.log('disconnecting from peer: ' + host);
  17. peer.disconnect();
  18. }
  19. }, 2000);
  20. });
  21. peer.connect();