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

117 lines
3.6 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)
* 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
2016-12-01 21:27:00 +00:00
If using NodeJS and the node REPL:
[source,bash]
----
$ npm install -g bitcore-lib bitcore-p2p
----
==== 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 = {
2021-03-04 22:32:40 +00:00
txId: txId, // transaction id containing an unspent output
outputIndex: outputIndex, // output index (e.g. 0)
2016-12-01 21:27:00 +00:00
address: addressOfUtxo,
script: bitcore.Script.buildPublicKeyHashOut(addressOfUtxo).toString(),
2021-03-04 22:32:40 +00:00
satoshis: satoshis // amount sent to the address
2016-12-01 21:27:00 +00:00
}
> 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();
----