From 5e708a3d9624b5568a68f59c85c5c7d60196bbb7 Mon Sep 17 00:00:00 2001 From: yssf-io Date: Fri, 4 Aug 2023 12:40:58 +0200 Subject: [PATCH 1/2] add Signer signature to signTypedData --- src/signer.ts | 6 ++++-- src/utils/signTypedData.ts | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/signer.ts b/src/signer.ts index 78a595a..352438f 100644 --- a/src/signer.ts +++ b/src/signer.ts @@ -2,7 +2,7 @@ import { TransactionResponse, Provider, TransactionRequest } from '@ethersproject/abstract-provider' import { Signer, TypedDataDomain, TypedDataField, TypedDataSigner } from '@ethersproject/abstract-signer' import { Deferrable } from '@ethersproject/properties' -import { StaticJsonRpcProvider } from '@ethersproject/providers' +import { JsonRpcSigner, StaticJsonRpcProvider } from '@ethersproject/providers' import { keccak256 } from '@ethersproject/solidity' import { Bytes, hexlify } from "@ethersproject/bytes"; import { toUtf8Bytes } from "@ethersproject/strings"; @@ -127,7 +127,9 @@ class AvoSigner extends Signer implements TypedDataSigner { domain, types, value - }) + }, + this.signer as JsonRpcSigner + ) if (!result.signature) { throw Error("Failed to get signature"); diff --git a/src/utils/signTypedData.ts b/src/utils/signTypedData.ts index 92dec4b..fa735bb 100644 --- a/src/utils/signTypedData.ts +++ b/src/utils/signTypedData.ts @@ -1,5 +1,5 @@ // https://github.com/enzymefinance/protocol/blob/c9621dd5f8234bd45126772fc626252a38d46eee/packages/ethers/src/utils/signTypedData.ts -import { JsonRpcProvider } from '@ethersproject/providers'; +import { JsonRpcProvider, JsonRpcSigner } from '@ethersproject/providers'; import { hexlify } from '@ethersproject/bytes'; import { toUtf8Bytes } from '@ethersproject/strings'; import type { TypedData } from './typedData'; @@ -10,6 +10,7 @@ export async function signTypedData( provider: JsonRpcProvider, address: string, data: TypedData, + signer?: JsonRpcSigner ): Promise<{ signature?: string; method?: string; cancelled?: boolean }> { const message = await getTypedDataMessage(provider, data.domain, data.types, data.value); @@ -50,6 +51,26 @@ export async function signTypedData( return { cancelled: true }; } + if(!signer) + throw new Error(typeof error === 'string' ? error : 'An error occured.'); + + } + + // Fallback if using a signer + try { + const method = "eth_sign"; + const signature = await signer._signTypedData( + data.domain, + data.types, + data.value + ); + + return { method, signature }; + } catch (error) { + if (typeof error === 'string' && error.startsWith('Error: Transaction was rejected')) { + return { cancelled: true }; + } + throw new Error(typeof error === 'string' ? error : 'An error occured.'); } From 71885486dca802db8d350032983f0952fc28ecee Mon Sep 17 00:00:00 2001 From: yssf-io Date: Sat, 5 Aug 2023 15:55:34 +0200 Subject: [PATCH 2/2] doing Wallet check in --- src/signer.ts | 12 +++++++++--- src/utils/signTypedData.ts | 23 +---------------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/signer.ts b/src/signer.ts index 352438f..a092351 100644 --- a/src/signer.ts +++ b/src/signer.ts @@ -121,15 +121,21 @@ class AvoSigner extends Signer implements TypedDataSigner { } async _signTypedData(domain: TypedDataDomain, types: Record, value: Record): Promise { + if("privateKey" in this.signer) { + return await (this.signer as Signer as JsonRpcSigner)._signTypedData( + domain, + types, + value + ); + } + const result = await signTypedData(this.signer.provider as any, await this.getSignerAddress(), { domain, types, value - }, - this.signer as JsonRpcSigner - ) + }) if (!result.signature) { throw Error("Failed to get signature"); diff --git a/src/utils/signTypedData.ts b/src/utils/signTypedData.ts index fa735bb..92dec4b 100644 --- a/src/utils/signTypedData.ts +++ b/src/utils/signTypedData.ts @@ -1,5 +1,5 @@ // https://github.com/enzymefinance/protocol/blob/c9621dd5f8234bd45126772fc626252a38d46eee/packages/ethers/src/utils/signTypedData.ts -import { JsonRpcProvider, JsonRpcSigner } from '@ethersproject/providers'; +import { JsonRpcProvider } from '@ethersproject/providers'; import { hexlify } from '@ethersproject/bytes'; import { toUtf8Bytes } from '@ethersproject/strings'; import type { TypedData } from './typedData'; @@ -10,7 +10,6 @@ export async function signTypedData( provider: JsonRpcProvider, address: string, data: TypedData, - signer?: JsonRpcSigner ): Promise<{ signature?: string; method?: string; cancelled?: boolean }> { const message = await getTypedDataMessage(provider, data.domain, data.types, data.value); @@ -51,26 +50,6 @@ export async function signTypedData( return { cancelled: true }; } - if(!signer) - throw new Error(typeof error === 'string' ? error : 'An error occured.'); - - } - - // Fallback if using a signer - try { - const method = "eth_sign"; - const signature = await signer._signTypedData( - data.domain, - data.types, - data.value - ); - - return { method, signature }; - } catch (error) { - if (typeof error === 'string' && error.startsWith('Error: Transaction was rejected')) { - return { cancelled: true }; - } - throw new Error(typeof error === 'string' ? error : 'An error occured.'); }