-
Notifications
You must be signed in to change notification settings - Fork 1
/
cyber.js
58 lines (51 loc) · 1.73 KB
/
cyber.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
const conf = require('./config.json');
const axios = require('axios');
const builder = require('./node_modules/auto_cyb/builder');
const codec = require('./node_modules/auto_cyb/codec');
const crypto = require('./node_modules/auto_cyb/crypto');
const constants = require('./node_modules/auto_cyb/constants');
const import_data = crypto.recover(conf.seed, 'en');
const sender = {
'address': import_data.address,
'privateKey': import_data.privateKey,
'node': conf.cyber_node,
'chain': "euler-6"
}
async function link(from, to) {
const addressInfo = await axios({
method: 'get',
url: `${sender.node}/account?address="${sender.address}"`,
});
if(!addressInfo.data.result) { return console.error('error: addressInfo.data.result undefined') };
const account = addressInfo.data.result.account;
if(!account) { return console.error('error: addressInfo.data.result.account undefined') }
const acc = {
address: account.address,
chain_id: sender.chain,
account_number: parseInt(account.account_number, 10),
sequence: parseInt(account.sequence, 10)
};
const sendRequest = {
acc,
fromCid: from,
toCid: to,
type: 'link'
};
const txRequest = builder.buildAndSignTxRequest(sendRequest, sender.privateKey, sender.chain);
const signedSendHex = codec.hex.stringToHex(JSON.stringify(txRequest));
return axios({
method: 'get',
url: `${sender.node}/submit_signed_link?data="${signedSendHex}"`,
})
.then(res => {
if (!res.data) {
throw new Error('Empty data');
}
if (res.data.error) {
throw res.data.error;
}
return res.data;
})
.catch(error => console.log('Cannot send', error));
}
module.exports.link = link;