Skip to content

Commit

Permalink
Fix: optimize Cosmos & Polkadot dApp OK-20488 OK-20489 (#3086)
Browse files Browse the repository at this point in the history
* fix: cosmos dapp connect

* fix: dot dapp interface

* fix: kaspa transfer accounts

* feat: Support overlay polkadot-js
  • Loading branch information
ByteZhang1024 authored Jun 9, 2023
1 parent b198405 commit 1dd206a
Show file tree
Hide file tree
Showing 26 changed files with 500 additions and 304 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@
},
"dependencies": {
"@onekeyfe/blockchain-libs": "0.0.48",
"@onekeyfe/cross-inpage-provider-core": "1.1.28",
"@onekeyfe/cross-inpage-provider-errors": "1.1.28",
"@onekeyfe/cross-inpage-provider-injected": "1.1.28",
"@onekeyfe/cross-inpage-provider-types": "1.1.28",
"@onekeyfe/extension-bridge-hosted": "1.1.28",
"@onekeyfe/cross-inpage-provider-core": "1.1.29",
"@onekeyfe/cross-inpage-provider-errors": "1.1.29",
"@onekeyfe/cross-inpage-provider-injected": "1.1.29",
"@onekeyfe/cross-inpage-provider-types": "1.1.29",
"@onekeyfe/extension-bridge-hosted": "1.1.29",
"@onekeyfe/hd-ble-sdk": "0.3.11",
"@onekeyfe/hd-core": "0.3.11",
"@onekeyfe/hd-shared": "0.3.11",
"@onekeyfe/hd-transport": "0.3.11",
"@onekeyfe/hd-web-sdk": "0.3.11",
"@onekeyfe/onekey-cross-webview": "1.1.28",
"@onekeyfe/onekey-cross-webview": "1.1.29",
"@starcoin/starcoin": "2.1.5",
"@web3-react/core": "8.0.35-beta.0",
"@web3-react/empty": "8.0.20-beta.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import timelinePerfTrace, {
} from '@onekeyhq/shared/src/perf/timelinePerfTrace';
import platformEnv from '@onekeyhq/shared/src/platformEnv';
import type { Avatar } from '@onekeyhq/shared/src/utils/emojiUtils';
import { getValidUnsignedMessage } from '@onekeyhq/shared/src/utils/messageUtils';
import type { IOneKeyDeviceFeatures } from '@onekeyhq/shared/types';

import { DbApi } from './dbs';
Expand Down Expand Up @@ -155,7 +156,6 @@ import type {
IVaultSettings,
} from './vaults/types';
import type { IJsonRpcRequest } from '@onekeyfe/cross-inpage-provider-types';
import { getValidUnsignedMessage } from '@onekeyhq/shared/src/utils/messageUtils';

