This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
177 lines (130 loc) · 4.25 KB
/
app.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
const express = require('express')
const formidable = require('express-formidable')
const session = require('express-session')
const app = express()
const port = 3000
// API keys
const API_BASEURL = 'https://api-playground.eu.upvest.co/1.0/'
const DECRYPTION_KEY = 'HOg8uH+u5X72hM5fv1Dw6wizLBbNs0QZMP2Yy9Kk30E='
const API_KEY = '3xn7XH5kqoiYYjS5Pl9P3A'
const API_SECRET = '29zWI0XhXvGjamqOsmnasTmakM8EGDEIOWZgvYALWx0'
const API_PASSPHRASE = 'vrg4DSpZUgwQwSlc9kJkiPJHmPwCvIeDEh7IcjbXoJE'
const API_OAUTH_ID = 'cyJqUWANySJXmu37FUKTD7M5x7F7Rz4W6LzRIkjF'
const API_OAUTH_SECRET = '6cGjSJYAFORez2UxYSeYuJDzGCyS4o5nPAwq9X0gs2cmJbLTCsuxHPtYZSdCmFYibglYOW7lRwHJ8pKz5neqgn0XinQIp9i2ge5WZf3XeB7mIzBpJNTLFu2cXEQWO2tP'
const { UpvestTenancyAPI } = require('@upvest/tenancy-api')
const { UpvestClienteleAPI, UpvestClienteleAPIFromOAuth2Token } = require('@upvest/clientele-api')
const tenancy = new UpvestTenancyAPI(
API_BASEURL, API_KEY, API_SECRET, API_PASSPHRASE
)
// Main application
app.set('view engine', 'pug')
app.use('/js', express.static(__dirname + '/node_modules/jquery/dist'))
app.use(express.static(__dirname + '/node_modules/bootstrap/dist'))
app.use(formidable())
app.use(session({
secret: 'sup3rs3cr3t',
resave: false,
saveUninitialized: false
}))
app.get('/', (req, res) => {
res.render('index')
})
// Sign up
app.get('/signup', (req, res) => {
res.render('signup')
})
app.post('/signup', async(req, res) => {
const username = req.fields['username']
const password = req.fields['password']
try {
user = await tenancy.users.create(username, password)
} catch (error) {
console.log(error)
}
res.render('signup', {
'username': user.username,
'recoverykit': user.recoverykit
})
})
// Log in
app.get('/login', (req, res) => {
res.render('login')
})
app.post('/login', async(req, res) => {
const username = req.fields['username']
const password = req.fields['password']
const clientele = new UpvestClienteleAPI(
API_BASEURL, API_OAUTH_ID, API_OAUTH_SECRET,
username, password,
)
const echo = await clientele.echo('foobar')
req.session.oauth_token = await clientele.getCachedToken()
res.redirect('/wallets')
})
app.get('/wallets', async(req, res) => {
const token = req.session.oauth_token || false
if (!token) {
return res.redirect('/login')
}
const clientele = new UpvestClienteleAPIFromOAuth2Token(
API_BASEURL, token,
)
var wallets = []
for await (const wallet of clientele.wallets.list()) {
wallets.push(wallet)
}
res.render('wallets', {'wallets': wallets})
})
app.post('/wallets', async(req, res) => {
const token = req.session.oauth_token || false
if (!token) {
return res.redirect('/login')
}
const clientele = new UpvestClienteleAPIFromOAuth2Token(
API_BASEURL, token,
)
const password = req.fields['password']
const asset_id = req.fields['asset_id']
const wallet = await clientele.wallets.create(asset_id, password, null)
res.redirect('/wallets')
})
app.get('/wallet/:wallet_id', async(req, res) => {
const token = req.session.oauth_token || false
if (!token) {
return res.redirect('/login')
}
const clientele = new UpvestClienteleAPIFromOAuth2Token(
API_BASEURL, token,
)
const wallet = await clientele.wallets.retrieve(req.params.wallet_id)
res.render('wallet', {
'wallet': wallet,
})
})
// https://etherconverter.online/
app.post('/wallet/:wallet_id', async(req, res) => {
const token = req.session.oauth_token || false
if (!token) {
return res.redirect('/login')
}
const clientele = new UpvestClienteleAPIFromOAuth2Token(
API_BASEURL, token,
)
const recipient = req.fields['recipient']
const amount = req.fields['amount']
const password = req.fields['password']
const asset_id = req.fields['asset_id']
const fee = 41180000000000
const tx = await clientele.transactions.create(
req.params.wallet_id,
password,
recipient,
asset_id,
amount,
fee
)
res.render('transaction', {
'transaction': tx
})
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))