Skip to content

Commit d9897e0

Browse files
committedAug 16, 2022
Added ACTION_REJECTED error for UI-based Signers.

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed
 

‎packages/logger/src.ts/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ export enum ErrorCode {
146146
// - replacement: the full TransactionsResponse for the replacement
147147
// - receipt: the receipt of the replacement
148148
TRANSACTION_REPLACED = "TRANSACTION_REPLACED",
149+
150+
151+
///////////////////
152+
// Interaction Errors
153+
154+
// The user rejected the action, such as signing a message or sending
155+
// a transaction
156+
ACTION_REJECTED = "ACTION_REJECTED",
149157
};
150158

151159
const HEX = "0123456789abcdef";

‎packages/providers/src.ts/json-rpc-provider.ts

+48-7
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ export class JsonRpcSigner extends Signer implements TypedDataSigner {
255255
return this.provider.send("eth_sendTransaction", [ hexTx ]).then((hash) => {
256256
return hash;
257257
}, (error) => {
258+
if (typeof(error.message) === "string" && error.message.match(/user denied/i)) {
259+
logger.throwError("user rejected transaction", Logger.errors.ACTION_REJECTED, {
260+
action: "sendTransaction",
261+
transaction: tx
262+
});
263+
}
264+
258265
return checkError("sendTransaction", error, hexTx);
259266
});
260267
});
@@ -292,15 +299,38 @@ export class JsonRpcSigner extends Signer implements TypedDataSigner {
292299
const data = ((typeof(message) === "string") ? toUtf8Bytes(message): message);
293300
const address = await this.getAddress();
294301

295-
return await this.provider.send("personal_sign", [ hexlify(data), address.toLowerCase() ]);
302+
303+
try {
304+
return await this.provider.send("personal_sign", [ hexlify(data), address.toLowerCase() ]);
305+
} catch (error) {
306+
if (typeof(error.message) === "string" && error.message.match(/user denied/i)) {
307+
logger.throwError("user rejected signing", Logger.errors.ACTION_REJECTED, {
308+
action: "signMessage",
309+
from: address,
310+
message: data
311+
});
312+
}
313+
throw error;
314+
}
296315
}
297316

298317
async _legacySignMessage(message: Bytes | string): Promise<string> {
299318
const data = ((typeof(message) === "string") ? toUtf8Bytes(message): message);
300319
const address = await this.getAddress();
301320

302-
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
303-
return await this.provider.send("eth_sign", [ address.toLowerCase(), hexlify(data) ]);
321+
try {
322+
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
323+
return await this.provider.send("eth_sign", [ address.toLowerCase(), hexlify(data) ]);
324+
} catch (error) {
325+
if (typeof(error.message) === "string" && error.message.match(/user denied/i)) {
326+
logger.throwError("user rejected signing", Logger.errors.ACTION_REJECTED, {
327+
action: "_legacySignMessage",
328+
from: address,
329+
message: data
330+
});
331+
}
332+
throw error;
333+
}
304334
}
305335

306336
async _signTypedData(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): Promise<string> {
@@ -311,10 +341,21 @@ export class JsonRpcSigner extends Signer implements TypedDataSigner {
311341

312342
const address = await this.getAddress();
313343

314-
return await this.provider.send("eth_signTypedData_v4", [
315-
address.toLowerCase(),
316-
JSON.stringify(_TypedDataEncoder.getPayload(populated.domain, types, populated.value))
317-
]);
344+
try {
345+
return await this.provider.send("eth_signTypedData_v4", [
346+
address.toLowerCase(),
347+
JSON.stringify(_TypedDataEncoder.getPayload(populated.domain, types, populated.value))
348+
]);
349+
} catch (error) {
350+
if (typeof(error.message) === "string" && error.message.match(/user denied/i)) {
351+
logger.throwError("user rejected signing", Logger.errors.ACTION_REJECTED, {
352+
action: "_signTypedData",
353+
from: address,
354+
message: { domain: populated.domain, types, value: populated.value }
355+
});
356+
}
357+
throw error;
358+
}
318359
}
319360

320361
async unlock(password: string): Promise<boolean> {

0 commit comments

Comments
 (0)
Please sign in to comment.