mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-11-22 16:18:11 +00:00
117 lines
3.6 KiB
Plaintext
117 lines
3.6 KiB
Plaintext
[[appdx_bitcore]]
|
|
[appendix]
|
|
|
|
== Bitcore
|
|
|
|
|
|
((("Bitcore", id="bitcore16")))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)
|
|
* Integrating services directly with Bitcoin Core (bitcore-node)
|
|
|
|
=== Bitcore Library Examples
|
|
|
|
==== Prerequisites
|
|
|
|
* NodeJS >= 4.x
|
|
|
|
If using NodeJS and the node REPL:
|
|
|
|
[source,bash]
|
|
----
|
|
$ npm install -g bitcore-lib bitcore-p2p
|
|
----
|
|
|
|
==== 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 an UTXO:
|
|
|
|
----
|
|
> utxo = {
|
|
txId: txId, // transaction id containing an unspent output
|
|
outputIndex: outputIndex, // output index (e.g. 0)
|
|
address: addressOfUtxo,
|
|
script: bitcore.Script.buildPublicKeyHashOut(addressOfUtxo).toString(),
|
|
satoshis: 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="bitcore16"))):
|
|
|
|
----
|
|
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();
|
|
----
|