-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontract.js
41 lines (35 loc) · 1.37 KB
/
contract.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
export async function handle(state, action) {
const input = action.input;
const names = state.names;
const signatures = state.signatures;
const verification_message = state.verification_message;
const ton_molecule_endpoint = state.ton_molecule_endpoint;
if (input.function === "register") {
const name = input.name;
const caller = input.caller;
const signature = input.signature;
ContractAssert(name.trim().length, "error invalid name");
ContractAssert(!(name in names), "name already registered");
ContractAssert(caller && signature, "missing required arguments");
ContractAssert(
!signatures.includes(signature),
"error signed message used"
);
const message = btoa(verification_message);
const res = await _moleculeSignatureVerification(caller, message, signature);
state.names[res.address] = `${name.trim()}.ton`;
signatures.push(signature);
return { state };
}
async function _moleculeSignatureVerification(caller, message, signature) {
try {
const isValid = await EXM.deterministicFetch(
`${ton_molecule_endpoint}/ton-auth/${caller}/${message}/${signature}`
);
ContractAssert(isValid.asJSON()?.result, "unauthorized caller");
return isValid.asJSON();
} catch (error) {
throw new ContractError("molecule res error");
}
}
}