Skip to content

Commit

Permalink
👾 Discord worker'i güncelle
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed Aug 26, 2024
1 parent 45d893b commit af171eb
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.DS_Store
.gizli
.gizli*
.vscode
*lock*
*.vars
Expand Down
2 changes: 1 addition & 1 deletion dapp
Submodule dapp updated 53 files
+5 −16 al/Makefile
+16 −15 al/imeceİptal/birim.js
+1 −1 al/sayfa.html
+7 −8 al/sayfa.js
+2 −15 al/tanışma/Makefile
+9 −37 al/tanışma/birim.js
+1 −1 al/tanışma/powWorker.d.js
+1 −0 al/tanışma/powWorker.js
+16 −14 al/ödeme/birim.js
+8 −16 ana/Makefile
+46 −0 ana/build.js
+0 −1 ana/hero/sergi/birim.js
+2 −1 ana/kazan/Makefile
+1 −0 ana/kazan/birim.html
+2 −2 ana/sayfa.html
+4 −3 ana/sayfa.js
+3 −2 birim/ağlar/adlar.js
+5 −5 birim/ağlar/birim.js
+1 −1 birim/başlık/birim.html
+1 −0 birim/blog/Makefile
+13 −1 birim/blog/mina-berkeley/Makefile
+ birim/blog/mina-berkeley/banner.png
+5 −3 birim/blog/mina-berkeley/birim.html
+10 −22 birim/blog/mina-berkeley/birim.js
+3 −3 birim/cüzdan/ağDüğmesi.js
+1 −1 birim/cüzdan/birim.html
+5 −4 birim/cüzdan/birim.js
+13 −5 birim/cüzdan/evmBağlantısı.d.js
+45 −8 birim/cüzdan/evmBağlantısı.js
+2 −0 birim/cüzdan/minaBağlantısı.d.js
+27 −9 birim/cüzdan/minaBağlantısı.js
+4 −3 birim/dil/birim.js
+2 −2 birim/kaydol/birim.js
+1 −0 birim/kpass/birim.js
+14 −0 dapp.yaml
+4 −16 iptal/Makefile
+1 −1 iptal/sayfa.html
+3 −1 iptal/sayfa.js
+2 −2 jsconfig.json
+4 −15 kpassim/Makefile
+6 −1 kpassim/discord.d.js
+3 −13 kpassim/discord.js
+14 −9 kpassim/pencere/birim.js
+1 −1 kpassim/sayfa.html
+14 −19 kpassim/sayfa.js
+1 −1 lib
+4 −15 oyla/Makefile
+1 −1 oyla/sayfa.html
+1 −197 oyla/sayfa.js
+9 −7 package.json
+2 −20 sunucu/Makefile
+2 −5 sunucu/prod.js
+1 −0 sunucu/prod.toml
33 changes: 0 additions & 33 deletions discord/Makefile

This file was deleted.

17 changes: 17 additions & 0 deletions discord/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { uploadWorker } from "@kimlikdao/lib/birimler/cloudflare/targets";
import { readDefines } from "@kimlikdao/lib/birimler/js/util";
import { compile as compileJs } from "@kimlikdao/lib/kdjs/compile";

const compile = async () => compileJs({
entry: "discord/worker.js",
output: "build/discord/worker.js",
define: await readDefines("discord/.gizli.toml", "discord/worker")
});

const deployToCf = (env) => compile()
.then((code) => uploadWorker(env.cloudflare.auth, "kimlikdao-discord", code));

export default {
compile,
deployToCf
};
21 changes: 0 additions & 21 deletions discord/prod.d.js

This file was deleted.

19 changes: 0 additions & 19 deletions discord/prod.toml

This file was deleted.

99 changes: 60 additions & 39 deletions discord/prod.js → discord/worker.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { getHmac, roleRequestChallenge } from "/dapp/kpassim/discord";
import { ChainId } from "/lib/crosschain/chains";
import { roleRequestChallenge } from "/dapp/kpassim/discord";
import oauth2 from "/lib/api/oauth2.d";
import { ModuleWorker } from "/lib/birimler/cloudflare/moduleWorker.d";
import { ChainId, ChainGroup, chainIdToGroup } from "/lib/crosschain/chains";
import { keccak256 } from "/lib/crypto/sha3";
import evm from "/lib/ethereum/evm";
import { KPass as ServerKPass } from "/sdk/server-js/KPass";
import { signerAddress } from "/lib/ethereum/signer";
import { verifyMessage } from "/lib/mina/signer";

