gamerpc 8.0.8 → 9.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils/util.js CHANGED
@@ -5,16 +5,11 @@
5
5
 
6
6
  'use strict';
7
7
 
8
- //const assert = require('assert');
9
- //const nodeUtil = require('util');
10
- // const aes = require('./aes');
11
- // const createHmac = require('create-hmac/browser');
12
- const io = require('socket.io-client');
13
- // const Buffer = require('safe-buffer').Buffer
14
-
15
- function assert(ok) {
16
- throw new Error('something wrong');
17
- }
8
+ const assert = require('assert');
9
+ const nodeUtil = require('util');
10
+ const aes = require('./aes');
11
+ const createHmac = require('create-hmac/browser');
12
+ const Buffer = require('safe-buffer').Buffer
18
13
 
19
14
  /**
20
15
  * @exports utils/util
@@ -86,6 +81,73 @@ util.sprintf = function sprintf() {
86
81
  return o.join('');
87
82
  }
88
83
 
84
+ /**
85
+ * 加密方法
86
+ * @param key 加密key
87
+ * @param iv 向量
88
+ * @param data 需要加密的数据
89
+ * @returns string
90
+ */
91
+ util.encrypt = function encrypt(key, iv, data) {
92
+ if(!Buffer.isBuffer(key)) {
93
+ assert(typeof key === 'string');
94
+ key = Buffer.from(key, 'binary')
95
+ }
96
+
97
+ if(!Buffer.isBuffer(iv)) {
98
+ assert(typeof iv === 'string');
99
+ iv = Buffer.from(iv, 'binary')
100
+ }
101
+
102
+ if(!Buffer.isBuffer(data)) {
103
+ assert(typeof data === 'string');
104
+ data = Buffer.from(data, 'binary')
105
+ }
106
+
107
+ var crypted = aes.encipher(data, key, iv);
108
+
109
+ crypted = Buffer.from(crypted, 'binary');
110
+
111
+ let ret = Buffer.alloc(crypted.length*2);
112
+ for(let i = 0; i < crypted.length; i++){
113
+ ret[2*i] = (crypted[i] >> 4) + 97;
114
+ ret[2*i+1] = (crypted[i] & 0x0F) + 97;
115
+ }
116
+ return ret.toString('binary');
117
+ };
118
+
119
+ /**
120
+ * 解密方法
121
+ * @param key 解密的key
122
+ * @param iv 向量
123
+ * @param data 密文
124
+ * @returns string
125
+ */
126
+ util.decrypt = function decrypt(key, iv, data) {
127
+ if(!Buffer.isBuffer(key)) {
128
+ assert(typeof key === 'string');
129
+ key = Buffer.from(key, 'binary')
130
+ }
131
+
132
+ if(!Buffer.isBuffer(iv)) {
133
+ assert(typeof iv === 'string');
134
+ iv = Buffer.from(iv, 'binary')
135
+ }
136
+
137
+ if(!Buffer.isBuffer(data)) {
138
+ assert(typeof data === 'string');
139
+ data = Buffer.from(data, 'binary')
140
+ }
141
+
142
+ let ret = Buffer.alloc(data.length/2);
143
+ for(let i = 0; i < ret.length; i++){
144
+ ret[i] = ((data[i*2]-97)<<4) | (data[i*2+1]-97);
145
+ }
146
+
147
+ var decoded = aes.decipher(ret, key, iv);
148
+ return decoded.toString('binary');
149
+ };
150
+
89
151
  /**
90
152
  * Test whether a number is Number,
91
153
  * finite, and below MAX_SAFE_INTEGER.
@@ -196,10 +258,9 @@ util.stringify = function(data, exclude) {
196
258
  }
197
259
  });
198
260
  return base;
261
+ } else if(Buffer.isBuffer(data)) {
262
+ return data.toString('base64');
199
263
  }
200
- // else if(Buffer.isBuffer(data)) {
201
- // return data.toString('base64');
202
- // }
203
264
 
204
265
  return data;
205
266
  }
@@ -413,39 +474,39 @@ util.isSafeAddition = function isSafeAddition(a, b) {
413
474
  * @return {String}
414
475
  */
415
476
 
