-
Notifications
You must be signed in to change notification settings - Fork 0
/
scp_invite.js
287 lines (254 loc) · 12.9 KB
/
scp_invite.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Distributed under MS-RSL license: see /LICENSE for terms. Copyright 2019-2021 Dominic Morris.
//
// scp_invites -- invites & notifies the referrer with arbitrary info from the referred person
// (links the two accounts with arbitrary data-sharing, e.g. pubkeys)
//
'use strict';
const sql = require('mssql');
const config = require('./config');
const utils = require('./scp_util.js');
const email_lib = require('./email_lib.js');
module.exports = {
send_invite_link: async function (req, res) { // sends an invite-link: singular invite - email only
try {
if (!req.body) return res.sendStatus(400);
// validation
let owner = req.body.owner;
let e_email = req.body.e_email;
let symbol = req.body.symbol;
let target_email = req.body.target_email;
let target_name = req.body.target_name; // optional (fallback: target_email)
let source_email = req.body.source_email;
let source_name = req.body.source_name; // optional (fallback: source_email)
let source_gender = req.body.source_gender; // optional
if (!owner || !e_email || !symbol || !target_email || !source_email) return res.sendStatus(400);
if (owner.length==0 || e_email.length==0 || symbol.length==0 || target_email.length==0 || source_email.length==0) return res.sendStatus(400);
if (symbol != 'BTC_TEST' && symbol != 'BTC_SEG') return res.sendStatus(400);
// authentication
const authenticated = await utils.check_auth(owner, e_email);
if (authenticated == false) {
return res.status(403).send({ msg: "Permission denied" });
}
if (await exists(owner, target_email, symbol)) {
return res.status(201).send({ res: "ok", msg: `Already invited ${target_email}` });
}
// create the invite row
const save = await scp_sql_pool.request()
.input('owner', sql.NVarChar, `${owner}`)
.input('symbol', sql.NVarChar, `${symbol}`)
.input('target_email', sql.NVarChar, `${target_email}`)
.input('target_name', sql.NVarChar, `${target_name}`)
.input('source_email', sql.NVarChar, source_email)
.input('source_name', sql.NVarChar, source_name || null)
.input('source_gender', sql.NVarChar, source_gender || null)
.query(`INSERT INTO [_scpx_invite]\
([owner], [accepted_utc], [payload_json], [target_email], [source_email], [source_name], [symbol], [source_gender], [target_name])\
VALUES (@owner, NULL, NULL, @target_email, @source_email, @source_name, @symbol, @source_gender, @target_name); \
SELECT @@IDENTITY`)
.catch(err => {
console.error(`## send_invite_link - INSERT failed! ${err.message}`, err);
});
const id = Object.values(save.recordset[0])[0];
const row = (await by_id(id))[0];
const invite_id = row.invite_id.toLowerCase();
const invite_url = `${config.WEBSITE_URL}/accept/${invite_id}`;
const asset = asset_name(symbol);
const source_first_name = source_name ? source_name.split(' ')[0] : undefined;
// send email to target
const { p, pp } = pronouns(source_gender);
const sendResult = await email_lib.send_mail({
to: target_email,
subject: `${source_name || source_email} would like you to spend ${pp} ${asset}...`,
html:
`${source_first_name || source_email} is inviting you to spend ${pp} ${asset} in case one day ${p} can’t.<br/>\
<br/>\
Follow this link to accept ${pp} invitation: <a href='${invite_url}'>${invite_url}</a><br/>\
<br/>\
${source_first_name || source_email} can then complete ${pp} transaction to make you ${pp} beneficiary.`
});
if (!sendResult || sendResult.length == 0 || sendResult[0].statusCode != 202) {
console.log(`## send_invite_link: unexpected sendResult for owner=${owner}`, sendResult);
return res.status(500).send({ msg: "Send failed" });
}
else {
console.log(`$$ send_invite_link: invited ${target_email} with sendResult ${sendResult[0].statusCode} for owner=${owner}`);
}
res.status(201).send({ res: "ok", msg: `${target_email}`,});
}
catch(err) {
console.log(`## send_invite_link`, err);
return res.sendStatus(500);
}
},
get_invite_links: async function (req, res) { // gets all invite-links sent by the specified (& authenticated) owner
try {
if (!req.body) return res.sendStatus(400);
// validation
let owner = req.body.owner;
let e_email = req.body.e_email;
if (!owner || !e_email) return res.sendStatus(400);
if (owner.length==0 || e_email.length==0) return res.sendStatus(400);
if (!(await utils.check_auth(owner, e_email))) {
return res.status(403).send({ msg: "Permission denied" });
}
const invites = await global.scp_sql_pool.request()
.input('owner', sql.NVarChar, `${owner}`)
.query(`SELECT [created_utc], [accepted_utc], [payload_json], [target_email], [target_name], [symbol], [invite_id] \
FROM [_scpx_invite] \
WHERE [owner] = @owner ORDER BY [id] DESC`)
.catch(err => { console.error(`## get_invite_links: SQL failed - ${err.message}`); });
console.log(`$$ get_invite_links: ok for owner=${owner}`);
res.status(200).send({ res: "ok", invites: invites.recordset });
}
catch(err) {
console.log(`## get_invite_links`, err);
return res.sendStatus(500);
}
},
get_invite_link: async function (req, res) { // gets one invite-link by ID, unauthenticated
try {
// validation
let invite_id = req.params.iid
if (!invite_id) return res.sendStatus(400);
if (invite_id.length != 64) return res.status(200).send({ res: "ok", invite: undefined }); // 64 hex chars, 256 bits
const rowset = await by_invite_id(invite_id);
if (!rowset) return res.sendStatus(400);
console.log(`$$ get_invite_link: ok for invite_id=${invite_id}`);
res.status(200).send({ res: "ok", invite: rowset.length > 0 ? rowset[0] : undefined });
}
catch(err) {
console.log(`## get_invite_link`, err);
return res.sendStatus(500);
}
},
accept_invite_link: async function (req, res) { // accepts an invite-link - updates payload field
try {
if (!req.body) return res.sendStatus(400);
// validation
let owner = req.body.owner;
let e_email = req.body.e_email;
let symbol = req.body.symbol;
let invite_id = req.body.invite_id;
let payload_json = req.body.payload_json;
if (!owner || !e_email || !symbol || !invite_id || !payload_json) return res.sendStatus(400);
if (owner.length==0 || e_email.length==0 || symbol.length==0 || invite_id.length==0 || payload_json.length==0) return res.sendStatus(400);
if (symbol != 'BTC_TEST' && symbol != 'BTC_SEG') return res.sendStatus(400);
var payload_obj;
try {
payload_obj = JSON.parse(payload_json);
}
catch(err) {
console.warn(`## accept_invite_link: failed to parse payload_json for owner=${owner}, symbol=${symbol}, invite_id=${invite_id}`, payload_json);
return res.sendStatus(400);
}
if (!(await utils.check_auth(owner, e_email))) {
return res.status(403).send({ msg: "Permission denied" });
}
// get the invite
const rowset = await by_invite_id(invite_id);
console.log('req.body', req.body)
console.log('rowset', rowset)
if (!rowset) return res.sendStatus(400);
if (rowset.length == 0) return res.sendStatus(404);
const invite = rowset[0]
if (invite.symbol != symbol) return res.sendStatus(404);
if (invite.accepted_utc != null) {
console.error(`## accept_invite_link: already accepted for owner=${owner}, symbol=${symbol}, invite_id=${invite_id}`);
return res.sendStatus(409); // conflict
}
// update it
const update = await scp_sql_pool.request()
.input('invite_id', sql.NVarChar, `${invite_id}`)
.input('payload_json', sql.NVarChar, `${payload_json}`)
.query(`UPDATE [_scpx_invite]\
SET [accepted_utc]=GETUTCDATE(), [payload_json]=@payload_json\
WHERE [invite_id] = @invite_id\
`)
.catch(err => { console.error(`## accept_invite_link - UPDATE failed! ${err.message}`, err); });
if (update.rowsAffected != 1) {
console.error(`## accept_invite_link - unexpected update count for owner=${owner}, symbol=${symbol}, invite_id=${invite_id}`);
return res.sendStatus(400);
}
// send email to source: target has accepted the invite
const { p, pp } = pronouns(invite.source_gender);
const asset = asset_name(symbol);
const wallet_url = `${config.WEBSITE_URL}/unlock-wallet`;
const sendResult = await email_lib.send_mail({
to: invite.source_email,
subject: `${invite.target_name || invite.target_email} has accepted your invite`,
html:
`<b>${invite.target_name || invite.target_email}</b> has accepted your invite to be your ${asset} beneficiary.<br/>\
<br/>\
Open your <a href='${wallet_url}'>wallet</a> to complete the transaction.`
});
if (!sendResult || sendResult.length == 0 || sendResult[0].statusCode != 202) {
console.log(`## accept_invite_link: unexpected sendResult for owner=${owner}`, sendResult);
//return res.status(500).send({ msg: "Send failed" }); // keep going: report success, this isn't critical path
}
console.log(`$$ accept_invite_link: ok for owner=${owner}, symbol=${symbol}, invite_id=${invite_id}`, payload_json);
res.status(200).send({ res: "ok", });
}
catch(err) {
console.log(`## accept_invite_link`, err);
return res.sendStatus(500);
}
},
}
// const send_mail = async(p) => {
// const msg = {
// to: p.to,
// from: config.get('ref_mail_from'),
// subject: p.subject,
// html:
// `<div style='font-size:larger'>${p.html}\
// <br/>\
// <br/>\
// <b>${config.WEBSITE_DOMAIN}</b><br/>\
// <div style='font-style:italic;'>Trustless crypto social recovery: let someone else spend it when you can't.</div>
// An open <a href='${config.GITHUB_URL}'>source</a> project.\
// </div>`
// };
// const apiKey = config.get('sendgrid_apikey');
// sendgrid.setApiKey(apiKey);
// var sendResult = await sendgrid.send(msg);
// return sendResult;
// }
const exists = async (owner, target_email, symbol) => {
const result = await global.scp_sql_pool.request()
.input('owner', sql.NVarChar, `${owner}`)
.input('symbol', sql.NVarChar, `${symbol}`)
.input('target_email', sql.NVarChar, `${target_email}`)
.query(`SELECT TOP 1 [id] FROM [_scpx_invite] WHERE \
[owner] = @owner AND [symbol] = @symbol AND [target_email] = @target_email\
`)
.catch(err => { console.error(`## scp_invite.exists: SQL failed - ${err.message}`); });
return result && result.recordset && result.recordset.length > 0;
}
const by_id = async (id) => {
const result = await global.scp_sql_pool.request()
.input('id', sql.Int, `${id}`)
.query(`SELECT * FROM [_scpx_invite] WHERE [id] = @id`)
.catch(err => { console.error(`## scp_invite.by_id: SQL failed - ${err.message}`); });
return result.recordset;
}
const by_invite_id = async (invite_id) => {
const result = await global.scp_sql_pool.request()
.input('invite_id', sql.NVarChar, `${invite_id}`)
.query(`SELECT * FROM [_scpx_invite] WHERE [invite_id] = @invite_id`)
.catch(err => { console.error(`## scp_invite.by_invite_id: SQL failed - ${err.message}`); });
return result.recordset;
}
const pronouns = (source_gender) => {
const p = source_gender == "male" ? "he" // pronoun
: source_gender == "female" ? "she"
: "they";
const pp = source_gender == "male" ? "his" // possessive
: source_gender == "female" ? "her"
: "their";
return { p, pp }
}
const asset_name = (symbol) => {
return symbol == 'BTC_TEST' ? 'Test Bitcoin' :
symbol == 'BTC_SEG' ? 'Bitcoin'
: '?';
}