@onekeyfe/hd-web-sdk 0.0.4 → 0.0.7

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.
@@ -5006,6 +5006,24 @@ const getDeviceType = features => {
5006
5006
  const serialNo = features.serial_no;
5007
5007
  const miniFlag = serialNo.slice(0, 2);
5008
5008
  if (miniFlag.toLowerCase() === 'mi') return 'mini';
5009
+ if (miniFlag.toLowerCase() === 'tc') return 'touch';
5010
+ return 'classic';
5011
+ };
5012
+
5013
+ const getDeviceTypeByBleName = name => {
5014
+ if (!name) return 'classic';
5015
+ if (name.startsWith('MI')) return 'mini';
5016
+ if (name.startsWith('T')) return 'touch';
5017
+ return 'classic';
5018
+ };
5019
+
5020
+ const getDeviceTypeByDeviceId = deviceId => {
5021
+ if (!deviceId) {
5022
+ return 'classic';
5023
+ }
5024
+
5025
+ const miniFlag = deviceId.slice(0, 2);
5026
+ if (miniFlag.toLowerCase() === 'mi') return 'mini';
5009
5027
  return 'classic';
5010
5028
  };
5011
5029
 
@@ -5045,6 +5063,126 @@ const getDeviceBLEFirmwareVersion = features => {
5045
5063
  return features.ble_ver.split('.');
5046
5064
  };
5047
5065
 
