UNPKG

@biconomy-devx/modules

Version:

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

2 lines (1 loc) 3.4 kB
"use strict";import{createWalletClient as c,http as l,toHex as u}from"viem";import{WalletClientSigner as d}from"@alchemy/aa-core";import{mainnet as g}from"viem/chains";import{generatePrivateKey as S,privateKeyToAccount as n}from"viem/accounts";export class SessionLocalStorage{constructor(s){Object.defineProperty(this,"smartAccountAddress",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.smartAccountAddress=s.toLowerCase()}validateSearchParam(s){if(!s.sessionID){if(!s.sessionID&&s.sessionPublicKey&&s.sessionValidationModule)return;throw new Error("Either pass sessionId or a combination of sessionPublicKey and sessionValidationModule address.")}}getSessionStore(){const s=localStorage.getItem(this.getStorageKey("sessions"));return s?JSON.parse(s):{merkleRoot:"",leafNodes:[]}}getSignerStore(){const s=localStorage.getItem(this.getStorageKey("signers"));return s?JSON.parse(s):{}}getStorageKey(s){return`${this.smartAccountAddress}_${s}`}toLowercaseAddress(s){return s.toLowerCase()}async addSessionData(s){const e=this.getSessionStore();s.sessionValidationModule=this.toLowercaseAddress(s.sessionValidationModule),s.sessionPublicKey=this.toLowercaseAddress(s.sessionPublicKey),e.leafNodes.push(s),localStorage.setItem(this.getStorageKey("sessions"),JSON.stringify(e))}async getSessionData(s){this.validateSearchParam(s);const t=this.getSessionStore().leafNodes.find(o=>s.sessionID?o.sessionID===s.sessionID&&(!s.status||o.status===s.status):s.sessionPublicKey&&s.sessionValidationModule?o.sessionPublicKey===this.toLowercaseAddress(s.sessionPublicKey)&&o.sessionValidationModule===this.toLowercaseAddress(s.sessionValidationModule)&&(!s.status||o.status===s.status):void 0);if(!t)throw new Error("Session not found.");return t}async updateSessionStatus(s,e){this.validateSearchParam(s);const t=this.getSessionStore(),o=t.leafNodes.find(i=>s.sessionID?i.sessionID===s.sessionID:s.sessionPublicKey&&s.sessionValidationModule?i.sessionPublicKey===this.toLowercaseAddress(s.sessionPublicKey)&&i.sessionValidationModule===this.toLowercaseAddress(s.sessionValidationModule):void 0);if(!o)throw new Error("Session not found.");o.status=e,localStorage.setItem(this.getStorageKey("sessions"),JSON.stringify(t))}async clearPendingSessions(){const s=this.getSessionStore();s.leafNodes=s.leafNodes.filter(e=>e.status!=="PENDING"),localStorage.setItem(this.getStorageKey("sessions"),JSON.stringify(s))}async addSigner(s){const e=this.getSignerStore();let t;if(s)t=s;else{const a=S();t={pvKey:a,pbKey:n(a).publicKey}}const o=n(u(t.pvKey)),i=c({account:o,chain:s.chainId,transport:l()}),r=new d(i,"json-rpc");return e[this.toLowercaseAddress(o.address)]=s,localStorage.setItem(this.getStorageKey("signers"),JSON.stringify(e)),r}async getSignerByKey(s){const t=this.getSignerStore()[this.toLowercaseAddress(s)];if(!t)throw new Error("Signer not found.");const o=n(t.privateKey),i=c({account:o,chain:g,transport:l()});return new d(i,"viem")}async getSignerBySession(s){const e=await this.getSessionData(s);return this.getSignerByKey(e.sessionPublicKey)}async getAllSessionData(s){const e=this.getSessionStore().leafNodes;return!s||!s.status?e:e.filter(t=>t.status===s.status)}async getMerkleRoot(){return this.getSessionStore().merkleRoot}setMerkleRoot(s){const e=this.getSessionStore();return e.merkleRoot=s,localStorage.setItem(this.getStorageKey("sessions"),JSON.stringify(e)),Promise.resolve()}}