Each transaction can have multiple inputs and thus can have multiple senders.
Here's how you get the sender(s):
bitcoin-cli getrawtransaction 8386a8d2870c0df79f652ef4d981b21649ebf40601948c1c0709de0f02de8c8c 1
(the 1 flag indicates that you want the raw tx to be decoded)
Note: make sure that you have already indexed the blockchain or else this might not work. To index, restart bitcoind with txindex=1
as a config variable.
From that, you get a fairly big response, which contains the following:
"vin" : [{
"txid" : "5203d1db5eeef77de7c404ec14487892e3dd12b4a562537243533a169d45753c",
"vout" : 1,
"scriptSig" : {
"asm" : "304402200ee65c9f757eb6c240efe5a7e4427e04174a32da14b1eef459d36d61d031f6e702202d6b5383f86f155d92a494e80a1242af7e160faf16d597ea457b6e3bf08bb1ca01 02be7759e73363488269f0257158177f3295af42d1f3a6b2fdf8fb4380b1d16ae9",
"hex" : "47304402200ee65c9f757eb6c240efe5a7e4427e04174a32da14b1eef459d36d61d031f6e702202d6b5383f86f155d92a494e80a1242af7e160faf16d597ea457b6e3bf08bb1ca012102be7759e73363488269f0257158177f3295af42d1f3a6b2fdf8fb4380b1d16ae9"
},
"sequence" : 4294967295
}]
This means there's a single input, so there is in fact one sender.
Now, there are two ways to get the sender.
Method A (should always work but is slower):
- grab the
txid
and thevout
- do
bitcoin-cli getrawtransaction <txid> 1
- find the output of this new tx where the value
n
is equal to thevout
of our original tx - go to the
scriptPubKey
section and check thehex
oraddresses
field - that's the sender (in hex pubkey script or address form)
We now have:
"scriptPubKey" : {
"asm" : "OP_DUP OP_HASH160 17492e77be2c666af78993020b90235cd1d3738d OP_EQUALVERIFY OP_CHECKSIG",
"hex" : "76a91417492e77be2c666af78993020b90235cd1d3738d88ac",
"reqSigs" : 1,
"type" : "pubkeyhash",
"addresses" : [
"1388D9sHH4HXdGLxkSipxe2noZuekHZmaF"
]
}
and the sender is 1388D9sHH4HXdGLxkSipxe2noZuekHZmaF
Method B (faster but only works for certain types of transactions):
- check the 'asm' field inside the 'scriptSig' field
- if the field has two components separated by a space, and the second component is a valid public key, then we have a standard pay-to-pubkey(-hash) transaction, so convert the pubkey to an address
- fallback to method A
We see that there is in fact a valid pubkey in the original tx scriptSig:
02be7759e73363488269f0257158177f3295af42d1f3a6b2fdf8fb4380b1d16ae9
We use coinkitCoinkit to extract the address:
$ sudo pip install coinkit
$ python
>>> from coinkit import *BitcoinPublicKey
>>> pub = BitcoinPublicKey('02be7759e73363488269f0257158177f3295af42d1f3a6b2fdf8fb4380b1d16ae9')
>>> pub.address()
'1388D9sHH4HXdGLxkSipxe2noZuekHZmaF'
And we have the same answer!
P.S. you can inspect the Coinkit library I used above on github