-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1315 from tapexyz/lemon
feat: signups
- Loading branch information
Showing
11 changed files
with
426 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- Events | ||
CREATE TABLE events ( | ||
id UUID DEFAULT generateUUIDv4(), | ||
name String, | ||
actor Nullable(String), | ||
properties Nullable(String), | ||
url Nullable(String), | ||
city Nullable(String), | ||
country LowCardinality(String), | ||
region Nullable(String), | ||
referrer Nullable(String), | ||
platform String, | ||
browser Nullable(String), | ||
browser_version Nullable(String), | ||
os Nullable(String), | ||
utm_source Nullable(String), | ||
utm_medium Nullable(String), | ||
utm_campaign Nullable(String), | ||
utm_term Nullable(String), | ||
utm_content Nullable(String), | ||
fingerprint Nullable(String), | ||
created DateTime DEFAULT now() | ||
) ENGINE = MergeTree | ||
ORDER BY created; | ||
|
||
-- Signups | ||
CREATE TABLE signups ( | ||
id UUID DEFAULT generateUUIDv4(), | ||
handle String, | ||
address String, | ||
email String, | ||
ls_txn_id String, | ||
txn_hash String, | ||
created DateTime DEFAULT now() | ||
) ENGINE = MergeTree | ||
ORDER BY created; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { zValidator } from '@hono/zod-validator' | ||
import { Hono } from 'hono' | ||
import type { Address } from 'viem' | ||
import { createWalletClient, http } from 'viem' | ||
import { privateKeyToAccount } from 'viem/accounts' | ||
import { polygon, polygonMumbai } from 'viem/chains' | ||
import type { z } from 'zod' | ||
import { any, object } from 'zod' | ||
|
||
import { | ||
ERROR_MESSAGE, | ||
TAPE_SIGNUP_PROXY_ABI, | ||
TAPE_SIGNUP_PROXY_ADDRESS, | ||
ZERO_ADDRESS | ||
} from '@/helpers/constants' | ||
|
||
type Bindings = { | ||
RELAYER_PRIVATE_KEYS: string | ||
INGEST_REST_ENDPOINT: string | ||
LS_WEBHOOK_SECRET: string | ||
} | ||
|
||
const app = new Hono<{ Bindings: Bindings }>() | ||
|
||
const validationSchema = object({ | ||
meta: any(), | ||
data: any() | ||
}) | ||
type RequestInput = z.infer<typeof validationSchema> | ||
|
||
app.post('/', zValidator('json', validationSchema), async (c) => { | ||
const body = await c.req.json<RequestInput>() | ||
try { | ||
const secret = c.env.LS_WEBHOOK_SECRET | ||
const encoder = new TextEncoder() | ||
const secretKey = encoder.encode(secret) | ||
const data = encoder.encode(await c.req.text()) | ||
const providedSignature = c.req.header('X-Signature') | ||
const key = await crypto.subtle.importKey( | ||
'raw', | ||
secretKey, | ||
{ name: 'HMAC', hash: 'SHA-256' }, | ||
false, | ||
['sign'] | ||
) | ||
const digest = await crypto.subtle.sign('HMAC', key, data) | ||
const computedSignature = Array.from(new Uint8Array(digest)) | ||
.map((b) => b.toString(16).padStart(2, '0')) | ||
.join('') | ||
if (providedSignature !== computedSignature) { | ||
throw new Error('Invalid signature.') | ||
} | ||
} catch (error) { | ||
return c.json({ success: false, message: 'Invalid signature' }) | ||
} | ||
|
||
try { | ||
const { data, meta } = body | ||
const { custom_data, test_mode } = meta | ||
const { address, delegatedExecutor, handle } = custom_data | ||
const { order_number, user_email: email } = data.attributes | ||
|
||
// env is delimited by commas with no spaces | ||
const privateKeys = c.env.RELAYER_PRIVATE_KEYS | ||
const allPrivateKeys = privateKeys.split(',') | ||
const randomPrivateKey = | ||
allPrivateKeys[Math.floor(Math.random() * allPrivateKeys.length)] | ||
|
||
const account = privateKeyToAccount(randomPrivateKey as Address) | ||
|
||
const client = createWalletClient({ | ||
account, | ||
chain: test_mode ? polygonMumbai : polygon, | ||
transport: http() | ||
}) | ||
|
||
const hash = await client.writeContract({ | ||
abi: TAPE_SIGNUP_PROXY_ABI, | ||
address: TAPE_SIGNUP_PROXY_ADDRESS, | ||
args: [[address, ZERO_ADDRESS, '0x'], handle, [delegatedExecutor]], | ||
functionName: 'createProfileWithHandle' | ||
}) | ||
|
||
if (!test_mode) { | ||
const clickhouseBody = ` | ||
INSERT INTO signups ( | ||
address, | ||
email, | ||
handle, | ||
hash, | ||
order_number | ||
) VALUES ( | ||
'${address}', | ||
'${email}', | ||
'${handle}', | ||
'${hash}', | ||
'${order_number}' | ||
) | ||
` | ||
const clickhouseResponse = await fetch(c.env.INGEST_REST_ENDPOINT, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: clickhouseBody | ||
}) | ||
if (clickhouseResponse.status !== 200) { | ||
return c.json({ success: false, message: ERROR_MESSAGE }) | ||
} | ||
} | ||
|
||
return c.json({ success: true, hash }) | ||
} catch (error) { | ||
console.log('🚀 ~ signup ~ app.post ~ error:', error) | ||
return c.json({ success: false, message: ERROR_MESSAGE }) | ||
} | ||
}) | ||
|
||
export default app |
Oops, something went wrong.