-
Notifications
You must be signed in to change notification settings - Fork 26
/
scripts.py
162 lines (116 loc) · 3.98 KB
/
scripts.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
import hashlib
from enum import Enum, auto
from typing import Optional, List
import base58
import bech32
OP_0 = 0x00
OP_DUP = 0x76
OP_EQUAL = 0x87
OP_EQUALVERIFY = 0x88
OP_HASH160 = 0xa9
OP_CHECKSIG = 0xac
P2PKH_ADDRESS_HEADER = 0x00
P2SH_ADDRESS_HEADER = 0x05
BECH32_HRP = 'bc'
sha256 = lambda bytes: hashlib.sha256(bytes).digest()
ripemd160 = lambda bytes: hashlib.new('ripemd160', bytes).digest()
hash160 = lambda bytes: ripemd160(sha256(bytes))
class ScriptType(Enum):
"""
Single-key output script type.
"""
LEGACY = auto() # P2PKH
COMPAT = auto() # P2SH of P2WPKH
SEGWIT = auto() # P2WPKH
def build_output_script(self, pubkey: bytes) -> bytes:
"""
Compute the output script for a given public key.
"""
if self is ScriptType.LEGACY:
return _build_p2pkh_output_script(hash160(pubkey))
if self is ScriptType.COMPAT:
script = _build_segwit_output_script(hash160(pubkey))
return _build_p2sh_output_script(hash160(script))
if self is ScriptType.SEGWIT:
return _build_segwit_output_script(hash160(pubkey))
raise ValueError('Unrecognized address type')
def build_input_script(self, pubkey: bytes, signature: bytes) -> bytes:
"""
Compute the input script for a given public key and signature.
"""
if self is ScriptType.LEGACY:
return _build_p2pkh_input_script(pubkey, signature)
if self is ScriptType.COMPAT:
script = _build_segwit_output_script(hash160(pubkey))
return _build_p2sh_input_script(script)
if self is ScriptType.SEGWIT:
return bytes()
raise ValueError('Unrecognized address type')
def build_witness(self, pubkey: bytes, signature: bytes) -> List[bytes]:
"""
Compute the witness for a given public key and signature.
"""
if self is ScriptType.LEGACY:
return []
if self in [ScriptType.COMPAT, ScriptType.SEGWIT]:
return [signature, pubkey]
raise ValueError('Unrecognized address type')
def build_output_script_from_address(address: str) -> Optional[bytes]:
"""
Compute the output script for a given address.
"""
# Try to decode a base58 address
try:
decoded = base58.b58decode_check(address)
version = decoded[0]
hash = decoded[1:]
if version == P2PKH_ADDRESS_HEADER:
return _build_p2pkh_output_script(hash)
if version == P2SH_ADDRESS_HEADER:
return _build_p2sh_output_script(hash)
except ValueError:
pass
# Try to decode a bech32 address
try:
version, hash = bech32.decode(BECH32_HRP, address)
if version == 0:
return _build_segwit_output_script(hash)
except ValueError:
pass
return None
def _build_p2pkh_output_script(pubkey_hash: bytes) -> bytes:
script = bytearray()
script.append(OP_DUP)
script.append(OP_HASH160)
script.append(len(pubkey_hash))
script.extend(pubkey_hash)
script.append(OP_EQUALVERIFY)
script.append(OP_CHECKSIG)
return bytes(script)
def _build_p2sh_output_script(script_hash: bytes) -> bytes:
script = bytearray()
script.append(OP_HASH160)
script.append(len(script_hash))
script.extend(script_hash)
script.append(OP_EQUAL)
return bytes(script)
def _build_segwit_output_script(hash: bytes) -> bytes:
script = bytearray()
script.append(OP_0)
script.append(len(hash))
script.extend(hash)
return bytes(script)
def _build_p2pkh_input_script(pubkey: bytes, signature: bytes) -> bytes:
script = bytearray()
script.append(len(signature))
script.extend(signature)
script.append(len(pubkey))
script.extend(pubkey)
return bytes(script)
def _build_p2sh_input_script(*args: bytes) -> bytes:
script = bytearray()
for arg in args:
script.append(len(arg))
script.extend(arg)
return bytes(script)