1

I want to build a server, which provide following APIs:

  • 1.Get balance by address.
  • 2.Get latest unspent transaction.
  • 3.broadcast the transaction.

First by first, I know I can do above stuffes by blockchain and coinb, but I want to build my own APIs. What I have tried with the Python code has list as below:

import binascii
import json
from decimal import Decimal
from pprint import pprint
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from config import ConfigBasic

class BitPort(object):

    def __init__(self, config=None):
        self.conf = ConfigBasic()
        # pass
        self.rpc = AuthServiceProxy("http://{}:{}@{}".format(
        self.conf.rpc_user, self.conf.rpc_password, self.conf.rpc_host))

    def get_amount_by_addr(self, addr):
        return self.rpc.getreceivedbyaddress(addr)

    def get_unconfirm(self, addr):
        return self.rpc.getunconfirmedbalance(addr)

    def get_unspent(self, addr):
        """
        @param addr: A array with address, ["0xxxxxx", "0x122222"]
        """
        return self.rpc.listunspent(0, 999999, addr)

    def sign_rawtransaction(self, addr):
        fee_obj = self.rpc.estimatesmartfee(6)
        fee = fee_obj.get("feerate")

    def sendrawtransaction(self, address, pubkey):
        pass




if __name__ == '__main__':
    addr = '2NCTreR1GmHXMNSYnt2J76QZgv8PH1k4PHB'
    addr = '2MsHfXEmutS2GWPgK55JD4i1gKSFYmJgKXv'
    addr = 'n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi'
    bp = BitPort(config=ConfigBasic)
    print(bp.get_amount_by_addr(addr))
    #print(bp.get_unconfirm(addr))
    print(bp.get_unspent([addr]))

Let's say, Why I need to do such stuff. The client-side need to check how many money are available. so I need to provide the 1. The client-side also want to send its money to someone else, so I need to broadcast its transaction to the public's chain, but before do that, I the client-side need to make a transaction hex, which require the server side provide an API to get the unspent transaction. Why?

Because make a raw transaction need the following at least:

  • 1.vout (from unspent transaction)
  • 2.txid (from unspent transaction)
  • 3.amount (user custom)
  • 4.fee (default value is ok)
  • 5.public key (client side store or calculate it by itself)
  • 6.private key client side store or calculate it by itself)

From many tutorial, like How to create a raw transaction, which is good. but didn't mention how to get an unspent by a address. which don't store inside the bitcoind side.

Unfortunately, I can only fetch all transaction by my wallet's address, which live in the bitcoind node. Once I tried the someone address, and I got the following exception:

Traceback (most recent call last):
  File "core.py", line 43, in <module>
    print(bp.get_amount_by_addr(addr))
  File "core.py", line 17, in get_amount_by_addr
    return self.rpc.getreceivedbyaddress(addr)
  File "/Users/frank/venv/bitcoin/lib/python2.7/site-packages/bitcoinrpc/authproxy.py", line 141, in __call__
    raise JSONRPCException(response['error'])
bitcoinrpc.authproxy.JSONRPCException: -4: Address not found in wallet

BTW, I run it on testnet, Anyone help me?

1 Answer 1

4

Bitcoind does not index transactions for non-wallet addresses. If you wish to build this yourself, you will need to:

  1. Enable txindex=1 in your bitcoin.conf
  2. Rescan the chain
  3. Set up blocknotify and then parse the chain block by block, building an index of which outputs have been spent, and which haven't, and which addresses are involved in each transaction

Then, when working with a specific address, you first query your database for all unspent outputs, and then can use getrawtransaction to get the unspent tx information (although, you could store enough information in your index to not need to make the getrawtransaction call).

Alternatively, you can run something such as ElectrumX or insight, which will do all of the above for you.

1
  • What are the resources / cost estimates to run ElectrumX on AWS?
    – Jus12
    Jun 16, 2022 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.