传送门
Bitpay 架构体系初步分析
Bitpay-Insight接口分析-1
Bitpay-Insight接口分析-2
Bitpay-Insight接口分析-3
在Bitpay的架构中,我们发现bitcore-node中insight-api服务起了一个核心的承接作用,它既向下读取bitcoin节点中信息,又向上暴露rest服务,给区块链浏览器和钱包调用.
从源码index.js中,我们可以发现,它的服务包括:
// Block routes
// 块相关服务,一共4种,块列表,根据hash读取块信息,根据hash读取块原始信息(16进制),根据高度读取块信息.
var blocks = new BlockController(blockOptions);
app.get('/blocks', this.cacheShort(), blocks.list.bind(blocks));
app.get('/block/:blockHash', this.cacheShort(), blocks.checkBlockHash.bind(blocks), blocks.show.bind(blocks));
app.param('blockHash', blocks.block.bind(blocks));
app.get('/rawblock/:blockHash', this.cacheLong(), blocks.checkBlockHash.bind(blocks), blocks.showRaw.bind(blocks));
app.param('blockHash', blocks.rawBlock.bind(blocks));
app.get('/block-index/:height', this.cacheShort(), blocks.blockIndex.bind(blocks));
app.param('height', blocks.blockIndex.bind(blocks));
// Transaction routes
// 交易相关服务,一共4种,根据交易id读取交易信息,交易列表,发送交易(post),根据交易id读取交易原始信息(16进制)
var transactions = new TxController(this.node, this.translateAddresses);
app.get('/tx/:txid', this.cacheShort(), transactions.show.bind(transactions));
app.param('txid', transactions.transaction.bind(transactions));
app.get('/txs', this.cacheShort(), transactions.list.bind(transactions));
app.post('/tx/send', transactions.send.bind(transactions));
// Raw Routes
app.get('/rawtx/:txid', this.cacheLong(), transactions.showRaw.bind(transactions));
app.param('txid', transactions.rawTransaction.bind(transactions));
// Address routes
// 地址相关服务,一共6种,地址交易显示,未发费utxo显示,多地相关址utxo显示(get+post),多地址相关交易显示(get+post)
var addresses = new AddressController(this.node, this.translateAddresses);
app.get('/addr/:addr', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.show.bind(addresses));
app.get('/addr/:addr/utxo', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.utxo.bind(addresses));
app.get('/addrs/:addrs/utxo', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multiutxo.bind(addresses));
app.post('/addrs/utxo', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multiutxo.bind(addresses));
app.get('/addrs/:addrs/txs', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multitxs.bind(addresses));
app.post('/addrs/txs', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multitxs.bind(addresses));
// Address property routes
// 地址属性相关服务,一共4种,地址余额,地址总接收值,地址总发送值,未确认(6块)总额
app.get('/addr/:addr/balance', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.balance.bind(addresses));
app.get('/addr/:addr/totalReceived', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.totalReceived.bind(addresses));
app.get('/addr/:addr/totalSent', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.totalSent.bind(addresses));
app.get('/addr/:addr/unconfirmedBalance', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.unconfirmedBalance.bind(addresses));
// Status route
// 状态相关服务,一共4种,服务状态,同步信息,连接节点信息,版本号
var status = new StatusController(this.node);
app.get('/status', this.cacheShort(), status.show.bind(status));
app.get('/sync', this.cacheShort(), status.sync.bind(status));
app.get('/peer', this.cacheShort(), status.peer.bind(status));
app.get('/version', this.cacheShort(), status.version.bind(status));
// messages routes
// 消息通知服务,一共2种,获取消息,验证消息(这个是bitpay提供的bitcore-message)
var messages = new MessagesController(this.node);
app.get('/messages/verify', messages.verify.bind(messages));
app.post('/messages/verify', messages.verify.bind(messages));
// Utils route
// 工具服务,目前就一种,手续费评估
var utils = new UtilsController(this.node);
app.get('/utils/estimatefee', utils.estimateFee.bind(utils));
// Currency
// 汇率服务,提供kraken提供的当前价格.
var currency = new CurrencyController({
node: this.node,
currencyRefresh: this.currencyRefresh,
currency: this.currency
});
app.get('/currency', currency.index.bind(currency));
// Other api endpoints
// 其他浏览器支持的币种信息
app.get('/explorers' , this._getExplorers.bind(this));
下面就根据各个模块,从最新的5.0.0-beta.44接口提供者那里获取真实的交互,以便于后续调整和对接.
Block块相关服务
- 获取块:
请求:
https://bch-insight.bitpay.com/api/blocks
响应:
{
"blocks": [{
"height": 544972,
"size": 1126,
"virtualSize": 1126,
"hash": "000000000000000001aadeb33e33230bf35eaa6d4303c57074a8040077c7aef1",
"time": 1535208512,
"txlength": 5,
"poolInfo": {}
}, {
"height": 544971,
"size": 33307,
"virtualSize": 33307,
"hash": "000000000000000000b193ca28b0388c6900cde1ee3d77578136a4cbbe9a2d59",
"time": 1535208493,
"txlength": 72,
"poolInfo": {
"poolName": "AntMiner",
"url": "https://bitmaintech.com/"
}
}],
"length": 2,
"pagination": {
"next": "2018-08-26",
"prev": "2018-08-24",
"currentTs": 1535241599,
"current": "2018-08-25",
"isToday": true,
"more": false
}
}
- 获取块:
请求:
https://bch-insight.bitpay.com/api/block/000000000000000001aadeb33e33230bf35eaa6d4303c57074a8040077c7aef1
响应:
{
"hash": "000000000000000001aadeb33e33230bf35eaa6d4303c57074a8040077c7aef1",
"size": 1126,
"height": 544972,
"version": 536870912,
"merkleroot": "820df9ed8f57fe6464e5032fe0a9cbf4f986d793dcf78192b8a899993ba27d4b",
"tx": ["38f6aa1c8112d681be031ea5113c1d4ffa9d245e0515cf27ed45210307cba86f", "2b45705210e20bbcb1f8c22ab26cddca086964f0122df42cd3dc48a2e1c563aa", "382cc1a07ccfc94676aa6373c4d19ac3a8485064b665335d270713393f0d6514", "60fe5f9173a1c78f2e0cbcff2b69baef5b90ee437c7363a367cbffb0e3a3c755", "ec1e19f1e121b1650d174e322bd3b674e0c943e5db6bc4a5422d6f177ea89bf3"],
"time": 1535208512,
"nonce": 4137301843,
"bits": 402788059,
"difficulty": 534246483976,
"chainwork": "000000000000000000000000000000000000000000bc51a0a20e00709ff0f905",
"confirmations": 1,
"previousblockhash": "000000000000000000b193ca28b0388c6900cde1ee3d77578136a4cbbe9a2d59",
"reward": 12.50007423,
"isMainChain": true,
"poolInfo": {}
}
- 获取块原始数据:
请求:
https://bch-insight.bitpay.com/api/rawblock/000000000000000001aadeb33e33230bf35eaa6d4303c57074a8040077c7aef1
响应:
{"rawblock":"00000020592d9abecba4368157773deee1cd00698c38b028ca93b1000000000000000000820df9ed8f57fe6464e5032fe0a9cbf4f986d793dcf78192b8a899993ba27d4b406c815bdb0e021853379af60501000000010000000000000000000000000000000000000000000000000000000000000000ffffffff5c03cc5008192f5669614254432f4d696e6564206279206e6d73393133352f2cfabe6d6d184dc8e1ef0d19eddd4581a029620e0de25eaad2d6a1787c9df61d782045f37f0400000000000000109b63d30764e69a2d914f1943512b0100ffffffff017f99814a000000001976a914f1c075a01882ae0972f95d3a4177c86c852b7d9188ac000000000100000001e19cfde39d5ce89d39200dd0e7e7bbc9a2ea8bcf3a9c74b15edb40b45c921240000000006b483045022100f06cf926c0c630f678434cb6b8eb602064d0615f41bca5ad3c2b31d87692ab470220149e26ee6d4b0fb375dcb09ef81996365b853a8538be8cc0dc61b5db9f9e71be4121038dcabe66cc702624e6c7f2a437df7c36a68962a9becaa0f5f3e68a36839b5faaffffffff01c6dffe65010000001976a9146279b5d9e239500612d4f57d9cb1383bc6957a2288ac000000000200000001e7023fd9f341275c71181dc8797c987495d8fbe12db5619ac86d8c3e2fb1c197010000006b4830450221008a4a6b2d29bb5c5d96f55db2f513f8ef584aaa313c9112c745647fbd9d104cc00220646830de592a2ccea9abd9e30b4d9ded0cacbc66515792e56ca6291f02da84dd412102e730398dca951ded3e419829579c857f956d861ad5738d032ef17ccd2ca370aeffffffff02600e4e07000000001976a914cbe7d31810af5abb158d531fce0be1048021a30a88ace6b90100000000001976a9140a061221aa52f3942e8b39ad2b2c98abf058833488ac0000000002000000017755ce1733b8f89f14feceff96c9094ed571d78554478e733fa42abdfcffb5f3010000006b483045022100a9150be4afc7c0caffeed0b8f138701fc340be8ab0a0612862e600169b738acc0220792e8e9a274debb52c7a4ff76b84dbe1bc546efac36076dbac7d2211531f111b41210346e53d43ae00387c5198579129d4cf2e03ec6a66d7aee2f3efeba10c07cd64b3ffffffff02ff54bd03000000001976a914aa1604b040a7d3c86401fba95d1731b992d366e888ac19798c0d000000001976a9143bf58baf7fb8b69f7f4ffbb1a146c54ac78873e188ac00000000010000000145213921cec40e505c6f16e649b8205944e31dc85182a843909b234da605bc42010000006b483045022100bdf5c7dcf2baf938be5d336b222591f8e3435e9d67d8330544cc0cab40c631a002207979ad560335d35596e0d0141ff1e5ba7b363ce83a84328e6b549185b8506455412103cb8a7ef04cadfdc25425d84162f553bfee89b3109e939a3998c336b2f80642f0ffffffff02abdb00000000000017a9140c1ffe16047f2177e9e55b7b22e4ab699332b0d4870f763800000000001976a91429c69c5c468eab11fc22a558d80d7c555465595c88ac00000000"}
- 根据高度读取块信息:
请求:
https://bch-insight.bitpay.com/api//block-index/544972
响应:
{
"blockHash": "000000000000000001aadeb33e33230bf35eaa6d4303c57074a8040077c7aef1"
}
仅返回块的hash值
tx/txs/rawtx 交易相关服务
- 根据交易id读取交易信息:
请求:
https://bch-insight.bitpay.com/api/tx/9c22557a6c36127daea66dd80657ca6c65a67fe665f23f980b4df3c365d7cdc5
响应:
{
"txid": "9c22557a6c36127daea66dd80657ca6c65a67fe665f23f980b4df3c365d7cdc5",
"version": 1,
"locktime": 0,
"isCoinBase": true,
"vin": [{
"coinbase": "03c85008174d696e656420627920416e74506f6f6c3426205b816995350600008e0b0100",
"sequence": 4294967295,
"n": 0
}],
"vout": [{
"value": "12.50056121",
"n": 0,
"scriptPubKey": {
"hex": "76a9141fefe863a7e0ddc767780ce3bedc564cf5cbda6388ac",
"asm": "OP_DUP OP_HASH160 1fefe863a7e0ddc767780ce3bedc564cf5cbda63 OP_EQUALVERIFY OP_CHECKSIG",
"addresses": ["qq07l6rr5lsdm3m80qxw80ku2ex0tj76vvsxpvmgme"],
"type": "pubkeyhash"
},
"spentTxId": null,
"spentIndex": null,
"spentHeight": null
}, {
"value": "0.00007653",
"n": 1,
"scriptPubKey": {
"hex": "76a914000000000000000000000000000000000000000088ac",
"asm": "OP_DUP OP_HASH160 0000000000000000000000000000000000000000 OP_EQUALVERIFY OP_CHECKSIG",
"addresses": ["qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqfnhks603"],
"type": "pubkeyhash"
},
"spentTxId": null,
"spentIndex": null,
"spentHeight": null
}],
"blockhash": "000000000000000001d1617cbe09e0eebf084c3b718b7cbc274a1e1447059aaa",
"blockheight": 544968,
"confirmations": 8,
"time": 1535207829,
"blocktime": 1535207829,
"valueOut": 12.50063774,
"size": 155
}
- 交易列表:
根据块hash,获取块里所有的交易
请求:
https://bch-insight.bitpay.com/api/txs?block=000000000000000001d1617cbe09e0eebf084c3b718b7cbc274a1
响应:
{
"pagesTotal": 4,
"txs": [{
"txid": "a11d13c3cdb64ba593e7851771c076f6c50c13c4c2b0de64de020bf747409344",
"version": 1,
"locktime": 0,
"vin": [{
"txid": "56057dae302569a97f46edcf154fc594b6ba5cf7b7762c08d07fe7a5bac9fbf7",
"vout": 0,
"sequence": 4294967295,
"n": 0,
"scriptSig": {
"hex": "47304402202df3860fbb8923c1db8eb014170dfafe4aa9e373f301258b1156cb66aa7f48ea02203e512c69d677382509001c30de79e1ff8c578c46522907c3231772a958d897fb412102a9af7c509f7a638e97e3ea491a0b8e43dce5d01550607c1f0f9c10b1d903589d",
"asm": "304402202df3860fbb8923c1db8eb014170dfafe4aa9e373f301258b1156cb66aa7f48ea02203e512c69d677382509001c30de79e1ff8c578c46522907c3231772a958d897fb41 02a9af7c509f7a638e97e3ea491a0b8e43dce5d01550607c1f0f9c10b1d903589d"
},
"addr": "qpasg06lcecfmjc6w7da8swacmz8fm270g6lk5643x",
"valueSat": 38300000,
"value": 0.383,
"doubleSpentTxID": null,
"isConfirmed": null,
"confirmations": null,
"unconfirmedInput": null
}],
"valueIn": 0.383,
"fees": 0.00000452,
"vout": [{
"value": "0.18299548",
"n": 0,
"scriptPubKey": {
"hex": "76a914766f5bb028107c80ff928b9d8f7a2e4046e2b3be88ac",
"asm": "OP_DUP OP_HASH160 766f5bb028107c80ff928b9d8f7a2e4046e2b3be OP_EQUALVERIFY OP_CHECKSIG",
"addresses": ["qpmx7kas9qg8eq8lj29emrm69eqydc4nhcph9d6j7k"],
"type": "pubkeyhash"
},
"spentTxId": null,
"spentIndex": null,
"spentHeight": null
}, {
"value": "0.20000000",
"n": 1,
"scriptPubKey": {
"hex": "76a914c26a2d24934d7810add9725bc8266e230af6a17b88ac",
"asm": "OP_DUP OP_HASH160 c26a2d24934d7810add9725bc8266e230af6a17b OP_EQUALVERIFY OP_CHECKSIG",
"addresses": ["qrpx5tfyjdxhsy9dm9e9hjpxdc3s4a4p0v98sjs4qs"],
"type": "pubkeyhash"
},
"spentTxId": null,
"spentIndex": null,
"spentHeight": null
}],
"blockhash": "000000000000000001d1617cbe09e0eebf084c3b718b7cbc274a1e1447059aaa",
"blockheight": 544968,
"confirmations": 10,
"time": 1535207829,
"blocktime": 1535207829,
"valueOut": 0.38299548,
"size": 225
}]
}
根据地址hash,获取与该地址有关的所有交易
请求:
https://bch-insight.bitpay.com/api/rawtx/9c22557a6c36127daea66dd80657ca6c65a67fe665f23f980b4df3c365d7cdc5
响应:
{
"pagesTotal": 1,
"txs": [{
"txid": "f0a56e7eedb628e8ff5bd9399aa0f4a1cc909168bb66129e170708ca8ff7911f",
"version": 1,
"locktime": 0,
"vin": [{
"txid": "b1f14329a2ce13415f2c083aadb75e78cc01315a68a8448ecadc24794b8309fd",
"vout": 0,
"sequence": 4294967295,
"n": 0,
"scriptSig": {
"hex": "4730440220538318b4ed48c5da60e2f2d595f99d3162d8d4f6e3deedb4cfbc2d0a29a3763d022070d7830876c7570d34b80ec478d0cdac004ef6909e478c5cca1ea7b8e51139c541210381c5d018624c510d551646e605596e425c80cfa1c154dad7099501ce566179f8",
"asm": "30440220538318b4ed48c5da60e2f2d595f99d3162d8d4f6e3deedb4cfbc2d0a29a3763d022070d7830876c7570d34b80ec478d0cdac004ef6909e478c5cca1ea7b8e51139c541 0381c5d018624c510d551646e605596e425c80cfa1c154dad7099501ce566179f8"
},
"addr": "qzj93tjl4unastps6c4rmg0xd6tgfxmmsu83r39jac",
"valueSat": 1173800000,
"value": 11.738,
"doubleSpentTxID": null,
"isConfirmed": null,
"confirmations": null,
"unconfirmedInput": null
}],
"valueIn": 11.738,
"fees": 0.00000382,
"vout": [{
"value": "1.63466250",
"n": 0,
"scriptPubKey": {
"hex": "76a9141511dba6994fbd3dce14622a6ed366659fdaa37188ac",
"asm": "OP_DUP OP_HASH160 1511dba6994fbd3dce14622a6ed366659fdaa371 OP_EQUALVERIFY OP_CHECKSIG",
"addresses": ["qq23rkaxn98m60wwz33z5mknvejelk4rwyrprmpr0x"],
"type": "pubkeyhash"
},
"spentTxId": null,
"spentIndex": null,
"spentHeight": null
}, {
"value": "10.10333368",
"n": 1,
"scriptPubKey": {
"hex": "76a914760de75aa62c38473191ec82198e08dfbe2d780188ac",
"asm": "OP_DUP OP_HASH160 760de75aa62c38473191ec82198e08dfbe2d7801 OP_EQUALVERIFY OP_CHECKSIG",
"addresses": ["qpmqme665ckrs3e3j8kgyxvwpr0muttcqyaegfvjx0"],
"type": "pubkeyhash"
},
"spentTxId": null,
"spentIndex": null,
"spentHeight": null
}],
"blockhash": "000000000000000000ccaee8493b3bf4e6ce02b2e9b787803c037a537665b1a5",
"blockheight": 544973,
"confirmations": 5,
"time": 1535209927,
"blocktime": 1535209927,
"valueOut": 11.73799618,
"size": 225
}]
}
两个交易都还有一个pageNum参数,默认是第1页,
发送交易(post)--略:
根据交易id读取交易原始信息(16进制):
请求:
https://bch-insight.bitpay.com/api/txs?address=qzj93tjl4unastps6c4rmg0xd6tgfxmmsu83r39jac
响应:
{
"rawtx": "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff2403c85008174d696e656420627920416e74506f6f6c3426205b816995350600008e0b0100ffffffff02b957824a000000001976a9141fefe863a7e0ddc767780ce3bedc564cf5cbda6388ace51d0000000000001976a914000000000000000000000000000000000000000088ac00000000"
}
有了这些api,完全可以独立于insight,自行实现一个区块链浏览器网站.