416
- // util.inspectify = function inspectify(obj, color) {
417
- // if (typeof obj === 'string')
418
- // return obj;
477
+ util.inspectify = function inspectify(obj, color) {
478
+ if (typeof obj === 'string')
479
+ return obj;
419
480
 
420
- // inspectOptions.colors = color !== false;
481
+ inspectOptions.colors = color !== false;
421
482
 
422
- // return nodeUtil.inspect(obj, inspectOptions);
423
- // };
483
+ return nodeUtil.inspect(obj, inspectOptions);
484
+ };
424
485
 
425
- // /**
426
- // * Format a string.
427
- // * @function
428
- // * @param {...String} args
429
- // * @returns {String}
430
- // */
486
+ /**
487
+ * Format a string.
488
+ * @function
489
+ * @param {...String} args
490
+ * @returns {String}
491
+ */
431
492
 
432
- // util.fmt = nodeUtil.format;
493
+ util.fmt = nodeUtil.format;
433
494
 
434
- // /**
435
- // * Format a string.
436
- // * @param {Array} args
437
- // * @param {Boolean?} color
438
- // * @return {String}
439
- // */
495
+ /**
496
+ * Format a string.
497
+ * @param {Array} args
498
+ * @param {Boolean?} color
499
+ * @return {String}
500
+ */
440
501
 
441
- // util.format = function format(args, color) {
442
- // if (args.length > 0 && args[0] && typeof args[0] === 'object') {
443
- // if (color == null)
444
- // color = Boolean(process.stdout && process.stdout.isTTY);
445
- // return util.inspectify(args[0], color);
446
- // }
447
- // return util.fmt(...args);
448
- // };
502
+ util.format = function format(args, color) {
503
+ if (args.length > 0 && args[0] && typeof args[0] === 'object') {
504
+ if (color == null)
505
+ color = Boolean(process.stdout && process.stdout.isTTY);
506
+ return util.inspectify(args[0], color);
507
+ }
508
+ return util.fmt(...args);
509
+ };
449
510
 
450
511
  /**
451
512
  * Write a message to stdout (console in browser).
@@ -594,40 +655,40 @@ util.random = function random(min, max) {
594
655
  /**
595
656
  * Create a 32 or 64 bit nonce.
596
657
  * @param {Number} size
597
- * @returns {*}
658
+ * @returns {Buffer}
598
659
  */
599
660
 
600
- // util.nonce = function nonce(size) {
601
- // let n, data;
602
-
603
- // if (!size)
604
- // size = 8;
605
-
606
- // switch (size) {
607
- // case 8:
608
- // data = Buffer.allocUnsafe(8);
609
- // n = util.random(0, 0x100000000);
610
- // data.writeUInt32LE(n, 0, true);
611
- // n = util.random(0, 0x100000000);
612
- // data.writeUInt32LE(n, 4, true);
613
- // break;
614
- // case 4:
615
- // data = Buffer.allocUnsafe(4);
616
- // n = util.random(0, 0x100000000);
617
- // data.writeUInt32LE(n, 0, true);
618
- // break;
619
- // default:
620
- // assert(false, 'Bad nonce size.');
621
- // break;
622
- // }
623
-
624
- // return data;
625
- // };
661
+ util.nonce = function nonce(size) {
662
+ let n, data;
663
+
664
+ if (!size)
665
+ size = 8;
666
+
667
+ switch (size) {
668
+ case 8:
669
+ data = Buffer.allocUnsafe(8);
670
+ n = util.random(0, 0x100000000);
671
+ data.writeUInt32LE(n, 0, true);
672
+ n = util.random(0, 0x100000000);
673
+ data.writeUInt32LE(n, 4, true);
674
+ break;
675
+ case 4:
676
+ data = Buffer.allocUnsafe(4);
677
+ n = util.random(0, 0x100000000);
678
+ data.writeUInt32LE(n, 0, true);
679
+ break;
680
+ default:
681
+ assert(false, 'Bad nonce size.');
682
+ break;
683
+ }
684
+
685
+ return data;
686
+ };
626
687
 
627
688
  /**
628
689
  * String comparator (memcmp + length comparison).
629
- * @param {*} a
630
- * @param {*} b
690
+ * @param {Buffer} a
691
+ * @param {Buffer} b
631
692
  * @returns {Number} -1, 1, or 0.
632
693
  */
633
694
 