const updateTokenCache: {
[networkId: string]: boolean;
Expand Down Expand Up @@ -637,6 +637,7 @@ class Engine {
coinType: a.coinType,
tokens: [],
address: a.address,
pubKey: get(a, 'pub', ''),
}
: this.getVault({ accountId: a.id, networkId }).then((vault) =>
vault.getOutputAccount().catch((error) => {
Expand Down Expand Up @@ -1175,6 +1176,7 @@ class Engine {
coinType: dbAccount.coinType,
tokens: [],
address: dbAccount.address,
pubKey: get(dbAccount, 'pub', ''),
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/engine/src/types/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type DBAccount = DBSimpleAccount | DBUTXOAccount | DBVariantAccount;
type Account = DBBaseAccount & {
tokens: Array<Token>;
address: string;
pubKey?: string;
displayAddress?: string;
xpub?: string; // for btc fork chain
customAddresses?: string; // for btc custom address
Expand Down
3 changes: 2 additions & 1 deletion packages/engine/src/vaults/VaultBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint max-classes-per-file: "off" */

import BigNumber from 'bignumber.js';
import { isNil } from 'lodash';
import { get, isNil } from 'lodash';

import type {
BaseClient,
Expand Down Expand Up @@ -465,6 +465,7 @@ export abstract class VaultBase extends VaultBaseChainOnly {
tokens: [],
address: dbAccount.address,
template: dbAccount.template,
pubKey: get(dbAccount, 'pub', ''),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/vaults/impl/cosmos/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class Vault extends VaultBase {
)) as DBVariantAccount;

const chainInfo = await this.engine.providerManager.getChainInfoByNetworkId(
this.networkId,
networkId,
);

const address = baseAddressToAddress(
Expand Down
23 changes: 11 additions & 12 deletions packages/engine/src/vaults/impl/kaspa/KeyringHardware.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import { Transaction } from '@kaspa/core-lib';
import { bytesToHex } from '@noble/hashes/utils';

import { OneKeyHardwareError } from '@onekeyhq/engine/src/errors';
import { slicePathTemplate } from '@onekeyhq/engine/src/managers/derivation';
import { getAccountNameInfoByImpl } from '@onekeyhq/engine/src/managers/impl';
import { AccountType } from '@onekeyhq/engine/src/types/account';
import type { DBSimpleAccount } from '@onekeyhq/engine/src/types/account';
import type { SignedTx, UnsignedTx } from '@onekeyhq/engine/src/types/provider';
import { KeyringHardwareBase } from '@onekeyhq/engine/src/vaults/keyring/KeyringHardwareBase';
import type {
IHardwareGetAddressParams,
IPrepareHardwareAccountsParams,
} from '@onekeyhq/engine/src/vaults/types';
import { convertDeviceError } from '@onekeyhq/shared/src/device/deviceErrorUtils';
import {
IMPL_KASPA as COIN_IMPL,
COINTYPE_KASPA as COIN_TYPE,
} from '@onekeyhq/shared/src/engine/engineConsts';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';

import { OneKeyHardwareError } from '../../../errors';
import { slicePathTemplate } from '../../../managers/derivation';
import { getAccountNameInfoByImpl } from '../../../managers/impl';
import { AccountType } from '../../../types/account';
import { KeyringHardwareBase } from '../../keyring/KeyringHardwareBase';

import { SignType, publicKeyFromX } from './sdk';
import {
SignatureType,
SigningMethodType,
toTransaction,
} from './sdk/transaction';

import type { DBSimpleAccount } from '../../../types/account';
import type { SignedTx, UnsignedTx } from '../../../types/provider';
import type {
IHardwareGetAddressParams,
IPrepareHardwareAccountsParams,
} from '../../types';
import type { KaspaSignTransactionParams } from '@onekeyfe/hd-core';

// @ts-ignore
Expand Down
3 changes: 1 addition & 2 deletions packages/engine/src/vaults/impl/kaspa/KeyringHd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
import type { ExportedSeedCredential } from '@onekeyhq/engine/src/dbs/base';
import { OneKeyInternalError } from '@onekeyhq/engine/src/errors';
import { slicePathTemplate } from '@onekeyhq/engine/src/managers/derivation';
import { getAccountNameInfoByImpl } from '@onekeyhq/engine/src/managers/impl';
import { Signer } from '@onekeyhq/engine/src/proxy';
import { batchGetPublicKeys } from '@onekeyhq/engine/src/secret';
import { AccountType } from '@onekeyhq/engine/src/types/account';
Expand All @@ -20,8 +21,6 @@ import {
} from '@onekeyhq/shared/src/engine/engineConsts';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';

import { getAccountNameInfoByImpl } from '../../../managers/impl';

import {
addressFromPublicKey,
privateKeyFromOriginPrivateKey,
Expand Down
25 changes: 12 additions & 13 deletions packages/engine/src/vaults/impl/kaspa/KeyringImported.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';

import { OneKeyInternalError } from '@onekeyhq/engine/src/errors';
import { Signer } from '@onekeyhq/engine/src/proxy';
import { secp256k1 } from '@onekeyhq/engine/src/secret/curves';
import { AccountType } from '@onekeyhq/engine/src/types/account';
import type { DBSimpleAccount } from '@onekeyhq/engine/src/types/account';
import type { UnsignedTx } from '@onekeyhq/engine/src/types/provider';
import { KeyringImportedBase } from '@onekeyhq/engine/src/vaults/keyring/KeyringImportedBase';
import type {
IPrepareImportedAccountsParams,
ISignCredentialOptions,
SignedTxResult,
} from '@onekeyhq/engine/src/vaults/types';
import { COINTYPE_KASPA as COIN_TYPE } from '@onekeyhq/shared/src/engine/engineConsts';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';

import { OneKeyInternalError } from '../../../errors';
import { Signer } from '../../../proxy';
import { secp256k1 } from '../../../secret/curves';
import { AccountType } from '../../../types/account';
import { KeyringImportedBase } from '../../keyring/KeyringImportedBase';

import {
addressFromPublicKey,
privateKeyFromOriginPrivateKey,
publicKeyFromOriginPubkey,
} from './sdk';
import { signTransaction, toTransaction } from './sdk/transaction';

import type { DBSimpleAccount } from '../../../types/account';
import type { UnsignedTx } from '../../../types/provider';
import type {
IPrepareImportedAccountsParams,
ISignCredentialOptions,
SignedTxResult,
} from '../../types';
import type { PrivateKey } from '@kaspa/core-lib';

// @ts-ignore
Expand Down
12 changes: 5 additions & 7 deletions packages/engine/src/vaults/impl/kaspa/KeyringWatching.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { InvalidAddress } from '@onekeyhq/engine/src/errors';
import { AccountType } from '@onekeyhq/engine/src/types/account';
import type { DBSimpleAccount } from '@onekeyhq/engine/src/types/account';
import { KeyringWatchingBase } from '@onekeyhq/engine/src/vaults/keyring/KeyringWatchingBase';
import type { IPrepareWatchingAccountsParams } from '@onekeyhq/engine/src/vaults/types';
import { COINTYPE_KASPA as COIN_TYPE } from '@onekeyhq/shared/src/engine/engineConsts';

import { InvalidAddress } from '../../../errors';
import { AccountType } from '../../../types/account';
import { KeyringWatchingBase } from '../../keyring/KeyringWatchingBase';

import type { DBSimpleAccount } from '../../../types/account';
import type { IPrepareWatchingAccountsParams } from '../../types';

// @ts-ignore
export class KeyringWatching extends KeyringWatchingBase {
override async prepareAccounts(
Expand Down
48 changes: 25 additions & 23 deletions packages/engine/src/vaults/impl/kaspa/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,37 @@ import BigNumber from 'bignumber.js';
import { groupBy } from 'lodash';
import memoizee from 'memoizee';

import {
InvalidAddress,
OneKeyInternalError,
} from '@onekeyhq/engine/src/errors';
import { decrypt } from '@onekeyhq/engine/src/secret/encryptors/aes256';
import type { DBSimpleAccount } from '@onekeyhq/engine/src/types/account';
import { getTimeDurationMs } from '@onekeyhq/kit/src/utils/helper';

import { InvalidAddress, OneKeyInternalError } from '../../../errors';
import { decrypt } from '../../../secret/encryptors/aes256';
import { TransactionStatus } from '../../../types/provider';
import { TransactionStatus } from '@onekeyhq/engine/src/types/provider';
import type { KeyringSoftwareBase } from '@onekeyhq/engine/src/vaults/keyring/KeyringSoftwareBase';
import {
IDecodedTxActionType,
IDecodedTxDirection,
IDecodedTxStatus,
IEncodedTxUpdateType,
} from '../../types';
import { convertFeeValueToGwei } from '../../utils/feeInfoUtils';
import { VaultBase } from '../../VaultBase';
} from '@onekeyhq/engine/src/vaults/types';
import type {
IDecodedTx,
IDecodedTxActionNativeTransfer,
IDecodedTxLegacy,
IEncodedTxUpdateOptions,
IEncodedTxUpdatePayloadTransfer,
IFeeInfo,
IFeeInfoUnit,
IHistoryTx,
ISignedTxPro,
ITransferInfo,
IUnsignedTxPro,
} from '@onekeyhq/engine/src/vaults/types';
import type { TxInput } from '@onekeyhq/engine/src/vaults/utils/btcForkChain/types';
import { convertFeeValueToGwei } from '@onekeyhq/engine/src/vaults/utils/feeInfoUtils';
import { VaultBase } from '@onekeyhq/engine/src/vaults/VaultBase';
import { getTimeDurationMs } from '@onekeyhq/kit/src/utils/helper';

import { KeyringHardware } from './KeyringHardware';
import { KeyringHd } from './KeyringHd';
Expand All @@ -39,21 +56,6 @@ import {
import { toTransaction } from './sdk/transaction';
import settings from './settings';

import type { KeyringSoftwareBase } from '../../keyring/KeyringSoftwareBase';
import type {
IDecodedTx,
IDecodedTxActionNativeTransfer,
IDecodedTxLegacy,
IEncodedTxUpdateOptions,
IEncodedTxUpdatePayloadTransfer,
IFeeInfo,
IFeeInfoUnit,
IHistoryTx,
ISignedTxPro,
ITransferInfo,
IUnsignedTxPro,
} from '../../types';
import type { TxInput } from '../../utils/btcForkChain/types';
import type { IEncodedTxKaspa } from './types';

// @ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/vaults/impl/kaspa/sdk/publickey.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PublicKey } from '@kaspa/core-lib';
import { bytesToHex } from '@noble/hashes/utils';

import { tweakPublicKey } from '../../../../secret/bip340';
import { tweakPublicKey } from '@onekeyhq/engine/src/secret/bip340';

export enum SignType {
Schnorr = 'schnorr',
Expand Down
15 changes: 13 additions & 2 deletions packages/engine/src/vaults/impl/kaspa/sdk/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { bytesToHex } from '@noble/hashes/utils';
import * as necc from '@noble/secp256k1';
import BigNumber from 'bignumber.js';

import ecc from '../../../utils/btcForkChain/provider/nobleSecp256k1Wrapper';
import { hexlify, stripHexPrefix } from '../../../utils/hexUtils';
import { OneKeyInternalError } from '@onekeyhq/engine/src/errors';
import ecc from '@onekeyhq/engine/src/vaults/utils/btcForkChain/provider/nobleSecp256k1Wrapper';
import {
hexlify,
stripHexPrefix,
} from '@onekeyhq/engine/src/vaults/utils/hexUtils';

import { DEFAULT_SEQNUMBER } from './constant';
import { UnspentOutput } from './types';
Expand Down Expand Up @@ -42,6 +46,13 @@ export function toTransaction(tx: IEncodedTxKaspa): Transaction {
sendAmount = sendAmount.minus(mass);
}

if (sendAmount.isLessThan(0)) {
throw new OneKeyInternalError(
'Insufficient Balance.',
'msg__insufficient_balance',
);
}

const txn = new Transaction()
.from(inputs.map((input) => new UnspentOutput(input)))
.to(to, sendAmount.toNumber())
Expand Down
20 changes: 20 additions & 0 deletions packages/kit-bg/src/providers/HandleQueue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Queue {
private tasks: (() => Promise<any>)[] = [];

public enqueue(task: () => Promise<any>) {
this.tasks.push(task);
if (this.tasks.length === 1) {
this.dequeue();
}
}

private async dequeue() {
while (this.tasks.length > 0) {
const task = this.tasks[0];
await task();
this.tasks.shift();
}
}
}

export const queue = new Queue();
Loading

0 comments on commit 1dd206a

Please sign in to comment.