Bitcore是BitPay提供的一套工具。 其目標是為Bitcoin開發人員提供易于使用的工具。 幾乎所有的Bitcore的代碼都是用JavaScript編寫的。 有一些專門為NodeJS編寫的模塊。 最后,Bitcore的“節點”模塊包括Bitcoin Core的C ++代碼。 有關詳細信息,請參閱[https://bitcore.io](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](https://bitcore.io/playground)
如果使用NodeJS和節點REPL:
```
$ npm install -g bitcore-lib bitcore-p2p
$ NODE_PATH=$(npm list -g | head -1)/node_modules node
```
## 使用bitcore-lib的錢包示例
使用關聯的私鑰創建新的比特幣地址:
```
> bitcore = require('bitcore-lib')
> privateKey = new bitcore.PrivateKey()
> address = privateKey.toAddress().toString()
```
創建分層確定性私鑰和地址:
```
> hdPrivateKey = bitcore.HDPrivateKey()
> hdPublicKey = bitcore.HDPublicKey(hdPrivateKey)
> hdAddress = new bitcore.Address(hdPublicKey.publicKey).toString()
```
從UTXO創建和簽署交易:
```
> utxo = {
txId: transaction id containing an unspent output,
outputIndex: output indexi e.g. 0,
address: addressOfUtxo,
script: bitcore.Script.buildPublicKeyHashOut(addressOfUtxo).toString(),
satoshis: amount sent to the address
}
> fee = 3000 //set appropriately for conditions on the network
> tx = new bitcore.Transaction()
.from(utxo)
.to(address, 35000)
.fee(fee)
.enableRBF()
.sign(privateKeyOfUtxo)
```
替換mempool中的最后一個交易(替換費):
```
> rbfTx = new Transaction()
.from(utxo)
.to(address, 35000)
.fee(fee*2)
.enableRBF()
.sign(privateKeyOfUtxo);
> tx.serialize();
> rbfTx.serialize();
```
將交易廣播到比特幣網絡(注意:僅廣播有效交易;請參閱[https://bitnodes.21.co/nodes](https://bitnodes.21.co/nodes)):
將以下代碼復制到名為broadcast.js的文件中。
tx和rbfTx變量分別是tx.serialize()和rbfTx.serialize()的輸出。
為了更換費用,對等人必須支持bitcoind選項mempoolreplace并將其設置為1。
運行文件節點broadcast.js:
```
var p2p = require('bitcore-p2p');
var bitcore = require('bitcore-lib');
var tx = new bitcore.Transaction('output from serialize function');
var rbfTx = new bitcore.Transaction('output from serialize function');
var host = 'ip address'; //use valid peer listening on tcp 8333
var peer = new p2p.Peer({host: host});
var messages = new p2p.Messages();
peer.on('ready', function() {
var txs = [messages.Transaction(tx), messages.Transaction(rbfTx)];
var index = 0;
var interval = setInterval(function() {
peer.sendMessage(txs[index++]);
console.log('tx: ' + index + ' sent');
if (index === txs.length) {
clearInterval(interval);
console.log('disconnecting from peer: ' + host);
peer.disconnect();
}
}, 2000);
});
peer.connect();
```