Skip to content

Commit

Permalink
v4 fix: support EIP6963 & fix cosmos signMessage (#4018)
Browse files Browse the repository at this point in the history
* feat: support petra wallet switch

* feat: support cosmos signMessage

* feat: support EIP6963

* chore: fix lint
  • Loading branch information
ByteZhang1024 authored Jan 11, 2024
1 parent a55d505 commit 4088486
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 244 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@
"@cmdcode/crypto-utils": "1.9.5",
"@cmdcode/tapscript": "1.2.9",
"@legendapp/state": "^1.2.9",
"@onekeyfe/cross-inpage-provider-core": "1.1.50",
"@onekeyfe/cross-inpage-provider-errors": "1.1.50",
"@onekeyfe/cross-inpage-provider-injected": "1.1.50",
"@onekeyfe/cross-inpage-provider-types": "1.1.50",
"@onekeyfe/extension-bridge-hosted": "1.1.50",
"@onekeyfe/cross-inpage-provider-core": "1.1.51",
"@onekeyfe/cross-inpage-provider-errors": "1.1.51",
"@onekeyfe/cross-inpage-provider-injected": "1.1.51",
"@onekeyfe/cross-inpage-provider-types": "1.1.51",
"@onekeyfe/extension-bridge-hosted": "1.1.51",
"@onekeyfe/hd-ble-sdk": "0.3.37",
"@onekeyfe/hd-core": "0.3.37",
"@onekeyfe/hd-shared": "0.3.37",
"@onekeyfe/hd-transport": "0.3.37",
"@onekeyfe/hd-web-sdk": "0.3.37",
"@onekeyfe/onekey-cross-webview": "1.1.50",
"@onekeyfe/onekey-cross-webview": "1.1.51",
"@starcoin/starcoin": "2.1.5",
"@web3-react/core": "8.0.35-beta.0",
"@web3-react/empty": "8.0.20-beta.0",
Expand Down
37 changes: 37 additions & 0 deletions packages/engine/src/vaults/impl/cosmos/KeyringHardware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
import { HardwareError } from '@onekeyfe/hd-shared';
import BigNumber from 'bignumber.js';

import type { DBVariantAccount } from '@onekeyhq/engine/src/types/account';
import { AccountType } from '@onekeyhq/engine/src/types/account';
Expand All @@ -14,7 +15,10 @@ import { stripHexPrefix } from '../../utils/hexUtils';

import { pubkeyToBaseAddress } from './sdk/address';
import { generateSignBytes, serializeSignedTx } from './sdk/txBuilder';
import { TransactionWrapper } from './sdk/wrapper';
import { getADR36SignDoc, getDataForADR36 } from './utils';

import type { CommonMessage } from '../../../types/message';
import type {
IHardwareGetAddressParams,
IPrepareHardwareAccountsParams,
Expand Down Expand Up @@ -225,4 +229,37 @@ export class KeyringHardware extends KeyringHardwareBase {

throw convertDeviceError(response.payload);
}

override async signMessage(messages: CommonMessage[]): Promise<string[]> {
const dbAccount = (await this.getDbAccount()) as DBVariantAccount;

return Promise.all(
messages.map(async (commonMessage) => {
const { data, signer } = JSON.parse(commonMessage.message);

const [messageData] = getDataForADR36(data);
const unSignDoc = getADR36SignDoc(signer, messageData);
const encodedTx = TransactionWrapper.fromAminoSignDoc(
unSignDoc,
undefined,
);

const { rawTx } = await this.signTransaction({
inputs: [
{
address: dbAccount.address,
value: new BigNumber(0),
publicKey: dbAccount.pub,
},
],
outputs: [],
payload: {
encodedTx,
},
});

return rawTx;
}),
);
}
}
43 changes: 43 additions & 0 deletions packages/engine/src/vaults/impl/cosmos/KeyringHd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sha256 } from '@noble/hashes/sha256';
import BigNumber from 'bignumber.js';

import type { ExportedSeedCredential } from '@onekeyhq/engine/src/dbs/base';
import { OneKeyInternalError } from '@onekeyhq/engine/src/errors';
Expand All @@ -14,7 +15,10 @@ import { KeyringHdBase } from '../../keyring/KeyringHdBase';

import { pubkeyToBaseAddress } from './sdk/address';
import { serializeSignedTx, serializeTxForSignature } from './sdk/txBuilder';
import { TransactionWrapper } from './sdk/wrapper';
import { getADR36SignDoc, getDataForADR36 } from './utils';

import type { CommonMessage } from '../../../types/message';
import type {
IPrepareSoftwareAccountsParams,
ISignCredentialOptions,
Expand Down Expand Up @@ -148,4 +152,43 @@ export class KeyringHd extends KeyringHdBase {
rawTx: Buffer.from(rawTx).toString('base64'),
};
}

