-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathEIP155RequestHandlerUtil.ts
100 lines (88 loc) · 3.74 KB
/
EIP155RequestHandlerUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { EIP155_CHAINS, EIP155_SIGNING_METHODS, TEIP155Chain } from '@/data/EIP155Data'
import { getWallet } from '@/utils/EIP155WalletUtil'
import { getSignParamsMessage, getSignTypedDataParamsData } from '@/utils/HelperUtil'
import { formatJsonRpcError, formatJsonRpcResult } from '@json-rpc-tools/utils'
import { SignClientTypes } from '@walletconnect/types'
import { getSdkError } from '@walletconnect/utils'
import { providers } from 'ethers'
import { KernelSmartAccountLib } from '@/lib/smart-accounts/KernelSmartAccountLib'
import SettingsStore from '@/store/SettingsStore'
type RequestEventArgs = Omit<SignClientTypes.EventArguments['session_request'], 'verifyContext'>
export async function approveEIP155Request(requestEvent: RequestEventArgs) {
const { params, id } = requestEvent
const { chainId, request } = params
SettingsStore.setActiveChainId(chainId)
const wallet = await getWallet(params)
switch (request.method) {
case EIP155_SIGNING_METHODS.PERSONAL_SIGN:
case EIP155_SIGNING_METHODS.ETH_SIGN:
try {
const message = getSignParamsMessage(request.params)
const signedMessage = await wallet.signMessage(message)
return formatJsonRpcResult(id, signedMessage)
} catch (error: any) {
console.error(error)
alert(error.message)
return formatJsonRpcError(id, error.message)
}
case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA:
case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V3:
case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V4:
try {
const {
domain,
types,
message: data,
primaryType
} = getSignTypedDataParamsData(request.params)
// intercept for smart account getPermissions mock
if (domain.name === 'eth_getPermissions_v1' && wallet instanceof KernelSmartAccountLib) {
const permissionContext = await wallet.issueSessionKey(
data.targetAddress,
data.permissions
)
return formatJsonRpcResult(id, permissionContext)
}
// https://github.com/ethers-io/ethers.js/issues/687#issuecomment-714069471
delete types.EIP712Domain
const signedData = await wallet._signTypedData(domain, types, data, primaryType)
return formatJsonRpcResult(id, signedData)
} catch (error: any) {
console.error(error)
alert(error.message)
return formatJsonRpcError(id, error.message)
}
case EIP155_SIGNING_METHODS.ETH_SEND_TRANSACTION:
try {
const provider = new providers.JsonRpcProvider(EIP155_CHAINS[chainId as TEIP155Chain].rpc)
const sendTransaction = request.params[0]
const connectedWallet = await wallet.connect(provider)
const txResponse = await connectedWallet.sendTransaction({
to: sendTransaction.to,
value: sendTransaction.value,
data: sendTransaction.data
})
const txHash = typeof txResponse === 'string' ? txResponse : txResponse?.hash
return formatJsonRpcResult(id, txHash)
} catch (error: any) {
console.error(error)
return formatJsonRpcError(id, error.message)
}
case EIP155_SIGNING_METHODS.ETH_SIGN_TRANSACTION:
try {
const signTransaction = request.params[0]
const signature = await wallet.signTransaction(signTransaction)
return formatJsonRpcResult(id, signature)
} catch (error: any) {
console.error(error)
alert(error.message)
return formatJsonRpcError(id, error.message)
}
default:
throw new Error(getSdkError('INVALID_METHOD').message)
}
}
export function rejectEIP155Request(request: RequestEventArgs) {
const { id } = request
return formatJsonRpcError(id, getSdkError('USER_REJECTED').message)
}