mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-11-05 14:59:38 +00:00
131 lines
3.7 KiB
Plaintext
131 lines
3.7 KiB
Plaintext
[[appdx_bitcore]]
|
|
[appendix]
|
|
|
|
=== Bitcore
|
|
|
|
|
|
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 hosted online playground (https://bitcore.io/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
|
|
|
|
====
|
|
---
|
|
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();
|
|
---
|
|
====
|