-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (137 loc) · 3.84 KB
/
index.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
import { privateKeyToAccount } from 'viem/accounts';
import { getAddress, isAddress } from 'viem';
// Counter Class
export class Indexer {
constructor(state, env) {
this.state = state;
}
async fetch(request) {
const url = new URL(request.url.toLowerCase());
let value = (await this.state.storage.get("value")) || 0;
if (url.pathname.startsWith('/verify')) {
++value;
await this.state.storage.put("value", value);
}
return new Response(value);
}
}
// Main
export default {
// Handle Input
async fetch(request, env, ctx) {
const url = new URL(request.url.toLowerCase());
if (request.method == "OPTIONS") {
return this.output("", 200);
} else if (request.method == "GET") {
if (url.pathname.startsWith('/verify')) {
let paths = url.pathname.split("/");
switch (paths.length) {
case 3:
return await this.generate(request, paths[2], env);
default:
break;
}
} else if (url.pathname.startsWith('/view')) {
let paths = url.pathname.split("/");
switch (paths.length) {
case 3:
return this.output({
key: paths[2],
value: JSON.parse(await env.DATA.get(paths[2])),
}, 200);
default:
return this.output({
key: paths[2],
value: false,
}, 200);
}
} else if (url.pathname.startsWith('/count')) {
let paths = url.pathname.split("/");
switch (paths.length) {
case 2:
let _total = env.INDEXER.idFromName('TOTAL');
let _counter = env.INDEXER.get(_total);
let _response = await _counter.fetch(request);
let _value = await _response.text();
return this.output({
total: _value,
}, 200);
default:
return this.output({
total: null
}, 200);
}
}
return this.output({
error: "Bad Request"
}, 400);
}
return this.output({
error: `Method ${request.method} not allowed`
}, 405);
},
// Handle Output
async output(data, status) {
return new Response(JSON.stringify(data, null, 2), {
status: status,
headers: {
"Allow": "GET, OPTIONS",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
"Cache-Control": "no-cache"
}
});
},
// Handle Signer Validation
async generate(request, githubID, env) {
try {
const gateway = `https://${githubID}.github.io`;
const result = await fetch(`${gateway}/verify.json`).then((res) => {
if (res.status == 200)
return res.json();
else if (res.status == 404) {
throw new Error(`${res.status}: ${gateway}/verify.json not found`);
} else {
throw new Error(`${res.status}: ${res.error}`);
}
});
let addr = result.signer;
if (!isAddress(result.signer)) {
throw new Error(`${addr} is not a valid ethereum signer`);
}
addr = getAddress(addr);
const approver = privateKeyToAccount(env.PRIV_KEY);
const payload = `Requesting Signature To Approve ENS Records Signer\n\nGateway: ${gateway}\nResolver: eip155:${env.CHAIN_ID}:${env.RESOLVER}\nApproved Signer: eip155:${env.CHAIN_ID}:${addr}`
const approvedSig = await approver.signMessage({
message: payload
});
/// Indexer Functions
let _now = JSON.parse(await env.DATA.get(githubID))
// Update INDEXER
if (!_now || _now === null) {
let _total = env.INDEXER.idFromName('TOTAL');
let counter = env.INDEXER.get(_total);
let response = await counter.fetch(request);
let index = await response.text();
let _value = {
index: index,
timestamp: Date.now()
};
// Put on KV_NAMESPACE
await env.DATA.put(githubID, JSON.stringify(_value));
}
return this.output({
gateway: `${githubID}.github.io`,
payload: payload,
approver: approver.address,
approvedFor: addr,
approvalSig: approvedSig
}, 200);
} catch (error) {
return this.output({
error: error.message
}, 404);
}
}
};