morvaridio / hdtools

HD Tools for cryptocurrencies
https://pypi.org/project/hdtools/
MIT License
5 stars 3 forks source link

How to sign transactions #3

Closed MischaD closed 4 years ago

MischaD commented 4 years ago

Hi,

I'm having trouble trying to understand how to use the extended_keys after I derived them using hdtools. I generated my address in a similar way as the one you described in the README: xprv = (m/44./0./0./0/0) address = xprv.address()

I sent some BTC on a private regtest chain to this address and now I would like to understand how to send them back using hdtools.

Usually I would do this by creating a rawtransaction and signing it with the private key derived by the Bip39 online tool.

Is there a way to either retrieve the private key that can be used to sign a transaction from this address or, even better, directlly sign the transaction using xprv ?

professormahi commented 4 years ago

Thanks for using hdtools, If you want to generate the derived private key in format of iancoleman, as you mentioned, you should use the wif method from the key property. Below is an example for the key derived from the README seed:

>>> (M/44./0./0./0/0).address()
'1DgEh5Y6NioqaxHBBc2puDYq6SvG5NDsG9'
>>> (M/44./0./0./0/0).to_xpub().key.hex(compressed=True)
'0374c186f04319e53efe661dad911331cf8cd4da07c773a12541db59f617375d9c'
>>> (M/44./0./0./0/0).key.wif(compressed=True)
b'L4vZ6EBVfQmijJrfkBvQxNcEf2VhqNS9YydGP88RNTcVEwKZR11u'

We are not a full wallet so you can not sign a transaction using hdtools but you can sign any hash with xprv.key.sign_hash(<your hash>).

MischaD commented 4 years ago

Awesome, that works exactly how I wanted. Thank you for your help!

professormahi commented 4 years ago

You're welcome.