@onekeyfe/hd-web-sdk 0.0.3 → 0.0.6

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.
@@ -5045,6 +5045,126 @@ const getDeviceBLEFirmwareVersion = features => {
5045
5045
  return features.ble_ver.split('.');
5046
5046
  };
5047
5047
 
5048
+ const HD_HARDENED = 0x80000000;
5049
+
5050
+ const toHardened = n => (n | HD_HARDENED) >>> 0;
5051
+
5052
+ const fromHardened = n => (n & ~HD_HARDENED) >>> 0;
5053
+
5054
+ const PATH_NOT_VALID = TypedError('Method_InvalidParameter', 'Not a valid path');
5055
+ const PATH_NEGATIVE_VALUES = TypedError('Method_InvalidParameter', 'Path cannot contain negative values');
5056
+
5057
+ const getHDPath = path => {
5058
+ const parts = path.toLowerCase().split('/');
5059
+ if (parts[0] !== 'm') throw PATH_NOT_VALID;
5060
+ return parts.filter(p => p !== 'm' && p !== '').map(p => {
5061
+ let hardened = false;
5062
+
5063
+ if (p.substr(p.length - 1) === "'") {
5064
+ hardened = true;
5065
+ p = p.substr(0, p.length - 1);
5066
+ }
5067
+
5068
+ let n = parseInt(p);
5069
+
5070
+ if (Number.isNaN(n)) {
5071
+ throw PATH_NOT_VALID;
5072
+ } else if (n < 0) {
5073
+ throw PATH_NEGATIVE_VALUES;
5074
+ }
5075
+
5076
+ if (hardened) {
5077
+ n = toHardened(n);
5078
+ }
5079
+
5080
+ return n;
5081
+ });
5082
+ };
5083
+
5084
+ const isMultisigPath = path => Array.isArray(path) && path[0] === toHardened(48);
5085
+
5086
+ const isSegwitPath = path => Array.isArray(path) && path[0] === toHardened(49);
5087
+
5088
+ const getScriptType = path => {
5089
+ if (!Array.isArray(path) || path.length < 1) return 'SPENDADDRESS';
5090
+ const p1 = fromHardened(path[0]);
5091
+
5092
+ switch (p1) {
5093
+ case 48:
5094
+ return 'SPENDMULTISIG';
5095
+
5096
+ case 49:
5097
+ return 'SPENDP2SHWITNESS';
5098
+
5099
+ case 84:
5100
+ return 'SPENDWITNESS';
5101
+
5102
+ default:
5103
+ return 'SPENDADDRESS';
5104
+ }
5105
+ };
5106
+
5107
+ const getOutputScriptType = path => {
5108
+ if (!Array.isArray(path) || path.length < 1) return 'PAYTOADDRESS';
5109
+
5110
+ if (path[0] === 49) {
5111
+ return 'PAYTOP2SHWITNESS';
5112
+ }
5113
+
5114
+ const p = fromHardened(path[0]);
5115
+
5116
+ switch (p) {
5117
+ case 48:
5118
+ return 'PAYTOMULTISIG';
5119
+
5120
+ case 49:
5121
+ return 'PAYTOP2SHWITNESS';
5122
+
5123
+ case 84:
5124
+ return 'PAYTOWITNESS';
5125
+
5126
+ default:
5127
+ return 'PAYTOADDRESS';
5128
+ }
5129
+ };
5130
+
5131
+ const serializedPath = path => {
5132
+ const pathStr = path.map(p => {
5133
+ if (p & HD_HARDENED) {
5134
+ return `${p & ~HD_HARDENED}'`;
5135
+ }
5136
+
5137
+ return p;
5138
+ }).join('/');
5139
+ return `m/${pathStr}`;
5140
+ };
5141
+
5142
+ const validatePath = (path, length = 0, base = false) => {
5143
+ let valid;
5144
+
5145
+ if (typeof path === 'string') {
5146
+ valid = getHDPath(path);
5147
+ } else if (Array.isArray(path)) {
5148
+ valid = path.map(p => {
5149
+ const n = parseInt(p);
5150
+
5151
+ if (Number.isNaN(n)) {
5152
+ throw PATH_NOT_VALID;
5153
+ } else if (n < 0) {
5154
+ throw PATH_NEGATIVE_VALUES;
5155
+ }
5156
+
5157
+ return n;
5158
+ });
5159
+ } else {
5160
+ valid = undefined;
5161
+ }
5162
+
5163
+ if (!valid) throw PATH_NOT_VALID;
5164
+ if (length > 0 && valid.length < length) throw PATH_NOT_VALID;
5165
+ return base ? valid.splice(0, 3) : valid;
5166
+ };
5167
+
5048
5168
  var nested = {
5049
5169
  BinanceGetAddress: {
5050
5170
  fields: {
@@ -13946,7 +14066,12 @@ const createErrorMessage = error => ({
13946
14066
 
13947
14067
  const UI_EVENT = 'UI_EVENT';
13948
14068
  const UI_REQUEST$1 = {
13949
- REQUEST_PIN: 'ui-request_pin'
14069
+ REQUEST_PIN: 'ui-request_pin',
14070
+ INVALID_PIN: 'ui-invalid_pin',
14071
+ REQUEST_BUTTON: 'ui-button',
14072
+ CLOSE_UI_WINDOW: 'ui-close_window',
14073
+ BLUETOOTH_PERMISSION: 'ui-bluetooth_permission',
14074
+ LOCATION_PERMISSION: 'ui-location_permission'
13950
14075
  };
13951
14076
 
13952
14077
  const createUiMessage = (type, payload) => ({
@@ -13987,6 +14112,7 @@ const createUiResponse = (type, payload) => ({
13987
14112
  payload
13988
14113
  });
13989
14114
 
14115
+ const DEVICE_EVENT = 'DEVICE_EVENT';
13990
14116
  const DEVICE = {
13991
14117
  CONNECT: 'device-connect',
13992
14118
  CONNECT_UNACQUIRED: 'device-connect_unacquired',
@@ -14006,6 +14132,12 @@ const DEVICE = {
14006
14132
  WORD: 'word'
14007
14133
  };
14008
14134
 
14135
+ const createDeviceMessage = (type, payload) => ({
14136
+ event: DEVICE_EVENT,
14137
+ type,
14138
+ payload
14139
+ });
14140
+
14009
14141
  const assertType = (res, resType) => {
14010
14142
  const splitResTypes = Array.isArray(resType) ? resType : resType.split('|');
14011
14143
 
@@ -14752,126 +14884,6 @@ class GetFeatures extends BaseMethod {
14752
14884
 
14753
14885
  }
14754
14886
 
14755
- const HD_HARDENED = 0x80000000;
14756
-
14757
- const toHardened = n => (n | HD_HARDENED) >>> 0;
14758
-
14759
- const fromHardened = n => (n & ~HD_HARDENED) >>> 0;
14760
-
14761
- const PATH_NOT_VALID = TypedError('Method_InvalidParameter', 'Not a valid path');
14762
- const PATH_NEGATIVE_VALUES = TypedError('Method_InvalidParameter', 'Path cannot contain negative values');
14763
-
14764
- const getHDPath = path => {
14765
- const parts = path.toLowerCase().split('/');
14766
- if (parts[0] !== 'm') throw PATH_NOT_VALID;
14767
- return parts.filter(p => p !== 'm' && p !== '').map(p => {
14768
- let hardened = false;
14769
-
14770
- if (p.substr(p.length - 1) === "'") {
14771
- hardened = true;
14772
- p = p.substr(0, p.length - 1);
14773
- }
14774
-
14775
- let n = parseInt(p);
14776
-
14777
- if (Number.isNaN(n)) {
14778
- throw PATH_NOT_VALID;
14779
- } else if (n < 0) {
14780
- throw PATH_NEGATIVE_VALUES;
14781
- }
14782
-
14783
- if (hardened) {
14784
- n = toHardened(n);
14785
- }
14786
-
14787
- return n;
14788
- });
14789
- };
14790
-
14791
- const isMultisigPath = path => Array.isArray(path) && path[0] === toHardened(48);
14792
-
14793
- const isSegwitPath = path => Array.isArray(path) && path[0] === toHardened(49);
14794
-
14795
- const getScriptType = path => {
14796
- if (!Array.isArray(path) || path.length < 1) return 'SPENDADDRESS';
14797
- const p1 = fromHardened(path[0]);
14798
-
14799
- switch (p1) {
14800
- case 48:
14801
- return 'SPENDMULTISIG';
14802
-
14803
- case 49:
14804
- return 'SPENDP2SHWITNESS';
14805
-
14806
- case 84:
14807
- return 'SPENDWITNESS';
14808
-
14809
- default:
14810
- return 'SPENDADDRESS';
14811
- }
14812
- };
14813
-
14814
- const getOutputScriptType = path => {
14815
- if (!Array.isArray(path) || path.length < 1) return 'PAYTOADDRESS';
14816
-
14817
- if (path[0] === 49) {
14818
- return 'PAYTOP2SHWITNESS';
14819
- }
14820
-
14821
- const p = fromHardened(path[0]);
14822
-
14823
- switch (p) {
14824
- case 48:
14825
- return 'PAYTOMULTISIG';
14826
-
14827
- case 49:
14828
- return 'PAYTOP2SHWITNESS';
14829
-
14830
- case 84:
14831
- return 'PAYTOWITNESS';
14832
-
14833
- default:
14834
- return 'PAYTOADDRESS';
14835
- }
14836
- };
14837
-
14838
- const serializedPath = path => {
14839
- const pathStr = path.map(p => {
14840
- if (p & HD_HARDENED) {
14841
- return `${p & ~HD_HARDENED}'`;
14842
- }
14843
-
14844
- return p;
14845
- }).join('/');
14846
- return `m/${pathStr}`;
14847
- };
14848
-
14849
- const validatePath = (path, length = 0, base = false) => {
14850
- let valid;
14851
-
14852
- if (typeof path === 'string') {
14853
- valid = getHDPath(path);
14854
- } else if (Array.isArray(path)) {
14855
- valid = path.map(p => {
14856
- const n = parseInt(p);
14857
-
14858
- if (Number.isNaN(n)) {
14859
- throw PATH_NOT_VALID;
14860
- } else if (n < 0) {
14861
- throw PATH_NEGATIVE_VALUES;
14862
- }
14863
-
14864
- return n;
14865
- });
14866
- } else {
14867
- valid = undefined;
14868
- }
14869
-
14870
- if (!valid) throw PATH_NOT_VALID;
14871
- if (length > 0 && valid.length < length) throw PATH_NOT_VALID;
14872
- return base ? valid.splice(0, 3) : valid;
14873
- };
14874
-
14875
14887
  const hasHexPrefix = str => str.slice(0, 2).toLowerCase() === '0x';
14876
14888
 
14877
14889
  const stripHexPrefix = str => hasHexPrefix(str) ? str.slice(2) : str;
@@ -15285,8 +15297,8 @@ class BTCGetPublicKey extends BaseMethod {
15285
15297
 
15286
15298
  init() {
15287
15299
  this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
15288
- const hasBundle = Object.prototype.hasOwnProperty.call(this.payload, 'bundle');
15289
- const payload = hasBundle ? this.payload : {
15300
+ this.hasBundle = Object.prototype.hasOwnProperty.call(this.payload, 'bundle');
15301
+ const payload = this.hasBundle ? this.payload : {
15290
15302
  bundle: [this.payload]
15291
15303
  };
15292
15304
  validateParams(payload, [{
@@ -17293,7 +17305,7 @@ let _uiPromises = [];
17293
17305
  let _callPromise;
17294
17306
 
17295
17307
  const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
17296
- var _a;
17308
+ var _a, _b;
17297
17309
 
17298
17310
  if (!message.id || !message.payload || message.type !== IFRAME.CALL) {
17299
17311
  return Promise.reject(TypedError('Method_InvalidParameter', 'onCall: message.id or message.payload is missing'));
@@ -17315,7 +17327,10 @@ const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
17315
17327
  const response = yield method.run();
17316
17328
  return createResponseMessage(method.responseID, true, response);
17317
17329
  } catch (error) {
17318
- return createResponseMessage(method.responseID, false, error);
17330
+ return createResponseMessage(method.responseID, false, {
17331
+ code: error.code,
17332
+ error: (_a = error.message) !== null && _a !== void 0 ? _a : error
17333
+ });
17319
17334
  }
17320
17335
  }
17321
17336
 
@@ -17339,8 +17354,11 @@ const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
17339
17354
  }
17340
17355
 
17341
17356
  Log.debug('Call API - setDevice: ', device);
17342
- (_a = method.setDevice) === null || _a === void 0 ? void 0 : _a.call(method, device);
17357
+ (_b = method.setDevice) === null || _b === void 0 ? void 0 : _b.call(method, device);
17343
17358
  device.on(DEVICE.PIN, onDevicePinHandler);
17359
+ device.on(DEVICE.BUTTON, (d, code) => {
17360
+ onDeviceButtonHandler(d, code);
17361
+ });
17344
17362
 
17345
17363
  try {
17346
17364
  const inner = () => __awaiter(void 0, void 0, void 0, function* () {
@@ -17371,7 +17389,13 @@ const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
17371
17389
  const deviceRun = () => device.run(inner);
17372
17390
 
17373
17391
  _callPromise = create(deviceRun);
17374
- return yield _callPromise.promise;
17392
+
17393
+ try {
17394
+ return yield _callPromise.promise;
17395
+ } catch (e) {
17396
+ console.log('Device Run Error: ', e);
17397
+ return createResponseMessage(method.responseID, false, e.message);
17398
+ }
17375
17399
  } catch (error) {
17376
17400
  messageResponse = createResponseMessage(method.responseID, false, error);
17377
17401
  _callPromise === null || _callPromise === void 0 ? void 0 : _callPromise.reject(TypedError('Call_API', error));
@@ -17383,6 +17407,9 @@ const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
17383
17407
  method.dispose();
17384
17408
  }
17385
17409
  }
17410
+
17411
+ closePopup();
17412
+ cleanup();
17386
17413
  }
17387
17414
  });
17388
17415
 
@@ -17446,6 +17473,15 @@ function initDeviceForBle(method) {
17446
17473
  return device;
17447
17474
  }
17448
17475
 
17476
+ const cleanup = () => {
17477
+ _uiPromises = [];
17478
+ Log.debug('Cleanup...');
17479
+ };
17480
+
17481
+ const closePopup = () => {
17482
+ postMessage(createUiMessage(UI_REQUEST$1.CLOSE_UI_WINDOW));
17483
+ };
17484
+
17449
17485
  const onDevicePinHandler = (...[device, type, callback]) => __awaiter(void 0, void 0, void 0, function* () {
17450
17486
  console.log('onDevicePinHandler');
17451
17487
  const uiPromise = createUiPromise(UI_RESPONSE.RECEIVE_PIN, device);
@@ -17457,6 +17493,15 @@ const onDevicePinHandler = (...[device, type, callback]) => __awaiter(void 0, vo
17457
17493
  callback(null, uiResp.payload);
17458
17494
  });
17459
17495
 
17496
+ const onDeviceButtonHandler = (...[device, request]) => {
17497
+ postMessage(createDeviceMessage(DEVICE.BUTTON, Object.assign(Object.assign({}, request), {
17498
+ device: device.toMessageObject()
17499
+ })));
17500
+ postMessage(createUiMessage(UI_REQUEST$1.REQUEST_BUTTON, {
17501
+ device: device.toMessageObject()
17502
+ }));
17503
+ };
17504
+
17460
17505
  const postMessage = message => {
17461
17506
  _core.emit(CORE_EVENT, message);
17462
17507
  };
@@ -17491,6 +17536,13 @@ class Core extends events.exports {
17491
17536
  break;
17492
17537
  }
17493
17538
 
17539
+ case UI_REQUEST$1.BLUETOOTH_PERMISSION:
17540
+ case UI_REQUEST$1.LOCATION_PERMISSION:
17541
+ {
17542
+ postMessage(message);
17543
+ break;
17544
+ }
17545
+
17494
17546
  case IFRAME.CALL:
17495
17547
  {
17496
17548
  const response = yield callAPI(message);
@@ -17525,7 +17577,7 @@ const init = (settings, Transport) => __awaiter(void 0, void 0, void 0, function
17525
17577
  try {
17526
17578
  yield DataManager.load(settings);
17527
17579
  initTransport(Transport);
17528
- } catch (_b) {
17580
+ } catch (_c) {
17529
17581
  Log.error('DataManager.load error');
17530
17582
  }
17531
17583
 
@@ -17564,6 +17616,7 @@ __webpack_unused_export__ = CORE_EVENT;
17564
17616
  __webpack_unused_export__ = Core;
17565
17617
  __webpack_unused_export__ = DEFAULT_PRIORITY;
17566
17618
  __webpack_unused_export__ = DEVICE;
17619
+ __webpack_unused_export__ = DEVICE_EVENT;
17567
17620
  __webpack_unused_export__ = DataManager;
17568
17621
  exports.Sg = errors;
17569
17622
  exports.Bg = IFRAME;
@@ -17573,6 +17626,7 @@ __webpack_unused_export__ = UI_REQUEST$1;
17573
17626
  __webpack_unused_export__ = UI_RESPONSE;
17574
17627
  __webpack_unused_export__ = corsValidator;
17575
17628
  exports.Ue = create;
17629
+ __webpack_unused_export__ = createDeviceMessage;
17576
17630
  exports.xG = createErrorMessage;
17577
17631
  __webpack_unused_export__ = createIFrameMessage;
17578
17632
  __webpack_unused_export__ = createResponseMessage;
@@ -17580,12 +17634,12 @@ __webpack_unused_export__ = createUiMessage;
17580
17634
  __webpack_unused_export__ = createUiResponse;
17581
17635
  exports.ZP = HardwareSdk;
17582
17636
  exports.yI = enableLog;
17583
- __webpack_unused_export__ = getDeviceBLEFirmwareVersion;
17584
- __webpack_unused_export__ = getDeviceFirmwareVersion;
17585
17637
  __webpack_unused_export__ = getDeviceLabel;
17586
17638
  __webpack_unused_export__ = getDeviceType;
17587
17639
  __webpack_unused_export__ = getDeviceUUID;
17588
17640
  __webpack_unused_export__ = getEnv;
17641
+ __webpack_unused_export__ = getHDPath;
17642
+ __webpack_unused_export__ = getScriptType;
17589
17643
  __webpack_unused_export__ = getTimeStamp;
17590
17644
  __webpack_unused_export__ = httpRequest;
17591
17645
  __webpack_unused_export__ = init;