-
Notifications
You must be signed in to change notification settings - Fork 496
Feat/cosmos enigma utils OK-50764 OK-50786 OK-50962 #10456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
06b84eb
630f344
428d914
7649397
700a5c5
1f0691f
dbf22ef
4065616
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| declare module 'miscreant' { | ||
| interface ICryptoProvider { | ||
| importBlockCipherKey(keyData: Uint8Array): Promise<IBlockCipher>; | ||
| } | ||
|
|
||
| interface IBlockCipher { | ||
| clear(): this; | ||
| encryptBlock(block: Uint8Array): Promise<Uint8Array>; | ||
| } | ||
|
|
||
| export class PolyfillCryptoProvider implements ICryptoProvider { | ||
| importBlockCipherKey(keyData: Uint8Array): Promise<IBlockCipher>; | ||
| } | ||
|
|
||
| export class WebCryptoProvider implements ICryptoProvider { | ||
| importBlockCipherKey(keyData: Uint8Array): Promise<IBlockCipher>; | ||
| } | ||
|
|
||
| export class SIV { | ||
| static importKey( | ||
| keyData: Uint8Array, | ||
| algorithm: string, | ||
| provider: ICryptoProvider, | ||
| ): Promise<SIV>; | ||
|
|
||
| seal( | ||
| plaintext: Uint8Array, | ||
| associatedData: Uint8Array[], | ||
| ): Promise<Uint8Array>; | ||
|
|
||
| open( | ||
| ciphertext: Uint8Array, | ||
| associatedData: Uint8Array[], | ||
| ): Promise<Uint8Array>; | ||
|
|
||
| clear(): this; | ||
| } | ||
|
|
||
| export class AEAD { | ||
| static importKey( | ||
| keyData: Uint8Array, | ||
| algorithm: string, | ||
| provider: ICryptoProvider, | ||
| ): Promise<AEAD>; | ||
|
|
||
| seal( | ||
| plaintext: Uint8Array, | ||
| nonce: Uint8Array, | ||
| associatedData?: Uint8Array, | ||
| ): Promise<Uint8Array>; | ||
|
|
||
| open( | ||
| ciphertext: Uint8Array, | ||
| nonce: Uint8Array, | ||
| associatedData?: Uint8Array, | ||
| ): Promise<Uint8Array>; | ||
|
|
||
| clear(): this; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,10 @@ import { | |
| IMPL_COSMOS, | ||
| } from '@onekeyhq/shared/src/engine/engineConsts'; | ||
| import { OneKeyLocalError } from '@onekeyhq/shared/src/errors'; | ||
| import { | ||
| EDAppConnectionModal, | ||
| EModalRoutes, | ||
| } from '@onekeyhq/shared/src/routes'; | ||
| import { defaultLogger } from '@onekeyhq/shared/src/logger/logger'; | ||
| import accountUtils from '@onekeyhq/shared/src/utils/accountUtils'; | ||
| import hexUtils from '@onekeyhq/shared/src/utils/hexUtils'; | ||
|
|
@@ -33,8 +37,11 @@ import type { INetworkAccount } from '@onekeyhq/shared/types/account'; | |
| import type { IConnectionAccountInfo } from '@onekeyhq/shared/types/dappConnection'; | ||
| import { EMessageTypesCommon } from '@onekeyhq/shared/types/message'; | ||
|
|
||
| import type { SecretNetworkEncryption } from '../vaults/impls/cosmos/sdkCosmos/SecretNetworkEncryption'; | ||
| import { vaultFactory } from '../vaults/factory'; | ||
| import ProviderApiBase from './ProviderApiBase'; | ||
|
|
||
| import type VaultCosmos from '../vaults/impls/cosmos/Vault'; | ||
| import type { IProviderBaseBackgroundNotifyInfo } from './ProviderApiBase'; | ||
| import type { IJsBridgeMessagePayload } from '@onekeyfe/cross-inpage-provider-types'; | ||
|
|
||
|
|
@@ -165,7 +172,7 @@ class ProviderApiCosmos extends ProviderApiBase { | |
| networkId, | ||
| }); | ||
| if (!network) { | ||
| throw new OneKeyLocalError('Invalid chainId'); | ||
| return undefined; | ||
| } | ||
ByteZhang1024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
|
|
@@ -722,6 +729,90 @@ class ProviderApiCosmos extends ProviderApiBase { | |
| }); | ||
| } | ||
|
|
||
| // Enigma (Secret Network) support | ||
| private async _getOrCreateEnigmaUtils( | ||
| request: IJsBridgeMessagePayload, | ||
| chainId: string, | ||
| ): Promise<SecretNetworkEncryption> { | ||
| const networkId = this.convertCosmosChainId(chainId); | ||
| if (!networkId) throw new OneKeyLocalError('Invalid chainId'); | ||
|
|
||
| const account = await this._getAccount(request, networkId); | ||
|
|
||
| const { accountInfo } = account; | ||
| const walletId = accountInfo?.walletId ?? ''; | ||
| const accountId = accountInfo?.accountId ?? account.account.id; | ||
|
|
||
| let password = await this.backgroundApi.servicePassword.getCachedPassword(); | ||
|
|
||
| if (!password) { | ||
| const result = (await this.backgroundApi.serviceDApp.openModal({ | ||
| request, | ||
| screens: [ | ||
| EModalRoutes.DAppConnectionModal, | ||
|
Comment on lines
+748
to
+752
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| EDAppConnectionModal.CosmosEnigmaUnlockModal, | ||
| ], | ||
| params: { | ||
| walletId, | ||
| accountId, | ||
| networkId: accountInfo?.networkId ?? networkId, | ||
| }, | ||
| fullScreen: true, | ||
| })) as { password: string }; | ||
| password = result.password; | ||
| } | ||
|
|
||
| const vault = (await vaultFactory.getVault({ | ||
| networkId: accountInfo?.networkId ?? networkId, | ||
| accountId, | ||
| })) as VaultCosmos; | ||
|
|
||
| return vault.getOrCreateEnigmaUtils({ password }); | ||
| } | ||
|
|
||
| @providerApiMethod() | ||
| public async getEnigmaPubKey( | ||
| request: IJsBridgeMessagePayload, | ||
| params: { chainId: string }, | ||
| ): Promise<string> { | ||
| const utils = await this._getOrCreateEnigmaUtils(request, params.chainId); | ||
| const pubkey = await utils.getPubkey(); | ||
| return bytesToHex(pubkey); | ||
| } | ||
|
Comment on lines
+773
to
+781
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Missing All existing Cosmos provider methods that perform sensitive cryptographic operations ( Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| @providerApiMethod() | ||
| public async enigmaEncrypt( | ||
| request: IJsBridgeMessagePayload, | ||
| params: { chainId: string; contractCodeHash: string; msg: object }, | ||
| ): Promise<string> { | ||
| const utils = await this._getOrCreateEnigmaUtils(request, params.chainId); | ||
| const encrypted = await utils.encrypt(params.contractCodeHash, params.msg); | ||
| return bytesToHex(encrypted); | ||
| } | ||
|
|
||
| @providerApiMethod() | ||
| public async enigmaDecrypt( | ||
| request: IJsBridgeMessagePayload, | ||
| params: { chainId: string; ciphertext: string; nonce: string }, | ||
| ): Promise<string> { | ||
| const utils = await this._getOrCreateEnigmaUtils(request, params.chainId); | ||
| const decrypted = await utils.decrypt( | ||
| hexToBytes(params.ciphertext), | ||
| hexToBytes(params.nonce), | ||
| ); | ||
| return bytesToHex(decrypted); | ||
| } | ||
|
|
||
| @providerApiMethod() | ||
| public async enigmaGetTxEncryptionKey( | ||
| request: IJsBridgeMessagePayload, | ||
| params: { chainId: string; nonce: string }, | ||
| ): Promise<string> { | ||
| const utils = await this._getOrCreateEnigmaUtils(request, params.chainId); | ||
| const key = await utils.getTxEncryptionKey(hexToBytes(params.nonce)); | ||
| return bytesToHex(key); | ||
| } | ||
|
|
||
| @providerApiMethod() | ||
| public async cosmos_signDirect( | ||
| request: IJsBridgeMessagePayload, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.