Skip to content

Commit

Permalink
feat: Improve bulk copy address for hardware wallet (#2814)
Browse files Browse the repository at this point in the history
* feat: support batchGetAddress for hardware keyring

* refactor: use batch getaddress for set range copy

* feat: fetch hardware wallet accounts

* refactor: use hooks to clean code

* feat: use redux to handler event

* refactor: useFetchWalletAddress hooks

* chore: optimize loop

* chore: upgrade sdk

* fix: cancel progress

* chore: revert pod file
  • Loading branch information
originalix authored Apr 11, 2023
1 parent 3990de9 commit 51ad70c
Show file tree
Hide file tree
Showing 30 changed files with 1,280 additions and 476 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@
"@onekeyfe/cross-inpage-provider-injected": "1.1.24",
"@onekeyfe/cross-inpage-provider-types": "1.1.24",
"@onekeyfe/extension-bridge-hosted": "1.1.24",
"@onekeyfe/hd-ble-sdk": "0.2.43",
"@onekeyfe/hd-core": "0.2.43",
"@onekeyfe/hd-shared": "0.2.43",
"@onekeyfe/hd-transport": "0.2.43",
"@onekeyfe/hd-web-sdk": "0.2.43",
"@onekeyfe/hd-ble-sdk": "0.2.44",
"@onekeyfe/hd-core": "0.2.44",
"@onekeyfe/hd-shared": "0.2.44",
"@onekeyfe/hd-transport": "0.2.44",
"@onekeyfe/hd-web-sdk": "0.2.44",
"@onekeyfe/network-list": "1.6.54",
"@onekeyfe/onekey-cross-webview": "1.1.24",
"@starcoin/starcoin": "2.1.5",
Expand Down
36 changes: 36 additions & 0 deletions packages/engine/src/vaults/impl/ada/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,42 @@ export class KeyringHardware extends KeyringHardwareBase {
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IHardwareGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const { derivationType, addressType, networkId, protocolMagic } =
await getCardanoConstant();
const response = await HardwareSDK.cardanoGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => {
const stakingPath = `${path.split('/').slice(0, 4).join('/')}/2/0`;
return {
addressParameters: {
addressType,
path,
stakingPath,
},
networkId,
protocolMagic,
derivationType,
isCheck: true,
showOnOneKey: !!showOnOneKey,
};
}),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.serializedPath,
address: item.address,
}));
}

override async signTransaction(unsignedTx: UnsignedTx): Promise<SignedTx> {
const { PROTO } = await CoreSDKLoader();
const HardwareSDK = await this.getHardwareSDKInstance();
Expand Down
24 changes: 24 additions & 0 deletions packages/engine/src/vaults/impl/algo/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import sdk from './sdkAlgo';
import type { DBSimpleAccount } from '../../../types/account';
import type {
IGetAddressParams,
IHardwareGetAddressParams,
IPrepareHardwareAccountsParams,
} from '../../types';
import type { ISdkAlgoEncodedTransaction } from './sdkAlgo';
Expand Down Expand Up @@ -129,4 +130,27 @@ export class KeyringHardware extends KeyringHardwareBase {
}
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IHardwareGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const response = await HardwareSDK.algoGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
showOnOneKey: !!showOnOneKey,
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}
}
23 changes: 23 additions & 0 deletions packages/engine/src/vaults/impl/apt/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ export class KeyringHardware extends KeyringHardwareBase {
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IHardwareGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const response = await HardwareSDK.aptosGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
showOnOneKey: !!showOnOneKey,
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}

