-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from pimlicolabs/reduce-chainid-call
Add getPackedUserOperation
- Loading branch information
Showing
6 changed files
with
163 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"permissionless": patch | ||
--- | ||
|
||
Added util function getPackedUserOperation |
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
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
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
114 changes: 114 additions & 0 deletions
114
packages/permissionless/utils/getPackedUserOperation.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,114 @@ | ||
import { type Hex, concat, getAddress, pad, slice, toHex } from "viem" | ||
import type { UserOperation } from "../types/userOperation" | ||
import type { Hex32, PackedUserOperation } from "../types/userOperation" | ||
|
||
export function getInitCode(unpackedUserOperation: UserOperation<"v0.7">) { | ||
return unpackedUserOperation.factory | ||
? concat([ | ||
unpackedUserOperation.factory, | ||
unpackedUserOperation.factoryData || ("0x" as Hex) | ||
]) | ||
: "0x" | ||
} | ||
|
||
export function unPackInitCode(initCode: Hex) { | ||
if (initCode === "0x") { | ||
return { | ||
factory: null, | ||
factoryData: null | ||
} | ||
} | ||
return { | ||
factory: getAddress(slice(initCode, 0, 20)), | ||
factoryData: slice(initCode, 20) | ||
} | ||
} | ||
|
||
export function getAccountGasLimits( | ||
unpackedUserOperation: UserOperation<"v0.7"> | ||
) { | ||
return concat([ | ||
pad(toHex(unpackedUserOperation.verificationGasLimit), { | ||
size: 16 | ||
}), | ||
pad(toHex(unpackedUserOperation.callGasLimit), { size: 16 }) | ||
]) as Hex32 | ||
} | ||
|
||
export function unpackAccountGasLimits(accountGasLimits: Hex) { | ||
return { | ||
verificationGasLimit: BigInt(slice(accountGasLimits, 0, 16)), | ||
callGasLimit: BigInt(slice(accountGasLimits, 16)) | ||
} | ||
} | ||
|
||
export function getGasLimits(unpackedUserOperation: UserOperation<"v0.7">) { | ||
return concat([ | ||
pad(toHex(unpackedUserOperation.maxPriorityFeePerGas), { | ||
size: 16 | ||
}), | ||
pad(toHex(unpackedUserOperation.maxFeePerGas), { size: 16 }) | ||
]) as Hex32 | ||
} | ||
|
||
export function unpackGasLimits(gasLimits: Hex) { | ||
return { | ||
maxPriorityFeePerGas: BigInt(slice(gasLimits, 0, 16)), | ||
maxFeePerGas: BigInt(slice(gasLimits, 16)) | ||
} | ||
} | ||
|
||
export function getPaymasterAndData( | ||
unpackedUserOperation: UserOperation<"v0.7"> | ||
) { | ||
return unpackedUserOperation.paymaster | ||
? concat([ | ||
unpackedUserOperation.paymaster, | ||
pad( | ||
toHex( | ||
unpackedUserOperation.paymasterVerificationGasLimit || 0n | ||
), | ||
{ | ||
size: 16 | ||
} | ||
), | ||
pad(toHex(unpackedUserOperation.paymasterPostOpGasLimit || 0n), { | ||
size: 16 | ||
}), | ||
unpackedUserOperation.paymasterData || ("0x" as Hex) | ||
]) | ||
: "0x" | ||
} | ||
|
||
export function unpackPaymasterAndData(paymasterAndData: Hex) { | ||
if (paymasterAndData === "0x") { | ||
return { | ||
paymaster: null, | ||
paymasterVerificationGasLimit: null, | ||
paymasterPostOpGasLimit: null, | ||
paymasterData: null | ||
} | ||
} | ||
return { | ||
paymaster: getAddress(slice(paymasterAndData, 0, 20)), | ||
paymasterVerificationGasLimit: BigInt(slice(paymasterAndData, 20, 36)), | ||
paymasterPostOpGasLimit: BigInt(slice(paymasterAndData, 36, 52)), | ||
paymasterData: slice(paymasterAndData, 52) | ||
} | ||
} | ||
|
||
export const getPackedUserOperation = ( | ||
userOperation: UserOperation<"v0.7"> | ||
): PackedUserOperation => { | ||
return { | ||
sender: userOperation.sender, | ||
nonce: userOperation.nonce, | ||
initCode: getInitCode(userOperation), | ||
callData: userOperation.callData, | ||
accountGasLimits: getAccountGasLimits(userOperation), | ||
preVerificationGas: userOperation.preVerificationGas, | ||
gasFees: getGasLimits(userOperation), | ||
paymasterAndData: getPaymasterAndData(userOperation), | ||
signature: userOperation.signature | ||
} | ||
} |
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