def __sign_bare_multisig__(self, n, private_key, public_key, script_pub_key, sighash_type): sighash = self.sig_hash(n, script_pub_key=script_pub_key, sighash_type=sighash_type) sighash = s2rh(sighash) if isinstance(sighash, str) else sighash sig = [sign_message(sighash, p, 0) + bytes([sighash_type]) for p in private_key] self["vIn"][n]['signatures'] = [s if self["format"] == "raw" else s.hex() for s in sig] return b''.join(self.__get_bare_multisig_script_sig__(self["vIn"][n]["scriptSig"], script_pub_key, public_key, sig, n))
def __sign_p2pkh__(self, n, private_key, public_key, script_pub_key, sighash_type): sighash = self.sig_hash(n, script_pub_key=script_pub_key, sighash_type=sighash_type) sighash = s2rh(sighash) if isinstance(sighash, str) else sighash signature = sign_message(sighash, private_key[0], 0) + bytes([sighash_type]) self["vIn"][n]['signatures'] = [signature, ] if self["format"] == "raw" else [signature.hex(), ] script_sig = b''.join([bytes([len(signature)]), signature, bytes([len(public_key[0])]), public_key[0]]) return script_sig
def test_sign_message(): s = "3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794d12d48" \ "2f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb" p = PrivateKey( "eb696a065ef48a2192da5b28b694f87544b30fae8327c4510137a922f32c6dcf") assert sign_message( "64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6", "eb696a065ef48a2192da5b28b694f87544b30fae8327c4510137a922f32c6dcf" ) == s assert sign_message( "64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6", bytearray(p.key)) == s assert sign_message( "64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6", p.wif) == s with pytest.raises(TypeError): assert sign_message( "64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6", "werwefwefwe") == s
def __sign_p2wsh_multisig(self, n, private_key, public_key, script_pub_key, redeem_script, sighash_type, amount): script_code = int_to_var_int(len(redeem_script)) + redeem_script sighash = self.sig_hash_segwit(n, amount, script_pub_key=script_code, sighash_type=sighash_type) sighash = bytes.fromhex(sighash) if isinstance(sighash, str) else sighash sig = [sign_message(sighash, p, 0) + bytes([sighash_type]) for p in private_key] self["vIn"][n]['signatures'] = [s if self["format"] == "raw" else s.hex() for s in sig] if "txInWitness" not in self["vIn"][n]: self["vIn"][n]["txInWitness"] = [] witness = self.__get_multisig_script_sig__(self["vIn"][n]["txInWitness"], public_key, sig, script_code, redeem_script, n, amount) if self["format"] == "raw": self["vIn"][n]['txInWitness'] = list(witness) else: self["vIn"][n]["txInWitness"] = list([w.hex() for w in witness]) return b""
def __sign_p2wpkh(self, n, private_key, public_key, script_pub_key, sighash_type, amount): s = [b'\x19', OP_DUP, OP_HASH160, script_pub_key[1:], OP_EQUALVERIFY, OP_CHECKSIG] if amount is None: try: amount = self["vIn"][n]["value"] except: raise RuntimeError("no input amount") sighash = self.sig_hash_segwit(n, amount, script_pub_key=b"".join(s), sighash_type=sighash_type) sighash = bytes.fromhex(sighash) if isinstance(sighash, str) else sighash signature = sign_message(sighash, private_key[0], 0) + bytes([sighash_type]) self["segwit"] = True if self["format"] == "raw": self["vIn"][n]['txInWitness'] = [signature, public_key[0]] else: self["vIn"][n]['txInWitness'] = [signature.hex(), public_key[0].hex()] self["vIn"][n]['signatures'] = [signature,] if self["format"] == "raw" else [signature.hex(),] return b""
def sign_message(msg, private_key, hex=True): return __parent__.sign_message(msg, private_key, hex=hex)