-
Notifications
You must be signed in to change notification settings - Fork 81
/
walletconnect-eth-sign-handlers.ts
196 lines (177 loc) · 6.42 KB
/
walletconnect-eth-sign-handlers.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { t } from 'i18next';
import { SessionTypes } from '@walletconnect/types';
import { EIP155_SIGNING_METHODS, decodeEthMessage } from './walletconnect';
import { ethSignMessage, ethSignTypedMessage, ethSignWalletConnectTx, getEthAccountCodeAndNameByAddress } from '../api/account';
import { alertUser } from '../components/alert/Alert';
type TWCParams = {
request: {
method: string,
params: any[]
};
chainId: string;
}
export type TEthSignHandlerParams = {
topic: string;
id: number;
params: TWCParams;
currentSession: SessionTypes.Struct;
launchSignDialog: ({ topic, id, dialogContent }: TLaunchSignDialog) => void;
};
export type TRequestDialogContent = {
accountName: string;
accountAddress: string; // 'accountAddress' is the account's "receive" / "wallet" address
chain: string;
signingData: string; // data / message coming from dapp
currentSession: SessionTypes.Struct;
method: string;
}
export type TLaunchSignDialog = {
topic: string,
id: number,
apiCaller: () => Promise<any>;
dialogContent: TRequestDialogContent
}
const fetchAccountNameAndAddress = async (address: string) => {
const accountDetail = await getEthAccountCodeAndNameByAddress(address);
if (!accountDetail.success) {
console.log('Failed in fetching account name and code'); //silently fails
return { accountName: '', accountCode: '' };
}
const { code, name } = accountDetail;
return { accountName: name, accountCode: code };
};
export async function handleWcEthSignRequest(method: string, args: TEthSignHandlerParams) {
switch (method) {
case EIP155_SIGNING_METHODS.ETH_SIGN:
case EIP155_SIGNING_METHODS.PERSONAL_SIGN:
await ethSignHandler(args, method);
break;
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:
await ethSignTypedDataHandler(args);
break;
case EIP155_SIGNING_METHODS.ETH_SIGN_TRANSACTION:
case EIP155_SIGNING_METHODS.ETH_SEND_TRANSACTION:
await ethSignOrSendTransactionHandler(args, method);
break;
default:
console.log(`${method} is unsupported`);
}
}
/**
* Wallet Connect's ETH_SIGN gives the params as [address, message]
* while PERSONAL_SIGN gives them as [message, address]
*/
async function ethSignHandler({ params, launchSignDialog, topic, id, currentSession }: TEthSignHandlerParams, method: string) {
const isPersonalSign = method === EIP155_SIGNING_METHODS.PERSONAL_SIGN;
const requestParams = params.request.params;
const accountAddress = isPersonalSign ? requestParams[1] : requestParams[0];
const signingData = isPersonalSign ? requestParams[0] : requestParams[1];
const decoded = decodeEthMessage(signingData);
if (decoded === null) {
alertUser(t('walletConnect.signingRequest.decodeError'));
return;
}
const { accountName, accountCode } = await fetchAccountNameAndAddress(accountAddress);
const apiCaller = async () => {
const result = await ethSignMessage(accountCode, signingData);
if (!result.success) {
return { success: false, error: result };
}
return {
response: { id, jsonrpc: '2.0', result: result.signature },
success: true
};
};
launchSignDialog({
topic,
id,
apiCaller,
dialogContent: {
signingData: decoded,
currentSession,
accountName,
accountAddress,
chain: params.chainId,
method: t('walletConnect.signingRequest.method.signMessage')
}
});
}
async function ethSignTypedDataHandler({ params, launchSignDialog, topic, id, currentSession }: TEthSignHandlerParams) {
const requestParams = params.request.params;
const accountAddress = requestParams[0];
const data = requestParams[1];
let typedData: any;
const { accountName, accountCode } = await fetchAccountNameAndAddress(accountAddress);
try {
typedData = JSON.parse(data);
} catch (e) {
console.error('Failed to parse JSON', e);
alertUser(t('walletConnect.signingRequest.dataParsingError'));
return;
// If JSON parsing fails, typedData will be original data (unchanged).
}
const apiCaller = async () => {
// If the typed data to be signed includes its own chainId, we use that.
// Otherwise, use the id in the params.
const chainId = typedData?.domain?.chainId ?
Number(typedData.domain.chainId) :
Number(params.chainId.replace(/^eip155:/, ''));
const result = await ethSignTypedMessage(accountCode, chainId, data);
if (result.success) {
const response = { id, jsonrpc: '2.0', result: result.signature };
return { response, success: true };
}
return { success: false, error: result };
};
launchSignDialog({
topic,
id,
apiCaller,
dialogContent: {
signingData: JSON.stringify(typedData, null, 2),
currentSession,
accountName,
accountAddress,
chain: params.chainId,
method: t('walletConnect.signingRequest.method.signTypedData')
}
});
}
async function ethSignOrSendTransactionHandler(args: TEthSignHandlerParams, method: string) {
const isSendAndSign = method === EIP155_SIGNING_METHODS.ETH_SEND_TRANSACTION;
const { params, launchSignDialog, topic, id, currentSession } = args;
const requestParams = params.request.params;
// for ETH_SIGN_TRANSACTION, there's only 1 item in the
// requestParams[] instaed of 2.
const accountAddress = requestParams[0].from; // this is our wallet address
const data = requestParams[0];
const { accountName, accountCode } = await fetchAccountNameAndAddress(accountAddress);
const apiCaller = async () => {
// If the typed data to be signed includes its own chainId, we use that, otherwise use the id in the params
const chainId = Number(params.chainId.replace(/^eip155:/, ''));
const result = await ethSignWalletConnectTx(accountCode, isSendAndSign, chainId, data);
if (result.success) {
const response = { id, jsonrpc: '2.0', result: isSendAndSign ? result.txHash : result.rawTx };
return { response, success: true };
}
return { success: false, error: result };
};
const formattedMethod = isSendAndSign ?
t('walletConnect.signingRequest.method.sendTransaction') :
t('walletConnect.signingRequest.method.signTransaction');
launchSignDialog({
topic,
id,
apiCaller,
dialogContent: {
signingData: JSON.stringify(data),
currentSession,
accountName,
accountAddress,
chain: params.chainId,
method: formattedMethod,
}
});
}