def sign_p2pk(tx, i, priv, pub, hashtype = SIGHASH_ALL): assert isinstance(tx, dict) i = int(i) assert isinstance(priv, int) assert isinstance(pub, bytes) prev_script = script.make_p2pk(pub) txhash = sighash(tx, i, prev_script, hashtype) sig = ecdsa.sign(txhash, priv) sig = ecdsa.serialize_sig(sig) + convert.int_to_byte(hashtype) tx["ins"][i]["script"] = script.serialize([sig])
def sign_multisig(tx, i, prev_script, privs, pubs, hashtype = SIGHASH_ALL): assert isinstance(tx, dict) i = int(i) assert len(privs) == len(pubs) txhash = sighash(tx, i, prev_script, hashtype) s = [ script.Opcode.OP_0, ] for j in range(len(privs)): priv = privs[j] pub = pubs[j] sig = ecdsa.sign(txhash, priv) sig = ecdsa.serialize_sig(sig) + convert.int_to_byte(hashtype) s.append(sig) tx["ins"][i]["script"] = script.serialize(s)
def test_serialize(self): for _ in range(10): sig = (None, random.randrange(2**256), random.randrange(2**256)) ser = ecdsa.serialize_sig(sig) sig2 = ecdsa.deserialize_sig(ser) self.assertEqual(sig, sig2)