def sign_multisig(hash_hex, redeem_script, secret_keys): assert len(redeem_script) > 0 m, pk_hexes = virtualchain.parse_multisig_redeemscript(redeem_script) privs = {} for sk in secret_keys: pk = virtualchain.ecdsalib.ecdsa_private_key(sk).public_key().to_hex() compressed_pubkey = keylib.key_formatting.compress(pk) uncompressed_pubkey = keylib.key_formatting.decompress(pk) privs[compressed_pubkey] = sk privs[uncompressed_pubkey] = sk used_keys, sigs = [],[] for pk in pk_hexes: if pk not in privs: continue if len(used_keys) == m: break assert pk not in used_keys, 'Tried to reuse key {}'.format(pk) sk_hex = privs[pk] used_keys.append(pk) b64sig = virtualchain.ecdsalib.sign_digest(hash_hex, sk_hex) sighex = binascii.hexlify(base64.b64decode(b64sig)) sigs.append(sighex) assert len(used_keys) == m, 'Missing private keys (used {}, required {})'.format(len(used_keys), m) return base64.b64encode(virtualchain.btc_script_serialize([None] + sigs + [redeem_script]))
def sign_multisig(hash_hex, redeem_script, secret_keys): assert len(redeem_script) > 0 m, pk_hexes = virtualchain.parse_multisig_redeemscript(redeem_script) privs = {} for sk in secret_keys: pk = virtualchain.ecdsalib.ecdsa_private_key(sk).public_key().to_hex() compressed_pubkey = keylib.key_formatting.compress(pk) uncompressed_pubkey = keylib.key_formatting.decompress(pk) privs[compressed_pubkey] = sk privs[uncompressed_pubkey] = sk used_keys, sigs = [], [] for pk in pk_hexes: if pk not in privs: continue if len(used_keys) == m: break assert pk not in used_keys, 'Tried to reuse key {}'.format(pk) sk_hex = privs[pk] used_keys.append(pk) b64sig = virtualchain.ecdsalib.sign_digest(hash_hex, sk_hex) sighex = binascii.hexlify(base64.b64decode(b64sig)) sigs.append(sighex) assert len( used_keys) == m, 'Missing private keys (used {}, required {})'.format( len(used_keys), m) return base64.b64encode( virtualchain.btc_script_serialize([None] + sigs + [redeem_script]))
def sign(sk, plaintext): """ This returns a signature of the given plaintext with the given SK. This is in the form of a p2pkh scriptSig """ privkey_hex = sk.to_hex() hash_hex = binascii.hexlify(hashlib.sha256(plaintext).digest()) b64sig = virtualchain.ecdsalib.sign_digest(hash_hex, privkey_hex) sighex = binascii.hexlify(base64.b64decode(b64sig)) pubkey_hex = sk.public_key().to_hex() return base64.b64encode(virtualchain.btc_script_serialize([sighex, pubkey_hex]))