Skip to content

Commit

Permalink
refactor: change case of EthereumMethodType enum member (#11612)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed May 12, 2024
1 parent 9ca604b commit c79a0d0
Show file tree
Hide file tree
Showing 28 changed files with 206 additions and 231 deletions.
13 changes: 5 additions & 8 deletions packages/mask/background/services/wallet/services/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function send(payload: JsonRpcPayload, options?: TransactionOptions
: new Signer(owner || from!, signWithWallet)

switch (payload.method) {
case EthereumMethodType.ETH_SEND_TRANSACTION:
case EthereumMethodType.eth_sendTransaction:
case EthereumMethodType.MASK_REPLACE_TRANSACTION:
if (!signableConfig) throw new Error('No transaction to be sent.')

Expand All @@ -59,22 +59,22 @@ export async function send(payload: JsonRpcPayload, options?: TransactionOptions
} catch (error) {
throw ErrorEditor.from(error, null, 'Failed to send transaction.').error
}
case EthereumMethodType.ETH_SIGN:
case EthereumMethodType.PERSONAL_SIGN:
case EthereumMethodType.eth_sign:
case EthereumMethodType.personal_sign:
try {
if (!signableMessage) throw new Error('No message to be signed.')
return createJsonRpcResponse(pid, await signer.signMessage(signableMessage))
} catch (error) {
throw ErrorEditor.from(error, null, 'Failed to sign message.').error
}
case EthereumMethodType.ETH_SIGN_TYPED_DATA:
case EthereumMethodType.eth_signTypedData_v4:
try {
if (!signableMessage) throw new Error('No typed data to be signed.')
return createJsonRpcResponse(pid, await signer.signTypedData(signableMessage))
} catch (error) {
throw ErrorEditor.from(error, null, 'Failed to sign typed data.').error
}
case EthereumMethodType.ETH_SIGN_TRANSACTION:
case EthereumMethodType.eth_signTransaction:
try {
if (!signableConfig) throw new Error('No transaction to be signed.')
return createJsonRpcResponse(pid, await signer.signTransaction(signableConfig))
Expand All @@ -89,9 +89,6 @@ export async function send(payload: JsonRpcPayload, options?: TransactionOptions
} catch (error) {
throw ErrorEditor.from(error, null, 'Failed to deploy.').error
}
case EthereumMethodType.ETH_DECRYPT:
case EthereumMethodType.ETH_GET_ENCRYPTION_PUBLIC_KEY:
throw new Error('Method not implemented.')
default:
try {
const result = await EVMRequestReadonly.request(
Expand Down
20 changes: 10 additions & 10 deletions packages/mask/entry-sdk/bridge/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import { isValidChecksumAddress } from '@ethereumjs/util'

const PassthroughMethods = [
...readonlyMethodType,
EthereumMethodType.ETH_GET_FILTER_CHANGES,
EthereumMethodType.ETH_GET_FILTER_LOGS,
EthereumMethodType.ETH_NEW_BLOCK_FILTER,
EthereumMethodType.ETH_NEW_FILTER,
EthereumMethodType.ETH_UNINSTALL_FILTER,
EthereumMethodType.eth_getFilterChanges,
EthereumMethodType.eth_getFilterLogs,
EthereumMethodType.eth_newBlockFilter,
EthereumMethodType.eth_newFilter,
EthereumMethodType.eth_uninstallFilter,
]
type PassthroughMethods = (typeof PassthroughMethods)[number]
const passthroughMethods: Record<PassthroughMethods, (...params: any[]) => Promise<any>> = {} as any
Expand Down Expand Up @@ -120,7 +120,7 @@ const methods: Methods = {
silent: false,
readonly: false,
}).request({
method: EthereumMethodType.ETH_SIGN_TYPED_DATA,
method: EthereumMethodType.eth_signTypedData_v4,
params: [requestedAddress, typedData],
})
},
Expand All @@ -136,7 +136,7 @@ const methods: Methods = {
silent: false,
readonly: false,
}).request({
method: EthereumMethodType.PERSONAL_SIGN,
method: EthereumMethodType.personal_sign,
params: [challenge, requestedAddress],
})
},
Expand All @@ -154,7 +154,7 @@ const methods: Methods = {
readonly: false,
})
return p.request({
method: EthereumMethodType.ETH_SEND_TRANSACTION,
method: EthereumMethodType.eth_sendTransaction,
params: [options],
})
},
Expand All @@ -165,7 +165,7 @@ const methods: Methods = {
readonly: false,
})
return p.request({
method: EthereumMethodType.ETH_SEND_RAW_TRANSACTION,
method: EthereumMethodType.eth_sendRawTransaction,
params: [transaction] as any,
})
},
Expand Down Expand Up @@ -287,7 +287,7 @@ const methods: Methods = {
readonly: false,
})
p.request({
method: EthereumMethodType.WATCH_ASSET,
method: EthereumMethodType.wallet_watchAsset,
params: [{ type, options: { address, decimals, image, symbol, tokenId } }],
}).catch(() => {})
return true
Expand Down
40 changes: 14 additions & 26 deletions packages/mask/popups/pages/Wallet/Interaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ const useStyles = makeStyles()((theme) => ({
}))

