How to fetch the latest unspent transaction by an address using bitcoind?

How to fetch the latest unspent transaction by an address using bitcoind?

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?

https://ift.tt/2LIBfbu

Comments

Popular posts from this blog

sendrawtransaction and txn-mempool-conflict

couldn't connect to server: EOF reached (code 1)