-
Notifications
You must be signed in to change notification settings - Fork 10
/
authority.js
123 lines (103 loc) · 4.52 KB
/
authority.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
/*
Import this module in server app using:
const auth = require("./authority.js");
Call function auth.authorize(txId, fromChainId),
where:
`txId` - transaction hash,
`fromChainId` - chain ID where transaction was sent.
returns JSON string,
where:
if all good:
"isSuccess" - true,
"signature" - authority signature,
"token" - token to receive,
"value" - tokens amount,
"to"- receiver (user's) address,
"chainId" - chain ID where to claim token,
"bridge" - address of bridge on destination network.
in case of error:
"isSuccess" - false,
"message" - error description
Example:
auth.authorize(txId, fromChainId)
.then(resp => {
response.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
response.end(JSON.stringify(resp));
})
.catch(err => {
response.writeHead(404, {'Content-Type': 'text/html'});
response.end(err.toString());
})
In this example of `.env` the authority address is 0x3d40De3046a7D7E2Aa9E8097A86e49c699A0170B
*/
const Web3 = require('web3');
require('dotenv').config();
const pk = process.env.AUTHORITY_PK; // Private key should be hidden
const bridgeContracts = {
"20729" : "0x6664fD73ed95CF608B5e461A6cE89212F989EdCA", // CLO test net
"820" : "", // CLO main net
"97" : "0x05ef4B789C75c02d5182e6EfB7c64230Ec9B58b2", // BSC test net
"56" : "", // BSC main net
"42" : "", // ETH KOVAN test net
"1" : "", // ETH main netcls
};
const providers = {
"20729" : "https://testnet-rpc.callisto.network", // CLO test net
"820" : "https://clo-geth.0xinfra.com/", // CLO main net
"97" : "https://data-seed-prebsc-1-s1.binance.org:8545/", // BSC test net
"56" : "https://bsc-dataseed.binance.org/", // BSC main net
"42" : "https://kovan.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161", // ETH KOVAN test net
"1" : "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161", // ETH main net
}
const deposit_event_abi = [
{"type":"address","name":"token","internalType":"address","indexed":true},
{"type":"address","name":"sender","internalType":"address","indexed":true},
{"type":"uint256","name":"value","internalType":"uint256","indexed":false},
{"type":"uint256","name":"toChainId","internalType":"uint256","indexed":false},
{"type":"address","name":"toToken","internalType":"address","indexed":false}
];
// call this function to get authorization signature
// params: txId = deposit transaction hash, fromChainId = chain ID where transaction was sent.
// returns: on success {isSuccess: true, message: sig.signature};
// on error: {isSuccess: false, message: error_message};
async function authorize(txId, fromChainId) {
var provider = providers[fromChainId];
if (!provider) {
let msg = "No provider for chain ID:" + fromChainId;
//console.log(msg);
return {isSuccess: false, message: msg};
}
var web3 = new Web3(provider);
return web3.eth.getTransactionReceipt(txId)
.then(receipt => {
if (receipt && receipt.status) {
let element;
for (var i = 0; i < receipt.logs.length; i++) {
if (receipt.logs[i].topics[0] == "0xf5dd9317b9e63ac316ce44acc85f670b54b339cfa3e9076e1dd55065b922314b") {
element = receipt.logs[i];
element.topics.shift(); // remove
}
}
bridgeContract = bridgeContracts[fromChainId];
if (bridgeContract && element.address == bridgeContract && element.transactionHash == txId) {
let p = web3.eth.abi.decodeLog(deposit_event_abi, element.data, element.topics);
//console.log(p);
let messageHash = web3.utils.soliditySha3(p.toToken, p.sender, p.value, txId, fromChainId, p.toChainId);
//console.log(messageHash);
sig = web3.eth.accounts.sign(messageHash, pk);
//console.log(sig);
let ret = {isSuccess: true, signature: sig.signature, token: p.toToken, value: p.value, to: p.sender, chainId: p.toChainId, bridge: bridgeContracts[p.toChainId]};
//console.log(ret);
return JSON.stringify(ret)
}
}
let msg = "Wrong transaction hash:" + txId;
console.log(msg);
return JSON.stringify({isSuccess: false, message: msg});
})
.catch(err => {
console.log(err);
return JSON.stringify({isSuccess: false, message: err.toString()});
})
}
module.exports.authorize = authorize;