forked from randlabs/walgo-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
273 lines (238 loc) · 6.86 KB
/
tools.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* eslint-disable no-plusplus */
/*************************************************************************
* [2018] - [2020] Rand Labs Inc.
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Rand Labs Inc.
* The intellectual and technical concepts contained
* herein are proprietary to Rand Labs Inc.
*/
const sha512 = require("js-sha512");
const hibase32 = require("hi-base32");
const ALGORAND_ADDRESS_SIZE = 58;
function timeoutPromise(ms, promise) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error("promise timeout"));
}, ms);
promise.then(
(res) => {
clearTimeout(timeoutId);
resolve(res);
},
(err) => {
clearTimeout(timeoutId);
reject(err);
}
);
});
}
function getInt64Bytes(x, len) {
if (!len) {
len = 8;
}
let bytes = new Uint8Array(len);
do {
// eslint-disable-next-line no-bitwise
bytes[--len] = x & (255);
// eslint-disable-next-line no-bitwise
x >>= 8;
} while (len);
return bytes;
}
function addressFromByteBuffer(addr) {
const bytes = Buffer.from(addr, "base64");
//compute checksum
const checksum = sha512.sha512_256.array(bytes).slice(28, 32);
const c = new Uint8Array(bytes.length + checksum.length);
c.set(bytes);
c.set(checksum, bytes.length);
const v = hibase32.encode(c);
return v.toString().slice(0, ALGORAND_ADDRESS_SIZE);
}
function printAppCallDeltaArray(deltaArray) {
for (let i = 0; i < deltaArray.length; i++) {
if (deltaArray[i].address) {
console.log('Local state change address: ' + deltaArray[i].address);
for (let j = 0; j < deltaArray[i].delta.length; j++) {
printAppCallDelta(deltaArray[i].delta[j]);
}
}
else {
console.log('Global state change');
printAppCallDelta(deltaArray[i]);
}
}
}
function printAppCallDelta(state) {
let text = Buffer.from(state.key, 'base64').toString() + ': ';
if (state.value.bytes !== undefined) {
let addr = addressFromByteBuffer(state.value.bytes);
if (addr.length == ALGORAND_ADDRESS_SIZE) {
text += addr;
}
else {
text += state.value.bytes;
}
}
else if (state.value.uint !== undefined) {
text += state.value.uint;
}
else {
text += '0';
}
console.log(text);
}
function printAppStateArray(stateArray) {
for (let n = 0; n < stateArray.length; n++) {
this.printAppState(stateArray[n]);
}
}
function appValueState(stateValue) {
let text = '';
if (stateValue.type == 1) {
let addr = addressFromByteBuffer(stateValue.bytes);
if (addr.length == ALGORAND_ADDRESS_SIZE) {
text += addr;
}
else {
text += stateValue.bytes;
}
}
else if (stateValue.type == 2) {
text = stateValue.uint;
}
else {
text += stateValue.bytes;
}
return text;
}
function appValueStateString(stateValue) {
let text = '';
if (stateValue.type == 1) {
let addr = addressFromByteBuffer(stateValue.bytes);
if (addr.length == ALGORAND_ADDRESS_SIZE) {
text += addr;
}
else {
text += stateValue.bytes;
}
}
else if (stateValue.type == 2) {
text += stateValue.uint;
}
else {
text += stateValue.bytes;
}
return text;
}
// appValueStateInt: return the integer stored or 0 otherwise
// eslint-disable-next-line no-unused-vars
function appValueStateInt(stateValue) {
let text = 0;
if (stateValue.type == 2) {
text = stateValue.uint;
}
return text;
}
function printAppState(state) {
let text = Buffer.from(state.key, 'base64').toString() + ': ';
text += appValueStateString(state.value);
console.log(text);
}
async function printAppLocalState(algodClient, appId, accountAddr) {
let ret = await readAppLocalState(algodClient, appId, accountAddr);
if (ret) {
console.log("Application %d local state for account %s:", appId, accountAddr);
printAppStateArray(ret);
}
}
async function printAppGlobalState(algodClient, appId, accountAddr) {
let ret = await readAppGlobalState(algodClient, appId, accountAddr);
if (ret) {
console.log("Application %d global state:", appId);
printAppStateArray(ret);
}
}
// read global state of application
async function readAppGlobalState(algodClient, appId, accountAddr) {
const accountInfoResponse = await algodClient.accountInformation(accountAddr).do();
for (let i = 0; i < accountInfoResponse['created-apps'].length; i++) {
if (accountInfoResponse['created-apps'][i].id === appId) {
let globalState = accountInfoResponse['created-apps'][i].params['global-state'];
return globalState;
// this.printAppStateArray (globalState)
}
}
}
async function readAppGlobalStateByKey(algodClient, appId, accountAddr, key) {
const accountInfoResponse = await algodClient.accountInformation(accountAddr).do();
for (let i = 0; i < accountInfoResponse['created-apps'].length; i++) {
if (accountInfoResponse['created-apps'][i].id === appId) {
// console.log("Application's global state:")
let stateArray = accountInfoResponse['created-apps'][i].params['global-state'];
for (let j = 0; j < stateArray.length; j++) {
let text = Buffer.from(stateArray[j].key, 'base64').toString();
if (key === text) {
return appValueState(stateArray[j].value);
}
}
}
}
return 0;
}
// read local state of application from user account
async function readAppLocalState(algodClient, appId, accountAddr) {
const accountInfoResponse = await algodClient.accountInformation(accountAddr).do();
for (let i = 0; i < accountInfoResponse['apps-local-state'].length; i++) {
if (accountInfoResponse['apps-local-state'][i].id === appId) {
// console.log(accountAddr + " opted in, local state:")
if (accountInfoResponse['apps-local-state'][i]['key-value']) {
// this.printAppStateArray (accountInfoResponse['apps-local-state'][i]['key-value'])
return accountInfoResponse['apps-local-state'][i]['key-value'];
}
}
}
}
async function readAppLocalStateByKey(algodClient, appId, accountAddr, key) {
const accountInfoResponse = await algodClient.accountInformation(accountAddr).do();
for (let i = 0; i < accountInfoResponse['apps-local-state'].length; i++) {
if (accountInfoResponse['apps-local-state'][i].id === appId) {
let stateArray = accountInfoResponse['apps-local-state'][i]['key-value'];
if (!stateArray) {
return null;
}
for (let j = 0; j < stateArray.length; j++) {
let text = Buffer.from(stateArray[j].key, 'base64').toString();
if (key === text) {
return appValueState(stateArray[j].value);
}
}
// not found assume 0
return 0;
}
}
}
function uintArray8ToString(byteArray) {
return Array.from(byteArray, function(byte) {
// eslint-disable-next-line no-bitwise
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
module.exports = {
timeoutPromise,
getInt64Bytes,
addressFromByteBuffer,
printAppCallDeltaArray,
printAppCallDelta,
printAppStateArray,
printAppState,
printAppLocalState,
printAppGlobalState,
readAppGlobalState,
readAppGlobalStateByKey,
readAppLocalState,
readAppLocalStateByKey,
uintArray8ToString
};