Check a signer address

If you’re writing a contract that checks signatures, you may want to check which address is associated to the signer.

The patter to follow is explained here: Check whether a public_key or public_key_hash matches an address.

Here is a full example with the sp.check_signature statement. In practice, sp.bytes("0x00") is replaced by the content to be signed.

import smartpy as sp

@sp.module
def main():
    class C(sp.Contract):
        def __init__(self, signer):
            self.data.signer = signer

        @sp.entrypoint
        def ep(self, public_key, signature):
            assert sp.check_signature(
                public_key, signature, sp.bytes("0x00")
            ), "BadSig"
            public_key_hash = sp.hash_key(public_key)
            address = sp.to_address(sp.implicit_account(public_key_hash))
            assert sp.sender == address

if "main" in __name__:
    @sp.add_test("Test")
    def test():
        alice = sp.test_account("alice")
        sc = sp.test_scenario(main)
        c1 = main.C(alice.address)
        sc += c1
        signature = sp.make_signature(alice.secret_key, sp.bytes("0x00"), message_format="Raw")
        c1.ep(public_key=alice.public_key, signature=signature).run(sender=alice)