-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
151 lines (120 loc) · 4 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
import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';
import {HDNodeWallet, getDefaultProvider} from 'ethers';
import {encrypt, decrypt} from "./utils/crypto";
import bcrypt from "bcryptjs";
import {getRow, insertRow, supabase} from "./providers/supabase";
import BIP39 from "bip39";
dotenv.config();
const app: Express = express();
app.use(express.json());
const port = process.env.PORT;
app.post('/seed/generate', async (req: Request, res: Response) => {
const password = req.body.password;
const email = req.body.email;
const BIP39 = require('bip39')
const mnemonic = BIP39.generateMnemonic();
const {error} = await insertRow(
'Users',
{
id: Date.now(),
email,
password: bcrypt.hashSync(password, 10),
accounts: 0,
}
)
if(error) {
return res.status(400).json({
error: "Error creating user",
message: error?.message
})
}
return res.json({
seed: encrypt(mnemonic, password as string),
mnemonic
});
});
app.post('/account/new', async (req: Request, res: Response) => {
const encryptedSeed: string = req.body.seed;
const password: string = req.body.password;
const name: string = req.body.name;
const email: string = req.body.email;
const mnemonic = decrypt(encryptedSeed, password);
const HDWallet = HDNodeWallet.fromPhrase(mnemonic);
// Save wallet count to database
let { error } = await supabase
.rpc('increment', {
useremail: email
})
if (error) {
console.log(error)
return res.status(400).json({
error: "Error creating account",
message: error?.message
})
}
const {data: user, error: getUserError} = await getRow('Users', 'email', email);
if(!user || getUserError) {
console.log(getUserError)
return res.status(400).json({
error: "User not found"
})
}
const accounts = user.accounts;
const wallet = HDWallet.derivePath(`m/44'/60'/0'/0/${accounts}`)
return res.json({
address: wallet.address,
privateKey: wallet.privateKey,
name
})
})
app.post('/seed/recover', async (req: Request, res: Response) => {
const mnemonic: string = req.body.mnemonic;
const email: string = req.body.email;
const password: string = req.body.password;
const BIP39 = require('bip39')
if(!BIP39.validateMnemonic(mnemonic)) {
return res.status(400).json({
error: "Invalid mnemonic"
})
}
const {data: user, error} = await getRow('Users', 'email', email);
if(!user || error) {
return res.status(400).json({
error: "User not found"
})
}
const accounts = user.accounts;
const HDWallet = HDNodeWallet.fromPhrase(mnemonic);
const recoveredWallets: Array<{address: string, privateKey: string, name: string}> = [];
for (let i = 0; i < accounts; i++) {
const wallet = HDWallet.derivePath(`m/44'/60'/0'/0/${i + 1}`)
recoveredWallets.push({
address: wallet.address,
privateKey: wallet.privateKey,
name: `Account ${i + 1}`
})
}
return res.json({
wallets: recoveredWallets,
seed: encrypt(mnemonic, password as string),
})
})
app.post('/seed/verify', async (req: Request, res: Response) => {
const enteredMnemonic: string = req.body.mnemonic;
const encryptedSeed: string = req.body.seed;
const password: string = req.body.password;
const mnemonic = decrypt(encryptedSeed, password);
console.log(enteredMnemonic.toLowerCase() === mnemonic.toLowerCase())
if(enteredMnemonic.toLowerCase() !== mnemonic.toLowerCase()) {
return res.status(401).json({
verified: false
})
}
return res.json({
verified: true
})
})
app.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});