Skip to content

Commit

Permalink
chore: optimize allnetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZhang1024 committed Oct 8, 2024
1 parent 906f360 commit 4c9a335
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,15 @@ const api: PlaygroundProps[] = [
// ton chain has a different derivation path
},
{
network: 'alephium',
network: 'alph',
path: "m/44'/1234'/0'/0/0",
showOnOneKey: false,
},
{
network: 'nostr',
path: "m/44'/1237'/0'/0/0",
showOnOneKey: false,
},
],
},
},
Expand Down
149 changes: 105 additions & 44 deletions packages/core/src/api/allnetwork/AllNetworkGetAddress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import semver from 'semver';
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
import { get } from 'lodash';
import { UI_REQUEST } from '../../constants/ui-request';
import { serializedPath } from '../helpers/pathUtils';
import { BaseMethod } from '../BaseMethod';
Expand All @@ -9,6 +10,7 @@ import { CoreApi } from '../../types';
import type {
AllNetworkAddress,
AllNetworkAddressParams,
CommonResponseParams,
INetwork,
} from '../../types/api/allNetworkGetAddress';
import { findMethod } from '../utils';
Expand All @@ -21,7 +23,8 @@ const Mainnet = 'mainnet';

type NetworkConfig = {
methodName: keyof CoreApi;
getParams?: (baseParams: AllNetworkAddressParams, chainName?: string) => any;
getParams?: (baseParams: AllNetworkAddressParams, chainName?: string, methodName?: string) => any;
dependOnMethodName?: (keyof CoreApi)[];
};

type INetworkReal = Exclude<INetwork, 'tbtc' | 'bch' | 'doge' | 'ltc' | 'neurai'>;
Expand Down Expand Up @@ -96,7 +99,7 @@ const networkConfigMap: NetworkConfigMap = {
methodName: 'xrpGetAddress',
},
cosmos: {
methodName: 'cosmosGetAddress',
methodName: 'cosmosGetPublicKey',
getParams: (baseParams: AllNetworkAddressParams) => {
const { path, prefix, showOnOneKey } = baseParams;
return {
Expand Down Expand Up @@ -138,6 +141,14 @@ const networkConfigMap: NetworkConfigMap = {
},
sui: {
methodName: 'suiGetAddress',
dependOnMethodName: ['suiGetPublicKey'],
getParams: (baseParams: AllNetworkAddressParams) => {
const { path, showOnOneKey } = baseParams;
return {
path,
showOnOneKey,
};
},
},
fil: {
methodName: 'filecoinGetAddress',
Expand Down Expand Up @@ -216,6 +227,9 @@ const networkConfigMap: NetworkConfigMap = {
alph: {
methodName: 'alephiumGetAddress',
},
nostr: {
methodName: 'nostrGetPublicKey',
},
};

export default class AllNetworkGetAddress extends BaseMethod<
Expand Down Expand Up @@ -252,7 +266,13 @@ export default class AllNetworkGetAddress extends BaseMethod<
payload: AllNetworkAddressParams;
}): {
methodName: keyof CoreApi;
params: Parameters<CoreApi[keyof CoreApi]>[0];
params: Parameters<CoreApi[keyof CoreApi]>[0] & { originPayload: AllNetworkAddressParams };
dependOnMethods:
| {
methodName: keyof CoreApi;
params: Parameters<CoreApi[keyof CoreApi]>[0];
}[]
| undefined;
} {
const { name: networkName, coin } = networkAliases[network] || {
name: network,
Expand All @@ -263,64 +283,105 @@ export default class AllNetworkGetAddress extends BaseMethod<
throw new Error(`Unsupported network: ${network}`);
}

const dependOnMethods = config.dependOnMethodName?.map(dependOnMethodName => ({
methodName: dependOnMethodName,
params: config?.getParams?.(payload, coin, dependOnMethodName),
}));

return {
methodName: config.methodName,
params: config?.getParams?.(payload, coin) ?? payload,
params: {
...(config?.getParams?.(payload, coin, config.methodName) ?? payload),
originPayload: payload,
},
dependOnMethods,
};
}

async callMethod(methodName: keyof CoreApi, params: any, baseParams: CommonResponseParams) {
const method: BaseMethod = findMethod({
event: IFRAME.CALL,
type: IFRAME.CALL,
payload: {
connectId: this.payload.connectId,
deviceId: this.payload.deviceId,
method: methodName,
...params,
},
});

method.connector = this.connector;
method.postMessage = this.postMessage;
method.init();
method.setDevice?.(this.device);

let result: AllNetworkAddress;
try {
const response = await method.run();
result = {
...baseParams,
success: true,
error: response.error,
payload: response,
};
} catch (e: any) {
const errorMessage =
e instanceof Error ? handleHardwareError(e, this.device, method) : 'Unknown error';

result = {
...baseParams,
success: false,
error: errorMessage,
};
}

return result;
}

async run() {
const responses: AllNetworkAddress[] = [];
for (let i = 0; i < this.payload.bundle.length; i++) {
const param = this.payload.bundle[i];
const { methodName, params } = this.generateMethodName({
const { methodName, params, dependOnMethods } = this.generateMethodName({
network: param.network as INetwork,
payload: param,
});

const method: BaseMethod = findMethod({
event: IFRAME.CALL,
type: IFRAME.CALL,
payload: {
connectId: this.payload.connectId,
deviceId: this.payload.deviceId,
method: methodName,
...params,
},
});

method.connector = this.connector;
method.postMessage = this.postMessage;
method.init();
method.setDevice?.(this.device);

const baseParams = {
network: param.network,
path: typeof param.path === 'string' ? param.path : serializedPath(param.path),
chainName: param.chainName,
prefix: param.prefix,
};

let result: AllNetworkAddress;
try {
const response = await method.run();
result = {
...baseParams,
success: true,
error: response.error,
payload: response,
};
} catch (e: any) {
const errorMessage =
e instanceof Error ? handleHardwareError(e, this.device, method) : 'Unknown error';
// run depend on methods
const dependOnMethodResults: AllNetworkAddress[] = [];
for (const dependOnMethod of dependOnMethods ?? []) {
const response = await this.callMethod(
dependOnMethod.methodName,
dependOnMethod.params,
param
);
dependOnMethodResults.push(response);
}

result = {
...baseParams,
// if any depend on method failed, return the error
if (dependOnMethodResults.some(result => !result.success)) {
responses.push({
...param,
success: false,
error: errorMessage,
};
error: dependOnMethodResults.find(result => !result.success)?.error,
});
return Promise.resolve(responses);
}

// call method
const response = await this.callMethod(methodName, params, param);

const result: AllNetworkAddress = {
...response,
// @ts-expect-error
payload: {
...response.payload,
...dependOnMethodResults.reduce(
(acc, cur) => ({ ...get(cur, 'payload', {}), ...acc }),
{}
),
},
};
responses.push(result);
this.postPreviousAddressMessage(result);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/types/api/allNetworkGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export type INetwork =
| 'nervos'
| 'scdo'
| 'ton'
| 'alph';
| 'alph'
| 'nostr';

type CommonResponseParams = {
export type CommonResponseParams = {
path: string;
network: INetwork;
chainName?: string;
Expand All @@ -51,6 +52,8 @@ type AllNetworkAddressPayload =
| {
address: string;
publicKey?: string;
pub?: string;
npub?: string;
}
| {
// Cardano
Expand Down

0 comments on commit 4c9a335

Please sign in to comment.