Skip to content

Commit

Permalink
optimize: all network (#372)
Browse files Browse the repository at this point in the history
* chore: optimize allnetwork

* chore: release version 1.0.12-alpha.1

* chore: fix lint
  • Loading branch information
ByteZhang1024 authored Oct 8, 2024
1 parent 906f360 commit dedf68e
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 85 deletions.
2 changes: 1 addition & 1 deletion packages/connect-examples/electron-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hardware-example",
"productName": "HardwareExample",
"executableName": "onekey-hardware-example",
"version": "1.0.12-alpha.0",
"version": "1.0.12-alpha.1",
"author": "OneKey",
"description": "End-to-end encrypted workspaces for teams",
"main": "dist/index.js",
Expand Down
10 changes: 5 additions & 5 deletions packages/connect-examples/expo-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "expo-example",
"version": "1.0.12-alpha.0",
"version": "1.0.12-alpha.1",
"scripts": {
"start": "CONNECT_SRC=https://localhost:8087/ yarn expo start --dev-client",
"android": "yarn expo run:android",
Expand All @@ -19,10 +19,10 @@
"@noble/ed25519": "^2.1.0",
"@noble/hashes": "^1.3.3",
"@noble/secp256k1": "^1.7.1",
"@onekeyfe/hd-ble-sdk": "^1.0.12-alpha.0",
"@onekeyfe/hd-common-connect-sdk": "^1.0.12-alpha.0",
"@onekeyfe/hd-core": "^1.0.12-alpha.0",
"@onekeyfe/hd-web-sdk": "^1.0.12-alpha.0",
"@onekeyfe/hd-ble-sdk": "^1.0.12-alpha.1",
"@onekeyfe/hd-common-connect-sdk": "^1.0.12-alpha.1",
"@onekeyfe/hd-core": "^1.0.12-alpha.1",
"@onekeyfe/hd-web-sdk": "^1.0.12-alpha.1",
"@onekeyfe/react-native-ble-plx": "3.0.0",
"@polkadot/util-crypto": "13.1.1",
"@react-native-async-storage/async-storage": "1.21.0",
Expand Down
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
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-core",
"version": "1.0.12-alpha.0",
"version": "1.0.12-alpha.1",
"description": "> TODO: description",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
Expand All @@ -25,8 +25,8 @@
"url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.12-alpha.0",
"@onekeyfe/hd-transport": "^1.0.12-alpha.0",
"@onekeyfe/hd-shared": "^1.0.12-alpha.1",
"@onekeyfe/hd-transport": "^1.0.12-alpha.1",
"axios": "^0.27.2",
"bignumber.js": "^9.0.2",
"bytebuffer": "^5.0.1",
Expand Down
148 changes: 104 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,104 @@ 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,
};
// 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);
}

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,
// 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 dependOnPayloads = dependOnMethodResults.reduce(
(acc, cur) => Object.assign(acc, get(cur, 'payload', {})),
{}
);

const result: AllNetworkAddress = {
...response,
// @ts-expect-error
payload: { ...response.payload, ...dependOnPayloads },
};
responses.push(result);
this.postPreviousAddressMessage(result);
}
Expand Down
8 changes: 6 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,9 @@ type AllNetworkAddressPayload =
| {
address: string;
publicKey?: string;
pub?: string;
// Nostr public key (bech32)
npub?: string;
}
| {
// Cardano
Expand Down
8 changes: 4 additions & 4 deletions packages/hd-ble-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-ble-sdk",
"version": "1.0.12-alpha.0",
"version": "1.0.12-alpha.1",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "ISC",
Expand All @@ -20,8 +20,8 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-core": "^1.0.12-alpha.0",
"@onekeyfe/hd-shared": "^1.0.12-alpha.0",
"@onekeyfe/hd-transport-react-native": "^1.0.12-alpha.0"
"@onekeyfe/hd-core": "^1.0.12-alpha.1",
"@onekeyfe/hd-shared": "^1.0.12-alpha.1",
"@onekeyfe/hd-transport-react-native": "^1.0.12-alpha.1"
}
}
12 changes: 6 additions & 6 deletions packages/hd-common-connect-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-common-connect-sdk",
"version": "1.0.12-alpha.0",
"version": "1.0.12-alpha.1",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "ISC",
Expand All @@ -20,10 +20,10 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-core": "^1.0.12-alpha.0",
"@onekeyfe/hd-shared": "^1.0.12-alpha.0",
"@onekeyfe/hd-transport-http": "^1.0.12-alpha.0",
"@onekeyfe/hd-transport-lowlevel": "^1.0.12-alpha.0",
"@onekeyfe/hd-transport-webusb": "^1.0.12-alpha.0"
"@onekeyfe/hd-core": "^1.0.12-alpha.1",
"@onekeyfe/hd-shared": "^1.0.12-alpha.1",
"@onekeyfe/hd-transport-http": "^1.0.12-alpha.1",
"@onekeyfe/hd-transport-lowlevel": "^1.0.12-alpha.1",
"@onekeyfe/hd-transport-webusb": "^1.0.12-alpha.1"
}
}
6 changes: 3 additions & 3 deletions packages/hd-transport-http/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-transport-http",
"version": "1.0.12-alpha.0",
"version": "1.0.12-alpha.1",
"description": "hardware http transport",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
Expand All @@ -24,8 +24,8 @@
"url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.12-alpha.0",
"@onekeyfe/hd-transport": "^1.0.12-alpha.0",
"@onekeyfe/hd-shared": "^1.0.12-alpha.1",
"@onekeyfe/hd-transport": "^1.0.12-alpha.1",
"axios": "^0.27.2"
}
}
6 changes: 3 additions & 3 deletions packages/hd-transport-lowlevel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-transport-lowlevel",
"version": "1.0.12-alpha.0",
"version": "1.0.12-alpha.1",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "MIT",
"main": "dist/index.js",
Expand All @@ -19,7 +19,7 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.12-alpha.0",
"@onekeyfe/hd-transport": "^1.0.12-alpha.0"
"@onekeyfe/hd-shared": "^1.0.12-alpha.1",
"@onekeyfe/hd-transport": "^1.0.12-alpha.1"
}
}
Loading

0 comments on commit dedf68e

Please sign in to comment.