-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathidentity.js
177 lines (148 loc) · 6.96 KB
/
identity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
*
*
*
* ░█████╗░██████╗░██╗░░██╗ ███╗░░██╗███████╗████████╗░██╗░░░░░░░██╗░█████╗░██████╗░██╗░░██╗
* ██╔══██╗██╔══██╗██║░██╔╝ ████╗░██║██╔════╝╚══██╔══╝░██║░░██╗░░██║██╔══██╗██╔══██╗██║░██╔╝
* ███████║██████╔╝█████═╝░ ██╔██╗██║█████╗░░░░░██║░░░░╚██╗████╗██╔╝██║░░██║██████╔╝█████═╝░
* ██╔══██║██╔══██╗██╔═██╗░ ██║╚████║██╔══╝░░░░░██║░░░░░████╔═████║░██║░░██║██╔══██╗██╔═██╗░
* ██║░░██║██║░░██║██║░╚██╗ ██║░╚███║███████╗░░░██║░░░░░╚██╔╝░╚██╔╝░╚█████╔╝██║░░██║██║░╚██╗
* ╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝ ╚═╝░░╚══╝╚══════╝░░░╚═╝░░░░░░╚═╝░░░╚═╝░░░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝
*
* @title: Ark Network Arweave oracle
* @version 0.0.3
* @author: charmful0x
* @license: MIT
* @website decent.land
*
**/
export async function handle(state, action) {
const input = action.input;
const caller = action.caller;
const admins = state.admins;
const identities = state.identities;
const ERROR_INVALID_DATA_TYPE = "EVM address/TXID must be a string";
const ERROR_INVALID_EVM_ADDRESS_SYNTAX = "invalid EVM address syntax";
const ERROR_INVALID_EVM_TXID_SYNTAX = "invalid EVM TXID syntax";
const ERROR_USER_NOT_FOUND =
"cannot find a user with the given Arweave address";
const ERROR_DOUBLE_INTERACTION =
"cannot reverse an identity validity within less than 3 network blocks";
const ERROR_INVALID_ARWEAVE_ADDRESS = "invalid Arweave address syntax";
const ERROR_CALLER_NOT_ADMIN = "invalid function caller";
const ERROR_USERNAME_NOT_STRING = "Telegram username must be a string";
const ERROR_FUNCTION_MISSING_ARGUMENTS =
"None of the function's required paramters have been passed in";
const ERROR_INVALID_VALIDITY = "the admin has passed an invalid validity";
// USERS FUNCTION
if (input.function === "linkIdentity") {
const address = input?.address;
const verificationReq = input?.verificationReq;
let telegram_enc = input?.telegram_enc; // telegram username passed under AES encryption
if (!address && !verificationReq && !telegram_enc) {
throw new ContractError(ERROR_FUNCTION_MISSING_ARGUMENTS);
}
const userIndex = _getUserIndex(caller);
if (telegram_enc) {
telegram_enc = _validateTelegramUsername(telegram_enc);
}
if (userIndex === -1) {
_validateEvmAddress(address);
_validateEvmTx(verificationReq);
identities.push({
arweave_address: caller,
evm_address: address,
verification_req: verificationReq,
telegram_username: telegram_enc ? telegram_enc : null,
identity_id: SmartWeave.transaction.id,
is_verified: false,
is_evaluated: false,
last_modification: SmartWeave.block.height,
});
return { state };
}
if (address) {
_validateEvmAddress(address);
_validateEvmTx(verificationReq);
// updating the address means that
// the verificationReq should exist
identities[userIndex].evm_address = address;
identities[userIndex].verification_req = verificationReq;
// reset the account verification state
identities[userIndex].is_evaluated = false;
identities[userIndex].is_verified = false;
// generate a new identity ID for the new address
identities[userIndex].identity_id = SmartWeave.transaction.id;
}
if (telegram_enc) {
// telegram username got already checked
identities[userIndex].telegram_username = telegram_enc;
// reset the account verification state
identities[userIndex].is_evaluated = false;
identities[userIndex].is_verified = false;
}
// log the update's blockheight
identities[userIndex].last_modification = SmartWeave.block.height;
return { state };
}
// ADMINS FUNCTION
if (input.function === "verifyIdentity") {
// verify (or reverse verification) the identity of an Arweave
// available in the contract state
const identityOf = input?.identityOf;
const validity = input?.validity;
_validateArweaveAddress(identityOf);
_isAdmin(caller);
const identityIndex = _getUserIndex(identityOf);
ContractAssert([true, false].includes(validity), ERROR_INVALID_VALIDITY);
ContractAssert(identityIndex !== -1, ERROR_USER_NOT_FOUND);
ContractAssert(
identities[identityIndex].last_modification < SmartWeave.block.height + 3,
ERROR_DOUBLE_INTERACTION
);
identities[identityIndex].last_modification = SmartWeave.block.height;
identities[identityIndex].is_verified = validity;
identities[identityIndex].is_evaluated = true;
identities[identityIndex].last_validation = SmartWeave.block.height;
identities[identityIndex].validator = caller;
return { state };
}
// HELPER FUNCTIONS
function _validateEvmAddress(address) {
ContractAssert(typeof address === "string", ERROR_INVALID_DATA_TYPE);
ContractAssert(
/^0x[a-fA-F0-9]{40}$/.test(address),
ERROR_INVALID_EVM_ADDRESS_SYNTAX
);
}
function _validateEvmTx(txid) {
ContractAssert(typeof txid === "string", ERROR_INVALID_DATA_TYPE);
ContractAssert(
/^0x([A-Fa-f0-9]{64})$/.test(txid),
ERROR_INVALID_EVM_TXID_SYNTAX
);
}
function _validateArweaveAddress(address) {
ContractAssert(
/[a-z0-9_-]{43}/i.test(address),
ERROR_INVALID_ARWEAVE_ADDRESS
);
}
function _isAdmin(address) {
_validateArweaveAddress(address);
ContractAssert(admins.includes(address), ERROR_CALLER_NOT_ADMIN);
}
function _validateTelegramUsername(encrypted_username) {
ContractAssert(
typeof encrypted_username === "string",
ERROR_USERNAME_NOT_STRING
);
return encrypted_username.trim();
}
function _getUserIndex(address) {
const index = identities.findIndex(
(usr) => usr.arweave_address === address
);
return index;
}
}