-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (140 loc) · 3.94 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
156
157
158
159
160
161
162
163
164
165
166
167
import express from 'express';
import bodyParser from 'body-parser'
import cors from 'cors'
import morgan from 'morgan'
import nacl from 'tweetnacl'
import Web3 from 'web3'
import bitcoinMessage from 'bitcoinjs-message'
import { parseLWT, parseTemplate } from './js/src/lwt.js'
// TODO: https://github.com/bitcoinjs/bitcoinjs-message
// TODO: https://github.com/trezor/connect/blob/develop/docs/methods/server/server.js
const app = express()
const web3 = new Web3()
app.disable('x-powered-by')
app.use(morgan('combined'))
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(bodyParser.json())
app.use(cors())
app.get('/', (req, res) => res.send('0K'))
app.get('/robots.txt', (req, res) => {
res.header('Content-Type', 'text/plain')
res.send(`User-agent: *
Disallow: /`)
})
app.post('/v1/verify', (req, res) => {
// TODO: fetch from db
const { domain } = req.body
let ticket
try {
ticket = parseLWT(req.body.ticket)
console.log(`ticket: `, ticket)
console.log(`body: `, req.body)
const tpl = parseTemplate(ticket.ct, ticket.msg)
if(tpl.domain != domain) {
console.log(`domain mismatch: ${tpl.domain} != ${domain}`)
console.log(`template:`, tpl)
throw new Error('DOMAIN_MISMATCH')
}
} catch (err) {
console.log(`error: `, err)
return res.status(401).json({
valid: false,
error: err.code || err.message
})
}
switch(ticket.ct) {
case 0: { // Bitcoin
if(!bitcoinMessage.verify(ticket.msg, ticket.a, ticket.sig)) {
return res.status(401).json({
valid: false,
error: 'SIGNATURE_VERIFICATION_FAILED'
})
}
break;
}
case 60: { // Ethereum
const data = '0x' + bytesToHex(new TextEncoder().encode(ticket.msg))
const a = web3.eth.accounts.recover(data, ticket.sig)
// TODO: verify
if(a.toLowerCase() != ticket.pub.toLowerCase()) {
return res.status(401).json({
valid: false,
error: 'SIGNATURE_VERIFICATION_FAILED'
})
}
break;
}
case 501: { // Solana
console.log(`VVVV: msg: `, new TextEncoder().encode(ticket.msg))
console.log(`VVVV: sig hex: `, ticket.sig)
console.log(`VVVV: sig: `, hexToBytes(ticket.sig))
console.log(`VVVV: pub hex: `, ticket.pub)
console.log(`VVVV: pub: `, hexToBytes(ticket.pub))
// this sometimes FAILS WTF??
const valid = nacl.sign.detached.verify(
new TextEncoder().encode(ticket.msg),
hexToBytes(ticket.sig),
hexToBytes(ticket.pub)
)
if(!valid) {
return res.status(401).json({
valid: false,
error: 'SIGNATURE_VERIFICATION_FAILED'
})
}
break;
}
default:
throw new Error(`unsupported coin type: ${ticket.ct}`)
}
return res.json({
valid: true,
network: ctToNetwork(ticket.ct),
account: ticket.a,
display_name: abbreviate(ticket.a),
expires_at: ticket.exp,
__parsed_lwt: ticket
})
})
const port = process.env.PORT || 1235
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
console.log(new TextDecoder().decode(hexToBytes("68656c6c6f20e6bca2e5ad9720f09f918d")))
function ctToNetwork(ct) {
switch(ct) {
case 60:
return "ethereum";
case 501:
return "solana";
default:
return "<unknown>";
}
}
function bytesToHex(bytes) {
return Array.from(
bytes,
byte => byte.toString(16).padStart(2, "0")
).join("");
}
function hexToBytes(hex) {
const bytes = new Uint8Array(hex.length / 2);
for(let i = 0; i !== bytes.length; i++) {
bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
}
return bytes;
}
function abbreviate(addr, max = 20) {
if(addr.length > max) {
if(addr.startsWith("0x")) {
addr = addr.substr(2);
addr = addr.substr(0, 4) + "..." + addr.substr(-4);
addr = "0x" + addr;
} else {
addr = addr.substr(0, 4) + "..." + addr.substr(-4);
}
}
return addr;
}