-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
189 lines (162 loc) · 4.59 KB
/
index.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
import * as fs from "fs";
import Arweave from "arweave";
import { google } from "googleapis";
import Koa from "koa";
import cors from "@koa/cors";
import Router from "@koa/router";
import * as dotenv from "dotenv";
import { COMMUNITY as COMMUNITY_ID } from "arverify";
dotenv.config();
import {sendGenesis, tipReceived, getVerification} from "arverify";
import pkg from "./package.json";
const client = new Arweave({
host: "arweave.net",
port: 443,
protocol: "https",
});
let config: any;
try {
config = JSON.parse(
fs.readFileSync("config.json", {
encoding: "utf-8",
})
);
} catch (err) {
config = {
googleClientID: process.env.GOOGLE_CLIENT_ID,
googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
endpoint: process.env.ENDPOINT,
keyfile: process.env.KEYFILE,
};
}
const jwk = JSON.parse(
fs.readFileSync(config.keyfile, {
encoding: "utf-8",
})
);
const oauthClient = new google.auth.OAuth2(
config["googleClientID"],
config["googleClientSecret"],
config["endpoint"] +
(config["endpoint"].endsWith("/") ? "" : "/") +
"verify/callback"
);
const http = new Koa();
http.use(cors());
const router = new Router();
router.get("/ping", async (ctx, next) => {
ctx.body = {
status: "alive",
version: pkg.version,
};
await next();
});
router.get("/verify", async (ctx, next) => {
console.log("===== /verify =====");
const addr = ctx.query["address"];
const returnUri = ctx.query["return"];
const referral = ctx.query["referral"];
if (!addr) {
console.log("No address supplied.");
ctx.status = 400;
ctx.body = {
status: "error",
message: "address is required",
};
} else {
console.log("Received verification request for address:\n -", addr);
if ((await getVerification(addr)).txID) {
ctx.body = {
status: "success",
message: "already verified",
};
console.log("Address already verified.");
} else {
if (await tipReceived(addr, await client.wallets.jwkToAddress(jwk))) {
const uri = oauthClient.generateAuthUrl({
scope: ["openid", "email", "profile"],
state: JSON.stringify({ address: addr, returnUri, referral }),
});
ctx.body = {
status: "success",
uri,
};
console.log("Generated a unique auth URI.");
} else {
console.log("No tip received from this address yet.");
ctx.status = 400;
ctx.body = {
status: "error",
message: "no tip",
};
}
}
}
await next();
console.log("===================\n");
});
router.get("/verify/callback", async (ctx, next) => {
console.log("===== /verify/callback =====");
const code = ctx.query["code"];
const state = JSON.parse(ctx.query["state"]);
const addr = state["address"];
const uri = state["returnUri"];
const referral = state["referral"];
console.log("Received callback for address:\n -", addr);
const res = await oauthClient.getToken(code);
if (res.tokens.access_token) {
const info = await oauthClient.getTokenInfo(res.tokens.access_token);
if (info.email_verified) {
const tags = {
"Application": "ArVerify",
Action: "Verification",
Method: "Google",
Address: addr,
Referral: referral,
// arweave-activities tags
Service: "ArVerify",
"Community-ID": COMMUNITY_ID,
Message: `${addr} verified via Google Sign-In`,
Type: "ArweaveActivity",
};
const tx = await client.createTransaction(
{
target: addr,
data: Math.random().toString().slice(-4),
},
jwk
);
for (const [key, value] of Object.entries(tags)) {
tx.addTag(key, value);
}
await client.transactions.sign(tx, jwk);
await client.transactions.post(tx);
ctx.body = {
status: "success",
id: tx.id,
};
console.log("Sent Arweave transaction:\n -", tx.id);
} else {
console.log("Email address is not verified.");
}
} else {
console.log("No access token.");
}
if (uri) ctx.redirect(uri);
await next();
console.log("============================\n");
});
http.use(router.routes());
async function start() {
const genesis = await sendGenesis(jwk, config["endpoint"]);
if (genesis === "stake") {
console.log("Sorry, you don't have any stake.\n");
process.exit(1);
} else {
console.log("Sent genesis tx with id:\n -", genesis, "\n");
const port = process.env.PORT || 3000;
http.listen(port);
console.log("ArVerify Auth Node started at port: ", port, "\n");
}
}
start();