Why does BLOCKCHAIN.COM API only return recipient BASE58 addresses and omits BECH32s?

家住魔仙堡 提交于 2020-06-17 12:59:01

问题


Following this post, I am trying to access all transactions within the #630873 block in the bitcoin blockchain.

import requests

r = requests.get('https://blockchain.info/block-height/630873?format=json')
data = r.json()

When inspecting the (0-indexed) 4th transaction within this block (via data['blocks'][0]['tx'][4]['out']), I get this:

[{'n': 0,
  'script': '0014d0aba2c93bac0fcafafe43f2ad39d664ba51910d',
  'spent': False,
  'tx_index': 0,
  'type': 0,
  'value': 19571491},
 {'addr': '1A7tWftaGHohhGcJMVkkm4zAYnF53KjRnU',
  'n': 1,
  'script': '76a9146406a0a47d4ed716f6ddf2eeca20c725932763f188ac',
  'spending_outpoints': [{'n': 0, 'tx_index': 0}],
  'spent': True,
  'tx_index': 0,
  'type': 0,
  'value': 3928145371}]

Only the addr of the second recipient of this transaction is included. On the blockchain.com website this transaction looks like:

The bc1q6z469jfm4s8u47h7g0e26wwkvja9rygdqpeykd address is visible there. How to access it through the API?

The unaccessible address has BECH32 format, while the accessible one has BASE58 (which information I get by clicking on the address on the website). Those transactions which I was able to retrieve the reciepient address, had BASE58 format.

Link to the block I am talking about..


回答1:


The Blochchain.com API does not fully support bech32 addresses yet.

So you could use another provider such as Blockstream or Blockchair.

Or you can also derive the addresses from the P2WPKH script. For example using BitcoinLib (Disclaimer: my library):

from bitcoinlib.transactions import script_deserialize
from bitcoinlib.keys import Address

locking_script = '0014d0aba2c93bac0fcafafe43f2ad39d664ba51910d'
scr = script_deserialize(locking_script)
a = Address(hashed_data=scr['hashes'][0], script_type=scr['script_type'])
print(a.address)


来源:https://stackoverflow.com/questions/61880129/why-does-blockchain-com-api-only-return-recipient-base58-addresses-and-omits-bec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!