5066
+ const HD_HARDENED = 0x80000000;
5067
+
5068
+ const toHardened = n => (n | HD_HARDENED) >>> 0;
5069
+
5070
+ const fromHardened = n => (n & ~HD_HARDENED) >>> 0;
5071
+
5072
+ const PATH_NOT_VALID = TypedError('Method_InvalidParameter', 'Not a valid path');
5073
+ const PATH_NEGATIVE_VALUES = TypedError('Method_InvalidParameter', 'Path cannot contain negative values');
5074
+
5075
+ const getHDPath = path => {
5076
+ const parts = path.toLowerCase().split('/');
5077
+ if (parts[0] !== 'm') throw PATH_NOT_VALID;
5078
+ return parts.filter(p => p !== 'm' && p !== '').map(p => {
5079
+ let hardened = false;
5080
+
5081
+ if (p.substr(p.length - 1) === "'") {
5082
+ hardened = true;
5083
+ p = p.substr(0, p.length - 1);
5084
+ }
5085
+
5086
+ let n = parseInt(p);
5087
+
5088
+ if (Number.isNaN(n)) {
5089
+ throw PATH_NOT_VALID;
5090
+ } else if (n < 0) {
5091
+ throw PATH_NEGATIVE_VALUES;
5092
+ }
5093
+
5094
+ if (hardened) {
5095
+ n = toHardened(n);
5096
+ }
5097
+
5098
+ return n;
5099
+ });
5100
+ };
5101
+
5102
+ const isMultisigPath = path => Array.isArray(path) && path[0] === toHardened(48);
5103
+
5104
+ const isSegwitPath = path => Array.isArray(path) && path[0] === toHardened(49);
5105
+
5106
+ const getScriptType = path => {
5107
+ if (!Array.isArray(path) || path.length < 1) return 'SPENDADDRESS';
5108
+ const p1 = fromHardened(path[0]);
5109
+
5110
+ switch (p1) {
5111
+ case 48:
5112
+ return 'SPENDMULTISIG';
5113
+
5114
+ case 49:
5115
+ return 'SPENDP2SHWITNESS';
5116
+
5117
+ case 84:
5118
+ return 'SPENDWITNESS';
5119
+
5120
+ default:
5121
+ return 'SPENDADDRESS';
5122
+ }
5123
+ };
5124
+
5125
+ const getOutputScriptType = path => {
5126
+ if (!Array.isArray(path) || path.length < 1) return 'PAYTOADDRESS';
5127
+
5128
+ if (path[0] === 49) {
5129
+ return 'PAYTOP2SHWITNESS';
5130
+ }
5131
+
5132
+ const p = fromHardened(path[0]);
5133
+
5134
+ switch (p) {
5135
+ case 48:
5136
+ return 'PAYTOMULTISIG';
5137
+
5138
+ case 49:
5139
+ return 'PAYTOP2SHWITNESS';
5140
+
5141
+ case 84:
5142
+ return 'PAYTOWITNESS';
5143
+
5144
+ default:
5145
+ return 'PAYTOADDRESS';
5146
+ }
5147
+ };
5148
+
5149
+ const serializedPath = path => {
5150
+ const pathStr = path.map(p => {
5151
+ if (p & HD_HARDENED) {
5152
+ return `${p & ~HD_HARDENED}'`;
5153
+ }
5154
+
5155
+ return p;
5156
+ }).join('/');
5157
+ return `m/${pathStr}`;
5158
+ };
5159
+
5160
+ const validatePath = (path, length = 0, base = false) => {
5161
+ let valid;
5162
+
5163
+ if (typeof path === 'string') {
5164
+ valid = getHDPath(path);
5165
+ } else if (Array.isArray(path)) {
5166
+ valid = path.map(p => {
5167
+ const n = parseInt(p);
5168
+
5169
+ if (Number.isNaN(n)) {
5170
+ throw PATH_NOT_VALID;
5171
+ } else if (n < 0) {
5172
+ throw PATH_NEGATIVE_VALUES;
5173
+ }
5174
+
5175
+ return n;
5176
+ });
5177
+ } else {
5178
+ valid = undefined;
5179
+ }
5180
+
5181
+ if (!valid) throw PATH_NOT_VALID;
5182
+ if (length > 0 && valid.length < length) throw PATH_NOT_VALID;
5183
+ return base ? valid.splice(0, 3) : valid;
5184
+ };
5185
+
5048
5186
  var nested = {
5049
5187
  BinanceGetAddress: {
5050
5188
  fields: {
@@ -13949,7 +14087,9 @@ const UI_REQUEST$1 = {
13949
14087
  REQUEST_PIN: 'ui-request_pin',
13950
14088
  INVALID_PIN: 'ui-invalid_pin',
13951
14089
  REQUEST_BUTTON: 'ui-button',
13952
- CLOSE_UI_WINDOW: 'ui-close_window'
14090
+ CLOSE_UI_WINDOW: 'ui-close_window',
14091
+ BLUETOOTH_PERMISSION: 'ui-bluetooth_permission',
14092
+ LOCATION_PERMISSION: 'ui-location_permission'
13953
14093
  };
13954
14094
 
13955
14095
  const createUiMessage = (type, payload) => ({
@@ -14715,10 +14855,14 @@ class SearchDevices extends BaseMethod {
14715
14855
  const env = DataManager.getSettings('env');
14716
14856
 
14717
14857
  if (env === 'react-native') {
14718
- return devicesDescriptor.map(device => Object.assign(Object.assign({}, device), {
14719
- connectId: device.id,
14720
- deviceType: 'classic'
14721
- }));
14858
+ return devicesDescriptor.map(device => {
14859
+ var _a;
14860
+
14861
+ return Object.assign(Object.assign({}, device), {
14862
+ connectId: device.id,
14863
+ deviceType: getDeviceTypeByBleName((_a = device.name) !== null && _a !== void 0 ? _a : '')
14864
+ });
14865
+ });
14722
14866
  }
14723
14867
 
14724
14868
  const devices = [];
@@ -14762,126 +14906,6 @@ class GetFeatures extends BaseMethod {
14762
14906
 
14763
14907
  }
14764
14908
 
14765
- const HD_HARDENED = 0x80000000;
14766
-
14767
- const toHardened = n => (n | HD_HARDENED) >>> 0;
14768
-
14769
- const fromHardened = n => (n & ~HD_HARDENED) >>> 0;
14770
-
14771
- const PATH_NOT_VALID = TypedError('Method_InvalidParameter', 'Not a valid path');
14772
- const PATH_NEGATIVE_VALUES = TypedError('Method_InvalidParameter', 'Path cannot contain negative values');
14773
-
14774
- const getHDPath = path => {
14775
- const parts = path.toLowerCase().split('/');
14776
- if (parts[0] !== 'm') throw PATH_NOT_VALID;
14777
- return parts.filter(p => p !== 'm' && p !== '').map(p => {
14778
- let hardened = false;
14779
-
14780
- if (p.substr(p.length - 1) === "'") {
14781
- hardened = true;
14782
- p = p.substr(0, p.length - 1);
14783
- }
14784
-
14785
- let n = parseInt(p);
14786
-
14787
- if (Number.isNaN(n)) {
14788
- throw PATH_NOT_VALID;
14789
- } else if (n < 0) {
14790
- throw PATH_NEGATIVE_VALUES;
14791
- }
14792
-
14793
- if (hardened) {
14794
- n = toHardened(n);
14795
- }
14796
-
14797
- return n;
14798
- });
14799
- };
14800
-
14801
- const isMultisigPath = path => Array.isArray(path) && path[0] === toHardened(48);
14802
-
14803
- const isSegwitPath = path => Array.isArray(path) && path[0] === toHardened(49);
14804
-
14805
- const getScriptType = path => {
14806
- if (!Array.isArray(path) || path.length < 1) return 'SPENDADDRESS';
14807
- const p1 = fromHardened(path[0]);
14808
-
14809
- switch (p1) {
14810
- case 48:
14811
- return 'SPENDMULTISIG';
14812
-
14813
- case 49:
14814
- return 'SPENDP2SHWITNESS';
14815
-
14816
- case 84:
14817
- return 'SPENDWITNESS';
14818
-
14819
- default:
14820
- return 'SPENDADDRESS';
14821
- }
14822
- };
14823
-
14824
- const getOutputScriptType = path => {
14825
- if (!Array.isArray(path) || path.length < 1) return 'PAYTOADDRESS';
14826
-
14827
- if (path[0] === 49) {
14828
- return 'PAYTOP2SHWITNESS';
14829
- }
14830
-
14831
- const p = fromHardened(path[0]);
14832
-
14833
- switch (p) {
14834
- case 48:
14835
- return 'PAYTOMULTISIG';
14836
-
14837
- case 49:
14838
- return 'PAYTOP2SHWITNESS';
14839
-
14840
- case 84:
14841
- return 'PAYTOWITNESS';
14842
-
14843
- default:
14844
- return 'PAYTOADDRESS';
14845
- }
14846
- };
14847
-
14848
- const serializedPath = path => {
14849
- const pathStr = path.map(p => {
14850
- if (p & HD_HARDENED) {
14851
- return `${p & ~HD_HARDENED}'`;
14852
- }
14853
-
14854
- return p;
14855
- }).join('/');
14856
- return `m/${pathStr}`;
14857
- };
14858
-
14859
- const validatePath = (path, length = 0, base = false) => {
14860
- let valid;
14861
-
14862
- if (typeof path === 'string') {
14863
- valid = getHDPath(path);
14864
- } else if (Array.isArray(path)) {
14865
- valid = path.map(p => {
14866
- const n = parseInt(p);
14867
-
14868
- if (Number.isNaN(n)) {
14869
- throw PATH_NOT_VALID;
14870
- } else if (n < 0) {
14871
- throw PATH_NEGATIVE_VALUES;
14872
- }
14873
-
14874
- return n;
14875
- });
14876
- } else {
14877
- valid = undefined;
14878
- }
14879
-
14880
- if (!valid) throw PATH_NOT_VALID;
14881
- if (length > 0 && valid.length < length) throw PATH_NOT_VALID;
14882
- return base ? valid.splice(0, 3) : valid;
14883
- };
14884
-
14885
14909
  const hasHexPrefix = str => str.slice(0, 2).toLowerCase() === '0x';
14886
14910
 
14887
14911
  const stripHexPrefix = str => hasHexPrefix(str) ? str.slice(2) : str;
@@ -15295,8 +15319,8 @@ class BTCGetPublicKey extends BaseMethod {
15295
15319
 
15296
15320
  init() {
15297
15321
  this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
15298
- const hasBundle = Object.prototype.hasOwnProperty.call(this.payload, 'bundle');
15299
- const payload = hasBundle ? this.payload : {
15322
+ this.hasBundle = Object.prototype.hasOwnProperty.call(this.payload, 'bundle');
15323
+ const payload = this.hasBundle ? this.payload : {
15300
15324
  bundle: [this.payload]
15301
15325
  };
15302
15326
  validateParams(payload, [{
@@ -17303,7 +17327,7 @@ let _uiPromises = [];
17303
17327
  let _callPromise;
17304
17328
 
17305
17329
  const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
17306
- var _a;
17330
+ var _a, _b;
17307
17331
 
17308
17332
  if (!message.id || !message.payload || message.type !== IFRAME.CALL) {
17309
17333
  return Promise.reject(TypedError('Method_InvalidParameter', 'onCall: message.id or message.payload is missing'));
@@ -17325,7 +17349,10 @@ const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
17325
17349
  const response = yield method.run();
17326
17350
  return createResponseMessage(method.responseID, true, response);
17327
17351
  } catch (error) {
17328
- return createResponseMessage(method.responseID, false, error);
17352
+ return createResponseMessage(method.responseID, false, {
17353
+ code: error.code,
17354
+ error: (_a = error.message) !== null && _a !== void 0 ? _a : error
17355
+ });
17329
17356
  }
17330
17357
  }
17331
17358
 
@@ -17349,7 +17376,7 @@ const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
17349
17376
  }
17350
17377
 
17351
17378
  Log.debug('Call API - setDevice: ', device);
17352
- (_a = method.setDevice) === null || _a === void 0 ? void 0 : _a.call(method, device);
17379
+ (_b = method.setDevice) === null || _b === void 0 ? void 0 : _b.call(method, device);
17353
17380
  device.on(DEVICE.PIN, onDevicePinHandler);
17354
17381
  device.on(DEVICE.BUTTON, (d, code) => {
17355
17382
  onDeviceButtonHandler(d, code);
@@ -17384,7 +17411,13 @@ const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
17384
17411
  const deviceRun = () => device.run(inner);
17385
17412
 
17386
17413
  _callPromise = create(deviceRun);
17387
- return yield _callPromise.promise;
17414
+
17415
+ try {
17416
+ return yield _callPromise.promise;
17417
+ } catch (e) {
17418
+ console.log('Device Run Error: ', e);
17419
+ return createResponseMessage(method.responseID, false, e.message);
17420
+ }
17388
17421
  } catch (error) {
17389
17422
  messageResponse = createResponseMessage(method.responseID, false, error);
17390
17423
  _callPromise === null || _callPromise === void 0 ? void 0 : _callPromise.reject(TypedError('Call_API', error));
@@ -17525,6 +17558,13 @@ class Core extends events.exports {
17525
17558
  break;
17526
17559
  }
17527
17560
 
17561
+ case UI_REQUEST$1.BLUETOOTH_PERMISSION:
17562
+ case UI_REQUEST$1.LOCATION_PERMISSION:
17563
+ {
17564
+ postMessage(message);
17565
+ break;
17566
+ }
17567
+
17528
17568
  case IFRAME.CALL:
17529
17569
  {
17530
17570
  const response = yield callAPI(message);
@@ -17559,7 +17599,7 @@ const init = (settings, Transport) => __awaiter(void 0, void 0, void 0, function
17559
17599
  try {
17560
17600
  yield DataManager.load(settings);
17561
17601
  initTransport(Transport);
17562
- } catch (_b) {
17602
+ } catch (_c) {
17563
17603
  Log.error('DataManager.load error');
17564
17604
  }
17565
17605
 
@@ -17616,12 +17656,14 @@ __webpack_unused_export__ = createUiMessage;
17616
17656
  __webpack_unused_export__ = createUiResponse;
17617
17657
  exports.ZP = HardwareSdk;
17618
17658
  exports.yI = enableLog;
17619
- __webpack_unused_export__ = getDeviceBLEFirmwareVersion;
17620
- __webpack_unused_export__ = getDeviceFirmwareVersion;
17621
17659
  __webpack_unused_export__ = getDeviceLabel;
17622
17660
  __webpack_unused_export__ = getDeviceType;
17661
+ __webpack_unused_export__ = getDeviceTypeByBleName;
17662
+ __webpack_unused_export__ = getDeviceTypeByDeviceId;
17623
17663
  __webpack_unused_export__ = getDeviceUUID;
17624
17664
  __webpack_unused_export__ = getEnv;
17665
+ __webpack_unused_export__ = getHDPath;
17666
+ __webpack_unused_export__ = getScriptType;
17625
17667
  __webpack_unused_export__ = getTimeStamp;
17626
17668
  __webpack_unused_export__ = httpRequest;
17627
17669
  __webpack_unused_export__ = init;
@@ -17877,6 +17919,7 @@ const MESSAGE_TOP_CHAR = 0x003f;
17877
17919
  const MESSAGE_HEADER_BYTE = 0x23;
17878
17920
  const HEADER_SIZE = 1 + 1 + 4 + 2;
17879
17921
  const BUFFER_SIZE = 63;
17922
+ const COMMON_HEADER_SIZE = 6;
17880
17923
 
17881
17924
  const readHeader = buffer => {
17882
17925
  const typeId = buffer.readUint16();
@@ -18645,6 +18688,7 @@ var index = {
18645
18688
  parseConfigure
18646
18689
  };
18647
18690
  exports.BUFFER_SIZE = BUFFER_SIZE;
18691
+ exports.COMMON_HEADER_SIZE = COMMON_HEADER_SIZE;
18648
18692
  exports.HEADER_SIZE = HEADER_SIZE;
18649
18693
  exports.MESSAGE_HEADER_BYTE = MESSAGE_HEADER_BYTE;
18650
18694
  exports.MESSAGE_TOP_CHAR = MESSAGE_TOP_CHAR;