-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.ts
More file actions
87 lines (77 loc) · 2.62 KB
/
Copy pathserver.ts
File metadata and controls
87 lines (77 loc) · 2.62 KB
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
/**
* Copyright 2026 Circle Internet Group, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
import { serve } from '@hono/node-server'
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { initiateUserControlledWalletsClient } from '@circle-fin/user-controlled-wallets'
const client = initiateUserControlledWalletsClient({
apiKey: process.env.CIRCLE_API_KEY!,
})
const app = new Hono()
app.use('/api/*', cors())
// Device + email → tokens the Web SDK needs before verifyOtp
app.post('/api/email/token', async (c) => {
const { deviceId, email } = await c.req.json()
const { data } = await client.createDeviceTokenForEmailLogin({
deviceId,
email,
})
return c.json(data)
})
// No high-level SDK helper for initialize — call REST directly.
// Returns challengeId on first login; 155106 if already initialized.
app.post('/api/email/initialize', async (c) => {
const { userToken } = await c.req.json()
const res = await fetch('https://api.circle.com/v1/w3s/user/initialize', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CIRCLE_API_KEY!}`,
'Content-Type': 'application/json',
'X-User-Token': userToken,
},
body: JSON.stringify({
idempotencyKey: crypto.randomUUID(),
accountType: 'SCA',
blockchains: ['ARC-TESTNET'],
}),
})
const body = await res.json()
if (!res.ok) {
return new Response(JSON.stringify(body), {
status: res.status,
headers: { 'Content-Type': 'application/json' },
})
}
return c.json(body.data)
})
app.post('/api/wallets/list', async (c) => {
const { userToken } = await c.req.json()
const { data } = await client.listWallets({ userToken })
return c.json(data)
})
app.post('/api/wallets/balances', async (c) => {
const { userToken, walletId } = await c.req.json()
const { data } = await client.getWalletTokenBalance({
userToken,
walletId,
})
return c.json(data)
})
const port = Number(process.env.PORT) || 8787
console.log(`UCW Email API listening on http://localhost:${port}`)
serve({ fetch: app.fetch, port })