async signTransaction(
unsignedTx: UnsignedTx,
options: ISignCredentialOptions,
Expand Down
25 changes: 25 additions & 0 deletions packages/engine/src/vaults/impl/cfx/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,31 @@ export class KeyringHardware extends KeyringHardwareBase {
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const chainId = new BigNumber(await this.getNetworkChainId()).toNumber();
const response = await HardwareSDK.aptosGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
showOnOneKey: !!showOnOneKey,
chainId,
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}

async handleSignMessage(message: IUnsignedMessageCfx) {
const HardwareSDK = await this.getHardwareSDKInstance();
const path = await this.getAccountPath();
Expand Down
32 changes: 32 additions & 0 deletions packages/engine/src/vaults/impl/cosmos/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,38 @@ export class KeyringHardware extends KeyringHardwareBase {
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IHardwareGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const chainInfo = await this.getChainInfo();
const curve = chainInfo?.implOptions?.curve ?? 'secp256k1';
const addressPrefix = chainInfo?.implOptions?.addressPrefix;

if (curve === 'ed25519') {
throw new HardwareError('ed25519 curve is not supported');
}

const response = await HardwareSDK.cosmosGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
hrp: addressPrefix,
showOnOneKey: !!showOnOneKey,
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}

async signTransaction(unsignedTx: UnsignedTx): Promise<SignedTx> {
debugLogger.common.info('signTransaction', unsignedTx);
const dbAccount = await this.getDbAccount();
Expand Down
28 changes: 28 additions & 0 deletions packages/engine/src/vaults/impl/dot/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@ export class KeyringHardware extends KeyringHardwareBase {
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IHardwareGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const chainInfoImpl = await this.getChainInfoImplOptions();
const chainId = await this.getNetworkChainId();
const passphraseState = await this.getWalletPassphraseState();

const response = await HardwareSDK.polkadotGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
showOnOneKey: !!showOnOneKey,
prefix: chainInfoImpl.addressPrefix,
network: chainId,
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}

async signTransaction(
unsignedTx: UnsignedTx,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
25 changes: 25 additions & 0 deletions packages/engine/src/vaults/impl/evm/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,29 @@ export class KeyringHardware extends KeyringHardwareBase {

return address;
}

override async batchGetAddress(
params: IGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const chainId = await this.getNetworkChainId();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const response = await HardwareSDK.evmGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
showOnOneKey: !!showOnOneKey,
chainId: Number(chainId),
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}
}
25 changes: 25 additions & 0 deletions packages/engine/src/vaults/impl/fil/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ export class KeyringHardware extends KeyringHardwareBase {
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IHardwareGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const network = await this.getNetwork();
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const response = await HardwareSDK.filecoinGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
showOnOneKey: !!showOnOneKey,
isTestnet: network.isTestnet,
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}

async signTransaction(unsignedTx: IUnsignedTxPro): Promise<ISignedTxPro> {
const HardwareSDK = await this.getHardwareSDKInstance();
const network = await this.getNetwork();
Expand Down
23 changes: 23 additions & 0 deletions packages/engine/src/vaults/impl/near/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,27 @@ export class KeyringHardware extends KeyringHardwareBase {
}
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const response = await HardwareSDK.nearGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
showOnOneKey: !!showOnOneKey,
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}
}
23 changes: 23 additions & 0 deletions packages/engine/src/vaults/impl/sol/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,27 @@ export class KeyringHardware extends KeyringHardwareBase {
}
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const response = await HardwareSDK.solGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
showOnOneKey: !!showOnOneKey,
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}
}
23 changes: 23 additions & 0 deletions packages/engine/src/vaults/impl/stc/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,27 @@ export class KeyringHardware extends KeyringHardwareBase {
}
throw convertDeviceError(response.payload);
}

override async batchGetAddress(
params: IGetAddressParams[],
): Promise<{ path: string; address: string }[]> {
const HardwareSDK = await this.getHardwareSDKInstance();
const { connectId, deviceId } = await this.getHardwareInfo();
const passphraseState = await this.getWalletPassphraseState();
const response = await HardwareSDK.starcoinGetAddress(connectId, deviceId, {
...passphraseState,
bundle: params.map(({ path, showOnOneKey }) => ({
path,
showOnOneKey: !!showOnOneKey,
})),
});

if (!response.success) {
throw convertDeviceError(response.payload);
}
return response.payload.map((item) => ({
path: item.path ?? '',
address: item.address ?? '',
}));
}
}
Loading

0 comments on commit 51ad70c

Please sign in to comment.