This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
common.js
97 lines (84 loc) · 2.84 KB
/
common.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
const Irisnet = require('../index');
const chai = require('chai');
const assert = chai.assert;
const fetch = require('isomorphic-fetch');
const config = {
iris:{
host : "http://irisnet-lcd.dev.rainbow.one",
chainId:"",
},
cosmos:{
}
};
function randomWord(range) {
let str = "",
arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
for (let i = 0; i < range; i++) {
let pos = Math.round(Math.random() * (arr.length - 1));
str += arr[pos];
}
return str;
}
function randomHex(range){
let str = "",
arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
for (let i = 0; i < range; i++) {
let pos = Math.round(Math.random() * (arr.length - 1));
str += arr[pos];
}
return str.toUpperCase();
}
async function verifyTx(url, tx, privateKey, chainName,callback) {
let builder = Irisnet.getBuilder(chainName,"testnet");
let stdTx = builder.buildAndSignTx(tx, privateKey);
let exp = stdTx.Hash();
let payload = stdTx.GetData();
let response = await sendByAsync("POST",url,payload);
callback(response,exp,payload);
}
async function verifyAccount(url, account){
let payload = {
password: "1234567890",
seed: account.phrase
};
let response = await sendByAsync("POST",url,payload);
assert.strictEqual(response.address,account.address);
assert.strictEqual(response.pub_key,account.publicKey);
}
function sendBySync(method,url,payload) {
let req = require('sync-request');
let res = req(method, url, {
json: payload,
});
return JSON.parse(res.getBody('utf8'))
}
function getSequence(host,address) {
let url = `${host}/auth/accounts/${address}`;
let res = sendBySync("GET", url);
let account = res.result.value;
return account.sequence
}
function sendByAsync(method,url,data){
let payload = {
method: method,
body: JSON.stringify(data)
};
return fetch(url, payload).then(response => {
if (!response.ok) {
return Promise.reject(response.text().then(msg => {
return msg;
}))
}
let contentType = response.headers.get('content-type');
let result = '';
if (contentType.includes('text/plain')) {
result = response.text();
} else if (contentType.includes('application/json')) {
result = response.json();
}
return result;
}).catch(err => {
return Promise.reject(err)
})
}
module.exports = {randomWord,randomHex,verifyTx,verifyAccount,sendBySync,getSequence};