@onekeyfe/hd-core 0.1.52 → 0.1.53

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.
@@ -1,9 +1,9 @@
1
1
  import { AptosGetAddress as HardwareAptosGetAddress } from '@onekeyfe/hd-transport';
2
2
  import { BaseMethod } from '../BaseMethod';
3
- import { AptosAddress } from '../../types';
4
3
  export default class AptosGetAddress extends BaseMethod<HardwareAptosGetAddress[]> {
5
4
  hasBundle: boolean;
6
5
  init(): void;
7
- run(): Promise<AptosAddress | AptosAddress[]>;
6
+ publicKeyToAddress(publicKey: string): string;
7
+ run(): Promise<any>;
8
8
  }
9
9
  //# sourceMappingURL=AptosGetAddress.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AptosGetAddress.d.ts","sourceRoot":"","sources":["../../../src/api/aptos/AptosGetAddress.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,IAAI,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAGpF,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,EAAE,YAAY,EAAyB,MAAM,aAAa,CAAC;AAElE,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,UAAU,CAAC,uBAAuB,EAAE,CAAC;IAChF,SAAS,UAAS;IAElB,IAAI;IA6BE,GAAG;CAoBV"}
1
+ {"version":3,"file":"AptosGetAddress.d.ts","sourceRoot":"","sources":["../../../src/api/aptos/AptosGetAddress.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,IAAI,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAGpF,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAO3C,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,UAAU,CAAC,uBAAuB,EAAE,CAAC;IAChF,SAAS,UAAS;IAElB,IAAI;IA6BJ,kBAAkB,CAAC,SAAS,EAAE,MAAM;IAO9B,GAAG;CAkCV"}
@@ -4,4 +4,6 @@ export declare const addHexPrefix: (str: string) => string;
4
4
  export declare const isHexString: (value: string, length?: number | undefined) => boolean;
5
5
  export declare const stripHexStartZeroes: (str: string) => string;
6
6
  export declare const formatAnyHex: (value: any) => any;
7
+ export declare function bytesToHex(uint8a: Uint8Array): string;
8
+ export declare function hexToBytes(hex: string): Uint8Array;
7
9
  //# sourceMappingURL=hexUtils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hexUtils.d.ts","sourceRoot":"","sources":["../../../src/api/helpers/hexUtils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,QAAS,MAAM,KAAG,OAAiD,CAAC;AAG7F,eAAO,MAAM,cAAc,QAAS,MAAM,KAAG,MAAkD,CAAC;AAGhG,eAAO,MAAM,YAAY,QAAS,MAAM,KAAG,MAAgD,CAAC;AAE5F,eAAO,MAAM,WAAW,UAAW,MAAM,yCAQxC,CAAC;AAEF,eAAO,MAAM,mBAAmB,QAAS,MAAM,WAK9C,CAAC;AAQF,eAAO,MAAM,YAAY,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,GAiB1C,CAAC"}
1
+ {"version":3,"file":"hexUtils.d.ts","sourceRoot":"","sources":["../../../src/api/helpers/hexUtils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,QAAS,MAAM,KAAG,OAAiD,CAAC;AAG7F,eAAO,MAAM,cAAc,QAAS,MAAM,KAAG,MAAkD,CAAC;AAGhG,eAAO,MAAM,YAAY,QAAS,MAAM,KAAG,MAAgD,CAAC;AAE5F,eAAO,MAAM,WAAW,UAAW,MAAM,yCAQxC,CAAC;AAEF,eAAO,MAAM,mBAAmB,QAAS,MAAM,WAK9C,CAAC;AAQF,eAAO,MAAM,YAAY,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,GAiB1C,CAAC;AAMF,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAQrD;AAKD,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAclD"}
package/dist/index.d.ts CHANGED
@@ -1030,6 +1030,7 @@ declare function nearSignTransaction(connectId: string, deviceId: string, params
1030
1030
 
1031
1031
  declare type AptosAddress = {
1032
1032
  path: string;
1033
+ publicKey?: string;
1033
1034
  } & AptosAddress$1;
1034
1035
  declare type AptosGetAddressParams = {
1035
1036
  path: string | number[];
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ var axios = require('axios');
8
8
  var BigNumber = require('bignumber.js');
9
9
  var sha256 = require('js-sha256');
10
10
  var hdTransport = require('@onekeyfe/hd-transport');
11
+ var sha3 = require('js-sha3');
11
12
 
12
13
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
 
@@ -15,6 +16,7 @@ var semver__default = /*#__PURE__*/_interopDefaultLegacy(semver);
15
16
  var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
16
17
  var BigNumber__default = /*#__PURE__*/_interopDefaultLegacy(BigNumber);
17
18
  var sha256__default = /*#__PURE__*/_interopDefaultLegacy(sha256);
19
+ var sha3__default = /*#__PURE__*/_interopDefaultLegacy(sha3);
18
20
 
19
21
  const inject = ({ call, cancel, dispose, eventEmitter, init, uiResponse, }) => {
20
22
  const api = {
@@ -832,6 +834,12 @@ const getPassphraseState = (features, commands) => __awaiter(void 0, void 0, voi
832
834
  });
833
835
  return message.address;
834
836
  });
837
+ const supportBatchPublicKey = (features) => {
838
+ if (!features)
839
+ return false;
840
+ const currentVersion = getDeviceFirmwareVersion(features).join('.');
841
+ return semver__default["default"].gte(currentVersion, '2.6.0');
842
+ };
835
843
 
836
844
  var nested = {
837
845
  AptosGetAddress: {
@@ -11867,6 +11875,24 @@ const formatAnyHex = value => {
11867
11875
  }
11868
11876
  return value;
11869
11877
  };
11878
+ Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0'));
11879
+ function hexToBytes(hex) {
11880
+ if (typeof hex !== 'string') {
11881
+ throw new TypeError(`hexToBytes: expected string, got ${typeof hex}`);
11882
+ }
11883
+ if (hex.length % 2)
11884
+ throw new Error('hexToBytes: received invalid unpadded hex');
11885
+ const array = new Uint8Array(hex.length / 2);
11886
+ for (let i = 0; i < array.length; i++) {
11887
+ const j = i * 2;
11888
+ const hexByte = hex.slice(j, j + 2);
11889
+ const byte = Number.parseInt(hexByte, 16);
11890
+ if (Number.isNaN(byte) || byte < 0)
11891
+ throw new Error('Invalid byte sequence');
11892
+ array[i] = byte;
11893
+ }
11894
+ return array;
11895
+ }
11870
11896
 
11871
11897
  const invalidParameter = (message) => hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, message);
11872
11898
  const validateParams = (values, fields) => {
@@ -14863,6 +14889,7 @@ class NearSignTransaction extends BaseMethod {
14863
14889
  }
14864
14890
  }
14865
14891
 
14892
+ const { sha3_256: sha3Hash } = sha3__default["default"];
14866
14893
  class AptosGetAddress extends BaseMethod {
14867
14894
  constructor() {
14868
14895
  super(...arguments);
@@ -14890,8 +14917,27 @@ class AptosGetAddress extends BaseMethod {
14890
14917
  });
14891
14918
  });
14892
14919
  }
14920
+ publicKeyToAddress(publicKey) {
14921
+ const hash = sha3Hash.create();
14922
+ hash.update(hexToBytes(publicKey));
14923
+ hash.update("\x00");
14924
+ return hash.hex();
14925
+ }
14893
14926
  run() {
14927
+ var _a;
14894
14928
  return __awaiter(this, void 0, void 0, function* () {
14929
+ if (this.hasBundle && supportBatchPublicKey((_a = this.device) === null || _a === void 0 ? void 0 : _a.features)) {
14930
+ const res = yield this.device.commands.typedCall('BatchGetPublickeys', 'EcdsaPublicKeys', {
14931
+ paths: this.params,
14932
+ ecdsa_curve_name: 'ed25519',
14933
+ });
14934
+ const result = res.message.public_keys.map((publicKey, index) => ({
14935
+ path: serializedPath(this.params[index].address_n),
14936
+ publicKey,
14937
+ address: this.publicKeyToAddress(publicKey),
14938
+ }));
14939
+ return Promise.resolve(result);
14940
+ }
14895
14941
  const responses = [];
14896
14942
  for (let i = 0; i < this.params.length; i++) {
14897
14943
  const param = this.params[i];
@@ -2,6 +2,7 @@ import { AptosAddress as HardwareAptosAddress } from '@onekeyfe/hd-transport';
2
2
  import type { CommonParams, Response } from '../params';
3
3
  export declare type AptosAddress = {
4
4
  path: string;
5
+ publicKey?: string;
5
6
  } & HardwareAptosAddress;
6
7
  export declare type AptosGetAddressParams = {
7
8
  path: string | number[];
@@ -1 +1 @@
1
- {"version":3,"file":"aptosGetAddress.d.ts","sourceRoot":"","sources":["../../../src/types/api/aptosGetAddress.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,IAAI,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAExD,oBAAY,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,oBAAoB,CAAC;AAEzB,oBAAY,qBAAqB,GAAG;IAClC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,YAAY,GAAG,qBAAqB,GAC3C,QAAQ,CAAC,YAAY,CAAC,CAAC;AAE1B,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,YAAY,GAAG;IAAE,MAAM,CAAC,EAAE,qBAAqB,EAAE,CAAA;CAAE,GAC1D,QAAQ,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC"}
1
+ {"version":3,"file":"aptosGetAddress.d.ts","sourceRoot":"","sources":["../../../src/types/api/aptosGetAddress.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,IAAI,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAExD,oBAAY,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,GAAG,oBAAoB,CAAC;AAEzB,oBAAY,qBAAqB,GAAG;IAClC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,YAAY,GAAG,qBAAqB,GAC3C,QAAQ,CAAC,YAAY,CAAC,CAAC;AAE1B,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,YAAY,GAAG;IAAE,MAAM,CAAC,EAAE,qBAAqB,EAAE,CAAA;CAAE,GAC1D,QAAQ,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC"}
@@ -12,4 +12,5 @@ export declare const getDeviceBLEFirmwareVersion: (features: Features) => IVersi
12
12
  export declare const supportInputPinOnSoftware: (features: Features) => SupportFeatureType;
13
13
  export declare const supportNewPassphrase: (features?: import("packages/hd-transport/dist").Features | undefined) => SupportFeatureType;
14
14
  export declare const getPassphraseState: (features: Features, commands: DeviceCommands) => Promise<string | false>;
15
+ export declare const supportBatchPublicKey: (features?: import("packages/hd-transport/dist").Features | undefined) => boolean;
15
16
  //# sourceMappingURL=deviceFeaturesUtils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"deviceFeaturesUtils.d.ts","sourceRoot":"","sources":["../../src/utils/deviceFeaturesUtils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EACV,QAAQ,EACR,aAAa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AO,MAAM,aAAa,aAAc,QAAQ,WAM/C,CAAC;AAEF,eAAO,MAAM,cAAc,aAAc,QAAQ,WAOhD,CAAC;AAKF,eAAO,MAAM,wBAAwB,aAAc,QAAQ,GAAG,SAAS,kBAOtE,CAAC;AAKF,eAAO,MAAM,2BAA2B,aAAc,QAAQ,KAAG,aAAa,GAAG,IAQhF,CAAC;AAEF,eAAO,MAAM,yBAAyB,aAAc,QAAQ,KAAG,kBAU9D,CAAC;AAEF,eAAO,MAAM,oBAAoB,4EAA0B,kBAW1D,CAAC;AAEF,eAAO,MAAM,kBAAkB,aAAoB,QAAQ,YAAY,cAAc,4BAUpF,CAAC;AAEF,eAAO,MAAM,qBAAqB,4EAA0B,OAY3D,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-core",
3
- "version": "0.1.52",
3
+ "version": "0.1.53",
4
4
  "description": "> TODO: description",
5
5
  "author": "OneKey",
6
6
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
@@ -24,11 +24,12 @@
24
24
  "url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
25
25
  },
26
26
  "dependencies": {
27
- "@onekeyfe/hd-shared": "^0.1.52",
28
- "@onekeyfe/hd-transport": "^0.1.52",
27
+ "@onekeyfe/hd-shared": "^0.1.53",
28
+ "@onekeyfe/hd-transport": "^0.1.53",
29
29
  "axios": "^0.27.2",
30
30
  "bignumber.js": "^9.0.2",
31
31
  "js-sha256": "^0.9.0",
32
+ "js-sha3": "^0.8.0",
32
33
  "parse-uri": "^1.0.7",
33
34
  "semver": "^7.3.7"
34
35
  },
@@ -36,5 +37,5 @@
36
37
  "@types/parse-uri": "^1.0.0",
37
38
  "@types/semver": "^7.3.9"
38
39
  },
39
- "gitHead": "7a53d20af1efe25b5f94c1204bb1679cfeb16666"
40
+ "gitHead": "33a915c10a4f664e60f0eacc6b75bfc913b0e8e3"
40
41
  }
@@ -4,7 +4,10 @@ import { serializedPath, validatePath } from '../helpers/pathUtils';
4
4
  import { BaseMethod } from '../BaseMethod';
5
5
  import { validateParams } from '../helpers/paramsValidator';
6
6
  import { AptosAddress, AptosGetAddressParams } from '../../types';
7
-
7
+ import { supportBatchPublicKey } from '../../utils/deviceFeaturesUtils';
8
+ import sha3 from "js-sha3";
9
+ import { hexToBytes } from '../helpers/hexUtils';
10
+ const { sha3_256: sha3Hash } = sha3;
8
11
  export default class AptosGetAddress extends BaseMethod<HardwareAptosGetAddress[]> {
9
12
  hasBundle = false;
10
13
 
@@ -37,9 +40,30 @@ export default class AptosGetAddress extends BaseMethod<HardwareAptosGetAddress[
37
40
  });
38
41
  }
39
42
 
43
+ publicKeyToAddress(publicKey: string) {
44
+ const hash = sha3Hash.create();
45
+ hash.update(hexToBytes(publicKey));
46
+ hash.update("\x00");
47
+ return hash.hex();
48
+ }
49
+
40
50
  async run() {
41
- const responses: AptosAddress[] = [];
51
+ if (this.hasBundle && supportBatchPublicKey(this.device?.features)) {
52
+ // @ts-expect-error
53
+ const res = await this.device.commands.typedCall('BatchGetPublickeys', 'EcdsaPublicKeys', {
54
+ paths: this.params,
55
+ ecdsa_curve_name: 'ed25519',
56
+ });
57
+ // @ts-expect-error
58
+ const result = res.message.public_keys.map((publicKey: string, index: number) => ({
59
+ path: serializedPath((this.params as unknown as any[])[index].address_n),
60
+ publicKey,
61
+ address: this.publicKeyToAddress(publicKey),
62
+ }));
63
+ return Promise.resolve(result);
64
+ }
42
65
 
66
+ const responses: AptosAddress[] = [];
43
67
  for (let i = 0; i < this.params.length; i++) {
44
68
  const param = this.params[i];
45
69
 
@@ -47,3 +47,36 @@ export const formatAnyHex: (value: any) => any = value => {
47
47
 
48
48
  return value;
49
49
  };
50
+
51
+ const hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0'));
52
+ /**
53
+ * @example bytesToHex(Uint8Array.from([0xde, 0xad, 0xbe, 0xef]))
54
+ */
55
+ export function bytesToHex(uint8a: Uint8Array): string {
56
+ // pre-caching improves the speed 6x
57
+ if (!(uint8a instanceof Uint8Array)) throw new Error('Uint8Array expected');
58
+ let hex = '';
59
+ for (let i = 0; i < uint8a.length; i++) {
60
+ hex += hexes[uint8a[i]];
61
+ }
62
+ return hex;
63
+ }
64
+
65
+ /**
66
+ * @example hexToBytes('deadbeef')
67
+ */
68
+ export function hexToBytes(hex: string): Uint8Array {
69
+ if (typeof hex !== 'string') {
70
+ throw new TypeError(`hexToBytes: expected string, got ${typeof hex}`);
71
+ }
72
+ if (hex.length % 2) throw new Error('hexToBytes: received invalid unpadded hex');
73
+ const array = new Uint8Array(hex.length / 2);
74
+ for (let i = 0; i < array.length; i++) {
75
+ const j = i * 2;
76
+ const hexByte = hex.slice(j, j + 2);
77
+ const byte = Number.parseInt(hexByte, 16);
78
+ if (Number.isNaN(byte) || byte < 0) throw new Error('Invalid byte sequence');
79
+ array[i] = byte;
80
+ }
81
+ return array;
82
+ }
@@ -3,6 +3,7 @@ import type { CommonParams, Response } from '../params';
3
3
 
4
4
  export type AptosAddress = {
5
5
  path: string;
6
+ publicKey?: string;
6
7
  } & HardwareAptosAddress;
7
8
 
8
9
  export type AptosGetAddressParams = {
@@ -131,3 +131,17 @@ export const getPassphraseState = async (features: Features, commands: DeviceCom
131
131
 
132
132
  return message.address;
133
133
  };
134
+
135
+ export const supportBatchPublicKey = (features?: Features): boolean => {
136
+ if (!features) return false;
137
+
138
+ // TODO: support batch public key for touch
139
+ // const deviceType = getDeviceType(features);
140
+ // if (deviceType === 'touch' || deviceType === 'pro') {
141
+ // return true;
142
+ // }
143
+
144
+ const currentVersion = getDeviceFirmwareVersion(features).join('.');
145
+
146
+ return semver.gte(currentVersion, '2.6.0');
147
+ };