1
0
mirror of https://github.com/bitcoinbook/bitcoinbook synced 2024-11-18 06:08:18 +00:00
bitcoinbook/appdx-bitcore.asciidoc

119 lines
3.8 KiB
Plaintext
Raw Normal View History

2016-12-01 21:27:00 +00:00
[[appdx_bitcore]]
[appendix]
2017-04-18 19:31:52 +00:00
== Bitcore
2016-12-01 21:27:00 +00:00
((("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.
2016-12-01 21:27:00 +00:00
=== Bitcore's Feature List
* Bitcoin full node (bitcore-node)
2017-05-10 17:04:27 +00:00
* Block explorer (insight)
* Block, transaction, and wallet utilities (bitcore-lib)
2016-12-01 21:27:00 +00:00
* Communicating directly with Bitcoin's P2P network (bitcore-p2p)
* Seed entropy mnemonic generation (bitcore-mnemonic)
2017-05-10 17:04:27 +00:00
* Payment protocol (bitcore-payment-protocol)
2016-12-01 21:27:00 +00:00
* Message verification and signing (bitcore-message)
2017-05-10 17:04:27 +00:00
* Elliptic curve Integrated Encryption Scheme (bitcore-ecies)
* Wallet service (bitcore-wallet-service)
* Wallet client (bitcore-wallet-client)
2016-12-01 21:27:00 +00:00
* Playground (bitcore-playground)
2017-05-10 17:04:27 +00:00
* Integrating services directly with Bitcoin Core (bitcore-node)
2016-12-01 21:27:00 +00:00
=== Bitcore Library Examples
2019-08-27 12:31:52 +00:00
==== Prerequisites
2016-12-01 21:27:00 +00:00
* NodeJS >= 4.x or use our https://bitcore.io/playground[hosted online playground]
2016-12-01 21:27:00 +00:00
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
2017-05-10 17:04:27 +00:00
Creating a new bitcoin address with associated private key:
2016-12-01 21:27:00 +00:00
----
> bitcore = require('bitcore-lib')
> privateKey = new bitcore.PrivateKey()
> address = privateKey.toAddress().toString()
----
2017-05-10 17:04:27 +00:00
Creating a hierarchical deterministic private key and address:
2016-12-01 21:27:00 +00:00
----
> hdPrivateKey = bitcore.HDPrivateKey()
> hdPublicKey = bitcore.HDPublicKey(hdPrivateKey)
> hdAddress = new bitcore.Address(hdPublicKey.publicKey).toString()
----
Creating and signing a transaction from an UTXO:
2016-12-01 21:27:00 +00:00
----
> utxo = {
txId: transaction id containing an unspent output,
2018-03-26 21:27:06 +00:00
outputIndex: output index e.g. 0,
2016-12-01 21:27:00 +00:00
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)
----
2017-05-10 17:04:27 +00:00
Replace the last transaction in the mempool (replace-by-fee):
2016-12-01 21:27:00 +00:00
----
2016-12-01 21:27:00 +00:00
> rbfTx = new Transaction()
.from(utxo)
.to(address, 35000)
.fee(fee*2)
.enableRBF()
.sign(privateKeyOfUtxo);
> tx.serialize();
> rbfTx.serialize();
----
2016-12-01 21:27:00 +00:00
Broadcasting a transaction to the Bitcoin network
2017-05-10 17:04:27 +00:00
(note: broadcast valid transactions only; refer to https://bitnodes.21.co/nodes[] for peer hosts):
2016-12-01 21:27:00 +00:00
2017-05-10 17:04:27 +00:00
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"))):
2016-12-01 21:27:00 +00:00
----
2016-12-01 21:27:00 +00:00
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();
----