MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / sign

Method sign

test/functional/test_framework/key.py:148–178  ·  view source on GitHub ↗
(self, hash, low_s = True)

Source from the content-addressed store, hash-verified

146 return kdf(r)
147
148 def sign(self, hash, low_s = True):
149 # FIXME: need unit tests for below cases
150 if not isinstance(hash, bytes):
151 raise TypeError('Hash must be bytes instance; got %r' % hash.__class__)
152 if len(hash) != 32:
153 raise ValueError('Hash must be exactly 32 bytes long')
154
155 sig_size0 = ctypes.c_uint32()
156 sig_size0.value = ssl.ECDSA_size(self.k)
157 mb_sig = ctypes.create_string_buffer(sig_size0.value)
158 result = ssl.ECDSA_sign(0, hash, len(hash), mb_sig, ctypes.byref(sig_size0), self.k)
159 assert 1 == result
160 assert mb_sig.raw[0] == 0x30
161 assert mb_sig.raw[1] == sig_size0.value - 2
162 total_size = mb_sig.raw[1]
163 assert mb_sig.raw[2] == 2
164 r_size = mb_sig.raw[3]
165 assert mb_sig.raw[4 + r_size] == 2
166 s_size = mb_sig.raw[5 + r_size]
167 s_value = int.from_bytes(mb_sig.raw[6+r_size:6+r_size+s_size], byteorder='big')
168 if (not low_s) or s_value <= SECP256K1_ORDER_HALF:
169 return mb_sig.raw[:sig_size0.value]
170 else:
171 low_s_value = SECP256K1_ORDER - s_value
172 low_s_bytes = (low_s_value).to_bytes(33, byteorder='big')
173 while len(low_s_bytes) > 1 and low_s_bytes[0] == 0 and low_s_bytes[1] < 0x80:
174 low_s_bytes = low_s_bytes[1:]
175 new_s_size = len(low_s_bytes)
176 new_total_size_byte = (total_size + new_s_size - s_size).to_bytes(1,byteorder='big')
177 new_s_size_byte = (new_s_size).to_bytes(1,byteorder='big')
178 return b'\x30' + new_total_size_byte + mb_sig.raw[2:5+r_size] + new_s_size_byte + low_s_bytes
179
180 def verify(self, hash, sig):
181 """Verify a DER signature"""

Callers 6

sign_txMethod · 0.45
sign_p2pk_witness_inputFunction · 0.45
run_testMethod · 0.45
sign_txMethod · 0.45

Calls

no outgoing calls

Tested by 2