/** @define {string} */
const HOST_URL = "https://discord.kimlikdao.org";
/** @define {string} */
const DISCORD_CLIENT_ID = "1068629633970487428";

/** @define {string} */
const DISCORD_GUILD_ID = "951587582712639548";

/** @const {string} */
const HOST_URL = "https://discord.kimlikdao.org";
/** @define {string} */
const DISCORD_CLIENT_SECRET = "DISCORD_CLIENT_SECRET";
/** @define {string} */
const KIMLIKDAO_BOT_TOKEN = "KIMLIKDAO_BOT_TOKEN";
/** @define {string} */
const HMAC_SECRET = "HMAC_SECRET";

/**
* @const {string}
Expand All @@ -33,11 +42,19 @@ const KPass = new ServerKPass({
[ChainId.xfa]: "https://rpc.ankr.com/fantom",
});

/**
* @param {!discord.SignedID} discordID
* @param {string} secret
* @return {string} hmac for the data fields
*/
const getHmac = (discordID, secret) => keccak256(
JSON.stringify(discordID, ["id", "username"]) + secret);

/**
* @param {number} status
* @return {!Response}
*/
const err = (status) => new Response(null, {
const respondWith = (status) => new Response(null, {
status,
headers: { "access-control-allow-origin": "https://kimlikdao.org" }
});
Expand All @@ -46,37 +63,46 @@ const err = (status) => new Response(null, {
* Adds the requested role if the requirements are satisfied.
*
* @param {!Request} req
* @param {!DiscordEnv} env
* @return {!Promise<!Response>|!Response}
*/
const addRole = (req, env) => req.json()
.then(/** @type {function(*)} */((/** @type {!discord.RoleRequest} */ roleReq) => {
if (getHmac(roleReq.discordID, env.HMAC_SECRET) != roleReq.discordID.hmac)
return err(401);
const addRole = (req) => req.json()
.then(/** @type {function(*)} */((/** @type {discord.RoleRequest} */ roleReq) => {
if (getHmac(roleReq.discordID, HMAC_SECRET) != roleReq.discordID.hmac)
return respondWith(401);
/** @const {ChainId} */
const chainId = /** @type {ChainId} */(roleReq.chainID);
/** @const {ChainGroup} */
const chainGroup = chainIdToGroup(chainId);
/** @const {string} */
const digest = evm.personalDigest(
roleRequestChallenge(roleReq.discordID, roleReq.role, roleReq.lang == "tr"));
const message = roleRequestChallenge(roleReq.discordID, roleReq.role, roleReq.lang == "tr");
/** @const {string} */
const address = evm.signerAddress(digest, roleReq.signature);
const address = chainGroup == ChainGroup.EVM
? signerAddress(evm.personalDigest(message),
/** @type {eth.CompactSignature} */(roleReq.signerSignature))
: /** @type {mina.SignerSignature} */(roleReq.signerSignature).signer;
if (chainGroup == ChainGroup.MINA &&
!verifyMessage(message, /** @type {mina.SignerSignature} */(roleReq.signerSignature)))
return respondWith(400);

switch (roleReq.role) {
case "KPASS HOLDER":
return KPass.handleOf(/** @type {ChainId} */(roleReq.chainID), address)
return KPass.handleOf(chainId, address)
.then((/** @type {string} */ cidHex) => {
if (evm.isZero(cidHex)) return err(412);
if (evm.isZero(cidHex)) return respondWith(412);
/** @const {string} */
const roleID = ROLE_IDS[roleReq.role];
return fetch(DISCORD_API_URL + `guilds/${DISCORD_GUILD_ID}/`
+ `members/${roleReq.discordID.id}/roles/${roleID}`, {
method: "PUT",
headers: {
"authorization": "Bot " + env.KIMLIKDAO_BOT_TOKEN,
"authorization": "Bot " + KIMLIKDAO_BOT_TOKEN,
"content-type": "application/json"
}
}).then((res) => res.ok ? err(200) : err(401), () => err(400))
}).then((res) => res.ok ? respondWith(200) : respondWith(401), () => respondWith(400))
},
() => err(404))
() => respondWith(404))
default:
return err(405);
return respondWith(405);
}
}))

Expand All @@ -88,10 +114,9 @@ const kapat = () =>

/**
* @param {!Request} req
* @param {!DiscordEnv} env
* @return {!Promise<!Response>|!Response}
*/
const getDiscordID = (req, env) => {
const getDiscordID = (req) => {
/** @const {string} */
const code = new URLSearchParams(req.url.slice(HOST_URL.length + 1)).get("code") || "";
if (!code) return kapat();
Expand All @@ -101,7 +126,7 @@ const getDiscordID = (req, env) => {
grant_type: "authorization_code",
code,
client_id: DISCORD_CLIENT_ID,
client_secret: env.DISCORD_CLIENT_SECRET,
client_secret: DISCORD_CLIENT_SECRET,
redirect_uri: HOST_URL
};
return fetch(DISCORD_API_URL + "oauth2/token", {
Expand All @@ -112,19 +137,19 @@ const getDiscordID = (req, env) => {
body: new URLSearchParams(/** @type {!Object<string, string>} */(tokenRequest))
})
.then((res) => res.json())
.then((data) => fetch(DISCORD_API_URL + "users/@me", {
.then((/** !Object */ data) => fetch(DISCORD_API_URL + "users/@me", {
headers: { "authorization": "Bearer " + /** @type {!oauth2.AccessToken} */(data).access_token }
}))
.then((res) => res.json())
.then((data) => {
.then((/** !Response */ res) => res.json())
.then((/** !Object<string, string> */ data) => {
/** @const {string} */
const disc = data["discriminator"];
/** @const {!discord.SignedID} */
const discordID = {
id: data["id"],
username: disc == "0" ? data["username"] : data["username"] + "#" + disc
};
discordID.hmac = getHmac(discordID, env.HMAC_SECRET);
discordID.hmac = getHmac(discordID, HMAC_SECRET);
return new Response(
`<!doctypehtml><html><script>window.opener.postMessage(${JSON.stringify(discordID)
},"https://kimlikdao.org");window.close()</script></html>`, {
Expand All @@ -142,27 +167,23 @@ const approveCors = () => new Response("", {
}
});

/**
* @implements {cloudflare.ModuleWorker}
*/
/** @implements {ModuleWorker} */
const DiscordWorker = {
/**
* @override
*
* @param {!cloudflare.Request} req
* @param {!DiscordEnv} env
* @param {!cloudflare.Context} _
* @param {!Request} req
* @return {!Promise<!Response>|!Response}
*/
fetch(req, env, _) {
fetch(req) {
return req.url.length == HOST_URL.length + 1
? req.method == "GET"
? Response.redirect("https://discord.com/invite/H2wg6pcWXG")
: req.method == "OPTIONS"
? approveCors()
: addRole(req, env)
: getDiscordID(req, env);
: addRole(req)
: getDiscordID(req);
}
}
};

globalThis["DiscordWorker"] = DiscordWorker;
export default DiscordWorker;
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "kimlikdao-apps",
"version": "0.0.0",
"description": "KimlikDAO apps",
"author": "KimlikDAO <[email protected]>",
"private": true,
Expand All @@ -11,9 +12,6 @@
"test": "vitest"
},
"devDependencies": {
"@swc/cli": "^0.3.12",
"@swc/core": "^1.5.7",
"@swc/helpers": "^0.5.11",
"csso-cli": "^4.0.2",
"express": "^4.19.2",
"google-closure-compiler": "^20240317.0.0",
Expand All @@ -23,12 +21,13 @@
"svgexport": "^0.4.2",
"svgo": "^3.3.2",
"toml": "^3.0.0",
"typescript": "^5.4.5",
"uglify-js": "^3.16.2",
"vite": "^5.2.11",
"wrangler": "^3.57.0"
"typescript": "^5.5.3",
"uglify-js": "^3.18.0",
"vite": "^5.3.2",
"wrangler": "^3.72.2"
},
"dependencies": {
"o1js": "^1.2.0"
"mina-signer": "^3.0.7",
"o1js": "^1.4.0"
}
}
5 changes: 5 additions & 0 deletions run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bun
import discord from "./discord/build";
import env from "./.gizli.toml";

console.log(await discord.deployToCf(env));

0 comments on commit af171eb

Please sign in to comment.