Contract con_gamma_phi_profile_v1


Contract Code


  
1 # con_gamma_phi_profile_v1
2 metadata = Hash(default_value=None)
3 usernames = Hash(default_value=None)
4 total_users = Variable()
5 owner = Variable()
6
7
8 DEFAULT_METADATA_FIELDS = [
9 'username',
10 'display_name',
11 'telegram',
12 'twitter',
13 'instagram',
14 'facebook',
15 'discord',
16 'icon_base64_svg',
17 'icon_base64_png',
18 'icon_url',
19 'public_rsa_key'
20 ]
21
22
23 @construct
24 def seed():
25 total_users.set(0)
26 owner.set(ctx.caller)
27
28
29 @export
30 def create_profile(
31 username: str,
32 display_name: str,
33 telegram: str = None,
34 twitter: str = None,
35 instagram: str = None,
36 facebook: str = None,
37 discord: str = None,
38 icon_base64_svg: str = None,
39 icon_base64_png: str = None,
40 icon_url: str = None,
41 public_rsa_key: str = None # Expected to be "{n}|{e}"
42 ):
43
44 user_address = ctx.caller
45
46 validate_username(username)
47 assert usernames[username] is None, f'Username {username} already exists.'
48
49 usernames[username] = user_address
50 metadata[user_address, 'username'] = username
51 metadata[user_address, 'display_name'] = display_name or username
52 metadata[user_address, 'telegram'] = telegram
53 metadata[user_address, 'twitter'] = twitter
54 metadata[user_address, 'instagram'] = instagram
55 metadata[user_address, 'facebook'] = facebook
56 metadata[user_address, 'discord'] = discord
57 metadata[user_address, 'icon_base64_svg'] = icon_base64_svg
58 metadata[user_address, 'icon_base64_png'] = icon_base64_png
59 metadata[user_address, 'icon_url'] = icon_url
60 update_public_rsa_key(user_address=user_address, key=public_rsa_key)
61 total_users.set(total_users.get()+1)
62
63
64 def validate_username(username: str):
65 assert username is not None and len(username) > 0, 'Username cannot be null or empty'
66 assert isinstance(username, str), 'Username must be a string.'
67 assert len(username) <= 16, 'Usernames cannot be longer than 16 characters.'
68 assert all([c.isalnum() or c in ('_', '-') for c in username]), 'Username has invalid characters. Each character must be alphanumeric, a hyphen, or an underscore.'
69 assert username[0] not in ('-', '_') and username[-1] not in ('-', '_'), 'Usernames cannot start or end with a hyphen or underscore.'
70
71
72 def update_public_rsa_key(user_address, key: str):
73 if key is None:
74 metadata[user_address, 'public_rsa_key'] = None
75 else:
76 parts = key.split('|')
77 assert len(key) != 2, 'Invalid key format'
78 metadata[user_address, 'public_rsa_key'] = [int(parts[0]), int(parts[1])]
79
80
81 def update_profile_helper(user_address: str, key: str, value: Any):
82 assert key != 'extra_fields', 'You cannot update extra_fields with this method.'
83 if key == 'username':
84 validate_username(value)
85
86 username = metadata[user_address, 'username']
87 assert username is not None, 'This user does not exist.'
88 assert usernames[value] is None, f'Username {value} already exists.'
89 assert value is not None, 'No username provided. Call delete_profile to remove a user profile.'
90
91 usernames[value] = user_address
92
93 elif key == 'public_rsa_key':
94 update_public_rsa_key(user_address=user_address, key=value)
95
96 elif key not in DEFAULT_METADATA_FIELDS:
97 extra_fields = metadata[user_address, 'extra_fields'] or []
98 if key not in extra_fields:
99 extra_fields.append(key)
100 metadata[user_address, 'extra_fields'] = extra_fields
101
102 metadata[user_address, key] = value
103
104
105 def delete_profile_helper(user_address: str):
106 username = metadata[user_address, 'username']
107 assert username is not None, 'This user does not exist.'
108 usernames[username] = None
109 metadata[user_address, 'username'] = None
110 metadata[user_address, 'display_name'] = None
111 metadata[user_address, 'telegram'] = None
112 metadata[user_address, 'twitter'] = None
113 metadata[user_address, 'instagram'] = None
114 metadata[user_address, 'facebook'] = None
115 metadata[user_address, 'discord'] = None
116 metadata[user_address, 'icon_base64_svg'] = None
117 metadata[user_address, 'icon_base64_png'] = None
118 metadata[user_address, 'icon_url'] = None
119 update_public_rsa_key(user_address=user_address, key=None)
120 extra_fields = metadata[user_address, 'extra_fields'] or []
121 for field in extra_fields:
122 metadata[user_address, field] = None
123
124 @export
125 def delete_profile():
126 delete_profile_helper(user_address=ctx.caller)
127
128
129 @export
130 def force_delete_profile(user_address: str):
131 assert ctx.caller == owner.get(), 'Only the owner can call force_delete_profile'
132 delete_profile_helper(user_address=user_address)
133
134
135 @export
136 def update_profile(key: str, value: Any):
137 update_profile_helper(user_address=ctx.caller, key=key, value=value)
138
139
140 @export
141 def force_update_profile(user_address: str, key: str, value: Any):
142 assert ctx.caller == owner.get(), 'Only the owner can call force_update_profile'
143 update_profile_helper(user_address=user_address, key=key, value=value)
144
145 @export
146 def change_ownership(new_owner: str):
147 assert ctx.caller == owner.get(), 'Only the owner can change ownership!'
148
149 owner.set(new_owner)

