Generate a random destination of the specified type and return the corresponding public key, scriptPubKey and address. Supported types are 'legacy', 'p2sh-segwit' and 'bech32'. Can be used when a random destination is needed, but no compiled wallet is available (e.g. as r
(address_type='bech32', hrp='ert', version=235, prefix=75)
| 223 | |
| 224 | |
| 225 | def getnewdestination(address_type='bech32', hrp='ert', version=235, prefix=75): |
| 226 | """Generate a random destination of the specified type and return the |
| 227 | corresponding public key, scriptPubKey and address. Supported types are |
| 228 | 'legacy', 'p2sh-segwit' and 'bech32'. Can be used when a random |
| 229 | destination is needed, but no compiled wallet is available (e.g. as |
| 230 | replacement to the getnewaddress/getaddressinfo RPCs).""" |
| 231 | key = ECKey() |
| 232 | key.generate() |
| 233 | pubkey = key.get_pubkey().get_bytes() |
| 234 | if address_type == 'legacy': |
| 235 | scriptpubkey = key_to_p2pkh_script(pubkey) |
| 236 | address = key_to_p2pkh(pubkey, version=version) |
| 237 | elif address_type == 'p2sh-segwit': |
| 238 | scriptpubkey = key_to_p2sh_p2wpkh_script(pubkey) |
| 239 | address = key_to_p2sh_p2wpkh(pubkey, prefix=prefix) |
| 240 | elif address_type == 'bech32': |
| 241 | scriptpubkey = key_to_p2wpkh_script(pubkey) |
| 242 | address = key_to_p2wpkh(pubkey, hrp=hrp) |
| 243 | # TODO: also support bech32m (need to generate x-only-pubkey) |
| 244 | else: |
| 245 | assert False |
| 246 | return pubkey, scriptpubkey, address |
| 247 | |
| 248 | |
| 249 | def address_to_scriptpubkey(address): |