const signRequest = [
EthereumMethodType.ETH_SIGN,
EthereumMethodType.ETH_SIGN_TYPED_DATA,
EthereumMethodType.PERSONAL_SIGN,
EthereumMethodType.eth_sign,
EthereumMethodType.eth_signTypedData_v4,
EthereumMethodType.personal_sign,
]

const approveParametersType = [
Expand Down Expand Up @@ -199,26 +199,15 @@ const Interaction = memo((props: InteractionProps) => {
if (!signRequest.includes(currentRequest.request.arguments.method)) return undefined
const { method, params } = currentRequest.request.arguments

if (method === EthereumMethodType.ETH_SIGN_TYPED_DATA) {
if (method === EthereumMethodType.eth_signTypedData_v4) {
if (typeof params[1] === 'object') return params[1]
return undefined
}
if (method === EthereumMethodType.PERSONAL_SIGN) {
if (method === EthereumMethodType.personal_sign) {
if (typeof params[0] === 'string') return params[0]
return undefined
}

// https://medium.com/metamask/scaling-web3-with-signtypeddata-91d6efc8b290
if (method === EthereumMethodType.ETH_SIGN_TYPED_DATA_OLD_V1) {
if (typeof params[0] === 'object') return params[0]
return undefined
}
// https://medium.com/metamask/eip712-is-coming-what-to-expect-and-how-to-use-it-bb92fd1a7a26
if (method === EthereumMethodType.ETH_SIGN_TYPED_DATA_OLD_V3) {
if (typeof params[1] === 'object') return params[0]
return undefined
}
if (method === EthereumMethodType.ETH_SIGN) {
if (method === EthereumMethodType.eth_sign) {
if (typeof params[1] === 'string') return params[1]
return undefined
}
Expand Down Expand Up @@ -265,11 +254,9 @@ const Interaction = memo((props: InteractionProps) => {
getInteractingWallet: {
const req = currentRequest.request.arguments
if (!req) break getInteractingWallet
if (req.method === EthereumMethodType.ETH_DECRYPT) interactingWallet = req.params[1]
if (req.method === EthereumMethodType.ETH_GET_ENCRYPTION_PUBLIC_KEY) interactingWallet = req.params[0]
if (req.method === EthereumMethodType.ETH_SIGN_TYPED_DATA) interactingWallet = req.params[0]
if (req.method === EthereumMethodType.PERSONAL_SIGN) interactingWallet = req.params[1]
if (req.method === EthereumMethodType.ETH_SEND_TRANSACTION) interactingWallet = req.params[0]?.from
if (req.method === EthereumMethodType.eth_signTypedData_v4) interactingWallet = req.params[0]
if (req.method === EthereumMethodType.personal_sign) interactingWallet = req.params[1]
if (req.method === EthereumMethodType.eth_sendTransaction) interactingWallet = req.params[0]?.from

interactingWallet = String(interactingWallet)
if (interactingWallet && !interactingWallet.startsWith('0x')) interactingWallet = undefined
Expand All @@ -284,7 +271,7 @@ const Interaction = memo((props: InteractionProps) => {
async (paymentToken: string, approveAmount: string, gasConfig: GasConfig | undefined) => {
try {
let params = currentRequest.request.arguments.params
if (currentRequest.request.arguments.method === EthereumMethodType.WATCH_ASSET) {
if (currentRequest.request.arguments.method === EthereumMethodType.wallet_watchAsset) {
const type = params[0].type
const address = params[0].options.address
if (type === 'ERC21') {
Expand Down Expand Up @@ -373,11 +360,11 @@ const Interaction = memo((props: InteractionProps) => {
)
}

if (currentRequest.request.arguments.method === EthereumMethodType.ETH_SIGN_TYPED_DATA) {
if (currentRequest.request.arguments.method === EthereumMethodType.eth_signTypedData_v4) {
if (typeof params[1] === 'object') params[1] = JSON.stringify(params[1])
}

if (currentRequest.request.arguments.method === EthereumMethodType.ETH_SEND_TRANSACTION) {
if (currentRequest.request.arguments.method === EthereumMethodType.eth_sendTransaction) {
if (params[0].type === '0x0') {
delete params[0].type
delete params[0].gasPrice
Expand Down Expand Up @@ -553,7 +540,8 @@ const InteractionItem = memo((props: InteractionItemProps) => {
const isUnlockERC20 = transaction.formattedTransaction?.popup?.spender
const isUnlockERC721 = transaction.formattedTransaction?.popup?.erc721Spender
const content =
transaction.payload.method === EthereumMethodType.WATCH_ASSET ? <WatchTokenRequest transaction={transaction} />
transaction.payload.method === EthereumMethodType.wallet_watchAsset ?
<WatchTokenRequest transaction={transaction} />
: isSignRequest ? <SignRequestInfo message={message} rawMessage={rawMessage} origin={requestOrigin} />
: isUnlockERC20 ?
<UnlockERC20Token
Expand Down
18 changes: 9 additions & 9 deletions packages/web3-providers/src/Web3/EVM/apis/ConnectionAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ export class ConnectionAPI
case 'message':
return this.Request.request<string>(
{
method: EthereumMethodType.PERSONAL_SIGN,
method: EthereumMethodType.personal_sign,
params: [message, options.account, ''].filter((x) => typeof x !== 'undefined'),
},
options,
)
case 'typedData':
return this.Request.request<string>(
{
method: EthereumMethodType.ETH_SIGN_TYPED_DATA,
method: EthereumMethodType.eth_signTypedData_v4,
params: [options.account, message],
},
options,
Expand All @@ -262,7 +262,7 @@ export class ConnectionAPI
override async signTransaction(transaction: Transaction, initial?: EVMConnectionOptions) {
return this.Request.request<string>(
{
method: EthereumMethodType.ETH_SIGN_TRANSACTION,
method: EthereumMethodType.eth_signTransaction,
params: [transaction],
},
initial,
Expand All @@ -276,7 +276,7 @@ export class ConnectionAPI
override supportedChainIds(initial?: EVMConnectionOptions) {
return this.Request.request<ChainId[]>(
{
method: EthereumMethodType.ETH_SUPPORTED_CHAIN_IDS,
method: EthereumMethodType.eth_supportedChainIds,
params: [],
},
initial,
Expand All @@ -287,7 +287,7 @@ export class ConnectionAPI
const options = this.ConnectionOptions.fill(initial)
return this.Request.request<string>(
{
method: EthereumMethodType.ETH_CALL_USER_OPERATION,
method: EthereumMethodType.eth_callUserOperation,
params: [
owner,
{
Expand All @@ -304,7 +304,7 @@ export class ConnectionAPI
const options = this.ConnectionOptions.fill(initial)
return this.Request.request<string>(
{
method: EthereumMethodType.ETH_SEND_USER_OPERATION,
method: EthereumMethodType.eth_sendUserOperation,
params: [
owner,
{
Expand All @@ -330,7 +330,7 @@ export class ConnectionAPI

return this.Request.request<string>(
{
method: EthereumMethodType.ETH_SEND_TRANSACTION,
method: EthereumMethodType.eth_sendTransaction,
params: [
{
...tx,
Expand All @@ -355,7 +355,7 @@ export class ConnectionAPI

return this.Request.request<string>(
{
method: EthereumMethodType.ETH_SEND_TRANSACTION,
method: EthereumMethodType.eth_sendTransaction,
params: [
{
...tx,
Expand Down Expand Up @@ -418,7 +418,7 @@ export class ConnectionAPI
// send a transaction which will add into the internal transaction list and start to watch it for confirmation
const hash = await this.Request.request<string>(
{
method: EthereumMethodType.ETH_SEND_TRANSACTION,
method: EthereumMethodType.eth_sendTransaction,
params: [new AccountTransaction(transaction).fill(options.overrides)],
},
options,
Expand Down
26 changes: 13 additions & 13 deletions packages/web3-providers/src/Web3/EVM/apis/ConnectionReadonlyAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class EVMConnectionReadonlyAPI
async getGasPrice(initial?: EVMConnectionOptions): Promise<string> {
return this.Request.request<string>(
{
method: EthereumMethodType.ETH_GAS_PRICE,
method: EthereumMethodType.eth_gasPrice,
params: [],
},
initial,
Expand Down Expand Up @@ -628,7 +628,7 @@ export class EVMConnectionReadonlyAPI
const options = this.ConnectionOptions.fill(initial)
const accounts = await this.Request.request<string[]>(
{
method: EthereumMethodType.ETH_ACCOUNTS,
method: EthereumMethodType.eth_accounts,
params: [],
},
options,
Expand All @@ -640,7 +640,7 @@ export class EVMConnectionReadonlyAPI
const options = this.ConnectionOptions.fill(initial)
const chainId = await this.Request.request<string>(
{
method: EthereumMethodType.ETH_CHAIN_ID,
method: EthereumMethodType.eth_chainId,
params: [],
},
options,
Expand All @@ -651,7 +651,7 @@ export class EVMConnectionReadonlyAPI
getBlock(noOrId: number | string, initial?: EVMConnectionOptions) {
return this.Request.request<Block>(
{
method: EthereumMethodType.ETH_GET_BLOCK_BY_NUMBER,
method: EthereumMethodType.eth_getBlockByNumber,
params: [web3_utils.toHex(noOrId), false],
},
initial,
Expand All @@ -661,7 +661,7 @@ export class EVMConnectionReadonlyAPI
getBlockNumber(initial?: EVMConnectionOptions) {
return this.Request.request<number>(
{
method: EthereumMethodType.ETH_BLOCK_NUMBER,
method: EthereumMethodType.eth_blockNumber,
params: [],
},
initial,
Expand All @@ -678,7 +678,7 @@ export class EVMConnectionReadonlyAPI
getBalance(address: string, initial?: EVMConnectionOptions) {
return this.Request.request<string>(
{
method: EthereumMethodType.ETH_GET_BALANCE,
method: EthereumMethodType.eth_getBalance,
params: [address, 'latest'],
},
initial,
Expand All @@ -688,7 +688,7 @@ export class EVMConnectionReadonlyAPI
getCode(address: string, initial?: EVMConnectionOptions) {
return this.Request.request<string>(
{
method: EthereumMethodType.ETH_GET_CODE,
method: EthereumMethodType.eth_getCode,
params: [address, 'latest'],
},
initial,
Expand All @@ -698,7 +698,7 @@ export class EVMConnectionReadonlyAPI
async getTransaction(hash: string, initial?: EVMConnectionOptions) {
return this.Request.request<TransactionDetailed>(
{
method: EthereumMethodType.ETH_GET_TRANSACTION_BY_HASH,
method: EthereumMethodType.eth_getTransactionByHash,
params: [hash],
},
initial,
Expand All @@ -710,7 +710,7 @@ export class EVMConnectionReadonlyAPI
const options = this.ConnectionOptions.fill(initial)
return await this.Request.request<string>(
{
method: EthereumMethodType.ETH_ESTIMATE_GAS,
method: EthereumMethodType.eth_estimateGas,
params: [
new AccountTransaction({
from: options.account,
Expand All @@ -729,7 +729,7 @@ export class EVMConnectionReadonlyAPI
getTransactionReceipt(hash: string, initial?: EVMConnectionOptions) {
return this.Request.request<TransactionReceipt>(
{
method: EthereumMethodType.ETH_GET_TRANSACTION_RECEIPT,
method: EthereumMethodType.eth_getTransactionReceipt,
params: [hash],
},
initial,
Expand All @@ -744,7 +744,7 @@ export class EVMConnectionReadonlyAPI
async getTransactionNonce(address: string, initial?: EVMConnectionOptions) {
const nonce = await this.Request.request<number | string>(
{
method: EthereumMethodType.ETH_GET_TRANSACTION_COUNT,
method: EthereumMethodType.eth_getTransactionCount,
params: [address, 'latest'],
},
initial,
Expand Down Expand Up @@ -800,7 +800,7 @@ export class EVMConnectionReadonlyAPI
const options = this.ConnectionOptions.fill(initial)
return this.Request.request<string>(
{
method: EthereumMethodType.ETH_CALL,
method: EthereumMethodType.eth_call,
params: [new AccountTransaction(transaction).fill(options.overrides), 'latest'],
},
options,
Expand All @@ -814,7 +814,7 @@ export class EVMConnectionReadonlyAPI
sendSignedTransaction(signature: string, initial?: EVMConnectionOptions) {
return this.Request.request<string>(
{
method: EthereumMethodType.ETH_SEND_RAW_TRANSACTION,
method: EthereumMethodType.eth_sendRawTransaction,
params: [signature],
},
initial,
Expand Down

0 comments on commit c79a0d0

Please sign in to comment.