《精通比特币》(第二版) – 附录D、Bitcore

来源:https://github.com/tianmingyun/MasterBitcoin2CN/blob/master/appdx-bitcore.md

Bitcore是BitPay提供的一套工具。 其目标是为Bitcoin开发人员提供易于使用的工具。 几乎所有的Bitcore的代码都是用JavaScript编写的。 有一些专门为NodeJS编写的模块。 最后,Bitcore的“节点”模块包括Bitcoin Core的C ++代码。 有关详细信息,请参阅https://bitcore.io

Bitcore的功能列表

  • 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库示例

先决条件

  • NodeJS >= 4.x

如果使用NodeJS和节点REPL:

$ npm install -g bitcore-lib bitcore-p2p

使用bitcore-lib的钱包示例

使用关联的私钥创建新的比特币地址:

> bitcore = require('bitcore-lib')
> privateKey = new bitcore.PrivateKey()
> address = privateKey.toAddress().toString()

创建分层确定性私钥和地址:

> hdPrivateKey = bitcore.HDPrivateKey()
> hdPublicKey = bitcore.HDPublicKey(hdPrivateKey)
> hdAddress = new bitcore.Address(hdPublicKey.publicKey).toString()

从UTXO创建和签署交易:

> utxo = {
  txId: transaction id containing an unspent output,
  outputIndex: output index 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)

替换mempool中的最后一个交易(替换费):

> rbfTx = new Transaction()
           .from(utxo)
           .to(address, 35000)
           .fee(fee*2)
           .enableRBF()
           .sign(privateKeyOfUtxo);
> tx.serialize();
> rbfTx.serialize();

将交易广播到比特币网络(注意:仅广播有效交易;请参阅https://bitnodes.21.co/nodes):
1.将以下代码复制到名为broadcast.js的文件中。
2.tx和rbfTx变量分别是tx.serialize()和rbfTx.serialize()的输出。
3.为了更换费用,对等端必须支持bitcoind选项mempoolreplace并将其设置为1。
4.运行文件节点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();

主题测试文章,只做测试使用。发布者:web3free,转转请注明出处:https://web3free.club/?p=2524

(0)
web3freeweb3free
上一篇 2025年2月7日 上午11:02
下一篇 2025年2月7日 上午11:02

相关推荐

  • 《精通比特币》(第二版) – 原版序言

    关于本书 我第一次偶遇比特币是在2011年年中,当时的反应大概是“哈!书呆子的钱嘛!”因为没能领会它的重要性,我忽视它长达六个月之久,而让我稍感宽慰的是,许多我认识的一些聪明绝顶的人也是这种反应。在一次邮件列表讨论时,我再次接触到了比特币,于是决定阅读中本聪(Satoshi Nakamoto)写的白皮书,研究比特币的权威解释,看看到底是怎么一回事。我仍记得刚…

    2025年2月7日
    00
  • 比特幣跌到八萬三怎麼辦? #BTC

    https://www.youtube.com/watch?v=3Z_jBOBB3h4 https://cointelegraph.com/news/bybit-hack-forensics-show-safe-wallet-compromise-led-to-stolen-funds https://cointelegraph.com/news/bybit…

    2025年2月27日
    00
  • 《精通比特币》(第二版) – 中文版序言

    送你一把打开区块链世界大门的钥匙——《精通比特币第二版》序言 2008年比特币诞生,原本只是一个密码学极客之间的玩物,没想到犹如打开的潘多拉盒子,慢慢席卷全球。在08年以前还没有人能成功地研发出一个运行良好的数字货币出来,直到比特币问世;另外,区块链作为比特币的底层技术,在此之前也是闻所未闻。那么区块链到底有什么魔力,让整个世界为之疯狂呢? 相信很多初学者都…

    2025年2月7日
    00
  • 《精通比特币》(第二版) – 译者序

    郎咸平说过:比特币白给我都不要 巴菲特:比特币是泡沫,不是一种能够生产价值的资产 紫色的股:为什么说比特币是典型的泡沫 …… 也有人说,区块链是最伟大的发明,堪比互联网。 到底比特币是什么?如何判断?难道我们能做的就是人云亦云? 如何能有自己的独立判断? 每个人都有自己的“全知遮蔽”,就像每个人都看不见自己的后脑勺一样。在自己的视野内…

    2025年2月7日
    00
  • 《精通比特币》(第二版) – 第二版更新内容

    第1章 –什么是比特币 更新了比特币的发展历史 更多的使用者案例和更新 更多比特币用户和客户端的示例 更新了如何获取、使用和花费比特币的教程 第2章 – 比特币的原理 很多小的改变、更新和改进 第3章 – 比特币客户端 改进并更新了示例与代码 配置选项和示例 运行比特币节点 更新的库文件 第4章 – 密钥和地址 很多小的改进 改进与新增图表 第5章 – 钱包…

    2025年2月7日
    00

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

在线咨询: QQ交谈

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
自6.2开始主题新增页头通知功能,购买用户可免费升级到最新版体验