Skip to content

Commit

Permalink
chore: Add logs for dApp (#5603)
Browse files Browse the repository at this point in the history
* chore: Add logs for dApp

* feat: Add logs
  • Loading branch information
originalix authored Aug 21, 2024
1 parent 42c038e commit 7e654ab
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/kit-bg/src/providers/ProviderApiAlgo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
providerApiMethod,
} from '@onekeyhq/shared/src/background/backgroundDecorators';
import { SEPERATOR } from '@onekeyhq/shared/src/engine/engineConsts';
import { defaultLogger } from '@onekeyhq/shared/src/logger/logger';

import ProviderApiBase from './ProviderApiBase';

Expand Down Expand Up @@ -82,7 +83,7 @@ class ProviderApiAlgo extends ProviderApiBase {

@providerApiMethod()
public async connect(request: IJsBridgeMessagePayload) {
console.log('algo connect', request);
defaultLogger.discovery.dapp.dappRequest({ request });
const accounts = await this.accounts(request);

if (accounts && accounts.length > 0) {
Expand All @@ -98,6 +99,7 @@ class ProviderApiAlgo extends ProviderApiBase {
request: IJsBridgeMessagePayload,
walletTransactions: Array<{ txn: string; signers: [] }>,
): Promise<(string | null)[]> {
defaultLogger.discovery.dapp.dappRequest({ request });
const txsToSign: string[] = [];
for (let i = 0; i < walletTransactions.length; i += 1) {
// transaction with signers means that this transaction is not meant to be signed
Expand Down
11 changes: 11 additions & 0 deletions packages/kit-bg/src/providers/ProviderApiAptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
permissionRequired,
providerApiMethod,
} from '@onekeyhq/shared/src/background/backgroundDecorators';
import { defaultLogger } from '@onekeyhq/shared/src/logger/logger';
import bufferUtils from '@onekeyhq/shared/src/utils/bufferUtils';
import hexUtils from '@onekeyhq/shared/src/utils/hexUtils';
import { EMessageTypesAptos } from '@onekeyhq/shared/types/message';
Expand Down Expand Up @@ -108,6 +109,7 @@ class ProviderApiAptos extends ProviderApiBase {

@providerApiMethod()
public async connect(request: IJsBridgeMessagePayload) {
defaultLogger.discovery.dapp.dappRequest({ request });
const accountsInfo =
await this.backgroundApi.serviceDApp.dAppGetConnectedAccountsInfo(
request,
Expand Down Expand Up @@ -177,6 +179,7 @@ class ProviderApiAptos extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: IEncodedTxAptos,
): Promise<string> {
defaultLogger.discovery.dapp.dappRequest({ request });
const encodeTx = params;

const accounts = await this.getAccountsInfo(request);
Expand Down Expand Up @@ -260,6 +263,7 @@ class ProviderApiAptos extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: string,
): Promise<string> {
defaultLogger.discovery.dapp.dappRequest({ request });
const { account, accountInfo } = await this._getAccount(request);
const vault = await this.getAptosVault(request);

Expand Down Expand Up @@ -287,6 +291,7 @@ class ProviderApiAptos extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: string,
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const { account, accountInfo } = await this._getAccount(request);
const vault = await this.getAptosVault(request);

Expand Down Expand Up @@ -317,6 +322,7 @@ class ProviderApiAptos extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: IEncodedTxAptos,
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const { account, accountInfo } = await this._getAccount(request);
const result =
await this.backgroundApi.serviceDApp.openSignAndSendTransactionModal({
Expand All @@ -336,6 +342,7 @@ class ProviderApiAptos extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: ISignMessagePayload,
): Promise<ISignMessageResponse> {
defaultLogger.discovery.dapp.dappRequest({ request });
// @ts-expect-error
const isPetra = request.data?.aptosProviderType === 'petra';

Expand Down Expand Up @@ -377,6 +384,7 @@ class ProviderApiAptos extends ProviderApiBase {
type_args: any[];
},
): Promise<string> {
defaultLogger.discovery.dapp.dappRequest({ request });
const encodeTx: IEncodedTxAptos = {
type: 'entry_function_payload',
function: params.func,
Expand Down Expand Up @@ -443,6 +451,7 @@ class ProviderApiAptos extends ProviderApiBase {
property_types?: Array<string>;
},
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const { account, accountInfo } = await this._getAccount(request);
const encodeTx = generateTransferCreateNft(
account.address,
Expand Down Expand Up @@ -511,6 +520,7 @@ class ProviderApiAptos extends ProviderApiBase {
};
},
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const vault = await this.getAptosVault(request);
const rawTx = await vault.client.generateTransaction(
params.sender,
Expand All @@ -527,6 +537,7 @@ class ProviderApiAptos extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: Uint8Array | string,
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const { account, accountInfo } = await this._getAccount(request);
const bcsTxn: Uint8Array = decodeBytesTransaction(params);
const encodedTx = {
Expand Down
10 changes: 9 additions & 1 deletion packages/kit-bg/src/providers/ProviderApiBtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
providerApiMethod,
} from '@onekeyhq/shared/src/background/backgroundDecorators';
import { getNetworkIdsMap } from '@onekeyhq/shared/src/config/networkIds';
import { defaultLogger } from '@onekeyhq/shared/src/logger/logger';
import accountUtils from '@onekeyhq/shared/src/utils/accountUtils';
import { memoizee } from '@onekeyhq/shared/src/utils/cacheUtils';
import networkUtils from '@onekeyhq/shared/src/utils/networkUtils';
Expand Down Expand Up @@ -99,6 +100,7 @@ class ProviderApiBtc extends ProviderApiBase {
@providerApiMethod()
public async requestAccounts(request: IJsBridgeMessagePayload) {
return this.semaphore.runExclusive(async () => {
defaultLogger.discovery.dapp.dappRequest({ request });
const accounts = await this.getAccounts(request);
if (accounts && accounts.length) {
return accounts;
Expand Down Expand Up @@ -155,7 +157,7 @@ class ProviderApiBtc extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: ISwitchNetworkParams,
) {
console.log('ProviderApiBtc.switchNetwork');
defaultLogger.discovery.dapp.dappRequest({ request });
const accountsInfo =
await this.backgroundApi.serviceDApp.dAppGetConnectedAccountsInfo(
request,
Expand Down Expand Up @@ -223,6 +225,7 @@ class ProviderApiBtc extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: ISendBitcoinParams,
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const { toAddress, satoshis, feeRate } = params;
const accountsInfo = await this.getAccountsInfo(request);
const { accountInfo: { accountId, networkId, address } = {} } =
Expand Down Expand Up @@ -279,6 +282,7 @@ class ProviderApiBtc extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: ISignMessageParams,
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const { message, type } = params;
const accountsInfo = await this.getAccountsInfo(request);
const { accountInfo: { accountId, networkId } = {} } = accountsInfo[0];
Expand Down Expand Up @@ -317,6 +321,7 @@ class ProviderApiBtc extends ProviderApiBase {

@providerApiMethod()
public async pushTx(request: IJsBridgeMessagePayload, params: IPushTxParams) {
defaultLogger.discovery.dapp.dappRequest({ request });
const { rawTx } = params;
const accountsInfo = await this.getAccountsInfo(request);
const { accountInfo: { accountId, networkId, address } = {} } =
Expand Down Expand Up @@ -352,6 +357,7 @@ class ProviderApiBtc extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: ISignPsbtParams,
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const accountsInfo = await this.getAccountsInfo(request);
const { accountInfo: { accountId, networkId } = {} } = accountsInfo[0];

Expand Down Expand Up @@ -393,6 +399,7 @@ class ProviderApiBtc extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: ISignPsbtsParams,
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const accountsInfo = await this.getAccountsInfo(request);
const { accountInfo: { accountId, networkId } = {} } = accountsInfo[0];

Expand Down Expand Up @@ -530,6 +537,7 @@ class ProviderApiBtc extends ProviderApiBase {
request: IJsBridgeMessagePayload,
params: IPushPsbtParams,
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const accountsInfo = await this.getAccountsInfo(request);
const { accountInfo: { accountId, networkId, address } = {} } =
accountsInfo[0];
Expand Down
5 changes: 5 additions & 0 deletions packages/kit-bg/src/providers/ProviderApiCardano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
backgroundClass,
providerApiMethod,
} from '@onekeyhq/shared/src/background/backgroundDecorators';
import { defaultLogger } from '@onekeyhq/shared/src/logger/logger';
import { EMessageTypesCommon } from '@onekeyhq/shared/types/message';

import { vaultFactory } from '../vaults/factory';
Expand Down Expand Up @@ -84,6 +85,7 @@ class ProviderApiCardano extends ProviderApiBase {
@providerApiMethod()
async connect(request: IJsBridgeMessagePayload) {
return this.semaphore.runExclusive(async () => {
defaultLogger.discovery.dapp.dappRequest({ request });
const connectedAddress = await this.cardano_accounts(request);
if (connectedAddress) {
return connectedAddress;
Expand Down Expand Up @@ -173,6 +175,7 @@ class ProviderApiCardano extends ProviderApiBase {

@providerApiMethod()
async signTx(request: IJsBridgeMessagePayload, params: { tx: string }) {
defaultLogger.discovery.dapp.dappRequest({ request });
const vault = await this.getAdaVault(request);
if (!vault) {
throw new Error('Not connected to any account.');
Expand Down Expand Up @@ -200,6 +203,7 @@ class ProviderApiCardano extends ProviderApiBase {
payload: string;
},
) {
defaultLogger.discovery.dapp.dappRequest({ request });
if (typeof params.payload !== 'string') {
throw web3Errors.rpc.invalidInput();
}
Expand All @@ -226,6 +230,7 @@ class ProviderApiCardano extends ProviderApiBase {

@providerApiMethod()
async submitTx(request: IJsBridgeMessagePayload, params: string) {
defaultLogger.discovery.dapp.dappRequest({ request });
const { accountInfo: { accountId, networkId, address } = {} } = (
await this.getAccountsInfo(request)
)[0];
Expand Down
8 changes: 6 additions & 2 deletions packages/kit-bg/src/providers/ProviderApiConflux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
permissionRequired,
providerApiMethod,
} from '@onekeyhq/shared/src/background/backgroundDecorators';
import { defaultLogger } from '@onekeyhq/shared/src/logger/logger';
import { memoizee } from '@onekeyhq/shared/src/utils/cacheUtils';
import { toBigIntHex } from '@onekeyhq/shared/src/utils/numberUtils';
import { EMessageTypesEth } from '@onekeyhq/shared/types/message';
Expand Down Expand Up @@ -189,7 +190,7 @@ class ProviderApiConflux extends ProviderApiBase {

@providerApiMethod()
async cfx_requestAccounts(request: IJsBridgeMessagePayload) {
console.log('ProviderApiConflux.cfx_requestAccounts', request);
defaultLogger.discovery.dapp.dappRequest({ request });

const accounts = await this.cfx_accounts(request);
if (accounts && accounts.length) {
Expand All @@ -205,7 +206,7 @@ class ProviderApiConflux extends ProviderApiBase {
request: IJsBridgeMessagePayload,
transaction: IEncodedTxCfx,
) {
console.log('cfx_sendTransaction', request, transaction);
defaultLogger.discovery.dapp.dappRequest({ request });

const { accountInfo: { accountId, networkId } = {} } = (
await this.getAccountsInfo(request)
Expand All @@ -232,6 +233,7 @@ class ProviderApiConflux extends ProviderApiBase {

@providerApiMethod()
cfx_signTypedData_v4(request: IJsBridgeMessagePayload, ...messages: any[]) {
defaultLogger.discovery.dapp.dappRequest({ request });
return this._showSignMessageModal(request, {
type: EMessageTypesEth.TYPED_DATA_V4,
message: messages[1],
Expand All @@ -246,6 +248,7 @@ class ProviderApiConflux extends ProviderApiBase {

@providerApiMethod()
async personal_sign(request: IJsBridgeMessagePayload, ...messages: any[]) {
defaultLogger.discovery.dapp.dappRequest({ request });
const message = messages[0];

return this._showSignMessageModal(request, {
Expand All @@ -257,6 +260,7 @@ class ProviderApiConflux extends ProviderApiBase {

@providerApiMethod()
cfx_sign(request: IJsBridgeMessagePayload, ...messages: any[]) {
defaultLogger.discovery.dapp.dappRequest({ request });
return this._showSignMessageModal(request, {
type: EMessageTypesEth.ETH_SIGN,
message: messages[1],
Expand Down
7 changes: 7 additions & 0 deletions packages/kit-bg/src/providers/ProviderApiCosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
permissionRequired,
providerApiMethod,
} from '@onekeyhq/shared/src/background/backgroundDecorators';
import { defaultLogger } from '@onekeyhq/shared/src/logger/logger';
import accountUtils from '@onekeyhq/shared/src/utils/accountUtils';
import networkUtils from '@onekeyhq/shared/src/utils/networkUtils';
import timerUtils from '@onekeyhq/shared/src/utils/timerUtils';
Expand Down Expand Up @@ -239,6 +240,7 @@ class ProviderApiCosmos extends ProviderApiBase {
signOptions?: any;
},
): Promise<any> {
defaultLogger.discovery.dapp.dappRequest({ request });
const txWrapper = TransactionWrapper.fromAminoSignDoc(
params.signDoc,
undefined,
Expand Down Expand Up @@ -309,6 +311,7 @@ class ProviderApiCosmos extends ProviderApiBase {
signOptions?: any;
},
): Promise<any> {
defaultLogger.discovery.dapp.dappRequest({ request });
const networkId = this.convertCosmosChainId(params.signDoc.chainId);
if (!networkId) throw new Error('Invalid chainId');

Expand Down Expand Up @@ -383,6 +386,7 @@ class ProviderApiCosmos extends ProviderApiBase {
mode: string;
},
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const networkId = this.convertCosmosChainId(params.chainId);
if (!networkId) throw new Error('Invalid chainId');

Expand Down Expand Up @@ -411,6 +415,7 @@ class ProviderApiCosmos extends ProviderApiBase {
},
) {
return this.signMessageSemaphore.runExclusive(async () => {
defaultLogger.discovery.dapp.dappRequest({ request });
const paramsData = {
data: params.data,
signer: params.signer,
Expand Down Expand Up @@ -452,6 +457,7 @@ class ProviderApiCosmos extends ProviderApiBase {
data: string;
},
): Promise<any> {
defaultLogger.discovery.dapp.dappRequest({ request });
const txInfo = await this.signArbitraryMessage(request, params);

const [signerInfo] = txInfo.authInfo.signerInfos;
Expand Down Expand Up @@ -487,6 +493,7 @@ class ProviderApiCosmos extends ProviderApiBase {
};
},
): Promise<any> {
defaultLogger.discovery.dapp.dappRequest({ request });
const txInfo = await this.signArbitraryMessage(request, params);

const [signerInfo] = txInfo.authInfo.signerInfos;
Expand Down
Loading

0 comments on commit 7e654ab

Please sign in to comment.