mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-11-01 13:10:04 +00:00
119 lines
3.8 KiB
Plaintext
119 lines
3.8 KiB
Plaintext
[[appdx_bitcore]]
|
|
[appendix]
|
|
|
|
== Bitcore
|
|
|
|
|
|
((("Bitcore", id="bitcore17")))Bitcore is a suite of tools provided by BitPay. Its goal is to provide easy-to-use tools for Bitcoin developers. Almost all of Bitcore's code is written in JavaScript. There are some modules written specifically for NodeJS. Finally, the "node" module of Bitcore includes Bitcoin Core's C++ code. Please see https://bitcore.io for more information.
|
|
|
|
=== Bitcore's Feature List
|
|
|
|
* 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 Library Examples
|
|
|
|
==== Prerequisities
|
|
|
|
* NodeJS >= 4.x or use our https://bitcore.io/playground[hosted online playground]
|
|
|
|
If using NodeJS and the node REPL:
|
|
|
|
[source,bash]
|
|
----
|
|
$ npm install -g bitcore-lib bitcore-p2p
|
|
$ NODE_PATH=$(npm list -g | head -1)/node_modules node
|
|
----
|
|
|
|
==== Wallet Examples using bitcore-lib
|
|
|
|
Creating a new bitcoin address with associated private key:
|
|
|
|
----
|
|
> bitcore = require('bitcore-lib')
|
|
> privateKey = new bitcore.PrivateKey()
|
|
> address = privateKey.toAddress().toString()
|
|
----
|
|
|
|
Creating a hierarchical deterministic private key and address:
|
|
|
|
----
|
|
> hdPrivateKey = bitcore.HDPrivateKey()
|
|
> hdPublicKey = bitcore.HDPublicKey(hdPrivateKey)
|
|
> hdAddress = new bitcore.Address(hdPublicKey.publicKey).toString()
|
|
----
|
|
|
|
Creating and signing a transaction from a 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)
|
|
----
|
|
|
|
Replace the last transaction in the mempool (replace-by-fee):
|
|
|
|
----
|
|
> rbfTx = new Transaction()
|
|
.from(utxo)
|
|
.to(address, 35000)
|
|
.fee(fee*2)
|
|
.enableRBF()
|
|
.sign(privateKeyOfUtxo);
|
|
> tx.serialize();
|
|
> rbfTx.serialize();
|
|
----
|
|
|
|
Broadcasting a transaction to the Bitcoin network
|
|
(note: broadcast valid transactions only; refer to https://bitnodes.21.co/nodes[] for peer hosts):
|
|
|
|
1. Copy the code below into a file called _broadcast.js_.
|
|
2. The +tx+ and +rbfTx+ variables are the output of +tx.serialize()+ and +rbfTx.serialize()+, respectively.
|
|
3. In order to replace-by-fee, the peer must support bitcoind option +mempoolreplace+ and have it set to +1+.
|
|
4. Run the file node _broadcast.js_((("", startref="bitcore17"))):
|
|
|
|
----
|
|
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();
|
|
----
|