-
Notifications
You must be signed in to change notification settings - Fork 1
/
zilliqa.js
210 lines (174 loc) · 5.13 KB
/
zilliqa.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { Zilliqa } from '@zilliqa-js/zilliqa'
import { RPCMethod } from '@zilliqa-js/core'
import { Long, BN, bytes, validation } from '@zilliqa-js/util'
import { BrowserStorage, BuildObject } from '../../../lib/storage'
import fields from '../../../config/fields'
import errorsCode from './errors'
export class ZilliqaControl extends Zilliqa {
constructor(provider) {
super(provider);
}
async getBalance(address) {
// Get the balance by address. //
let { result } = await this.blockchain.getBalance(address);
let nonce = 0;
if (!result) {
result = 0;
} else {
nonce = result ? result.nonce : 0;
result = result.balance;
}
return { result, nonce };
}
async singTransaction(txData, seedOrPrivateKey, index, currentNonce, msgId) {
/**
* @param {txData}: Object with data about transaction.
* @param seedOrPrivateKey: type String, seed phrase or private key.
* @param index: type Number, the index of address from seed phrase.
* @prarm msgId: Message version network.
*/
// importing account from private key or seed phrase. //
if (validation.isPrivateKey(seedOrPrivateKey)) {
this.wallet.addByPrivateKey(seedOrPrivateKey);
} else {
this.wallet.addByMnemonic(seedOrPrivateKey, index);
}
const pubKey = this.wallet.defaultAccount.publicKey;
const balance = await this.getBalance(
this.wallet.defaultAccount.address
);
let {
amount, // Amount of zil. type Number.
code, // Value contract code. type String.
data, // Call contract transition. type Object.
gasLimit,
gasPrice,
toAddr, // Recipient address. type String.
nonce,
version // Netowrk version. type Number.
} = txData;
if (!version) {
version = await this.version(msgId);
}
if (isNaN(nonce)) {
nonce = balance.nonce;
}
if (currentNonce > balance.nonce) {
nonce = currentNonce;
}
amount = new BN(amount);
gasPrice = new BN(gasPrice);
gasLimit = Long.fromNumber(gasLimit);
nonce++;
const zilTxData = this.transactions.new({
nonce,
gasPrice,
amount,
gasLimit,
version,
toAddr,
pubKey,
code,
data
});
// Sign transaction by current account. //
const { txParams } = await this.wallet.sign(zilTxData);
return await this.provider.send( // Send to shard node.
RPCMethod.CreateTransaction, txParams
);
}
async version(msgVerison=1) {
const { result } = await this.network.GetNetworkId();
return bytes.pack(result, msgVerison);
}
async getAccountBySeed(seed, index) {
if (typeof seed !== 'string' || isNaN(index)) {
throw new Error(errorsCode.WrongParams);
}
this.wallet.addByMnemonic(seed, index);
const {
address,
publicKey,
privateKey
} = this.wallet.defaultAccount;
const { result } = await this.getBalance(address);
return {
index, publicKey, address, privateKey,
balance: result
};
}
async getAccountByPrivateKey(importPrivateKey, index=0) {
this.wallet.addByPrivateKey(importPrivateKey);
const account = this.wallet.defaultAccount;
const { result } = await this.getBalance(account.address);
return Object.assign(account, { index, balance: result });
}
async addForSingTransaction(payload) {
[
'amount',
'toAddr'
].forEach(key => {
if (!payload.hasOwnProperty(key)) {
throw new Error(
errorsCode.WrongRequiredparam + key
);
}
});
const storage = new BrowserStorage();
let forConfirm = await storage.get(fields.CONFIRM_TX);
try {
forConfirm = forConfirm[fields.CONFIRM_TX];
forConfirm.push(payload);
} catch(err) {
forConfirm = [payload];
}
await storage.set(new BuildObject(fields.CONFIRM_TX, forConfirm));
}
async rmForSingTransaction() {
const storage = new BrowserStorage();
let forConfirm = await storage.get(fields.CONFIRM_TX);
forConfirm = forConfirm[fields.CONFIRM_TX];
const removedConfirm = forConfirm.pop();
await storage.set(new BuildObject(fields.CONFIRM_TX, forConfirm));
return removedConfirm;
}
async addTransactionList(tx, net) {
const storage = new BrowserStorage();
const { from } = tx;
let txsList = await storage.get(fields.TRANSACTIONS);
const data = {
Info: tx.Info,
TranID: tx.TranID,
amount: tx.amount,
toAddr: tx.toAddr,
nonce: tx.nonce
};
if (!net) {
throw new Error(
errorsCode.WrongRequiredparam + 'net'
);
}
Object.keys(data).forEach(key => {
if (!data[key]) {
throw new Error(
errorsCode.WrongRequiredparam + key
);
}
});
try {
txsList = txsList[fields.TRANSACTIONS];
txsList[from][net].push(data);
} catch(err) {
txsList = {};
txsList[from] = {};
txsList[from][net] = [];
txsList[from][net].push(data);
}
if (txsList[from][net].length > 5) {
txsList[from][net].shift();
}
await storage.set(
new BuildObject(fields.TRANSACTIONS, txsList)
);
}
}