forked from matrix-org/synapse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·137 lines (118 loc) · 3.55 KB
/
Copy pathindex.js
File metadata and controls
executable file
·137 lines (118 loc) · 3.55 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
#!/usr/bin/env node
const Web3 = require("web3");
const Web3Token = require("web3-token/dist/web3-token.common");
global.fetch = require("node-fetch");
const { ERC725 } = require("@erc725/erc725.js");
const erc725schema = require("@erc725/erc725.js/schemas/LSP3UniversalProfileMetadata.json");
const permissionsSchema = require("@erc725/erc725.js/schemas/LSP12IssuedAssets.json");
// Network and storage
const RPC_ENDPOINT = "https://rpc.l16.lukso.network";
const IPFS_GATEWAY = "https://2eff.lukso.dev/ipfs/";
// Parameters for the ERC725 instance
const provider = new Web3.providers.HttpProvider(RPC_ENDPOINT);
const config = { ipfsGateway: IPFS_GATEWAY };
const web3 = new Web3(provider);
const getData = async (address, keys, web3) => {
const Contract = new web3.eth.Contract(
[
{
stateMutability: "view",
type: "function",
inputs: [
{
internalType: "bytes32[]",
name: "_keys",
type: "bytes32[]",
},
],
name: "getData",
outputs: [
{
internalType: "bytes[]",
name: "values",
type: "bytes[]",
},
],
},
],
address
);
let data = [];
try {
data = await Contract.methods.getData(keys).call();
} catch (err) {
console.log(err.message);
}
return data;
};
async function readProfile(address, item) {
try {
const result = await getData(address, [item.key], web3);
const schema = [item];
const erc725 = new ERC725(schema, address, web3.currentProvider);
const decodedData = erc725.decodeData([
{
keyName: item.name,
result,
},
]);
if (item.keyType === "Array") {
const result = await erc725.getData(item.name);
return result.value;
}
throw new Error("No address list found");
} catch (err) {
console.error("error", err);
process.exit(1);
}
}
const zmq = require("zeromq");
function hexDecode(str) {
str = str.replace(/^0x/, "");
return Buffer.from(str, "hex");
}
async function run() {
const sock = new zmq.Reply();
await sock.bind("tcp://127.0.0.1:5555");
for await (const [msg] of sock) {
try {
const data = JSON.parse(msg.toString());
console.log("Data", data);
const { address: tokenAddress, body } = await Web3Token.verify(
data.token
);
const [_ignpore, address, machine] = /@([^:]*):(.*)$/.exec(data.address);
const local = hexDecode(tokenAddress);
if (local.compare(hexDecode(address)) === 0) {
data.success = true;
data.body = body;
console.log("Success Address & Body", address, machine, body);
// Disallow metamask logins.
await sock.send(Buffer.from(JSON.stringify({ success: false })));
continue;
}
console.log("UP Address & Body", address, machine, body);
const value = await readProfile(address, {
name: "AddressPermissions[]",
key: "0xdf30dba06db6a30e65354d9a64c609861f089545ca58c6b4dbe31a5f338cb0e3",
keyType: "Array",
valueType: "address",
valueContent: "Address",
});
const success = value
.map((item) => hexDecode(item))
.some((buf) => buf.compare(local) === 0);
console.log("success", success);
await sock.send(Buffer.from(JSON.stringify({ success })));
} catch (err) {
console.log("Error", err);
await sock.send(Buffer.from(JSON.stringify(err)));
}
}
}
run()
.then(() => process.exit(0))
.catch((err) => {
console.error("Error", err);
process.exit(1);
});