Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Blockchain by (4.1k points)

I have been trying to extract input addresses from Namecoin transactions using some python code. This code works for regular transactions (where some namecoins are transferred from one address to another); however, this doesn't work on the transactions which have name operations, such as name_new. Here is some code:

raw = namecoind.getrawtransaction(tx_hash)
data = namecoind.decoderawtransaction(raw)

if 'vin' in data:
    inputs = data['vin']
    for input in inputs:
        input_value = input.get('value')

        if 'scriptSig' in input:
            script_sig_asm = str(input['scriptSig'].get('asm'))
            script_sig_parts = script_sig_asm.split(' ')
            if len(script_sig_parts) > 1 and (len(script_sig_parts[-1]) == 130
                or len(script_sig_parts[-1]) == 66):
                public_key_string = script_sig_parts[-1]
                try:
                    recipient_address = NamecoinPublicKey(public_key_string, verify=False).address()
                    print recipient_address
                except Exception, e:
                    print str(e)
                       return
            elif len(script_sig_parts) == 1:
                print "coinbase transaction input"
                return


    #print "Inputs:"

Running this code on a regular transaction works, i,e, we get the recipient address. But running this code for a name operation such as this shows that its a coinbase transaction, that is,

len(script_sig_parts) == 1

is True, and so the recipient_address is empty.

Can anybody point me how I can get the recipient address (in the above transaction, it is: NCAzVGKq8JrsETxAkgw3MsDPinAEPwsTfn) in a Namecoin transaction which involves name operation?

1 Answer

0 votes
by (14.4k points)

Your code should work fine on most name transactions. 

For instance, if you take 499a1e4c7bb1388347e1bd1142425949971eaf1fa2521af625006f4f49ce85c5 which happens to be the latest update of d/domob, the relevant script sig of the input is:

"scriptSig" : {
  "asm" : "3045022100c489ac648d08416d83d5e561d222049242242ded5f1c2bfd35ea48bb78b6a90f02203a28dee3d9473755fc2288bcaec9105e973e306071d28e69e593668c94b19fc101 04a85b7360b6b95459f7286111220f7a1eaef23bc9ced8a3b56cd57360374381bcabf7182941be400ccdfb761e26fa62d50b8911358aceb6aa30de9e8df5c46742",
  "hex" : "483045022100c489ac648d08416d83d5e561d222049242242ded5f1c2bfd35ea48bb78b6a90f02203a28dee3d9473755fc2288bcaec9105e973e306071d28e69e593668c94b19fc1014104a85b7360b6b95459f7286111220f7a1eaef23bc9ced8a3b56cd57360374381bcabf7182941be400ccdfb761e26fa62d50b8911358aceb6aa30de9e8df5c46742"
},
Your code has two pieces in the scriptsig (pubkey and signature). 
The previous output in your transaction is not pay-to-pubkeyhash. Rather it is pay-to-pubkey. Relevant output of the previous transaction 92457dfc2831bdb6439fc03e72dbe3908140d43ec410f4a7396e3d65f5ab605b:
"scriptPubKey" : {
  "asm" : "046a77fa46493d61985c1157a6e3e498b3b97c878c9c23e5b4729d354b574eb33a20c0483551308e2bd08295ce238e8ad09a7a2477732eb2e995a3e20455e9d137 OP_CHECKSIG",
  "hex" : "41046a77fa46493d61985c1157a6e3e498b3b97c878c9c23e5b4729d354b574eb33a20c0483551308e2bd08295ce238e8ad09a7a2477732eb2e995a3e20455e9d137ac",
  "reqSigs" : 1,
  "type" : "pubkey",
  "addresses" : [
    "NCAzVGKq8JrsETxAkgw3MsDPinAEPwsTfn"
  ]
}
Owing to the nature of the pay-to-pubkey script, the scriptsig does not contains only the signature now. 
While handling such rare cases, fetch the previous output and see how it works.

Related questions

Browse Categories

...