Byte Code

e30000000000000000000000000e000000400000007330010000650064006401640264038d035a01650064006401640464038d035a0265036401640564068d025a0465036401640764068d025a0564086409640a640b640c640d640e640f641064116412670b5a066413641484005a0765086401830164306509650965096509650965096509650965096509650964159c0b64166417840583015a0a650964189c016419641a84045a0b6509641b9c01641c641d84045a0c65096509650d641e9c03641f642084045a0e650964219c016422642384045a0f65086401830164246425840083015a10650864018301650964219c0164266427840483015a116508640183016509650d64289c026429642a840483015a1265086401830165096509650d641e9c03642b642c840483015a136508640183016509642d9c01642e642f840483015a146400530029314eda18636f6e5f67616d6d615f7068695f70726f66696c655f7631da086d657461646174612903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65da09757365726e616d6573da0b746f74616c5f7573657273290272040000007205000000da056f776e6572da08757365726e616d65da0c646973706c61795f6e616d65da0874656c656772616dda0774776974746572da09696e7374616772616dda0866616365626f6f6bda07646973636f7264da0f69636f6e5f6261736536345f737667da0f69636f6e5f6261736536345f706e67da0869636f6e5f75726cda0e7075626c69635f7273615f6b6579630000000000000000000000000200000043000000731a00000074006a0164018301010074026a0174036a04830101006400530029024ee9000000002905da0d5f5f746f74616c5f7573657273da03736574da075f5f6f776e6572da03637478da0663616c6c6572a900721a000000721a000000da00da045f5f5f5f0d000000730400000000010a01721c000000290b7209000000720a000000720b000000720c000000720d000000720e000000720f0000007210000000721100000072120000007213000000630b000000000000000c000000040000004300000073d000000074006a017d0b74027c008301010074037c00190064006b08732a740464017c009b0064029d03830182017c0b74037c003c007c0074057c0b640366023c007c0170447c0074057c0b640466023c007c0274057c0b640566023c007c0374057c0b640666023c007c0474057c0b640766023c007c0574057c0b640866023c007c0674057c0b640966023c007c0774057c0b640a66023c007c0874057c0b640b66023c007c0974057c0b640c66023c0074067c0b7c0a640d8d02010074076a0874076a098300640e17008301010064005300290f4e7a09557365726e616d65207a1020616c7265616479206578697374732e7209000000720a000000720b000000720c000000720d000000720e000000720f0000007210000000721100000072120000002902da0c757365725f61646472657373da036b6579e901000000290a72180000007219000000da135f5f76616c69646174655f757365726e616d65da0b5f5f757365726e616d6573da0e417373657274696f6e4572726f72da0a5f5f6d65746164617461da175f5f7570646174655f7075626c69635f7273615f6b657972150000007216000000da03676574290c7209000000720a000000720b000000720c000000720d000000720e000000720f0000007210000000721100000072120000007213000000721d000000721a000000721a000000721b000000da0e6372656174655f70726f66696c651200000073220000000005060108010601160108010c0110010c010c010c010c010c010c010c010c010c0172260000002901720900000063010000000000000001000000030000004300000073800000007c0064006b09721474007c00830164016b04731c740164028301820174027c0074038302732e740164038301820174007c00830164046b017342740164058301820174046406640784007c00440083018301735c74016408830182017c0064011900640d6b0772747c00640e1900640f6b07737c7401640c830182016400530029104e72140000007a20557365726e616d652063616e6e6f74206265206e756c6c206f7220656d7074797a1a557365726e616d65206d757374206265206120737472696e672ee9100000007a2e557365726e616d65732063616e6e6f74206265206c6f6e676572207468616e20313620636861726163746572732e630100000000000000020000000500000053000000731c00000067007c005d147d017c016a00830070167c0164026b069102710453002903da015ffa012d2902722800000072290000002901da076973616c6e756d2902da022e30da0163721a000000721a000000721b000000fa0a3c6c697374636f6d703e30000000730200000006007a275f5f76616c69646174655f757365726e616d652e3c6c6f63616c733e2e3c6c697374636f6d703e7a61557365726e616d652068617320696e76616c696420636861726163746572732e204561636820636861726163746572206d75737420626520616c7068616e756d657269632c20612068797068656e2c206f7220616e20756e64657273636f72652e72290000007228000000721f0000007a3a557365726e616d65732063616e6e6f74207374617274206f7220656e64207769746820612068797068656e206f7220756e64657273636f72652e290272290000007228000000e9ffffffff2902722900000072280000002905da036c656e7222000000da0a6973696e7374616e6365da03737472da03616c6c29017209000000721a000000721a000000721b00000072200000002a000000731200000000010e010e01120106010e01140106011a0172200000002901721e00000063020000000000000003000000040000004300000073580000007c0164006b087216640074007c00640166023c006e3e7c016a01640283017d0274027c01830164036b037334740364048301820174047c0264051900830174047c02640619008301670274007c00640166023c006400530029074e7213000000fa017ce9020000007a12496e76616c6964206b657920666f726d61747214000000721f00000029057223000000da0573706c6974722f0000007222000000da03696e742903721d000000721e000000da057061727473721a000000721a000000721b000000722400000036000000730c000000000108010e020a0114010c0172240000002903721d000000721e000000da0576616c756563030000000000000005000000040000004300000073ce0000007c0164016b03731074006402830182017c0164036b02727274017c028301010074027c006403660219007d037c0364006b09733c740064048301820174037c02190064006b087358740064057c029b0064069d03830182017c0264006b09736874006407830182017c0074037c023c006e4c7c0164086b02728874047c007c0264098d0201006e367c0174056b0772be74027c00640166021900709e67007d047c017c046b0772be7c046a067c01830101007c0474027c00640166023c007c0274027c007c0166023c0064005300290a4eda0c65787472615f6669656c64737a30596f752063616e6e6f74207570646174652065787472615f6669656c647320776974682074686973206d6574686f642e72090000007a1954686973207573657220646f6573206e6f742065786973742e7a09557365726e616d65207a1020616c7265616479206578697374732e7a434e6f20757365726e616d652070726f76696465642e2043616c6c2064656c6574655f70726f66696c6520746f2072656d6f7665206120757365722070726f66696c652e72130000002902721d000000721e000000290772220000007220000000722300000072210000007224000000da1744454641554c545f4d455441444154415f4649454c4453da06617070656e642905721d000000721e000000723800000072090000007239000000721a000000721a000000721b000000da175f5f7570646174655f70726f66696c655f68656c70657240000000732000000000011001080108010c0110011c0110010a0108010e010801100108010a010c01723c0000002901721d00000063010000000000000004000000050000004300000073d600000074007c006401660219007d017c0164006b09731c7401640283018201640074027c013c00640074007c00640166023c00640074007c00640366023c00640074007c00640466023c00640074007c00640566023c00640074007c00640666023c00640074007c00640766023c00640074007c00640866023c00640074007c00640966023c00640074007c00640a66023c00640074007c00640b66023c0074037c006400640c8d02010074007c00640d6602190070b667007d0278187c0244005d107d03640074007c007c0366023c0071be570064005300290e4e72090000007a1954686973207573657220646f6573206e6f742065786973742e720a000000720b000000720c000000720d000000720e000000720f0000007210000000721100000072120000002902721d000000721e0000007239000000290472230000007222000000722100000072240000002904721d00000072090000007239000000da056669656c64721a000000721a000000721b000000da175f5f64656c6574655f70726f66696c655f68656c70657253000000732200000000010c01100108010c010c010c010c010c010c010c010c010c010c010c0110010a01723e0000006300000000000000000000000003000000430000007310000000740074016a0264018d0101006400530029024e2901721d0000002903723e00000072180000007219000000721a000000721a000000721a000000721b000000da0e64656c6574655f70726f66696c656700000073020000000002723f000000630100000000000000010000000300000043000000732400000074006a0174026a0383006b027316740464018301820174057c0064028d0101006400530029034e7a2c4f6e6c7920746865206f776e65722063616e2063616c6c20666f7263655f64656c6574655f70726f66696c652901721d000000290672180000007219000000721700000072250000007222000000723e0000002901721d000000721a000000721a000000721b000000da14666f7263655f64656c6574655f70726f66696c656c000000730600000000021001060172400000002902721e00000072380000006302000000000000000200000005000000430000007314000000740074016a027c007c0164018d0301006400530029024e2903721d000000721e00000072380000002903723c000000721800000072190000002902721e0000007238000000721a000000721a000000721b000000da0e7570646174655f70726f66696c6573000000730200000000027241000000630300000000000000030000000500000043000000732800000074006a0174026a0383006b027316740464018301820174057c007c017c0264028d0301006400530029034e7a2c4f6e6c7920746865206f776e65722063616e2063616c6c20666f7263655f7570646174655f70726f66696c652903721d000000721e0000007238000000290672180000007219000000721700000072250000007222000000723c0000002903721d000000721e0000007238000000721a000000721a000000721b000000da14666f7263655f7570646174655f70726f66696c6578000000730600000000021001060172420000002901da096e65775f6f776e6572630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174026a057c00830101006400530029024e7a244f6e6c7920746865206f776e65722063616e206368616e6765206f776e65727368697021290672180000007219000000721700000072250000007222000000721600000029017243000000721a000000721a000000721b000000da106368616e67655f6f776e6572736869707f000000730400000000021601724400000029094e4e4e4e4e4e4e4e4e2915da044861736872230000007221000000da085661726961626c6572150000007217000000723a000000721c000000da085f5f6578706f72747231000000722600000072200000007224000000da03416e79723c000000723e000000723f0000007240000000724100000072420000007244000000721a000000721a000000721a000000721b000000da083c6d6f64756c653e0100000073380000000601080106010801040108010c0106010a010a030805060100010001000126140e0c0e0a12130e1410050601100606011204060114060601