-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
85 lines (73 loc) · 2.4 KB
/
index.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
const { Mnemonic } = require('@liskhq/lisk-passphrase');
const liskCryptography = require('@liskhq/lisk-cryptography');
const liskTransactions = require('@liskhq/lisk-transactions');
const axios = require('axios');
const DEFAULT_API_MAX_PAGE_SIZE = 100;
const DEFAULT_API_TIMEOUT = 10000;
class LiskAdapter {
constructor(options) {
this.apiURL = options.apiURL;
this.apiMaxPageSize = options.apiMaxPageSize || DEFAULT_API_MAX_PAGE_SIZE;
this.apiTimeout = options.apiTimeout || DEFAULT_API_TIMEOUT;
}
async connect({ passphrase }) {
this.passphrase = passphrase;
}
async disconnect() {}
createTransfer({ amount, recipientAddress, message }) {
return liskTransactions.transfer({
amount,
recipientId: recipientAddress,
data: message,
passphrase: this.passphrase
});
}
createWallet() {
let passphrase = Mnemonic.generateMnemonic();
let address = this.getAddressFromPassphrase({ passphrase });
return {
address,
passphrase
};
}
validatePassphrase({ passphrase }) {
return Mnemonic.validateMnemonic(passphrase, Mnemonic.wordlists.english);
}
getAddressFromPassphrase({ passphrase }) {
return liskCryptography.getAddressAndPublicKeyFromPassphrase(passphrase).address;
}
async postTransaction({ transaction }) {
await axios.post(`${this.apiURL}/transactions`, transaction);
}
async getLatestOutboundTransactions({ address, limit }) {
let client = axios.create();
client.defaults.timeout = this.apiTimeout;
let result = await client.get(
`${this.apiURL}/transactions?senderId=${
address
}&limit=${
limit || this.apiMaxPageSize
}&sort=timestamp:desc`
);
let txnList = result.data.data;
for (let txn of txnList) {
txn.message = (txn.asset && txn.asset.data) || '';
}
return txnList;
}
async getAccountBalance({ address }) {
let client = axios.create();
client.defaults.timeout = this.apiTimeout;
let response = await client.get(`${this.apiURL}/accounts?address=${address}`);
let balanceList = Array.isArray(response.data) ? response.data : response.data.data;
if (!balanceList.length) {
throw new Error(
`Failed to fetch account balance for wallet address ${
address
} - Could not find any balance records for that account`
);
}
return balanceList[0].balance;
}
}
module.exports = LiskAdapter;