Skip to content

Commit

Permalink
feature: Allow background wallet initialisation for zkEvm
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenfowler authored Feb 16, 2024
1 parent e8286f0 commit 6e74cb7
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 442 deletions.
1 change: 0 additions & 1 deletion packages/passport/sdk/src/Passport.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ describe('Passport', () => {

expect(accounts).toEqual([mockOidcUserZkevm.profile.passport.zkevm_eth_address]);
expect(mockGetUser).toHaveBeenCalledTimes(1);
expect(mockMagicRequest).toHaveBeenCalledTimes(3);
});

describe('when the registration request fails', () => {
Expand Down
14 changes: 8 additions & 6 deletions packages/passport/sdk/src/zkEvm/sendTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JsonRpcProvider, TransactionRequest } from '@ethersproject/providers';
import { Signer } from '@ethersproject/abstract-signer';
import { getEip155ChainId, getNonce, getSignedMetaTransactions } from './walletHelpers';
import { sendTransaction } from './sendTransaction';
import { chainId, chainIdEip155, mockUserZkEvm } from '../test/mocks';
Expand All @@ -8,7 +9,6 @@ import { RelayerTransaction, RelayerTransactionStatus } from './types';
import { JsonRpcError, RpcErrorCode } from './JsonRpcError';
import GuardianClient from '../guardian';

jest.mock('@ethersproject/providers');
jest.mock('./walletHelpers');
jest.mock('../network/retry');
const withConfirmationScreenStub = jest.fn();
Expand All @@ -26,7 +26,6 @@ describe('sendTransaction', () => {
data: '0x456',
value: '0x00',
};
const magicProvider = {};
const jsonRpcProvider = {
ready: Promise.resolve({ chainId }),
};
Expand All @@ -40,6 +39,9 @@ describe('sendTransaction', () => {
withConfirmationScreen: jest.fn(() => (task: () => void) => task()),
loading: jest.fn(),
};
const ethSigner = {
getAddress: jest.fn(),
} as Partial<Signer> as Signer;

const imxFeeOption = {
tokenPrice: '1',
Expand Down Expand Up @@ -73,7 +75,7 @@ describe('sendTransaction', () => {

const result = await sendTransaction({
params: [transactionRequest],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
zkevmAddress: mockUserZkEvm.zkEvm.ethAddress,
Expand Down Expand Up @@ -105,7 +107,7 @@ describe('sendTransaction', () => {

const result = await sendTransaction({
params: [transactionRequest],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
zkevmAddress: mockUserZkEvm.zkEvm.ethAddress,
Expand Down Expand Up @@ -144,7 +146,7 @@ describe('sendTransaction', () => {

const result = await sendTransaction({
params: [transactionRequest],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
zkevmAddress: mockUserZkEvm.zkEvm.ethAddress,
Expand Down Expand Up @@ -189,7 +191,7 @@ describe('sendTransaction', () => {
await expect(
sendTransaction({
params: [transactionRequest],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
zkevmAddress: mockUserZkEvm.zkEvm.ethAddress,
Expand Down
15 changes: 7 additions & 8 deletions packages/passport/sdk/src/zkEvm/sendTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
ExternalProvider, JsonRpcProvider, JsonRpcSigner, TransactionRequest, Web3Provider,
JsonRpcProvider, TransactionRequest,
} from '@ethersproject/providers';
import { BigNumber, BigNumberish } from 'ethers';
import { Signer } from '@ethersproject/abstract-signer';
import { getEip155ChainId, getNonce, getSignedMetaTransactions } from './walletHelpers';
import { MetaTransaction, RelayerTransactionStatus } from './types';
import { JsonRpcError, RpcErrorCode } from './JsonRpcError';
Expand All @@ -13,7 +14,7 @@ const MAX_TRANSACTION_HASH_RETRIEVAL_RETRIES = 30;
const TRANSACTION_HASH_RETRIEVAL_WAIT = 1000;

export type EthSendTransactionParams = {
magicProvider: ExternalProvider;
ethSigner: Signer;
jsonRpcProvider: JsonRpcProvider;
guardianClient: GuardianClient;
relayerClient: RelayerClient;
Expand All @@ -26,7 +27,7 @@ const getMetaTransactions = async (
nonce: BigNumberish,
chainId: BigNumber,
walletAddress: string,
signer: JsonRpcSigner,
signer: Signer,
relayerClient: RelayerClient,
): Promise<MetaTransaction[]> => {
// NOTE: We sign the transaction before getting the fee options because
Expand Down Expand Up @@ -68,7 +69,7 @@ const getMetaTransactions = async (

export const sendTransaction = ({
params,
magicProvider,
ethSigner,
jsonRpcProvider,
relayerClient,
guardianClient,
Expand All @@ -82,8 +83,6 @@ export const sendTransaction = ({

const { chainId } = await jsonRpcProvider.ready;
const chainIdBigNumber = BigNumber.from(chainId);
const magicWeb3Provider = new Web3Provider(magicProvider);
const signer = magicWeb3Provider.getSigner();

const nonce = await getNonce(jsonRpcProvider, zkevmAddress);
const metaTransaction: MetaTransaction = {
Expand All @@ -99,7 +98,7 @@ export const sendTransaction = ({
nonce,
chainIdBigNumber,
zkevmAddress,
signer,
ethSigner,
relayerClient,
);

Expand All @@ -116,7 +115,7 @@ export const sendTransaction = ({
nonce,
chainIdBigNumber,
zkevmAddress,
signer,
ethSigner,
);

const relayerId = await relayerClient.ethSendTransaction(zkevmAddress, signedTransactions);
Expand Down
31 changes: 13 additions & 18 deletions packages/passport/sdk/src/zkEvm/signTypedDataV4.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JsonRpcProvider, Web3Provider } from '@ethersproject/providers';
import { JsonRpcProvider } from '@ethersproject/providers';
import { BigNumber } from 'ethers';
import GuardianClient from 'guardian';
import { Signer } from '@ethersproject/abstract-signer';
import { getEip155ChainId, getSignedTypedData } from './walletHelpers';
import {
chainId,
Expand All @@ -12,7 +13,6 @@ import { signTypedDataV4 } from './signTypedDataV4';
import { JsonRpcError, RpcErrorCode } from './JsonRpcError';
import { TypedDataPayload } from './types';

jest.mock('@ethersproject/providers');
jest.mock('./walletHelpers');

describe('signTypedDataV4', () => {
Expand All @@ -25,9 +25,7 @@ describe('signTypedDataV4', () => {
};
const relayerSignature = '02011b1d383526a2815d26550eb314b5d7e0551327330043c4d07715346a7d5517ecbc32304fc1ccdcd52fea386c94c3b58b90410f20cd1d5c6db8fa1f03c34e82dce78c3445ce38583e0b0689c69b8fbedbc33d3a2e45431b0103';
const combinedSignature = '0x000202011b1d383526a2815d26550eb314b5d7e0551327330043c4d07715346a7d5517ecbc32304fc1ccdcd52fea386c94c3b58b90410f20cd1d5c6db8fa1f03c34e82dce78c3445ce38583e0b0689c69b8fbedbc33d3a2e45431b01030001d25acf5eef26fb627f91e02ebd111580030ab8fb0a55567ac8cc66c34de7ae98185125a76adc6ee2fea042c7fce9c85a41e790ce3529f93dfec281bf56620ef21b02';

const magicProvider = {};
const magicSigner = {};
const ethSigner = {} as Signer;
const jsonRpcProvider = {
ready: Promise.resolve({ chainId }),
};
Expand All @@ -48,9 +46,6 @@ describe('signTypedDataV4', () => {
(getSignedTypedData as jest.Mock).mockResolvedValueOnce(
combinedSignature,
);
(Web3Provider as unknown as jest.Mock).mockImplementation(() => ({
getSigner: () => magicSigner,
}));
withConfirmationScreenStub.mockImplementation(() => (task: () => void) => task());
guardianClient.withConfirmationScreen = withConfirmationScreenStub;
});
Expand All @@ -60,7 +55,7 @@ describe('signTypedDataV4', () => {
const result = await signTypedDataV4({
method: 'eth_signTypedData_v4',
params: [address, JSON.stringify(eip712Payload)],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
guardianClient: guardianClient as unknown as GuardianClient,
Expand All @@ -76,7 +71,7 @@ describe('signTypedDataV4', () => {
relayerSignature,
BigNumber.from(chainId),
address,
magicSigner,
ethSigner,
);
});
});
Expand All @@ -86,7 +81,7 @@ describe('signTypedDataV4', () => {
const result = await signTypedDataV4({
method: 'eth_signTypedData_v4',
params: [address, eip712Payload],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
guardianClient: guardianClient as any,
Expand All @@ -102,7 +97,7 @@ describe('signTypedDataV4', () => {
relayerSignature,
BigNumber.from(chainId),
address,
magicSigner,
ethSigner,
);
});
});
Expand All @@ -113,7 +108,7 @@ describe('signTypedDataV4', () => {
signTypedDataV4({
method: 'eth_signTypedData_v4',
params: [address],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
guardianClient: guardianClient as any,
Expand All @@ -130,7 +125,7 @@ describe('signTypedDataV4', () => {
signTypedDataV4({
method: 'eth_signTypedData_v4',
params: [address, '*~<|8)-/-<'],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
guardianClient: guardianClient as any,
Expand All @@ -155,7 +150,7 @@ describe('signTypedDataV4', () => {
signTypedDataV4({
method: 'eth_signTypedData_v4',
params: [address, payload],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
guardianClient: guardianClient as any,
Expand All @@ -180,7 +175,7 @@ describe('signTypedDataV4', () => {
},
},
],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
guardianClient: guardianClient as any,
Expand All @@ -204,7 +199,7 @@ describe('signTypedDataV4', () => {
address,
payload,
],
magicProvider,
ethSigner,
jsonRpcProvider: jsonRpcProvider as JsonRpcProvider,
relayerClient: relayerClient as unknown as RelayerClient,
guardianClient: guardianClient as any,
Expand All @@ -220,7 +215,7 @@ describe('signTypedDataV4', () => {
relayerSignature,
BigNumber.from(chainId),
address,
magicSigner,
ethSigner,
);
});
});
15 changes: 5 additions & 10 deletions packages/passport/sdk/src/zkEvm/signTypedDataV4.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
ExternalProvider,
JsonRpcProvider,
Web3Provider,
} from '@ethersproject/providers';
import { JsonRpcProvider } from '@ethersproject/providers';
import { Signer } from '@ethersproject/abstract-signer';
import { BigNumber } from 'ethers';
import GuardianClient from 'guardian';
import { getSignedTypedData } from './walletHelpers';
Expand All @@ -11,7 +8,7 @@ import { JsonRpcError, RpcErrorCode } from './JsonRpcError';
import { RelayerClient } from './relayerClient';

export type SignTypedDataV4Params = {
magicProvider: ExternalProvider;
ethSigner: Signer;
jsonRpcProvider: JsonRpcProvider;
relayerClient: RelayerClient;
method: string;
Expand Down Expand Up @@ -68,7 +65,7 @@ const transformTypedData = (typedData: string | object, chainId: number): TypedD
export const signTypedDataV4 = async ({
params,
method,
magicProvider,
ethSigner,
jsonRpcProvider,
relayerClient,
guardianClient,
Expand All @@ -86,8 +83,6 @@ export const signTypedDataV4 = async ({

await guardianClient.validateMessage({ chainID: String(chainId), payload: typedData });
const relayerSignature = await relayerClient.imSignTypedData(fromAddress, typedData);
const magicWeb3Provider = new Web3Provider(magicProvider);
const signer = magicWeb3Provider.getSigner();

return getSignedTypedData(typedData, relayerSignature, BigNumber.from(chainId), fromAddress, signer);
return getSignedTypedData(typedData, relayerSignature, BigNumber.from(chainId), fromAddress, ethSigner);
});
1 change: 0 additions & 1 deletion packages/passport/sdk/src/zkEvm/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './registerZkEvmUser';
export * from './loginZkEvmUser';
74 changes: 0 additions & 74 deletions packages/passport/sdk/src/zkEvm/user/loginZkEvmUser.test.ts

This file was deleted.

Loading

0 comments on commit 6e74cb7

Please sign in to comment.