diff --git a/package.json b/package.json index 5895506..c3d5b64 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "@ubiquity-dao/rpc-handler": "1.3.0", "@uniswap/permit2-sdk": "^1.2.0", "dotenv": "^16.4.4", + "ethers": "5.7.2", "libsodium-wrappers": "^0.7.13" }, "devDependencies": { diff --git a/src/handlers/generate-erc20-permit.ts b/src/handlers/generate-erc20-permit.ts index 873e0ba..8068da0 100644 --- a/src/handlers/generate-erc20-permit.ts +++ b/src/handlers/generate-erc20-permit.ts @@ -1,5 +1,5 @@ import { PERMIT2_ADDRESS, PermitTransferFrom, SignatureTransfer } from "@uniswap/permit2-sdk"; -import { ethers, keccak256, MaxInt256, parseUnits, toUtf8Bytes } from "ethers"; +import { ethers, utils, constants } from "ethers"; import { Context, Logger } from "../types/context"; import { PermitReward, TokenType } from "../types"; import { decryptKeys } from "../utils"; @@ -105,17 +105,17 @@ export async function generateErc20PermitSignature( const permitTransferFromData: PermitTransferFrom = { permitted: { token: tokenAddress, - amount: parseUnits(amount.toString(), tokenDecimals), + amount: utils.parseUnits(amount.toString(), tokenDecimals), }, spender: _walletAddress, - nonce: BigInt(keccak256(toUtf8Bytes(`${_userId}-${_issueNodeId}`))), - deadline: MaxInt256, + nonce: BigInt(utils.keccak256(utils.toUtf8Bytes(`${_userId}-${_issueNodeId}`))), + deadline: constants.MaxInt256, }; const { domain, types, values } = SignatureTransfer.getPermitData(permitTransferFromData, PERMIT2_ADDRESS, _evmNetworkId); const signature = await adminWallet - .signTypedData( + ._signTypedData( { name: domain.name, version: domain.version, diff --git a/src/handlers/generate-erc721-permit.ts b/src/handlers/generate-erc721-permit.ts index 745a4a5..b5ea8fc 100644 --- a/src/handlers/generate-erc721-permit.ts +++ b/src/handlers/generate-erc721-permit.ts @@ -1,5 +1,5 @@ import { MaxUint256 } from "@uniswap/permit2-sdk"; -import { ethers, keccak256, toUtf8Bytes } from "ethers"; +import { ethers, utils } from "ethers"; import { Context, Logger } from "../types/context"; import { PermitReward, TokenType } from "../types"; import { isIssueEvent } from "../types/typeguards"; @@ -129,8 +129,8 @@ export async function generateErc721PermitSignature( const erc721SignatureData: Erc721PermitSignatureData = { beneficiary: _walletAddress, deadline: MaxUint256.toBigInt(), - keys: metadata.map(([key]) => keccak256(toUtf8Bytes(key))), - nonce: BigInt(keccak256(toUtf8Bytes(`${_userId}-${_issueNodeId}`))), + keys: metadata.map(([key]) => utils.keccak256(utils.toUtf8Bytes(key))), + nonce: BigInt(utils.keccak256(utils.toUtf8Bytes(`${_userId}-${_issueNodeId}`))), values: metadata.map(([, value]) => value), }; @@ -141,7 +141,7 @@ export async function generateErc721PermitSignature( chainId: _evmNetworkId, }; - const signature = await adminWallet.signTypedData(domain, types, erc721SignatureData).catch((error: unknown) => { + const signature = await adminWallet._signTypedData(domain, types, erc721SignatureData).catch((error: unknown) => { _logger.error("Failed to sign typed data", error); throw new Error(`Failed to sign typed data: ${error}`); }); diff --git a/src/handlers/register-wallet.ts b/src/handlers/register-wallet.ts index 6e653c8..b5fab26 100644 --- a/src/handlers/register-wallet.ts +++ b/src/handlers/register-wallet.ts @@ -1,4 +1,4 @@ -import { ethers, ZeroAddress } from "ethers"; +import { ethers, constants } from "ethers"; import { Context } from "../types/context"; export async function registerWallet(context: Context, address: string | null) { @@ -26,7 +26,7 @@ export async function registerWallet(context: Context, address: string | null) { return false; } - if (address === ZeroAddress) { + if (address === constants.AddressZero) { logger.error("Skipping to register a wallet address because user is trying to set their address to null address"); return false; } @@ -58,7 +58,7 @@ export async function registerWallet(context: Context, address: string | null) { export async function resolveAddress(ensName: string): Promise { // Gets the Ethereum address associated with an ENS (Ethereum Name Service) name // Explicitly set provider to Ethereum mainnet - const provider = new ethers.JsonRpcProvider(`https://eth.llamarpc.com`); // mainnet required for ENS + const provider = new ethers.providers.JsonRpcProvider(`https://eth.llamarpc.com`); // mainnet required for ENS return await provider.resolveName(ensName).catch((err) => { console.trace({ err }); return null; diff --git a/src/utils/get-fastest-provider.ts b/src/utils/get-fastest-provider.ts index c5fe8ea..21d1c93 100644 --- a/src/utils/get-fastest-provider.ts +++ b/src/utils/get-fastest-provider.ts @@ -1,5 +1,5 @@ import { RPCHandler, HandlerConstructorConfig } from "@ubiquity-dao/rpc-handler"; -import { JsonRpcProvider } from "ethers"; +import { providers } from "ethers"; function getHandler(networkId: number | string) { const config = { @@ -15,11 +15,11 @@ function getHandler(networkId: number | string) { return new RPCHandler(config as HandlerConstructorConfig); } -export async function getFastestProvider(networkId: number | string): Promise { +export async function getFastestProvider(networkId: number | string): Promise { try { const handler = getHandler(networkId); const provider = await handler.getFastestRpcProvider(); - return new JsonRpcProvider(provider.connection.url); + return new providers.JsonRpcProvider(provider.connection.url); } catch (e) { throw new Error(`Failed to get fastest provider for networkId: ${networkId}`); } diff --git a/tests/generate-erc721-permit.test.ts b/tests/generate-erc721-permit.test.ts index 13ae96f..b65da8c 100644 --- a/tests/generate-erc721-permit.test.ts +++ b/tests/generate-erc721-permit.test.ts @@ -1,5 +1,5 @@ import { MaxUint256 } from "@uniswap/permit2-sdk"; -import { BaseWallet, keccak256, toUtf8Bytes, TypedDataDomain, TypedDataField } from "ethers"; +import { Wallet, utils, TypedDataDomain, TypedDataField } from "ethers"; import { generateErc721PermitSignature } from "../src"; import { Context } from "../src/types/context"; import { Env } from "../src/types/env"; @@ -69,7 +69,7 @@ describe("generateErc721PermitSignature", () => { (context.adapters.supabase.wallet.getWalletByUserId as jest.Mock).mockReturnValue(SPENDER); (context.adapters.supabase.user.getUserIdByWallet as jest.Mock).mockReturnValue(userId); jest - .spyOn(BaseWallet.prototype, "signTypedData") + .spyOn(Wallet.prototype, "_signTypedData") // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementation((domain: TypedDataDomain, types: Record, value: Record) => { return Promise.resolve("0x0"); @@ -100,10 +100,10 @@ describe("generateErc721PermitSignature", () => { expect(result.erc721Request?.metadata).toBeDefined(); expect(result.beneficiary).toBe(SPENDER); expect(result.deadline).toBe(MaxUint256.toString()); - expect(result.nonce).toBe(BigInt(keccak256(toUtf8Bytes(`${userId}-${issueId}`))).toString()); + expect(result.nonce).toBe(BigInt(utils.keccak256(utils.toUtf8Bytes(`${userId}-${issueId}`))).toString()); expect(result.erc721Request?.values).toEqual([organizationName, repositoryName, issueNumber, userId, contributionType]); expect(result.networkId).toBe(context.config.evmNetworkId); - const keysHashed = keys.map((key) => keccak256(toUtf8Bytes(key))); + const keysHashed = keys.map((key) => utils.keccak256(utils.toUtf8Bytes(key))); expect(result.erc721Request?.keys).toEqual(keysHashed); } diff --git a/yarn.lock b/yarn.lock index b76d334..4865ab5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3582,7 +3582,7 @@ esutils@^2.0.2: resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64" integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g== -ethers@^5.7.0: +ethers@5.7.2, ethers@^5.7.0: version "5.7.2" resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.2.tgz#3a7deeabbb8c030d4126b24f84e525466145872e" integrity sha512-wswUsmWo1aOK8rR7DIKiWSw9DbLWe6x98Jrn8wcTflTVvaXhAMaB5zGAXy0GYQEQp9iO1iSHWVyARQm11zUtyg==