@@ -662,26 +723,26 @@ util.mb = function mb(size) {
662
723
 
663
724
  /**
664
725
  * Find index of a buffer in an array of buffers.
665
- * @param {buffer[]} items
666
- * @param {buffer} data - Target buffer to find.
726
+ * @param {Buffer[]} items
727
+ * @param {Buffer} data - Target buffer to find.
667
728
  * @returns {Number} Index (-1 if not found).
668
729
  */
669
730
 
670
- // util.indexOf = function indexOf(items, data) {
671
- // assert(Array.isArray(items));
672
- // assert(Buffer.isBuffer(data));
731
+ util.indexOf = function indexOf(items, data) {
732
+ assert(Array.isArray(items));
733
+ assert(Buffer.isBuffer(data));
673
734
 
674
- // for (let i = 0; i < items.length; i++) {
675
- // const item = items[i];
735
+ for (let i = 0; i < items.length; i++) {
736
+ const item = items[i];
676
737
 
677
- // assert(Buffer.isBuffer(item));
738
+ assert(Buffer.isBuffer(item));
678
739
 
679
- // if (item.equals(data))
680
- // return i;
681
- // }
740
+ if (item.equals(data))
741
+ return i;
742
+ }
682
743
 
683
- // return -1;
684
- // };
744
+ return -1;
745
+ };
685
746
 
686
747
  /**
687
748
  * Convert a number to a padded uint8
@@ -1223,7 +1284,7 @@ const CommMode = {
1223
1284
  */
1224
1285
  const NotifyType = {
1225
1286
  none: 0, //测试消息
1226
- test: 9999, //测试消息
1287
+ test: 9999, //测试消息
1227
1288
  };
1228
1289
 
1229
1290
  function Base64() {
@@ -1329,6 +1390,17 @@ function Base64() {
1329
1390
  }
1330
1391
  }
1331
1392
 
1393
+ /**
1394
+ * 利用HMAC算法,以及令牌固定量和令牌随机量,计算访问令牌
1395
+ * @param {*} token
1396
+ * @param {*} random
1397
+ */
1398
+ function signHMAC(token, random) {
1399
+ var hmac = createHmac('sha256', random);
1400
+ let sig = hmac.update(token).digest('hex');
1401
+ return sig;
1402
+ }
1403
+
1332
1404
  /**
1333
1405
  * 扩展对象,用于多个对象之间的属性注入
1334
1406
  * @note 对属性(get set)复制不会成功
@@ -1444,13 +1516,13 @@ function clone(obj) {
1444
1516
  throw new Error("Unable to copy obj! Its type isn't supported.");
1445
1517
  }
1446
1518
 
1447
- util.io = io;
1448
1519
  util.Base64 = Base64;
1520
+ util.signHMAC = signHMAC;
1449
1521
  util.ReturnCode = ReturnCode;
1450
1522
  util.ReturnCodeName = ReturnCodeName;
1451
1523
  util.CommMode = CommMode;
1452
1524
  util.NotifyType = NotifyType;
1453
- // util.createHmac = createHmac;
1525
+ util.createHmac = createHmac;
1454
1526
  util.extendObj = extendObj;
1455
1527
  util.clone = clone;
1456
1528
  util.CommStatus = CommStatus;
package/test/game/auth.js CHANGED
@@ -32,7 +32,7 @@ let phone_wx = ((Math.random() * 100000000) | 0).toString();
32
32
  //用于验证后期绑定成功的用户证书缓存变量
33
33
  let authUser = '';
34
34
 
35
- describe('钱包注册登录测试', () => {
35
+ describe.only('钱包注册登录测试', () => {
36
36
  it('用户注册并登录 - 使用两阶段认证模式', async () => {
37
37
  //执行登录操作,通过配置对象传入用户信息,并指定签证方式为两阶段认证
38
38
  let ret = await remote.init(/*初始化连接器,只保留原始配置信息*/).login({
package/test/game/crm.js CHANGED
@@ -22,7 +22,7 @@ let username = `${(Math.random()*1000000)|0}@vallnet.cn`;
22
22
  let password = crypto.createHash("sha1").update(((Math.random()*1000000)|0).toString() + salt).digest("hex");
23
23
  let mobilephone = `139${((Math.random()*100000000)|0).toString()}`;
24
24
 
25
- describe('CRM注册登录', () => {
25
+ describe.only('CRM注册登录', () => {
26
26
  it('用户注册 - 通过两阶段认证模式实现', async () => {
27
27
  //当用户点击'获取验证码'时执行如下流程:
28
28
  let ret = await remote.init(/*初始化连接器,只保留原始配置信息*/).login({
package/test/test.html CHANGED
@@ -1,43 +1,61 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
- <script src="../lib/gamerpc1.7.8.js"></script>
4
+ <meta charset="UTF-8">
5
+ <title>gamerpc V9.0.0 - 统一工具包测试</title>
6
+ <script src="../lib/gamerpc9.0.0.js"></script>
5
7
  </head>
6
8
  <body>
7
- <div id='content'></div>
9
+ <h1>gamerpc V9.0.0 浏览器测试</h1>
10
+ <div id='content'>加载中...</div>
8
11
  <script>
9
- //获取授权式连接器
10
- let remote = new toolkit.conn();
12
+ // V9.0.0 统一入口,通过全局 toolkit 对象访问所有功能
13
+ console.log('toolkit:', toolkit);
11
14
 
12
- let gameHelper = new toolkit.gameconn();
15
+ // 1. 区块链授权式连接器 (V7.1.3 API)
16
+ var blockchainConn = new toolkit.conn();
17
+ // 或: new toolkit.authConn();
13
18
 
14
- //测试签名算法
15
- let key = toolkit.generateKey();
16
- let obj = {msg:'hello world'};
17
- let sg = toolkit.signObj(obj, key.private);
18
- let ver = toolkit.verifyObj(obj, sg, key.public);
19
- console.log(ver);
19
+ // 2. 游戏云连接器 (V8.0.8 API)
20
+ var gameHelper = new toolkit.gameconn();
21
+ // 或: new toolkit.gameConn();
20
22
 
21
- //设置连接参数
22
- remote.setup({
23
- type: 'testnet', //对等网络类型,分为 testnet 和 main
24
- ip: '127.0.0.1', //远程全节点地址
25
- apiKey: '', //远程全节点基本校验密码
26
- id: 'primary', //默认访问的钱包编号
27
- cid: 'xxxxxxxx-vallnet-root-xxxxxxxxxxxxxx', //授权节点编号,用于访问远程钱包时的认证
28
- token: '', //授权节点令牌固定量,用于访问远程钱包时的认证
29
- });
23
+ // 3. 密码学工具 - 测试签名算法
24
+ var key = toolkit.generateKey();
25
+ var obj = {msg: 'hello world'};
26
+ var sg = toolkit.signObj(obj, key.private);
27
+ var ver = toolkit.verifyObj(obj, sg, key.public);
28
+ console.log('签名验证结果:', ver);
29
+
30
+ // 4. AES 加解密测试
31
+ var encKey = '$-._s1ZshKZ6WissH5gOs1ZshKZ6Wiss';
32
+ var encIv = '$-._aB9601152555';
33
+ var testData = 'hello gamerpc V9.0.0';
34
+ var encrypted = toolkit.encrypt(encKey, encIv, testData);
35
+ var decrypted = toolkit.decrypt(encKey, encIv, encrypted);
36
+ console.log('加解密测试:', testData === decrypted ? '通过' : '失败');
30
37
 
31
- (async () => {
32
- //查询账户余额
33
- let ret = await remote.execute('balance.all', []);
34
- document.getElementById('content').innerHTML = JSON.stringify(ret);
35
- })();
38
+ document.getElementById('content').innerHTML =
39
+ '<p>版本: <strong>gamerpc V9.0.0</strong></p>' +
40
+ '<p>签名验证: <strong>' + (ver ? '通过' : '失败') + '</strong></p>' +
41
+ '<p>AES加解密: <strong>' + (testData === decrypted ? '通过' : '失败') + '</strong></p>' +
42
+ '<p>连接器: gameconn(游戏云) + authConn(区块链)</p>';
43
+
44
+ // 5. 设置区块链连接参数
45
+ blockchainConn.setup({
46
+ type: 'testnet',
47
+ ip: '127.0.0.1',
48
+ apiKey: '',
49
+ id: 'primary',
50
+ cid: 'xxxxxxxx-vallnet-root-xxxxxxxxxxxxxx',
51
+ token: '',
52
+ });
36
53
 
37
- gameHelper.events.on('hello', msg=>{
38
- console.log(msg);
54
+ // 6. 事件模型测试
55
+ gameHelper.events.on('hello', function(msg) {
56
+ console.log('收到事件:', msg);
39
57
  });
40
- gameHelper.events.emit('hello', {id:3});
58
+ gameHelper.events.emit('hello', {id: 3, msg: 'V9.0.0 ready!'});
41
59
  </script>
42
60
  </body>
43
- </html>
61
+ </html>