UNPKG

13.9 kBJavaScriptView Raw
1/* eslint-disable */
2function bin2String(array) {
3 return String.fromCharCode.apply(String, array);
4}
5
6//比较两个byteArray是否相等
7function arrayEquals(array1, array2) {
8 if (array1.length != array2.length) {
9 return false;
10 }
11 var i;
12 for (i = 0; i < array1.length; i++) {
13 if (array1[i] != array2[i]) {
14 return false;
15 }
16 }
17 return true;
18}
19
20//从base64字符串中解析TransAction对象
21function getTransActionFromBase64String(base64String) {
22 var bytesDecode = base64DecodeFromString(base64String);
23 var transaction = proto.protocol.Transaction.deserializeBinary(bytesDecode);
24 //ToDo : ret is success
25 return transaction;
26}
27
28//Return a list contains contract object
29//从TransAction对象中获得合约列表
30function getContractListFromTransaction(transaction) {
31 var raw = transaction.getRawData();
32 var type = raw.getType();
33 if (type != 1) {
34 layer.alert("Invalid transaction type !!!!" + type);
35 return null;
36 }
37 var contractList = raw.getContractList();
38 var count = contractList.length;
39 if (count == 0) {
40 layer.alert("No contract !!!!");
41 return null;
42 }
43
44 array = new Array(count);
45 var unpack = proto.google.protobuf.Any.prototype.unpack;
46 while (count > 0) {
47 count--;
48 var oneContract = contractList[count];
49 var any = oneContract.getParameter();
50 var contarcType = oneContract.getType();
51 var obje;
52 switch (contarcType) {
53 case proto.protocol.Transaction.Contract.ContractType.ACCOUNTCREATECONTRACT:
54 obje = any.unpack(
55 proto.protocol.AccountCreateContract.deserializeBinary,
56 "protocol.AccountCreateContract");
57 break;
58
59 case proto.protocol.Transaction.Contract.ContractType.TRANSFERCONTRACT:
60 obje = any.unpack(
61 proto.protocol.TransferContract.deserializeBinary,
62 "protocol.TransferContract");
63 break;
64
65 case proto.protocol.Transaction.Contract.ContractType.TRANSFERASSETCONTRACT:
66 obje = any.unpack(
67 proto.protocol.TransferAsstContract.deserializeBinary,
68 "protocol.TransferAssetContract");
69 break;
70
71 case proto.protocol.Transaction.Contract.ContractType.VOTEASSETCONTRACT:
72 obje = any.unpack(
73 proto.protocol.VoteAssetContract.deserializeBinary,
74 "protocol.VoteAssetContract");
75 break;
76
77 case proto.protocol.Transaction.Contract.ContractType.VOTEWITNESSCONTRACT:
78 obje = any.unpack(
79 proto.protocol.VoteWitnessContract.deserializeBinary,
80 "protocol.VoteWitnessContract");
81 break;
82
83 case proto.protocol.Transaction.Contract.ContractType.WITNESSCREATECONTRACT:
84 obje = any.unpack(
85 proto.protocol.WitnessCreateContract.deserializeBinary,
86 "protocol.WitnessCreateContract");
87 break;
88
89 case proto.protocol.Transaction.Contract.ContractType.ASSETISSUECONTRACT:
90 obje = any.unpack(
91 proto.protocol.AssetIssueContract.deserializeBinary,
92 "protocol.AssetIssueContract");
93 break;
94
95 case proto.protocol.Transaction.Contract.ContractType.DEPLOYCONTRACT:
96 obje = any.unpack(
97 proto.protocol.DeployContract.deserializeBinary,
98 "protocol.DeployContract");
99 break;
100
101 case proto.protocol.Transaction.Contract.ContractType.WITNESSUPDATECONTRACT:
102 obje = any.unpack(
103 proto.protocol.WitnessUpdateContract.deserializeBinary,
104 "protocol.WitnessUpdateContract");
105 break;
106 }
107 array[count] = obje;
108 }
109 return array;
110}
111
112//字符串转byteArray数据格式
113function stringToBytes(str) {
114 var bytes = new Array();
115 var len, c;
116 len = str.length;
117 for (var i = 0; i < len; i++) {
118 c = str.charCodeAt(i);
119 if (c >= 0x010000 && c <= 0x10FFFF) {
120 bytes.push(((c >> 18) & 0x07) | 0xF0);
121 bytes.push(((c >> 12) & 0x3F) | 0x80);
122 bytes.push(((c >> 6) & 0x3F) | 0x80);
123 bytes.push((c & 0x3F) | 0x80);
124 } else if (c >= 0x000800 && c <= 0x00FFFF) {
125 bytes.push(((c >> 12) & 0x0F) | 0xE0);
126 bytes.push(((c >> 6) & 0x3F) | 0x80);
127 bytes.push((c & 0x3F) | 0x80);
128 } else if (c >= 0x000080 && c <= 0x0007FF) {
129 bytes.push(((c >> 6) & 0x1F) | 0xC0);
130 bytes.push((c & 0x3F) | 0x80);
131 } else {
132 bytes.push(c & 0xFF);
133 }
134 }
135 return bytes;
136
137}
138
139//byteArray数据格式转字符串
140function bytesToString(arr) {
141 if (typeof arr === 'string') {
142 return arr;
143 }
144 var str = '',
145 _arr = arr;
146 for (var i = 0; i < _arr.length; i++) {
147 var one = _arr[i].toString(2),
148 v = one.match(/^1+?(?=0)/);
149 if (v && one.length == 8) {
150 var bytesLength = v[0].length;
151 var store = _arr[i].toString(2).slice(7 - bytesLength);
152 for (var st = 1; st < bytesLength; st++) {
153 store += _arr[st + i].toString(2).slice(2);
154 }
155 str += String.fromCharCode(parseInt(store, 2));
156 i += bytesLength - 1;
157 } else {
158 str += String.fromCharCode(_arr[i]);
159 }
160 }
161 return str;
162}
163
164function hextoString(hex) {
165 var arr = hex.split("")
166 var out = ""
167 for (var i = 0; i < arr.length / 2; i++) {
168 var tmp = "0x" + arr[i * 2] + arr[i * 2 + 1]
169 var charValue = String.fromCharCode(tmp);
170 out += charValue
171 }
172 return out
173}
174
175/* Convert a hex char to value */
176function hexChar2byte(c) {
177 var d = 0;
178 if (c >= 'A' && c <= 'F') {
179 d = c.charCodeAt(0) - 'A'.charCodeAt(0) + 10;
180 }
181 else if (c >= 'a' && c <= 'f') {
182 d = c.charCodeAt(0) - 'a'.charCodeAt(0) + 10;
183 }
184 else if (c >= '0' && c <= '9') {
185 d = c.charCodeAt(0) - '0'.charCodeAt(0);
186 }
187 return d;
188}
189
190/* Check if a char is hex char */
191function isHexChar(c) {
192 if ((c >= 'A' && c <= 'F') ||
193 (c >= 'a' && c <= 'f') ||
194 (c >= '0' && c <= '9')) {
195 return 1;
196 }
197 return 0;
198}
199
200/* Convert HEX string to byte array */
201
202//16进制的ASCII字符串转为byteArray格式。
203function hexStr2byteArray(str) {
204 var byteArray = Array();
205 var d = 0;
206 var j = 0;
207 var k = 0;
208
209 for (let i = 0; i < str.length; i++) {
210 var c = str.charAt(i);
211 if (isHexChar(c)) {
212 d <<= 4;
213 d += hexChar2byte(c);
214 j++;
215 if (0 === (j % 2)) {
216 byteArray[k++] = d;
217 d = 0;
218 }
219 }
220 }
221 return byteArray;
222}
223
224
225/* Convert a byte to string */
226function byte2hexStr(byte) {
227 var hexByteMap = "0123456789ABCDEF";
228 var str = "";
229 str += hexByteMap.charAt(byte >> 4);
230 str += hexByteMap.charAt(byte & 0x0f);
231 return str;
232}
233
234/* Convert byte arry to HEX string */
235
236//byteArray格式数据转为16进制的ASCII字符串。
237function byteArray2hexStr(byteArray) {
238 var str = "";
239 for (var i = 0; i < (byteArray.length - 1); i++) {
240 str += byte2hexStr(byteArray[i]);
241 }
242 str += byte2hexStr(byteArray[i]);
243 return str;
244}
245
246//从base64字符串中解码出原文,格式为byteArray格式
247function base64DecodeFromString(string64) {
248 var b = new Base64();
249 var decodeBytes = b.decodeToByteArray(string64);
250 return decodeBytes;
251}
252
253//return baset64 String
254//将byteArray格式数据编码为base64字符串
255function base64EncodeToString(bytes) {
256 // var string = bytesToString(bytes);
257 var b = new Base64();
258 var string64 = b.encodeIgnoreUtf8(bytes);
259 return string64
260}
261
262function Base64() {
263
264 // private property
265 let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
266
267 // public method for encoding
268 this.encode = function (input) {
269 var output = "";
270 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
271 var i = 0;
272// input = _utf8_encode(input);
273 while (i < input.length) {
274 chr1 = input.charCodeAt(i++);
275 chr2 = input.charCodeAt(i++);
276 chr3 = input.charCodeAt(i++);
277 enc1 = chr1 >> 2;
278 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
279 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
280 enc4 = chr3 & 63;
281 if (isNaN(chr2)) {
282 enc3 = enc4 = 64;
283 } else if (isNaN(chr3)) {
284 enc4 = 64;
285 }
286 output = output +
287 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
288 _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
289 }
290 return output;
291 }
292
293 // public method for encoding
294 this.encodeIgnoreUtf8 = function (inputBytes) {
295 var output = "";
296 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
297 var i = 0;
298// input = _utf8_encode(input);
299 while (i < inputBytes.length) {
300 chr1 = inputBytes[i++];
301 chr2 = inputBytes[i++];
302 chr3 = inputBytes[i++];
303 enc1 = chr1 >> 2;
304 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
305 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
306 enc4 = chr3 & 63;
307 if (isNaN(chr2)) {
308 enc3 = enc4 = 64;
309 } else if (isNaN(chr3)) {
310 enc4 = 64;
311 }
312 output = output +
313 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
314 _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
315 }
316 return output;
317 }
318
319 // public method for decoding
320 this.decode = function (input) {
321 var output = "";
322 var chr1, chr2, chr3;
323 var enc1, enc2, enc3, enc4;
324 var i = 0;
325 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
326 while (i < input.length) {
327 enc1 = _keyStr.indexOf(input.charAt(i++));
328 enc2 = _keyStr.indexOf(input.charAt(i++));
329 enc3 = _keyStr.indexOf(input.charAt(i++));
330 enc4 = _keyStr.indexOf(input.charAt(i++));
331 chr1 = (enc1 << 2) | (enc2 >> 4);
332 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
333 chr3 = ((enc3 & 3) << 6) | enc4;
334 output = output + String.fromCharCode(chr1);
335 if (enc3 != 64) {
336 output = output + String.fromCharCode(chr2);
337 }
338 if (enc4 != 64) {
339 output = output + String.fromCharCode(chr3);
340 }
341 }
342 output = this._utf8_decode(output);
343 return output;
344 }
345
346 // public method for decoding
347 this.decodeToByteArray = function (input) {
348 var output = "";
349 var chr1, chr2, chr3;
350 var enc1, enc2, enc3, enc4;
351 var i = 0;
352 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
353 while (i < input.length) {
354 enc1 = _keyStr.indexOf(input.charAt(i++));
355 enc2 = _keyStr.indexOf(input.charAt(i++));
356 enc3 = _keyStr.indexOf(input.charAt(i++));
357 enc4 = _keyStr.indexOf(input.charAt(i++));
358 chr1 = (enc1 << 2) | (enc2 >> 4);
359 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
360 chr3 = ((enc3 & 3) << 6) | enc4;
361 output = output + String.fromCharCode(chr1);
362 if (enc3 != 64) {
363 output = output + String.fromCharCode(chr2);
364 }
365 if (enc4 != 64) {
366 output = output + String.fromCharCode(chr3);
367 }
368 }
369 var outBytes = this._out2ByteArray(output);
370 return outBytes;
371 };
372
373 // private method for UTF-8 decoding
374 this._out2ByteArray = function (utftext) {
375 let byteArray = new Array(utftext.length)
376 let i = 0;
377 let c = 0;
378 let c1 = 0;
379 let c2 = 0;
380 while (i < utftext.length) {
381 c = utftext.charCodeAt(i);
382 byteArray[i] = c;
383 i++;
384 }
385 return byteArray;
386 };
387
388 // private method for UTF-8 encoding
389 this._utf8_encode = function (string) {
390 string = string.replace(/\r\n/g, "\n");
391 var utftext = "";
392 for (var n = 0; n < string.length; n++) {
393 var c = string.charCodeAt(n);
394 if (c < 128) {
395 utftext += String.fromCharCode(c);
396 } else if ((c > 127) && (c < 2048)) {
397 utftext += String.fromCharCode((c >> 6) | 192);
398 utftext += String.fromCharCode((c & 63) | 128);
399 } else {
400 utftext += String.fromCharCode((c >> 12) | 224);
401 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
402 utftext += String.fromCharCode((c & 63) | 128);
403 }
404
405 }
406 return utftext;
407 }
408
409 // private method for UTF-8 decoding
410 this._utf8_decode = function (utftext) {
411 var string = "";
412 var i = 0;
413 var c = c1 = c2 = 0;
414 while (i < utftext.length) {
415 c = utftext.charCodeAt(i);
416 if (c < 128) {
417 string += String.fromCharCode(c);
418 i++;
419 } else if ((c > 191) && (c < 224)) {
420 c2 = utftext.charCodeAt(i + 1);
421 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
422 i += 2;
423 } else {
424 c2 = utftext.charCodeAt(i + 1);
425 c3 = utftext.charCodeAt(i + 2);
426 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3
427 & 63));
428 i += 3;
429 }
430 }
431 return string;
432 }
433}
434
435//yyyy-MM-DD HH-mm-ss
436function strToDate(str) {
437 var tempStrs = str.split(" ");
438 var dateStrs = tempStrs[0].split("-");
439 var year = parseInt(dateStrs[0], 10);
440 var month = parseInt(dateStrs[1], 10) - 1;
441 var day = parseInt(dateStrs[2], 10);
442 if (tempStrs.length > 1) {
443 var timeStrs = tempStrs[1].split("-");
444 var hour = parseInt(timeStrs [0], 10);
445 var minute = parseInt(timeStrs[1], 10) - 1;
446 var second = parseInt(timeStrs[2], 10);
447 return new Date(year, month, day, hour, minute, second);
448 }
449
450 return new Date(year, month, day);
451}
452
453function isNumber(c) {
454 if (c >= '0' && c <= '9') {
455 return 1;
456 }
457 return 0;
458}
459
460//return 1: address --- 20Bytes HexString
461//return 2: blockNumber ------ Decimal number
462//return 3: assetName ------ String
463//return other: error
464function getStringType(str) {
465 if (null == str) {
466 return -1;
467 }
468
469 if (typeof(str) != 'string') {
470 return -1;
471 }
472
473 if (str.length == 0 || str == "") {
474 return -1;
475 }
476
477 var i = 0;
478 if (str.length == 40) {
479 for (; i < 40; i++) {
480 var c = str.charAt(i);
481 if (!isHexChar(c)) {
482 break;
483 }
484 }
485 }
486 if (i == 40) {
487 return 1; //40 Hex, Address
488 }
489
490 for (i = 0; i < str.length; i++) {
491 var c = str.charAt(i);
492 if (!isNumber(c)) {
493 break;
494 }
495 }
496 if (i == str.length) {
497 return 2; //Alll Decimal number, BlockNumber
498 }
499
500 for (i = 0; i < str.length; i++) {
501 var c = str.charAt(i);
502 if (c > ' ') {
503 return 3; //At least one visible character
504 }
505 }
506
507 return -1;
508}
509
510module.exports = {
511 base64EncodeToString,
512 base64DecodeFromString,
513 hexStr2byteArray,
514 stringToBytes,
515};