UNPKG

@biconomy-devx/modules

Version:

This package provides different validation modules/plugins for ERC4337 compatible modular account

2 lines (1 loc) 3.02 kB
"use strict";import{concat as y,encodeAbiParameters as l,encodeFunctionData as v,getAddress as w,keccak256 as g,pad as d,parseAbi as I,parseAbiParameters as b,toBytes as A,toHex as f}from"viem";import{MerkleTree as O}from"merkletreejs";import{DEFAULT_MULTICHAIN_MODULE as S,MULTICHAIN_VALIDATION_MODULE_ADDRESSES_BY_VERSION as P}from"./utils/Constants.js";import{BaseValidationModule as D}from"./BaseValidationModule.js";import{getUserOpHash as M}from"./utils/Helper.js";import{convertSigner as U,Logger as E}from"@biconomy-devx/common";export class MultiChainValidationModule extends D{constructor(e){super(e),Object.defineProperty(this,"signer",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"moduleAddress",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"version",{enumerable:!0,configurable:!0,writable:!0,value:"V1_0_0"}),this.signer=e.signer}static async create(e){const{signer:r}=await U(e.signer,!0),n={...e,signer:r},t=new MultiChainValidationModule(n);if(e.moduleAddress)t.moduleAddress=e.moduleAddress;else if(e.version){const i=P[e.version];if(!i)throw new Error(`Invalid version ${e.version}`);t.moduleAddress=i,t.version=e.version}else t.moduleAddress=S;return t}getAddress(){return this.moduleAddress}async getSigner(){return Promise.resolve(this.signer)}async getDummySignature(){return`0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000${w(this.getAddress()).substring(2).padEnd(40,"0")}000000000000000000000000000000000000000000000000000000000000004181d4b4981670cb18f99f0b4a66446df1bf5b204d24cfcb659bf38ba27a4359b5711649ec2423c5e1247245eba2964679b6a1dbb85c992ae40b9b00c6935b02ff1b00000000000000000000000000000000000000000000000000000000000000`}async getInitData(){const e=await this.signer.getAddress(),r=I(["function initForSmartAccount(address owner)"]);return v({abi:r,functionName:"initForSmartAccount",args:[e]})}async signUserOpHash(e){return await this.signer.signMessage({raw:A(e)})}async signMessage(e){const r=typeof e=="string"?e:{raw:e};let n=await this.signer.signMessage(r);const t=parseInt(n.slice(-2),16);if(![27,28].includes(t)){const i=t+27;n=n.slice(0,-2)+i.toString(16)}return n}async signUserOps(e){try{const r=[];for(const s of e){const a=s.validUntil??0,o=s.validAfter??0,c=y([d(f(a),{size:6}),d(f(o),{size:6}),d(M(s.userOp,this.entryPointAddress,s.chainId),{size:32})]);r.push(g(c))}const n=new O(r,g,{sortPairs:!0});let t=await this.signer.signMessage({raw:A(n.getHexRoot())});const i=parseInt(t.slice(-2),16);if(![27,28].includes(i)){const s=i+27;t=t.slice(0,-2)+s.toString(16)}const u=[];for(let s=0;s<r.length;s++){const a=n.getHexProof(r[s]),o=e[s].validUntil??0,c=e[s].validAfter??0,p=l(b(["uint48, uint48, bytes32, bytes32[], bytes"]),[o,c,n.getHexRoot(),a,t]),m=l(b(["bytes, address"]),[p,this.getAddress()]),h={...e[s].userOp,signature:m};u.push(h)}return u}catch(r){throw E.error("Error in signing multi chain userops"),new Error(JSON.stringify(r))}}}