override async signMessage(
messages: CommonMessage[],
options: ISignCredentialOptions,
): Promise<string[]> {
const dbAccount = (await this.getDbAccount()) as DBVariantAccount;

return Promise.all(
messages.map(async (commonMessage) => {
const { data, signer } = JSON.parse(commonMessage.message);

const [messageData] = getDataForADR36(data);
const unSignDoc = getADR36SignDoc(signer, messageData);
const encodedTx = TransactionWrapper.fromAminoSignDoc(
unSignDoc,
undefined,
);

const { rawTx } = await this.signTransaction(
{
inputs: [
{
address: dbAccount.address,
value: new BigNumber(0),
publicKey: dbAccount.pub,
},
],
outputs: [],
payload: {
encodedTx,
},
},
options,
);

return rawTx;
}),
);
}
}
1 change: 0 additions & 1 deletion packages/engine/src/vaults/impl/cosmos/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import { TxAminoBuilder } from './sdk/amino/TxAminoBuilder';
import { defaultAminoMsgOpts } from './sdk/amino/types';
import { MessageType } from './sdk/message';
import { queryRegistry } from './sdk/query/IQuery';
import { Type } from './sdk/query/mintScanTypes';
import { OneKeyQuery } from './sdk/query/OneKeyQuery';
import { serializeSignedTx } from './sdk/txBuilder';
import { TxMsgBuilder } from './sdk/txMsgBuilder';
Expand Down
69 changes: 31 additions & 38 deletions packages/kit-bg/src/providers/ProviderApiCosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import { IInjectedProviderNames } from '@onekeyfe/cross-inpage-provider-types';
import { PubKey } from 'cosmjs-types/cosmos/crypto/ed25519/keys';
import { AuthInfo, TxBody } from 'cosmjs-types/cosmos/tx/v1beta1/tx';

import { CommonMessageTypes } from '@onekeyhq/engine/src/types/message';
import type { BroadcastMode } from '@onekeyhq/engine/src/vaults/impl/cosmos/NodeClient';
import type { StdSignDoc } from '@onekeyhq/engine/src/vaults/impl/cosmos/sdk/amino/types';
import { deserializeTx } from '@onekeyhq/engine/src/vaults/impl/cosmos/sdk/txBuilder';
import { TransactionWrapper } from '@onekeyhq/engine/src/vaults/impl/cosmos/sdk/wrapper';
import { getAminoSignDoc } from '@onekeyhq/engine/src/vaults/impl/cosmos/sdk/wrapper/utils';
import {
getADR36SignDoc,
getDataForADR36,
} from '@onekeyhq/engine/src/vaults/impl/cosmos/utils';
import type VaultCosmos from '@onekeyhq/engine/src/vaults/impl/cosmos/Vault';
import type { ISignedTxPro } from '@onekeyhq/engine/src/vaults/types';
import { getActiveWalletAccount } from '@onekeyhq/kit/src/hooks';
Expand Down Expand Up @@ -401,36 +398,51 @@ class ProviderApiCosmos extends ProviderApiBase {
return Promise.resolve(res.txid);
}

@permissionRequired()
@providerApiMethod()
public async signArbitrary(
private async signArbitraryMessage(
request: IJsBridgeMessagePayload,
params: {
chainId: string;
signer: string;
data: string;
},
): Promise<any> {
debugLogger.providerApi.info('cosmos signArbitrary', params);
) {
const paramsData = {
data: params.data,
signer: params.signer,
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [data, isADR36WithString] = getDataForADR36(params.data);
const unsignDoc = getADR36SignDoc(params.signer, data);
const networkId = this.convertCosmosChainId(params.chainId);

const encodeTx = unsignDoc;
const result = (await this.backgroundApi.serviceDapp.openSignAndSendModal(
request,
{
encodedTx: TransactionWrapper.fromAminoSignDoc(encodeTx, undefined),
unsignedMessage: {
type: CommonMessageTypes.SIGN_MESSAGE,
message: JSON.stringify(paramsData),
secure: true,
},
signOnly: true,
networkId,
},
)) as ISignedTxPro;
)) as string;

const txInfo = deserializeTx(
hexToBytes(Buffer.from(result.rawTx, 'base64').toString('hex')),
return deserializeTx(
hexToBytes(Buffer.from(result, 'base64').toString('hex')),
);
}

@permissionRequired()
@providerApiMethod()
public async signArbitrary(
request: IJsBridgeMessagePayload,
params: {
chainId: string;
signer: string;
data: string;
},
): Promise<any> {
debugLogger.providerApi.info('cosmos signArbitrary', params);

const txInfo = await this.signArbitraryMessage(request, params);

const [signerInfo] = txInfo.authInfo.signerInfos;
const [signature] = txInfo.signatures;
Expand Down Expand Up @@ -466,26 +478,7 @@ class ProviderApiCosmos extends ProviderApiBase {
},
): Promise<any> {
debugLogger.providerApi.info('cosmos verifyArbitrary', params);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [data, isADR36WithString] = getDataForADR36(params.data);
const unsignDoc = getADR36SignDoc(params.signer, data);

const networkId = this.convertCosmosChainId(params.chainId);

const encodeTx = unsignDoc;
const result = (await this.backgroundApi.serviceDapp.openSignAndSendModal(
request,
{
encodedTx: TransactionWrapper.fromAminoSignDoc(encodeTx, undefined),
signOnly: true,
networkId,
},
)) as ISignedTxPro;

const txInfo = deserializeTx(
hexToBytes(Buffer.from(result.rawTx, 'base64').toString('hex')),
);
const txInfo = await this.signArbitraryMessage(request, params);

const [signerInfo] = txInfo.authInfo.signerInfos;
const [signature] = txInfo.signatures;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const CWalletSwitchDefaultConfig: Record<string, WalletSwitchItem> = {
'APTOS-Petra': {
logo: AptosPetraLogo,
title: 'Petra',
propertyKeys: ['aptos'],
propertyKeys: ['aptos', 'petra'],
enable: true,
},
'APTOS-Martian': {
Expand Down
Loading

0 comments on commit 4088486

Please sign in to comment.