-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
277 lines (265 loc) · 7.63 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
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
274
275
276
277
const contractInfo = require('./contract.json');
const web3 = require('web3');
const CHUNK_SIZE = 40000;
const GAS_LIMIT = 0x33333333;
var g_web3;
var g_contract;
var g_contractAddress;
var g_defaultAccount;
var g_defaultAccountPassphrase;
var g_createDocumentCallbacks = {};
var g_createDocumentCallbacksCount = 0;
var g_transactionCallbacks = {};
function setServer(serverUrl, opt_contractAbi, opt_contractAddress) {
const contractAbi = opt_contractAbi ? opt_contractAbi : contractInfo.contractAbi;
g_contractAddress = opt_contractAddress ? opt_contractAddress : contractInfo.contractAddress;
g_web3 = new web3(new web3.providers.HttpProvider(serverUrl));
g_contract = g_web3.eth.contract(contractAbi).at(g_contractAddress);
}
function setAccount(account, accountPassphrase) {
g_defaultAccount = account;
g_defaultAccountPassphrase = accountPassphrase;
g_web3.eth.defaultAccount = g_defaultAccount;
}
function getAccounts(callback) {
if (g_web3) {
g_web3.eth.getAccounts(callback);
} else {
callback(undefined, []);
}
}
function writeFile(name, data, thumbnail, callback) {
const dataSize = Math.ceil(data.length / CHUNK_SIZE);
const thumbnailSize = Math.ceil(thumbnail.length / CHUNK_SIZE);
const meta = JSON.stringify({name: name, dataSize: dataSize, thumbnailSize: thumbnailSize});
const createDocumentData = g_contract.createDocument.getData(meta);
personalSendTransaction(createDocumentData, function(error, result) {
if (error) {
callback(error);
} else {
if (0 == g_createDocumentCallbacksCount) {
var event = g_contract.DocumentEvent(null, null, function(error, result) {
if (error) {
console.log(error);
} else {
var _callback = g_createDocumentCallbacks[result.transactionHash];
if (_callback) {
delete g_createDocumentCallbacks[result.transactionHash];
g_createDocumentCallbacksCount--;
if (0 == g_createDocumentCallbacksCount) {
event.stopWatching();
}
_callback(undefined, result.args['docId']);
}
}
});
}
if (!g_createDocumentCallbacks[result]) {
g_createDocumentCallbacksCount++;
g_createDocumentCallbacks[result] = function(error, docId) {
if (error) {
callback(error);
} else {
_writeChunks(docId, data, thumbnail, 0, callback);
}
};
}
}
});
}
function readFile(docId, callback) {
//docId++;//todo starts with 1
g_contract.docBase(docId, function(error, result) {
if (error) {
callback(error);
} else {
var chunks = [];
const blocksCount = result[0];
const meta = result[2];
if (blocksCount > 0) {
_readChunks(docId, meta, 0, blocksCount, chunks, callback);
} else {
callback(new Error('readFile'));
}
}
});
}
function getFilesCount(callback) {
g_contract.documentsCount(callback);
}
function createRecord(docId, data, callback, opt_callbackSendTransaction) {
//docId++;//todo starts with 1
const createRecordData = g_contract.createRecord.getData(docId, data);
personalSendTransaction(createRecordData, function(error, result) {
if (opt_callbackSendTransaction) {
opt_callbackSendTransaction(error, callback);
}
if (error) {
callback(error);
} else {
_waitForTx(result, callback);
}
});
}
function getRecord(index, callback) {
g_contract.getRecord(index, function(error, result) {
if (error) {
callback(error);
} else {
if (callback) {
callback(undefined, {'docId': result[0], 'data': result[1]});
}
}
});
}
function getRecordsCount(callback) {
g_contract.getRecordsCount(callback);
}
function _writeChunks(docId, data, thumbnail, index, callback) {
const dataToWrite = data ? data : thumbnail;
const dataEncoded = encodeHex(dataToWrite.subarray(index, index + CHUNK_SIZE));
const addTextData = g_contract.addBlock.getData(docId, dataEncoded);
personalSendTransaction(addTextData, function(error, result) {
if (error) {
callback(error);
} else {
if (index + CHUNK_SIZE < dataToWrite.length) {
_writeChunks(docId, data, thumbnail, index + CHUNK_SIZE, callback);
} else if (null != data) {
_writeChunks(docId, null, thumbnail, 0, callback);
} else {
_waitForTx(result, function(error) {
callback(error, docId);
});
}
}
});
}
function _readChunks(docId, meta, index, count, chunks, callback) {
g_contract.getBlock(docId, index, function(error, result) {
if (error) {
callback(error);
} else {
chunks.push(decodeHex(result));
if (index + 1 < count) {
_readChunks(docId, meta, index + 1, count, chunks, callback);
} else {
callback(undefined, _assembleChunks(meta, chunks));
}
}
});
}
function _assembleChunks(metaStr, chunks) {
var res = null;
if (chunks.length > 0) {
const meta = JSON.parse(metaStr);
var dataSize = 0;
var thumbnailSize = 0;
var dataChunks = [];
var thumbnailChunks = [];
for (var i = 0; i < chunks.length; ++i) {
var dataCur = chunks[i];
if (dataChunks.length < meta.dataSize) {
dataSize += dataCur.length;
dataChunks.push(dataCur);
} else {
thumbnailSize += dataCur.length;
thumbnailChunks.push(dataCur);
}
}
var dataBinary = new Uint8Array(dataSize);
var cur = 0;
for (var i = 0; i < dataChunks.length; ++i) {
var chunk = dataChunks[i];
if (0 == i) {
dataBinary.set(chunk);
} else {
dataBinary.set(chunk, cur);
}
cur += chunk.length;
}
var thumbnailBinary = new Uint8Array(thumbnailSize);
cur = 0;
for (var i = 0; i < thumbnailChunks.length; ++i) {
var chunk = thumbnailChunks[i];
if (0 == i) {
thumbnailBinary.set(chunk);
} else {
thumbnailBinary.set(chunk, cur);
}
cur += chunk.length;
}
res = {'name': meta.name, 'data': dataBinary, 'thumbnail': thumbnailBinary};
}
return res;
}
function _waitForTx(txhash, callback) {
g_transactionCallbacks[txhash] = callback;
const filter = g_web3.eth.filter('latest');
filter.watch(function(error, result) {
if (error) {
var _callback = g_transactionCallbacks[txhash];
if(_callback){
delete g_transactionCallbacks[txhash];
_callback(error);
}
} else {
g_web3.eth.getTransactionReceipt(txhash, function(error, result){
if (error) {
var _callback = g_transactionCallbacks[txhash];
if(_callback){
delete g_transactionCallbacks[txhash];
_callback(error);
}
} else {
if (result && result.transactionHash == txhash) {
filter.stopWatching();
var _callback = g_transactionCallbacks[txhash];
if(_callback){
delete g_transactionCallbacks[txhash];
_callback(undefined, result);
}
}
}
});
}
});
}
function personalSendTransaction(data, callback) {
g_web3.personal.sendTransaction({'to': g_contractAddress, 'gas': GAS_LIMIT, 'data': data},
g_defaultAccountPassphrase,
callback);
}
function decodeHex(s) {
var o = [];
var alpha = '0123456789abcdef';
for (var i = (s.substr(0, 2) == '0x' ? 2 : 0); i < s.length; i += 2) {
var index1 = alpha.indexOf(s[i]);
var index2 = alpha.indexOf(s[i + 1]);
if (index1 < 0 || index2 < 0)
throw("Bad input to hex decoding: " + s + " " + i + " " + index1 + " " + index2)
o.push(index1 * 16 + index2);
}
return o;
}
function encodeHex(val) {
var res = '';
for (var i = 0; i < val.length; ++i) {
var hex = val[i].toString(16);
if (hex.length > 1) {
res += hex;
} else {
res += '0' + hex;
}
}
return '0x' + res;
}
module.exports.setServer = setServer;
module.exports.setAccount = setAccount;
module.exports.getAccounts = getAccounts;
module.exports.writeFile = writeFile;
module.exports.readFile = readFile;
module.exports.getFilesCount = getFilesCount;
module.exports.createRecord = createRecord;
module.exports.getRecord = getRecord;
module.exports.getRecordsCount = getRecordsCount;