-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
256 lines (210 loc) · 8.29 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import express from 'express';
import { Router } from 'express';
import cors from 'cors';
import path from 'path';
import axios from 'axios';
import * as dotenv from 'dotenv';
import { createClient } from '@supabase/supabase-js';
dotenv.config();
// Create a Supabase client instance
function supabase() {
return createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY);
}
// Fetch the user's wallet information using their access token
async function getUserWallet(access_token) {
try {
// Fetch user data from Supabase using the access token
const { data, error } = await supabase().auth.getUser(access_token);
if (error) {
throw new Error('Failed to fetch user from Supabase');
}
// Fetch wallet data associated with the user
const { data: existingUser, error: fetchError } = await supabase()
.from('wallets')
.select('*')
.eq('user_id', data.user.id)
.single();
if (fetchError) {
throw new Error('Invalid access token or user not found');
}
// Return the user's wallet address
return { error: false, wallet: existingUser.wallet };
} catch (err) {
return { error: true, message: err.message };
}
}
const router = Router();
// Default route: Returns a simple hello message
router.get('/', (req, res) => {
res.status(200).json({ message: 'Hello!' });
});
// Serve a welcome HTML page
router.get('/auth/welcome', (req, res) => {
res.sendFile(path.resolve('welcome.html')); // Serve the welcome page
});
// Verify a user's email using a token
router.post('/auth/verify', async (req, res) => {
const { token } = req.body;
if (!token) {
return res.status(400).json({ error: 'Verification token is required' });
}
try {
const { data, error } = await supabase().auth.getUser(token);
if (error) throw new Error(error.message);
res.status(200).json({ message: 'Email verified successfully!', user: data });
} catch (err) {
res.status(400).json({ error: err.message });
}
});
// Sign up a new user with email and password
router.post('/auth/signup', async (req, res) => {
const { email, password } = req.body;
try {
const { data, error } = await supabase().auth.signUp({ email, password });
if (error) throw new Error(error.message);
res.status(200).json({ user: data.user });
} catch (err) {
res.status(400).json({ error: err.message });
}
});
// Resend a new OTP to the user
router.post('/auth/newOTP', async (req, res) => {
const { email } = req.body;
try {
const { error } = await supabase().auth.resend({ type: 'signup', email });
if (error) throw new Error(error.message);
res.status(200).json({ message: 'New OTP sent' });
} catch (err) {
res.status(400).json({ error: err.message });
}
});
// Sign in a user and manage wallet creation if needed
router.post('/auth/signin', async (req, res) => {
const { email, password } = req.body;
try {
// Sign in the user with Supabase
const { data, error } = await supabase().auth.signInWithPassword({ email, password });
if (error) throw new Error(error.message);
// Check if the user already has a wallet in the database
const { data: existingUser, error: fetchError } = await supabase()
.from('wallets')
.select('*')
.eq('user_id', data.user.id)
.single();
if (fetchError && fetchError.details === 'The result contains 0 rows') {
// If no wallet found, create a new wallet via Thirdweb API
const wallet = await axios.post(
`${process.env.THIRDWEB_URL}/backend-wallet/create`,
{ label: data.user.id },
{
headers: {
'Authorization': `Bearer ${process.env.THIRDWEB_BEARER_TOKEN}`,
'Content-Type': 'application/json',
},
}
);
if (wallet.data.result.status === 'success') {
// Insert the new wallet into Supabase
const { error: insertError } = await supabase().from('wallets').insert([{
user_id: data.user.id,
email: data.user.email,
wallet: wallet.data.result.walletAddress,
}]);
if (insertError) {
throw new Error('Error creating user wallet on Supabase');
}
data.user.wallet = wallet.data.result.walletAddress;
} else {
throw new Error('Error creating user wallet on Thirdweb');
}
} else {
// If wallet exists, use the existing wallet
data.user.wallet = existingUser.wallet;
}
res.status(200).json(data);
} catch (err) {
res.status(400).json({ error: err.message });
}
});
// Retrieve the user's wallet address based on their access token
router.get('/wallet/getAddress', async (req, res) => {
const { access_token } = req.headers;
if (!access_token) {
return res.status(400).json({ error: 'access_token in the header request is required' });
}
const { error, wallet, message } = await getUserWallet(access_token);
if (error) {
return res.status(400).json({ error: message });
}
return res.status(200).json({ wallet });
});
// Sign a transaction using the user's wallet
router.post('/wallet/signTransaction', async (req, res) => {
const { access_token } = req.headers;
const { transaction } = req.body;
if (!access_token) {
return res.status(400).json({ error: 'access_token in the header request is required' });
}
if (!transaction) {
return res.status(400).json({ error: 'transaction in the body request is required' });
}
try {
const { error, wallet, message } = await getUserWallet(access_token);
if (error) throw new Error(message);
const signedTransaction = await axios.post(
`${process.env.THIRDWEB_URL}/backend-wallet/sign-transaction`,
{ transaction },
{
headers: {
'x-backend-wallet-address': wallet,
'x-idempotency-key': `dsigner_${wallet}_${transaction.nonce}`,
'Authorization': `Bearer ${process.env.THIRDWEB_BEARER_TOKEN}`,
'Content-Type': 'application/json',
},
}
);
res.status(200).json({ signedTransaction: signedTransaction.data.result });
} catch (err) {
res.status(400).json({ error: err.message });
}
});
// Sign a message using the user's wallet
router.post('/wallet/signMessage', async (req, res) => {
const { access_token } = req.headers;
const { message } = req.body;
if (!access_token) {
return res.status(400).json({ error: 'access_token in the header request is required' });
}
if (!message) {
return res.status(400).json({ error: 'message in the body request is required' });
}
try {
const { error, wallet, message: userMessage } = await getUserWallet(access_token);
if (error) throw new Error(userMessage);
const signedMessage = await axios.post(
`${process.env.THIRDWEB_URL}/backend-wallet/sign-message`,
{ message, isBytes: false },
{
headers: {
'x-backend-wallet-address': wallet,
'x-idempotency-key': `dsigner_${wallet}_${new Date().getTime()}`,
'Authorization': `Bearer ${process.env.THIRDWEB_BEARER_TOKEN}`,
'Content-Type': 'application/json',
},
}
);
res.status(200).json({ signedMessage: signedMessage.data.result });
} catch (err) {
res.status(400).json({ error: err.message });
}
});
// Initialize the Express app and apply middleware
const app = express();
const port = process.env.PORT || 3000;
app.use(cors()); // Enable CORS
app.use(express.json()); // Parse JSON request bodies
app.use(router); // Use the router with defined routes
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});