-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathkeys_addresses.py
59 lines (43 loc) · 1.73 KB
/
keys_addresses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Copyright (C) 2018-2024 The python-bitcoin-utils developers
#
# This file is part of python-bitcoin-utils
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-bitcoin-utils, including this file, may be copied,
# modified, propagated, or distributed except according to the terms contained
# in the LICENSE file.
from bitcoinutils.setup import setup
from bitcoinutils.keys import PrivateKey, PublicKey
def main():
# always remember to setup the network
setup("mainnet")
# create a private key (deterministically)
priv = PrivateKey(secret_exponent=1)
# compressed is the default
print("\nPrivate key WIF:", priv.to_wif(compressed=True))
# could also instantiate from existing WIF key
# priv = PrivateKey.from_wif('KwDiBf89qGgbjEhKnhxjUh7LrciVRzI3qYjgd9m7Rfu73SvHnOwn')
# get the public key
pub = priv.get_public_key()
# compressed is the default
print("Public key:", pub.to_hex(compressed=True))
# get address from public key
address = pub.get_address()
# print the address and hash160 - default is compressed address
print("Address:", address.to_string())
print("Hash160:", address.to_hash160())
print("\n--------------------------------------\n")
# sign a message with the private key and verify it
message = "The test!"
signature = priv.sign_message(message)
assert signature is not None
print("The message to sign:", message)
print("The signature is:", signature)
if PublicKey.verify_message(address.to_string(), signature, message):
print("The signature is valid!")
else:
print("The signature is NOT valid!")
if __name__ == "__main__":
main()