derrend
derrend

Reputation: 4636

Is it possible to decode the public key from 'scriptSig' found in a raw transaction?

7.  "in":[
8.    {"prev_out":
9.      {"hash":"2007ae...",
10.      "n":0},
11.    "scriptSig":"304502... 042b2d..."}],

Line 11 contains the signature of the person sending the money,

304502...

followed by a space, and then the corresponding public key

04b2d.... Again, these are both in hexadecimal.

Can I decode the hex public key into the original bitcoin address or is this not possible? Thanks :)

Upvotes: 5

Views: 3231

Answers (1)

derrend
derrend

Reputation: 4636

Yes it is possible and here is the function that does it:

import hashlib
from base58 import b58encode
from binascii import unhexlify

pub = 'public key string you wish to decode'


def addr_decode(pub, testnet=True):
    h3 = hashlib.sha256(unhexlify(pub))
    h4 = hashlib.new('ripemd160', h3.digest())

    result =(b'\x00' if not testnet else b'\x6f') + h4.digest()

    h5 = hashlib.sha256(result)
    h6 = hashlib.sha256(h5.digest())

    result += h6.digest()[:4]

    return b58encode(result)

In the above form it is configured for the testnet, for use on the mainnet simply change testnet= to False or remove it from the functions arguments tuple.

Enjoy :)

*modified from yorro's addgen repository.

Upvotes: 1

Related Questions