-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
180 lines (137 loc) · 4.11 KB
/
app.js
File metadata and controls
180 lines (137 loc) · 4.11 KB
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
const express = require('express');
const app = express();
const fa = require("@glif/filecoin-address");
var _ethers = require("ethers");
var ODudeName = require("@odude/oduderesolve");
require('dotenv').config() //Remove this line if no environment variable is used
//nodemon app.js
const settings = {
matic_rpc_url: process.env.MATIC_RPC,
eth_rpc_url: process.env.ETH_RPC,
fvm_rpc_url: process.env.FVM_RPC,
wallet_pvt_key: process.env.PVT_KEY
};
console.log(settings);
const resolve = new ODudeName(settings);
var intro = "This is live API script which you can host on your node webserver application to get wallet address from the Web3 Domain Name.<hr> Eg. <code>http://....../api/?name=brad.eth&currency=ETH</code> | <code>type=uri</code>";
app.get('/', (req, res) => {
res.send(intro);
});
//Change port number as to your server
app.listen(3000, () => {
console.log('Server is up on port 3000');
});
app.get("/api", (req, res) => {
let query = req.query
console.log(query);
if ((typeof query.address !== 'undefined')) {
//Search for ETH address
if (!_ethers.utils.isAddress(query.address)) {
let fil = fa.validateAddressString(query.address); //Only check t4 address
if (fil) {
//This is FIL address and convert it ot ETH
const convert_t4 = fa.ethAddressFromDelegated(query.address).toString();
addr_to_domain(convert_t4, res)
}
else {
res.json({ error: 'Invalid address', code: 400 })
}
}
else {
//ETH address search
addr_to_domain(query.address, res)
}
}
else if ((typeof query.name !== 'undefined') && (typeof query.type !== 'undefined')) {
if(query.type == 'uri')
{
domain_to_uri(query.name, res)
}
else
{
domain_to_ipfs(query.name, res);
}
}
else {
if ((typeof query.name !== 'undefined')) {
if ((typeof query.currency === 'undefined')) {
domain_to_addr(query.name, 'ETH', res);
}
else {
domain_to_addr(query.name, query.currency, res);
}
}
else {
res.json({ error: 'Must define [name] & [currency] parameters' })
}
}
})
//Domain to Address
function domain_to_addr(name, currency, res) {
resolve.getAddress(name, currency).then(x => {
console.log(x);
if (x == null) {
res.json({ address: x, code: 404 })
}
else {
res.json({ address: x, code: 200 })
}
}).catch(console.error);
}
//Address to Domain
function addr_to_domain(address, res) {
// const convert_t4 = fa.delegatedFromEthAddress(address).toString();
const convert_f4 = fa.newDelegatedEthAddress(address).toString();
console.log(convert_f4);
//convert to f2
// const convert_ac = fa.newActorAddress(address).toString();
// console.log(convert_ac);
//const convert_bb = fa.newSecp256k1Address(convert_f4).toString();
//console.log(convert_bb);
//const convert_cc = fa.newBLSAddress(address).toString();
//console.log(convert_cc);
resolve.getDomain(address, "W3D").then(x => {
//EVM address to Web3Domain Name
if (x == null || x == '') {
addr_to_domain_ens(address, res);
}
else {
res.json({ domain: x, code: 200, fvm: convert_f4, eth: address })
}
}).catch(console.error);
}
//Address to domain for ENS
function addr_to_domain_ens(address, res) {
const convert_t4 = fa.delegatedFromEthAddress(address).toString();
resolve.getDomain(address, "ENS").then(x => {
//ENS address to ETH Domain
if (x == null) {
res.json({ domain: x, code: 404 })
}
else {
res.json({ domain: x, code: 200, fvm: convert_t4, eth: address })
}
}).catch(console.error);
}
//Find IPFS from domain
function domain_to_ipfs(name, res) {
resolve.getWeb(name).then(x => {
if (x == null) {
res.json({ ipfs: x, code: 404 })
}
else {
res.json({ ipfs: x, code: 200 })
}
}).catch(console.error);
}
//Find URI from domain
function domain_to_uri(name, res) {
resolve.w3d_tokenURI(name).then(x => {
if (x == null) {
res.json({ tokenURI: x, code: 404 })
}
else {
res.json({ tokenURI: x, code: 200 })
}
}).catch(console.error);
}