-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
120 lines (99 loc) · 3.41 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
const express = require('express');
require('express-async-errors');
const cors = require('cors');
const { Pool } = require('pg');
const axios = require('axios');
const app = express();
const port = 3001;
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.get('/', async (req, res) => {
res.status(200).send('Hello World');
});
app.get('/accounts', async function (req, res) {
const address = req.query['address'];
const connection = [
'postgres://public_readonly:[email protected]/mainnet_explorer',
'postgres://public_readonly:[email protected]/testnet_explorer',
];
const addPrefix = (rows, net) => {
if(rows.length){
rows.map((row)=>{
if(hasAccountId(row['account_id'])){
return row['account_id'];
}else{
return row['account_id'] = net + ':' + row['account_id'];
}
});
}
};
const addPrefixForKit = (rows, net) => {
const newRows = [];
if(rows.length){
rows.map((row)=>{
if(hasAccountId(row)){
newRows.push(row);
}else{
newRows.push(net + ':' + row);
}
});
}
return newRows;
};
const hasAccountId = (address) => {
return address.split('.').pop() == 'near' || address.split('.').pop() == 'testnet';
};
try {
const accountsIds = await Promise.all(connection.map(async (connectionString, idx) => {
const { rows } = await new Pool({
connectionString,
}).query(`
SELECT DISTINCT account_id
FROM access_keys
JOIN accounts USING (account_id)
WHERE public_key = $1
AND accounts.deleted_by_receipt_id IS NULL
AND access_keys.deleted_by_receipt_id IS NULL
`, [address]);
if(idx){
addPrefix(rows, 'testnet');
}else{
addPrefix(rows, 'mainnet');
}
return rows;
}));
const result = accountsIds.reduce((acc, cur) => acc.concat(cur)).map((account) => account['account_id']);
res.send(result);
} catch (e) {
try {
const kitUrl = [
'https://api.kitwallet.app/publicKey/'+address+'/accounts',
'https://testnet-api.kitwallet.app/publicKey/'+address+'/accounts'
];
const accountsIds = await Promise.all(kitUrl.map(async (kitString, idx) => {
const rows = (await axios.get(kitString)).data;
if(idx){
return addPrefixForKit(rows, 'testnet');
}else{
return addPrefixForKit(rows, 'mainnet');
}
}));
const result = accountsIds.reduce((acc, cur) => acc.concat(cur));
res.status(500);
res.send(result);
} catch (e) {
console.log(e);
res.status(500);
res.send([]);
}
}
});
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
res.status(500);
res.send([]);
});
app.listen(port, () => {
console.log(`App running on port ${port}.`);
});