-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
231 lines (200 loc) · 8.58 KB
/
main.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
const { Wallet, initKaspaFramework } = require('@kaspa/wallet');
const { RPC } = require('@kaspa/grpc-node');
const express = require('express')
const Keyv = require("keyv")
const crypto = require('crypto')
var cors = require('cors')
const validator = require('validator')
const uuid4 = require('uuid4')
const network = "kaspa";
const rpc = new RPC({ clientConfig: { host: (process.env.KASPAD_ADDR || "de4.kaspa.org") } });
const userStore = new Keyv(process.env.DB_ADDR || "sqlite://test.db", { serialize: JSON.stringify, deserialize: JSON.parse });
const app = express()
app.use(cors())
app.use(express.json());
// minimum required password
const checkPassword = (pw) => {
return (pw.length >= 8)
}
// api-docs route for some testing and documentation
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use(/^\/$/i, (req, res) => {
res.redirect(301, '/api-docs');
});
// get wallet info for uuid
app.get('/wallets/:wId', (req, res) => {
userStore.get(req.params.wId).then(async (walletInfo) => {
if (!!walletInfo) {
const publicAddress = walletInfo.publicAddress
const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
const [basicUser, basicPassword] = Buffer.from(b64auth, 'base64').toString().split(':')
if (!basicPassword) {
// simple info, if no PW given
res.json({
publicAddress
})
return
} else {
// password given - decrypt wallet
if (crypto.createHash('sha256').update(basicPassword).digest('hex') == walletInfo.hashPassword) {
// pw is fine. Decode wallet
const wallet = await Wallet.import(basicPassword, walletInfo.encryptedMnemonic, { network, rpc }, { disableAddressDerivation: true })
// return extended information
res.json({
"publicAddress": publicAddress,
"uuid": walletInfo.uuid,
"encryptedMnemonic": walletInfo.encryptedMnemonic,
"mnemonic": wallet.mnemonic
})
return
} else {
res.status(403).send('Password incorrect.')
return
}
}
} else {
res.status(404).send('Wallet not found')
}
}
)
})
// endpoint: create a new wallet
app.post('/wallets', (req, res) => {
// password needed for wallet creation
if (!req.body.password) {
res.status(400).send('Password needed.')
return
}
// check if password matches rules
if (checkPassword(req.body.password) === false) {
res.status(400).send('Password needs to have at least 8 chars.')
return
}
// either use given uuid or random uuid
const uuid = req.body.uuid || uuid4()
// check if given uuid is correct format
if (!validator.isUUID(uuid)) {
res.status(400).send("invalid UUID")
return
}
// check if uuid is free -> create wallet
userStore.get(uuid).then(async (data) => {
// uuid already set? return 400
if (data !== undefined) {
res.status(400).send('Uuid already set.')
return
} else {
// create a new wallet
// save hash of password for future verification
const hashPassword = crypto.createHash('sha256').update(req.body.password).digest('hex')
// create a wallet, disable address derivation => one static kaspa address
const wallet = new Wallet(null, null, { network, rpc }, { disableAddressDerivation: true })
const publicAddress = wallet.receiveAddress
const encryptedMnemonic = await wallet.export(req.body.password)
// first set password to userStore and then return the data
userStore.set(uuid, {
publicAddress,
encryptedMnemonic,
hashPassword
}).then(() => res.json({
"publicAddress": publicAddress,
"uuid": uuid,
encryptedMnemonic,
"mnemonic": wallet.mnemonic
}))
.catch(() => res.status(400).send('Error saving wallet.'))
}
})
})
app.put('/wallets/:wId', (req, res) => {
userStore.get(req.params.wId).then(async (data) => {
if (data === undefined) {
res.status(404).send('Wallet not found.')
return
} else {
const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
const [basicUser, basicPassword] = Buffer.from(b64auth, 'base64').toString().split(':')
if (crypto.createHash('sha256').update(basicPassword).digest('hex') == data.hashPassword) {
if (!!req.body.password) {
if (checkPassword(req.body.password) === false) {
res.status(400).send('Password needs to have at least 8 chars.')
return
}
const wallet = await Wallet.import(basicPassword, data.encryptedMnemonic, { network, rpc }, { disableAddressDerivation: true })
const encryptedMnemonic = await wallet.export(req.body.password)
// update data object
data.encryptedMnemonic = encryptedMnemonic
data.hashPassword = crypto.createHash('sha256').update(req.body.password).digest('hex')
// update database
userStore.set(req.params.wId, data).then(() =>
// return new object
res.json({
publicAddress: data.publicAddress,
encryptedMnemonic: data.encryptedMnemonic,
mnemonic: wallet.mnemonic
}))
.catch(() => res.status(400).send('Error saving wallet.'))
return
}
} else {
res.status(403).send('Password incorrect.')
return
}
}
}).catch(() => res.status(400).send('Error reading wallet.'))
})
app.post('/wallets/:wId/transactions', (req, res) => {
userStore.get(req.params.wId).then(async (data) => {
if (data === undefined) {
res.status(404).send('Wallet not found.')
return
} else {
if (!req.headers.authorization) {
res.status(400).send("Auth Basic password needed.")
return
}
// parse login and password from headers
const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
const [basicUser, basicPassword] = Buffer.from(b64auth, 'base64').toString().split(':')
if (crypto.createHash('sha256').update(basicPassword).digest('hex') == data.hashPassword) {
// pw is fine. Decode wallet
if (!req.body.toAddr) {
res.status(400).send("toAddr parameter needed"); return
}
if (!req.body.amount) {
res.status(400).send("amount parameter needed"); return
}
const wallet = await Wallet.import(basicPassword, data.encryptedMnemonic, { network, rpc }, { disableAddressDerivation: true })
await wallet.submitTransaction({
toAddr: req.body.toAddr,
amount: req.body.amount,
changeAddrOverride: wallet.receiveAddress,
calculateNetworkFee: true,
inclusiveFee: req.body.inclusiveFee ? true : false
}, true)
.then((e) => res.send(`${e.txid}`))
.catch((e) => {
if (JSON.stringify(e) !== "{}") {
res.status(400).send(`${JSON.stringify(e)}`)
return
} else {
res.status(400).send(`${e}`)
return
}
})
} else {
res.status(403).send('Password incorrect.')
return
}
}
}).catch(() => res.status(400).send('Error reading wallet.'))
})
// init wallet
const init = async () => {
console.log("initialize routine")
await initKaspaFramework()
};
app.listen(process.env.PORT || 3001)
init();