-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
155 lines (136 loc) · 4.24 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
require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const cors = require('cors')
const Twitter = require('twitter')
const Cache = require('./cache')
const {checkIfVerifiedAr, persistVerificationAr, signDocumentAr, forkDocumentAr} = require("./arweave")
app.use(bodyParser.urlencoded({extended: true}))
app.use(cors())
const port = process.env.PORT || 8080
const TWEET_TEMPLATE = "I am verifying for @verses_xyz: sig:"
const client = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
bearer_token: process.env.BEARER_TOKEN
})
const sigCache = new Cache()
app.get('/', (req, res) => {
res.send('ok')
})
app.post('/fork/:document', (req, res) => {
const documentId = req.params.document // if undefined, create new document
const {
authors,
text,
title,
} = req.body
const totalSize = [authors, text, title]
.map(arg => arg || "")
.map(txt => Buffer.from(txt).byteLength)
.reduce((size, total) => size + total, 0)
if (totalSize >= (2 << 22)) {
res.status(400).json({ status: "too large"})
return
}
forkDocumentAr(documentId, text, title, authors)
.then((data) => res.json(data))
.catch(e => {
console.log(`err @ /fork/:document : ${e}`)
res.status(500)
})
})
// post: include name, address (from MM), handle
app.post('/sign/:document', (req, res) => {
const documentId = req.params.document
const {
name,
address,
handle,
signature,
} = req.body
// did the user include a handle?
if (handle) {
// check if user is verified
const promise = sigCache.has(handle) ?
signDocumentAr(documentId, address, name, handle, signature, true) :
checkIfVerifiedAr(handle, signature).then(result => {
const verified = !!result
return signDocumentAr(documentId, address, name, handle, signature, verified)
})
promise
.then((data) => {
console.log(`new signee: ${name}, @${handle}, ${address}`)
res.json(data)
})
.catch(e => {
console.log(`err @ /sign/:document : ${e}`)
res.status(500)
});
} else {
// pure metamask sig
signDocumentAr(documentId, address, name, '', signature, false)
.then((data) => res.json(data))
.catch(e => {
console.log(`err @ /sign/:document : ${e}`)
res.status(500)
})
}
})
// post: include address (from MM)
app.post('/verify/:handle', (req, res) => {
const handle = req.params.handle
const {
address: signature,
} = req.body
if (sigCache.has(handle)) {
console.log(`already verified user: @${handle}`)
const txId = sigCache.get(handle)
res.json({ tx: txId })
return
}
client.get('statuses/user_timeline', {
screen_name: handle,
include_rts: false,
count: 5,
tweet_mode: 'extended',
}, (error, tweets, response) => {
if (!error) {
for (const tweet of tweets) {
const parsedSignature = tweet.full_text.slice(TWEET_TEMPLATE.length).split(" ")[0];
if (tweet.full_text.startsWith(TWEET_TEMPLATE) && (parsedSignature === signature)) {
// check to see if already linked
checkIfVerifiedAr(handle, signature)
.then(result => {
if (result) {
// already linked
console.log(`already verified user: @${handle}`)
sigCache.set(handle, result)
res.json({ tx: result })
} else {
// need to link
persistVerificationAr(handle, signature)
.then((tx) => {
console.log(`new verified user: @${handle}, ${signature}`)
sigCache.set(handle, tx)
res.status(201).json(tx)
})
.catch(e => {
console.log(`err @ /verify/:handle : ${e}`)
res.status(500).send(JSON.stringify(e))
});
}
});
return
}
}
res.status(500).json({message: 'No matching Tweets found'})
} else {
res.status(500).send({message: 'Internal Error'})
}
})
})
app.listen(port, () => {
console.log(`server started on port ${port}`)
})