-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathkeys_segwit_addresses.py
82 lines (65 loc) · 2.44 KB
/
keys_segwit_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright (C) 2018-2025 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.script import Script
from bitcoinutils.keys import P2wpkhAddress, P2wshAddress, P2shAddress, PrivateKey
def main():
# always remember to setup the network
setup("testnet")
# could also instantiate from existing WIF key
priv = PrivateKey.from_wif("cVdte9ei2xsVjmZSPtyucG43YZgNkmKTqhwiUA8M4Fc3LdPJxPmZ")
# compressed is the default
print("\nPrivate key WIF:", priv.to_wif(compressed=True))
# 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_segwit_address()
# print the address and hash - default is compressed address
print("Native Address:", address.to_string())
segwit_hash = address.to_witness_program()
print("Segwit Hash (witness program):", segwit_hash)
print("Segwit Version:", address.get_type())
# test to_string
addr2 = P2wpkhAddress.from_witness_program(segwit_hash)
print("Created P2wpkhAddress from Segwit Hash and calculate address:")
print("Native Address:", addr2.to_string())
#
# display P2SH-P2WPKH
#
# create segwit address
addr3 = (
PrivateKey.from_wif("cTmyBsxMQ3vyh4J3jCKYn2Au7AhTKvqeYuxxkinsg6Rz3BBPrYKK")
.get_public_key()
.get_segwit_address()
)
# wrap in P2SH address
addr4 = P2shAddress.from_script(addr3.to_script_pub_key())
print("P2SH(P2WPKH):", addr4.to_string())
#
# display P2WSH
#
p2wpkh_key = PrivateKey.from_wif(
"cNn8itYxAng4xR4eMtrPsrPpDpTdVNuw7Jb6kfhFYZ8DLSZBCg37"
)
script = Script(
["OP_1", p2wpkh_key.get_public_key().to_hex(), "OP_1", "OP_CHECKMULTISIG"]
)
p2wsh_addr = P2wshAddress.from_script(script)
print("P2WSH of P2PK:", p2wsh_addr.to_string())
#
# display P2SH-P2WSH
#
p2sh_p2wsh_addr = P2shAddress.from_script(p2wsh_addr.to_script_pub_key())
print("P2SH(P2WSH of P2PK):", p2sh_p2wsh_addr.to_string())
if __name__ == "__main__":
main()