【问题标题】:How to use RPC javascript to connect to bitcoin daemon running in the server?如何使用 RPC javascript 连接到服务器中运行的比特币守护进程?
【发布时间】:2018-09-10 17:07:55
【问题描述】:

我是 RPC 的新手。我开发了一个使用许多硬币交换的板。但是我的板子是基于其他网站的api来为用户获取BTC地址的。我希望它是独立的。所以我在搜索,我发现我可以在服务器上设置比特币守护进程并使用 RPC(RPCuser、RPCpassword、RPCport ......等)配置它,然后使用 javascript 将我的板连接到比特币守护进程以获取每个用户他的 BTC 钱包地址。我的问题是我是 RPC javascript 开发的新手。那么您能否给我一个简单的 javascript 代码,使用 RPC 连接到比特币守护进程并获取新的钱包地址、余额、发送和接收资金、历史记录……等等?我已经在我的服务器上安装了比特币守护程序并运行了它。我只留下了javascript代码。这是我的 bitcoin.conf 文件信息:

rpcuser=username
rpcpassword=mypassword 
rpcallowip=127.0.0.1
rpcallowip=xxx.xxx.xxx.xxx
rpcallowip=xxx.xxx.xxx.xxx
rpcport=8332
server=1
daemon=1
listen=1
txindex=1

【问题讨论】:

  • ................

标签: javascript ubuntu rpc bitcoin


【解决方案1】:

您可以使用以下命令从终端使用 cURL 进行 RPC 调用:

curl --user 用户名 --data-binary '{"jsonrpc": "1", "id":"", "method": "getblockchaininfo", "params": [] }' -H '内容-type: text/plain' http://localhost:8332/

它会提示输入密码。您需要输入我的密码。您将收到如下回复。

{"result":{"chain":"main","blocks":516395,"headers":516395,"bestblockhash":"00000000000000000041e29f2cc8a69d58f5a697911aa00cac2f85e804dddbcc","difficulty":3511060552899. VerificationProgress“:0.9999903671792401,”InitialBlockDownload“:False,”ChainWork“:”,“size_on_disk”:true,“pruneheight”:515612,“自动_pruning”:true,“prune_target_size”:true,“prune_target_size”:true,“prune_target_size”:true,“prune_target_size”:true,“prune_target_size”:true,“prune_target_size”:629145600,“prune_target_size”:629145600软分叉":[{"id":"bip34","version":2,"reject":{"status":true}},{"id":"bip66","version":3,"reject" :{"status":true}},{"id":"bip65","version":4,"reject":{"status":true}}],"bip9_softforks":{"csv":{" status":"active","startTime":1462060800,"timeout":1493596800,"since":419328},"segwit":{"status":"active","startTime":1479168000,"timeout":1510704000 ,"since":481824}},"warnings":""},"error":null,"id":""}

【讨论】:

  • 谢谢。以及如何在 javascript 中使用它? javascript中是否有任何示例代码?
【解决方案2】:

我正在使用 Node JS。你需要为比特币安装 npm。

请找到以下代码。

在终端中

npm install bitcoin 

在 AppRoutes 文件中:

var bitcoin = require('bitcoin');
var client = new bitcoin.Client({
 host: Your IP Address of Node,
 port: 18332,
 user: UserName,
 pass: Password
});

app.post("/chainInfo", function(req, res) {

   console.log('BlockChainInfo: need');
   client.getBlockchainInfo(function(err, info) {
     if (err) {
       console.log('info: completed');
       return console.error(err);
     }else{
       console.log('info: ' + info);
       res.send(info);
     }
   });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 2019-12-04
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2014-03-25
    • 2021-10-27
    相关资源
    最近更新 更多