templates.test_hash_functions

 1# Hash Functions - Example for illustrative purposes only.
 2
 3import smartpy as sp
 4
 5
 6@sp.module
 7def main():
 8    class TestHashes(sp.Contract):
 9        def __init__(self):
10            self.data.v = sp.bytes("0x")
11            self.data.b2b = sp.bytes("0x")
12            self.data.s256 = sp.bytes("0x")
13            self.data.s512 = sp.bytes("0x")
14            self.data.sha3 = sp.bytes("0x")
15            self.data.keccak = sp.bytes("0x")
16            self.data.tz1 = sp.key_hash("tz1M9CMEtsXm3QxA7FmMU2Qh7xzsuGXVbcDr")
17
18        @sp.entrypoint
19        def new_value(self, some_bytes):
20            self.data.v = some_bytes
21            self.data.b2b = sp.blake2b(some_bytes)
22            self.data.s256 = sp.sha256(some_bytes)
23            self.data.s512 = sp.sha512(some_bytes)
24            self.data.sha3 = sp.sha3(some_bytes)
25            self.data.keccak = sp.keccak(some_bytes)
26
27        @sp.entrypoint
28        def new_key(self, pubkey):
29            self.data.tz1 = sp.hash_key(pubkey)
30
31
32@sp.add_test()
33def test():
34    bob = sp.test_account("Robert")
35    scenario = sp.test_scenario("HashFunctions", main)
36    c1 = main.TestHashes()
37    scenario.h1("Hash Functions")
38    scenario += c1
39    c1.new_value(sp.bytes("0x001234"))
40    c1.new_key(bob.public_key)
41    scenario.verify_equal(
42        c1.data,
43        sp.record(
44            v=sp.bytes("0x001234"),
45            b2b=sp.bytes(
46                "0xFFFDFD672FF9075528F51A30408CF768A093D8C67FB3C5C8782DFF49EAB0724D"
47            ),
48            s256=sp.bytes(
49                "0x61A706DFE2DDB1339D7B1D6F10C15A26786DCD1C99B743E0B0E351A6A168D99F"
50            ),
51            s512=sp.bytes(
52                "0x0110F7F5DC329EED3F1D0E8D1AE204CC58B2A790506ACFB793200FC60BA22525DE2C5147FFF19128807352A3A33C44D673CBDA3B9840973FDFE4AD6516A73A49"
53            ),
54            sha3=sp.bytes(
55                "0x6c487a73002ced6e865cc88e87fc0ead2dbb1d8b4b5b90aea7e3254e9060ca95"
56            ),
57            keccak=sp.bytes(
58                "0xa3e1b4de203e0e5d02205d680485390711ef58bc4c3687b17277b62fde10a45d"
59            ),
60            tz1=bob.public_key_hash,
61        ),
62    )