-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Hayden Fowler <[email protected]>
- Loading branch information
1 parent
dce866e
commit 2cc70ec
Showing
6 changed files
with
291 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/passport/sdk/src/zkEvm/signEjectionTransaction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { TransactionRequest } from '@ethersproject/providers'; | ||
import { Signer } from '@ethersproject/abstract-signer'; | ||
import { Flow } from '@imtbl/metrics'; | ||
import { BigNumber } from 'ethers'; | ||
import { mockUserZkEvm } from '../test/mocks'; | ||
import * as transactionHelpers from './transactionHelpers'; | ||
import { signEjectionTransaction } from './signEjectionTransaction'; | ||
import { JsonRpcError, RpcErrorCode } from './JsonRpcError'; | ||
|
||
jest.mock('./transactionHelpers'); | ||
jest.mock('../network/retry'); | ||
|
||
describe('im_signEjectionTransaction', () => { | ||
const signedTransactionPayload = { | ||
to: mockUserZkEvm.zkEvm.ethAddress, | ||
data: '123', | ||
chainId: '1', | ||
}; | ||
|
||
const transactionRequest: TransactionRequest = { | ||
to: mockUserZkEvm.zkEvm.ethAddress, | ||
nonce: BigNumber.from(5), | ||
chainId: 1, | ||
value: BigNumber.from('5'), | ||
}; | ||
const ethSigner = { | ||
getAddress: jest.fn(), | ||
} as Partial<Signer> as Signer; | ||
const flow = { | ||
addEvent: jest.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
(transactionHelpers.prepareAndSignEjectionTransaction as jest.Mock).mockResolvedValue( | ||
signedTransactionPayload, | ||
); | ||
}); | ||
|
||
it('calls prepareAndSignEjectionTransaction with the correct arguments', async () => { | ||
await signEjectionTransaction({ | ||
params: [transactionRequest], | ||
ethSigner, | ||
zkEvmAddress: mockUserZkEvm.zkEvm.ethAddress, | ||
flow: flow as unknown as Flow, | ||
}); | ||
|
||
expect(transactionHelpers.prepareAndSignEjectionTransaction).toHaveBeenCalledWith({ | ||
transactionRequest, | ||
ethSigner, | ||
zkEvmAddress: mockUserZkEvm.zkEvm.ethAddress, | ||
flow: flow as unknown as Flow, | ||
}); | ||
}); | ||
|
||
it('calls signEjectionTransaction with invalid params', async () => { | ||
await expect(signEjectionTransaction({ | ||
params: [transactionRequest, { test: 'test' }], | ||
ethSigner, | ||
zkEvmAddress: mockUserZkEvm.zkEvm.ethAddress, | ||
flow: flow as unknown as Flow, | ||
})).rejects.toThrow( | ||
new JsonRpcError(RpcErrorCode.INVALID_PARAMS, 'im_signEjectionTransaction requires a singular param (hash)'), | ||
); | ||
}); | ||
|
||
it('returns the transaction hash', async () => { | ||
const result = await signEjectionTransaction({ | ||
params: [transactionRequest], | ||
ethSigner, | ||
zkEvmAddress: mockUserZkEvm.zkEvm.ethAddress, | ||
flow: flow as unknown as Flow, | ||
}); | ||
|
||
expect(result).toEqual(signedTransactionPayload); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/passport/sdk/src/zkEvm/signEjectionTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { TransactionRequest } from '@ethersproject/providers'; | ||
import { | ||
prepareAndSignEjectionTransaction, | ||
EjectionTransactionParams, | ||
EjectionTransactionResponse, | ||
} from './transactionHelpers'; | ||
import { JsonRpcError, RpcErrorCode } from './JsonRpcError'; | ||
|
||
type EthSendTransactionEjectionParams = EjectionTransactionParams & { | ||
params: Array<any>; | ||
}; | ||
|
||
export const signEjectionTransaction = async ({ | ||
params, | ||
ethSigner, | ||
zkEvmAddress, | ||
flow, | ||
}: EthSendTransactionEjectionParams): Promise<EjectionTransactionResponse> => { | ||
if (!params || params.length !== 1) { | ||
throw new JsonRpcError( | ||
RpcErrorCode.INVALID_PARAMS, | ||
'im_signEjectionTransaction requires a singular param (hash)', | ||
); | ||
} | ||
|
||
const transactionRequest = params[0] as TransactionRequest; | ||
return await prepareAndSignEjectionTransaction({ | ||
transactionRequest, | ||
ethSigner, | ||
zkEvmAddress, | ||
flow, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.