(self, hash)
| 350 | return self.signature_to_low_s(mb_sig.raw[:sig_size0.value]) |
| 351 | |
| 352 | def sign_compact(self, hash): # pylint: disable=redefined-builtin |
| 353 | if not isinstance(hash, bytes): |
| 354 | raise TypeError('Hash must be bytes instance; got %r' % hash.__class__) |
| 355 | if len(hash) != 32: |
| 356 | raise ValueError('Hash must be exactly 32 bytes long') |
| 357 | |
| 358 | sig_size0 = ctypes.c_uint32() |
| 359 | sig_size0.value = _ssl.ECDSA_size(self.k) |
| 360 | mb_sig = ctypes.create_string_buffer(sig_size0.value) |
| 361 | result = _ssl.ECDSA_sign(0, hash, len(hash), mb_sig, ctypes.byref(sig_size0), self.k) |
| 362 | assert 1 == result |
| 363 | |
| 364 | if bitcoin.core.script.IsLowDERSignature(mb_sig.raw[:sig_size0.value]): |
| 365 | sig = mb_sig.raw[:sig_size0.value] |
| 366 | else: |
| 367 | sig = self.signature_to_low_s(mb_sig.raw[:sig_size0.value]) |
| 368 | |
| 369 | sig = bitcoin.signature.DERSignature.deserialize(sig) |
| 370 | |
| 371 | r_val = sig.r |
| 372 | s_val = sig.s |
| 373 | |
| 374 | # assert that the r and s are less than 32 long, excluding leading 0s |
| 375 | assert len(r_val) <= 32 or r_val[0:-32] == b'\x00' |
| 376 | assert len(s_val) <= 32 or s_val[0:-32] == b'\x00' |
| 377 | |
| 378 | # ensure r and s are always 32 chars long by 0padding |
| 379 | r_val = ((b'\x00' * 32) + r_val)[-32:] |
| 380 | s_val = ((b'\x00' * 32) + s_val)[-32:] |
| 381 | |
| 382 | # tmp pubkey of self, but always compressed |
| 383 | pubkey = CECKey() |
| 384 | pubkey.set_pubkey(self.get_pubkey()) |
| 385 | pubkey.set_compressed(True) |
| 386 | |
| 387 | # bitcoin core does <4, but I've seen other places do <2 and I've never seen a i > 1 so far |
| 388 | for i in range(0, 4): |
| 389 | cec_key = CECKey() |
| 390 | cec_key.set_compressed(True) |
| 391 | |
| 392 | result = cec_key.recover(r_val, s_val, hash, len(hash), i, 1) |
| 393 | if result == 1: |
| 394 | if cec_key.get_pubkey() == pubkey.get_pubkey(): |
| 395 | return r_val + s_val, i |
| 396 | |
| 397 | raise ValueError |
| 398 | |
| 399 | def signature_to_low_s(self, sig): |
| 400 | der_sig = ECDSA_SIG_st() |
nothing calls this directly
no test coverage detected