@onekeyfe/hd-core 1.1.2 → 1.1.3-alpha.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/dist/api/BaseMethod.d.ts +2 -0
- package/dist/api/BaseMethod.d.ts.map +1 -1
- package/dist/api/allnetwork/AllNetworkGetAddress.d.ts +4 -28
- package/dist/api/allnetwork/AllNetworkGetAddress.d.ts.map +1 -1
- package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts +47 -0
- package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -0
- package/dist/api/allnetwork/AllNetworkGetAddressByLoop.d.ts +8 -0
- package/dist/api/allnetwork/AllNetworkGetAddressByLoop.d.ts.map +1 -0
- package/dist/api/btc/BTCGetPublicKey.d.ts.map +1 -1
- package/dist/api/cosmos/CosmosGetPublicKey.d.ts +1 -1
- package/dist/api/cosmos/CosmosGetPublicKey.d.ts.map +1 -1
- package/dist/api/device/DeviceUnlock.d.ts +0 -2
- package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
- package/dist/api/evm/EVMGetPublicKey.d.ts +1 -0
- package/dist/api/evm/EVMGetPublicKey.d.ts.map +1 -1
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/core/RequestQueue.d.ts +4 -0
- package/dist/core/RequestQueue.d.ts.map +1 -1
- package/dist/core/index.d.ts +3 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/device/Device.d.ts +4 -1
- package/dist/device/Device.d.ts.map +1 -1
- package/dist/device/DeviceCommands.d.ts +4 -4
- package/dist/events/call.d.ts +9 -0
- package/dist/events/call.d.ts.map +1 -1
- package/dist/events/core.d.ts +2 -2
- package/dist/events/core.d.ts.map +1 -1
- package/dist/events/iframe.d.ts +11 -1
- package/dist/events/iframe.d.ts.map +1 -1
- package/dist/index.d.ts +40 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +393 -140
- package/dist/inject.d.ts +3 -0
- package/dist/inject.d.ts.map +1 -1
- package/dist/types/api/allNetworkGetAddress.d.ts +9 -2
- package/dist/types/api/allNetworkGetAddress.d.ts.map +1 -1
- package/dist/types/api/evmGetPublicKey.d.ts +1 -0
- package/dist/types/api/evmGetPublicKey.d.ts.map +1 -1
- package/dist/types/api/index.d.ts +2 -1
- package/dist/types/api/index.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/api/BaseMethod.ts +3 -0
- package/src/api/allnetwork/AllNetworkGetAddress.ts +14 -407
- package/src/api/allnetwork/AllNetworkGetAddressBase.ts +473 -0
- package/src/api/allnetwork/AllNetworkGetAddressByLoop.ts +151 -0
- package/src/api/btc/BTCGetPublicKey.ts +13 -0
- package/src/api/cosmos/CosmosGetPublicKey.ts +1 -1
- package/src/api/device/DeviceUnlock.ts +1 -48
- package/src/api/evm/EVMGetPublicKey.ts +9 -3
- package/src/api/index.ts +1 -0
- package/src/core/RequestQueue.ts +26 -0
- package/src/core/index.ts +29 -3
- package/src/device/Device.ts +64 -0
- package/src/events/call.ts +10 -0
- package/src/events/core.ts +7 -1
- package/src/events/iframe.ts +12 -1
- package/src/index.ts +2 -1
- package/src/inject.ts +47 -0
- package/src/types/api/allNetworkGetAddress.ts +16 -2
- package/src/types/api/evmGetPublicKey.ts +1 -0
- package/src/types/api/index.ts +2 -1
|
@@ -1,59 +1,12 @@
|
|
|
1
1
|
import { LockDevice } from '@onekeyfe/hd-transport';
|
|
2
|
-
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
3
|
-
import semver from 'semver';
|
|
4
2
|
import { BaseMethod } from '../BaseMethod';
|
|
5
|
-
import { toHardened } from '../helpers/pathUtils';
|
|
6
|
-
import { DeviceFirmwareRange } from '../../types';
|
|
7
|
-
import { getDeviceFirmwareVersion, getMethodVersionRange } from '../../utils';
|
|
8
3
|
|
|
9
4
|
export default class DeviceUnlock extends BaseMethod<LockDevice> {
|
|
10
5
|
init() {
|
|
11
6
|
this.useDevicePassphraseState = false;
|
|
12
7
|
}
|
|
13
8
|
|
|
14
|
-
supportUnlockVersionRange(): DeviceFirmwareRange {
|
|
15
|
-
return {
|
|
16
|
-
pro: {
|
|
17
|
-
min: '4.15.0',
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
9
|
async run() {
|
|
23
|
-
|
|
24
|
-
const versionRange = getMethodVersionRange(
|
|
25
|
-
this.device.features,
|
|
26
|
-
type => this.supportUnlockVersionRange()[type]
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
if (versionRange && semver.gte(firmwareVersion, versionRange.min)) {
|
|
30
|
-
const res = await this.device.commands.typedCall('UnLockDevice', 'UnLockDeviceResponse');
|
|
31
|
-
if (this.device.features) {
|
|
32
|
-
this.device.features.unlocked = res.message.unlocked == null ? null : res.message.unlocked;
|
|
33
|
-
this.device.features.unlocked_attach_pin =
|
|
34
|
-
res.message.unlocked_attach_pin == null ? undefined : res.message.unlocked_attach_pin;
|
|
35
|
-
this.device.features.passphrase_protection =
|
|
36
|
-
res.message.passphrase_protection == null ? null : res.message.passphrase_protection;
|
|
37
|
-
|
|
38
|
-
return Promise.resolve(this.device.features);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const featuresRes = await this.device.commands.typedCall('GetFeatures', 'Features');
|
|
42
|
-
return Promise.resolve(featuresRes.message);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const { type } = await this.device.commands.typedCall('GetAddress', 'Address', {
|
|
46
|
-
address_n: [toHardened(44), toHardened(1), toHardened(0), 0, 0],
|
|
47
|
-
coin_name: 'Testnet',
|
|
48
|
-
script_type: 'SPENDADDRESS',
|
|
49
|
-
show_display: false,
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// @ts-expect-error
|
|
53
|
-
if (type === 'CallMethodError') {
|
|
54
|
-
throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'Get the passphrase state error');
|
|
55
|
-
}
|
|
56
|
-
const res = await this.device.commands.typedCall('GetFeatures', 'Features');
|
|
57
|
-
return Promise.resolve(res.message);
|
|
10
|
+
return this.device.unlockDevice();
|
|
58
11
|
}
|
|
59
12
|
}
|
|
@@ -13,6 +13,8 @@ import { batchGetPublickeys } from '../helpers/batchGetPublickeys';
|
|
|
13
13
|
export default class EVMGetPublicKey extends BaseMethod<EthereumGetPublicKeyOneKey[]> {
|
|
14
14
|
hasBundle = false;
|
|
15
15
|
|
|
16
|
+
confirmShowOnOneKey = false;
|
|
17
|
+
|
|
16
18
|
useBatch = false;
|
|
17
19
|
|
|
18
20
|
init() {
|
|
@@ -20,9 +22,13 @@ export default class EVMGetPublicKey extends BaseMethod<EthereumGetPublicKeyOneK
|
|
|
20
22
|
this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.NOT_INITIALIZE];
|
|
21
23
|
|
|
22
24
|
this.hasBundle = !!this.payload?.bundle;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
|
|
26
|
+
this.confirmShowOnOneKey = this.payload?.bundle?.some(
|
|
27
|
+
(item: EVMGetPublicKeyParams) => !!item.showOnOneKey
|
|
25
28
|
);
|
|
29
|
+
|
|
30
|
+
this.useBatch = !this.confirmShowOnOneKey && this.hasBundle && this.payload.useBatch;
|
|
31
|
+
|
|
26
32
|
const payload = this.hasBundle ? this.payload : { bundle: [this.payload] };
|
|
27
33
|
|
|
28
34
|
// check payload
|
|
@@ -66,7 +72,7 @@ export default class EVMGetPublicKey extends BaseMethod<EthereumGetPublicKeyOneK
|
|
|
66
72
|
async run() {
|
|
67
73
|
const responses: EVMPublicKey[] = [];
|
|
68
74
|
|
|
69
|
-
if (this.useBatch &&
|
|
75
|
+
if (this.useBatch && supportBatchPublicKey(this.device?.features)) {
|
|
70
76
|
try {
|
|
71
77
|
const res = await batchGetPublickeys(this.device, this.params, 'secp256k1', 60, {
|
|
72
78
|
includeNode: false,
|
package/src/api/index.ts
CHANGED
|
@@ -41,6 +41,7 @@ export { default as promptWebDeviceAccess } from './PromptWebDeviceAccess';
|
|
|
41
41
|
export { default as cipherKeyValue } from './CipherKeyValue';
|
|
42
42
|
|
|
43
43
|
export { default as allNetworkGetAddress } from './allnetwork/AllNetworkGetAddress';
|
|
44
|
+
export { default as allNetworkGetAddressByLoop } from './allnetwork/AllNetworkGetAddressByLoop';
|
|
44
45
|
|
|
45
46
|
export { default as btcGetAddress } from './btc/BTCGetAddress';
|
|
46
47
|
export { default as btcGetPublicKey } from './btc/BTCGetPublicKey';
|
package/src/core/RequestQueue.ts
CHANGED
|
@@ -13,6 +13,8 @@ export type RequestTask = {
|
|
|
13
13
|
export default class RequestQueue {
|
|
14
14
|
private requestQueue = new Map<number, RequestTask>();
|
|
15
15
|
|
|
16
|
+
private pendingCallbackTasks = new Map<string, Deferred<void>>();
|
|
17
|
+
|
|
16
18
|
// 生成唯一请求ID
|
|
17
19
|
public generateRequestId = (method?: BaseMethod) => {
|
|
18
20
|
if (method && method.responseID != null) {
|
|
@@ -104,4 +106,28 @@ export default class RequestQueue {
|
|
|
104
106
|
public releaseTask(requestId: number) {
|
|
105
107
|
this.requestQueue.delete(requestId);
|
|
106
108
|
}
|
|
109
|
+
|
|
110
|
+
public registerPendingCallbackTask(connectId: string, callbackPromise: Deferred<void>) {
|
|
111
|
+
this.pendingCallbackTasks.set(connectId, callbackPromise);
|
|
112
|
+
|
|
113
|
+
callbackPromise.promise.finally(() => {
|
|
114
|
+
Log.debug(`Callback task completed for connectId: ${connectId}`);
|
|
115
|
+
this.pendingCallbackTasks.delete(connectId);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public async waitForPendingCallbackTasks(connectId: string): Promise<void> {
|
|
120
|
+
const pendingTask = this.pendingCallbackTasks.get(connectId);
|
|
121
|
+
if (pendingTask) {
|
|
122
|
+
Log.debug(`Waiting for pending callback task to complete for connectId: ${connectId}`);
|
|
123
|
+
await pendingTask.promise;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public cancelCallbackTasks(connectId: string) {
|
|
128
|
+
const pendingTask = this.pendingCallbackTasks.get(connectId);
|
|
129
|
+
if (pendingTask) {
|
|
130
|
+
pendingTask.resolve();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
107
133
|
}
|
package/src/core/index.ts
CHANGED
|
@@ -52,7 +52,7 @@ import { getSynchronize } from '../utils/getSynchronize';
|
|
|
52
52
|
|
|
53
53
|
const Log = getLogger(LoggerNames.Core);
|
|
54
54
|
|
|
55
|
-
type CoreContext = ReturnType<Core['getCoreContext']>;
|
|
55
|
+
export type CoreContext = ReturnType<Core['getCoreContext']>;
|
|
56
56
|
|
|
57
57
|
function hasDeriveCardano(method: BaseMethod): boolean {
|
|
58
58
|
if (
|
|
@@ -195,6 +195,11 @@ const onCallDevice = async (
|
|
|
195
195
|
DevicePool.clearDeviceCache(method.payload.connectId);
|
|
196
196
|
}
|
|
197
197
|
|
|
198
|
+
// wait for previous callback tasks to complete (ensure device does not call concurrently)
|
|
199
|
+
if (method.connectId) {
|
|
200
|
+
await context.waitForCallbackTasks(method.connectId);
|
|
201
|
+
}
|
|
202
|
+
|
|
198
203
|
await waitForPendingPromise(getPrePendingCallPromise, setPrePendingCallPromise);
|
|
199
204
|
|
|
200
205
|
const task = requestQueue.createTask(method);
|
|
@@ -225,6 +230,7 @@ const onCallDevice = async (
|
|
|
225
230
|
|
|
226
231
|
Log.debug('Call API - setDevice: ', device.mainId);
|
|
227
232
|
method.setDevice?.(device);
|
|
233
|
+
method.context = context;
|
|
228
234
|
|
|
229
235
|
device.on(DEVICE.PIN, onDevicePinHandler);
|
|
230
236
|
device.on(DEVICE.BUTTON, onDeviceButtonHandler);
|
|
@@ -240,6 +246,10 @@ const onCallDevice = async (
|
|
|
240
246
|
);
|
|
241
247
|
|
|
242
248
|
try {
|
|
249
|
+
if (method.connectId) {
|
|
250
|
+
await context.waitForCallbackTasks(method.connectId);
|
|
251
|
+
}
|
|
252
|
+
|
|
243
253
|
await waitForPendingPromise(getPrePendingCallPromise, setPrePendingCallPromise);
|
|
244
254
|
|
|
245
255
|
const inner = async (): Promise<void> => {
|
|
@@ -710,6 +720,10 @@ export const cancel = (context: CoreContext, connectId?: string) => {
|
|
|
710
720
|
// }
|
|
711
721
|
// setPrePendingCallPromise(device?.interruptionFromUser());
|
|
712
722
|
// requestQueue.abortRequestsByConnectId(connectId);
|
|
723
|
+
|
|
724
|
+
// cancel callback tasks
|
|
725
|
+
requestQueue.cancelCallbackTasks(connectId);
|
|
726
|
+
|
|
713
727
|
const requestIds = requestQueue.getRequestTasksId();
|
|
714
728
|
Log.debug(
|
|
715
729
|
`Cancel Api connect requestQueues: length:${requestIds.length} requestIds:${requestIds.join(
|
|
@@ -843,7 +857,7 @@ const onDevicePinHandler = async (...[device, type, callback]: DeviceEvents['pin
|
|
|
843
857
|
callback(null, uiResp.payload);
|
|
844
858
|
};
|
|
845
859
|
|
|
846
|
-
const onDeviceButtonHandler = (...[device, request]: [...DeviceEvents['button']]) => {
|
|
860
|
+
export const onDeviceButtonHandler = (...[device, request]: [...DeviceEvents['button']]) => {
|
|
847
861
|
postMessage(createDeviceMessage(DEVICE.BUTTON, { ...request, device: device.toMessageObject() }));
|
|
848
862
|
|
|
849
863
|
if (request.code === 'ButtonRequest_PinEntry' || request.code === 'ButtonRequest_AttachPin') {
|
|
@@ -947,7 +961,7 @@ const removeUiPromise = (promise: Deferred<any>) => {
|
|
|
947
961
|
export default class Core extends EventEmitter {
|
|
948
962
|
private requestQueue = new RequestQueue();
|
|
949
963
|
|
|
950
|
-
//
|
|
964
|
+
// background task
|
|
951
965
|
private prePendingCallPromise: Promise<void> | undefined;
|
|
952
966
|
|
|
953
967
|
private methodSynchronize = getSynchronize();
|
|
@@ -960,6 +974,13 @@ export default class Core extends EventEmitter {
|
|
|
960
974
|
setPrePendingCallPromise: (promise: Promise<void> | undefined) => {
|
|
961
975
|
this.prePendingCallPromise = promise;
|
|
962
976
|
},
|
|
977
|
+
// callback 任务管理
|
|
978
|
+
registerCallbackTask: (connectId: string, callbackPromise: Deferred<any>) => {
|
|
979
|
+
this.requestQueue.registerPendingCallbackTask(connectId, callbackPromise);
|
|
980
|
+
},
|
|
981
|
+
waitForCallbackTasks: (connectId: string) =>
|
|
982
|
+
this.requestQueue.waitForPendingCallbackTasks(connectId),
|
|
983
|
+
cancelCallbackTasks: (connectId: string) => this.requestQueue.cancelCallbackTasks(connectId),
|
|
963
984
|
};
|
|
964
985
|
}
|
|
965
986
|
|
|
@@ -1010,6 +1031,11 @@ export default class Core extends EventEmitter {
|
|
|
1010
1031
|
cancel(this.getCoreContext(), message.payload.connectId);
|
|
1011
1032
|
break;
|
|
1012
1033
|
}
|
|
1034
|
+
case IFRAME.CALLBACK: {
|
|
1035
|
+
Log.log('callback message: ', message);
|
|
1036
|
+
postMessage(message);
|
|
1037
|
+
break;
|
|
1038
|
+
}
|
|
1013
1039
|
default:
|
|
1014
1040
|
break;
|
|
1015
1041
|
}
|
package/src/device/Device.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import EventEmitter from 'events';
|
|
2
|
+
import semver from 'semver';
|
|
2
3
|
import { OneKeyDeviceInfo as DeviceDescriptor } from '@onekeyfe/hd-transport';
|
|
3
4
|
import {
|
|
4
5
|
createDeferred,
|
|
@@ -16,6 +17,7 @@ import {
|
|
|
16
17
|
getDeviceType,
|
|
17
18
|
getDeviceUUID,
|
|
18
19
|
getLogger,
|
|
20
|
+
getMethodVersionRange,
|
|
19
21
|
LoggerNames,
|
|
20
22
|
} from '../utils';
|
|
21
23
|
import {
|
|
@@ -28,6 +30,7 @@ import type DeviceConnector from './DeviceConnector';
|
|
|
28
30
|
import { DeviceCommands, PassphrasePromptResponse } from './DeviceCommands';
|
|
29
31
|
|
|
30
32
|
import {
|
|
33
|
+
type DeviceFirmwareRange,
|
|
31
34
|
EOneKeyDeviceMode,
|
|
32
35
|
type Device as DeviceTyped,
|
|
33
36
|
type Features,
|
|
@@ -43,6 +46,7 @@ import {
|
|
|
43
46
|
import { PROTO } from '../constants';
|
|
44
47
|
import { DataManager } from '../data-manager';
|
|
45
48
|
import TransportManager from '../data-manager/TransportManager';
|
|
49
|
+
import { toHardened } from '../api/helpers/pathUtils';
|
|
46
50
|
|
|
47
51
|
export type InitOptions = {
|
|
48
52
|
initSession?: boolean;
|
|
@@ -151,6 +155,8 @@ export class Device extends EventEmitter {
|
|
|
151
155
|
|
|
152
156
|
passphraseState: string | undefined = undefined;
|
|
153
157
|
|
|
158
|
+
pendingCallbackPromise?: Deferred<void>;
|
|
159
|
+
|
|
154
160
|
constructor(descriptor: DeviceDescriptor) {
|
|
155
161
|
super();
|
|
156
162
|
this.originalDescriptor = descriptor;
|
|
@@ -264,6 +270,18 @@ export class Device extends EventEmitter {
|
|
|
264
270
|
(this.isUsedHere() && !this.keepSession && this.mainId) ||
|
|
265
271
|
(this.mainId && DataManager.isBleConnect(env))
|
|
266
272
|
) {
|
|
273
|
+
// wait for callback tasks to complete before releasing device
|
|
274
|
+
if (this.pendingCallbackPromise) {
|
|
275
|
+
try {
|
|
276
|
+
Log.debug(
|
|
277
|
+
'Waiting for callback tasks to complete before releasing device (in release method)'
|
|
278
|
+
);
|
|
279
|
+
await this.pendingCallbackPromise.promise;
|
|
280
|
+
} catch (error) {
|
|
281
|
+
Log.error('Error waiting for callback tasks in release method:', error);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
267
285
|
if (this.commands) {
|
|
268
286
|
this.commands.dispose(false);
|
|
269
287
|
if (this.commands.callPromise) {
|
|
@@ -732,6 +750,52 @@ export class Device extends EventEmitter {
|
|
|
732
750
|
return res.message;
|
|
733
751
|
}
|
|
734
752
|
|
|
753
|
+
supportUnlockVersionRange(): DeviceFirmwareRange {
|
|
754
|
+
return {
|
|
755
|
+
pro: {
|
|
756
|
+
min: '4.15.0',
|
|
757
|
+
},
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
async unlockDevice() {
|
|
762
|
+
const firmwareVersion = getDeviceFirmwareVersion(this.features)?.join('.');
|
|
763
|
+
const versionRange = getMethodVersionRange(
|
|
764
|
+
this.features,
|
|
765
|
+
type => this.supportUnlockVersionRange()[type]
|
|
766
|
+
);
|
|
767
|
+
|
|
768
|
+
if (versionRange && semver.gte(firmwareVersion, versionRange.min)) {
|
|
769
|
+
const res = await this.commands.typedCall('UnLockDevice', 'UnLockDeviceResponse');
|
|
770
|
+
if (this.features) {
|
|
771
|
+
this.features.unlocked = res.message.unlocked == null ? null : res.message.unlocked;
|
|
772
|
+
this.features.unlocked_attach_pin =
|
|
773
|
+
res.message.unlocked_attach_pin == null ? undefined : res.message.unlocked_attach_pin;
|
|
774
|
+
this.features.passphrase_protection =
|
|
775
|
+
res.message.passphrase_protection == null ? null : res.message.passphrase_protection;
|
|
776
|
+
|
|
777
|
+
return Promise.resolve(this.features);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
const featuresRes = await this.commands.typedCall('GetFeatures', 'Features');
|
|
781
|
+
return Promise.resolve(featuresRes.message);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
const { type } = await this.commands.typedCall('GetAddress', 'Address', {
|
|
785
|
+
address_n: [toHardened(44), toHardened(1), toHardened(0), 0, 0],
|
|
786
|
+
coin_name: 'Testnet',
|
|
787
|
+
script_type: 'SPENDADDRESS',
|
|
788
|
+
show_display: false,
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
// @ts-expect-error
|
|
792
|
+
if (type === 'CallMethodError') {
|
|
793
|
+
throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'unlock device error');
|
|
794
|
+
}
|
|
795
|
+
const res = await this.commands.typedCall('GetFeatures', 'Features');
|
|
796
|
+
return Promise.resolve(res.message);
|
|
797
|
+
}
|
|
798
|
+
|
|
735
799
|
async checkPassphraseStateSafety(
|
|
736
800
|
passphraseState?: string,
|
|
737
801
|
useEmptyPassphrase?: boolean,
|
package/src/events/call.ts
CHANGED
|
@@ -58,6 +58,16 @@ export interface IFrameSwitchTransportMessage {
|
|
|
58
58
|
payload: { env: ConnectSettings['env'] };
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
export interface IFrameCallbackMessage {
|
|
62
|
+
event: typeof IFRAME.CALLBACK;
|
|
63
|
+
type: typeof IFRAME.CALLBACK;
|
|
64
|
+
payload: {
|
|
65
|
+
callbackId: string;
|
|
66
|
+
data?: any;
|
|
67
|
+
error?: any;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
61
71
|
export const RESPONSE_EVENT = 'RESPONSE_EVENT';
|
|
62
72
|
|
|
63
73
|
export interface MethodResponseMessage {
|
package/src/events/core.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { HardwareError } from '@onekeyfe/hd-shared';
|
|
2
2
|
import { Unsuccessful } from '../types/params';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
IFrameCallMessage,
|
|
5
|
+
IFrameCancelMessage,
|
|
6
|
+
IFrameSwitchTransportMessage,
|
|
7
|
+
IFrameCallbackMessage,
|
|
8
|
+
} from './call';
|
|
4
9
|
import { DeviceEventMessage } from './device';
|
|
5
10
|
import { IFrameEventMessage } from './iframe';
|
|
6
11
|
import { UiEventMessage } from './ui-request';
|
|
@@ -18,6 +23,7 @@ export type CoreMessage = {
|
|
|
18
23
|
| IFrameCallMessage
|
|
19
24
|
| IFrameCancelMessage
|
|
20
25
|
| IFrameSwitchTransportMessage
|
|
26
|
+
| IFrameCallbackMessage
|
|
21
27
|
| UiResponseMessage
|
|
22
28
|
| UiEventMessage
|
|
23
29
|
| DeviceEventMessage
|
package/src/events/iframe.ts
CHANGED
|
@@ -8,6 +8,7 @@ export const IFRAME = {
|
|
|
8
8
|
CALL: 'iframe-call',
|
|
9
9
|
CANCEL: 'iframe-cancel',
|
|
10
10
|
SWITCH_TRANSPORT: 'iframe-switch-transport',
|
|
11
|
+
CALLBACK: 'iframe-callback',
|
|
11
12
|
} as const;
|
|
12
13
|
|
|
13
14
|
export interface IFrameInit {
|
|
@@ -29,7 +30,17 @@ export interface IFrameSwitchTransport {
|
|
|
29
30
|
};
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
export
|
|
33
|
+
export interface IFrameCallback {
|
|
34
|
+
type: typeof IFRAME.CALLBACK;
|
|
35
|
+
payload: {
|
|
36
|
+
callbackId: string;
|
|
37
|
+
data?: any;
|
|
38
|
+
error?: any;
|
|
39
|
+
finished?: boolean;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type IFrameEvent = IFrameInit | IFrameBridge | IFrameSwitchTransport | IFrameCallback;
|
|
33
44
|
export type IFrameEventMessage = IFrameEvent & { event: typeof UI_EVENT };
|
|
34
45
|
|
|
35
46
|
export const createIFrameMessage: MessageFactoryFn<typeof UI_EVENT, IFrameEvent> = (
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { inject, InjectApi } from './inject';
|
|
1
|
+
import { inject, InjectApi, executeCallback, cleanupCallback } from './inject';
|
|
2
2
|
import { lowLevelInject, LowLevelInjectApi, LowLevelCoreApi } from './lowLevelInject';
|
|
3
3
|
import { topLevelInject } from './topLevelInject';
|
|
4
4
|
import { CoreApi } from './types/api';
|
|
@@ -14,6 +14,7 @@ export * from './data-manager';
|
|
|
14
14
|
export * from './events';
|
|
15
15
|
export * from './types';
|
|
16
16
|
export { whitelist, whitelistExtension } from './data/config';
|
|
17
|
+
export { executeCallback, cleanupCallback };
|
|
17
18
|
|
|
18
19
|
const HardwareSdk = ({
|
|
19
20
|
init,
|
package/src/inject.ts
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
2
|
import { CallMethod } from './events';
|
|
3
3
|
import { CoreApi } from './types/api';
|
|
4
|
+
import type { AllNetworkAddress } from './types/api/allNetworkGetAddress';
|
|
5
|
+
|
|
6
|
+
type CallbackFunction = (data?: any, error?: { message: string; code?: number }) => void;
|
|
7
|
+
|
|
8
|
+
const callbackManager = new Map<string, CallbackFunction>();
|
|
9
|
+
|
|
10
|
+
const generateCallbackId = () =>
|
|
11
|
+
`callback_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
|
|
12
|
+
|
|
13
|
+
const registerCallback = (id: string, callback: CallbackFunction) => {
|
|
14
|
+
callbackManager.set(id, callback);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const executeCallback = (id: string, ...args: any[]) => {
|
|
18
|
+
const callback = callbackManager.get(id);
|
|
19
|
+
if (callback) {
|
|
20
|
+
callback(...args);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const cleanupCallback = (id: string) => {
|
|
25
|
+
callbackManager.delete(id);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { executeCallback, cleanupCallback };
|
|
4
29
|
|
|
5
30
|
export interface InjectApi {
|
|
6
31
|
call: CallMethod;
|
|
@@ -147,6 +172,28 @@ export const createCoreApi = (
|
|
|
147
172
|
|
|
148
173
|
allNetworkGetAddress: (connectId, deviceId, params) =>
|
|
149
174
|
call({ ...params, connectId, deviceId, method: 'allNetworkGetAddress' }),
|
|
175
|
+
allNetworkGetAddressByLoop: (connectId, deviceId, params) => {
|
|
176
|
+
const { onLoopItemResponse, onAllItemsResponse, ...restParams } = params;
|
|
177
|
+
|
|
178
|
+
const callbackId = generateCallbackId();
|
|
179
|
+
registerCallback(callbackId, onLoopItemResponse);
|
|
180
|
+
|
|
181
|
+
const callbackIdFinish = generateCallbackId();
|
|
182
|
+
registerCallback(callbackIdFinish, (data?: AllNetworkAddress[]) => {
|
|
183
|
+
onAllItemsResponse?.(data);
|
|
184
|
+
cleanupCallback(callbackIdFinish);
|
|
185
|
+
cleanupCallback(callbackId);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return call({
|
|
189
|
+
...restParams,
|
|
190
|
+
connectId,
|
|
191
|
+
deviceId,
|
|
192
|
+
method: 'allNetworkGetAddressByLoop',
|
|
193
|
+
callbackId,
|
|
194
|
+
callbackIdFinish,
|
|
195
|
+
});
|
|
196
|
+
},
|
|
150
197
|
|
|
151
198
|
evmGetAddress: (connectId, deviceId, params) =>
|
|
152
199
|
call({ ...params, connectId, deviceId, method: 'evmGetAddress' }),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CommonParams, Response } from '../params';
|
|
1
|
+
import type { CommonParams, Response, Unsuccessful } from '../params';
|
|
2
2
|
import type { CardanoAddressParameters } from './cardano';
|
|
3
3
|
|
|
4
4
|
export type INetwork =
|
|
@@ -91,7 +91,7 @@ type AllNetworkAddressPayload =
|
|
|
91
91
|
xpubSegwit: string;
|
|
92
92
|
};
|
|
93
93
|
|
|
94
|
-
export type AllNetworkAddress =
|
|
94
|
+
export type AllNetworkAddress = AllNetworkAddressParams & {
|
|
95
95
|
success: boolean;
|
|
96
96
|
payload?:
|
|
97
97
|
| AllNetworkAddressPayload
|
|
@@ -108,8 +108,22 @@ export type AllNetworkGetAddressParams = {
|
|
|
108
108
|
bundle: AllNetworkAddressParams[];
|
|
109
109
|
};
|
|
110
110
|
|
|
111
|
+
export type AllNetworkGetAddressParamsByLoop = AllNetworkGetAddressParams & {
|
|
112
|
+
callbackId?: string;
|
|
113
|
+
callbackIdFinish?: string;
|
|
114
|
+
onLoopItemResponse: (data?: AllNetworkAddress) => void;
|
|
115
|
+
onAllItemsResponse: (data?: AllNetworkAddress[], error?: Unsuccessful) => void;
|
|
116
|
+
};
|
|
117
|
+
|
|
111
118
|
export declare function allNetworkGetAddress(
|
|
112
119
|
connectId: string,
|
|
113
120
|
deviceId: string,
|
|
114
121
|
params: CommonParams & AllNetworkGetAddressParams
|
|
115
122
|
): Response<AllNetworkAddress[]>;
|
|
123
|
+
|
|
124
|
+
export declare function allNetworkGetAddressByLoop(
|
|
125
|
+
connectId: string,
|
|
126
|
+
deviceId: string,
|
|
127
|
+
params: CommonParams & AllNetworkGetAddressParamsByLoop
|
|
128
|
+
// only return empty array
|
|
129
|
+
): Response<AllNetworkAddress[]>;
|
package/src/types/api/index.ts
CHANGED
|
@@ -43,7 +43,7 @@ import { setU2FCounter } from './setU2FCounter';
|
|
|
43
43
|
|
|
44
44
|
import { cipherKeyValue } from './cipherKeyValue';
|
|
45
45
|
|
|
46
|
-
import { allNetworkGetAddress } from './allNetworkGetAddress';
|
|
46
|
+
import { allNetworkGetAddress, allNetworkGetAddressByLoop } from './allNetworkGetAddress';
|
|
47
47
|
|
|
48
48
|
import { evmGetAddress } from './evmGetAddress';
|
|
49
49
|
import { evmGetPublicKey } from './evmGetPublicKey';
|
|
@@ -230,6 +230,7 @@ export type CoreApi = {
|
|
|
230
230
|
* All network function
|
|
231
231
|
*/
|
|
232
232
|
allNetworkGetAddress: typeof allNetworkGetAddress;
|
|
233
|
+
allNetworkGetAddressByLoop: typeof allNetworkGetAddressByLoop;
|
|
233
234
|
|
|
234
235
|
/**
|
|
235
236
|
* EVM function
|