Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reference value #498

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @biconomy/account

## 4.4.2

### Patch Changes

- fix referenceValue padding

## 4.4.1

### Patch Changes
Expand Down
1 change: 1 addition & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"files": {
"ignore": [
"package.json",
"node_modules",
"**/node_modules",
"cache",
Expand Down
6 changes: 3 additions & 3 deletions examples/CREATE_AND_USE_A_BATCH_SESSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ const leaves: CreateSessionParams[] = [

const { wait, session } = await createBatchSession(
smartAccount,
sessionKeyAddress,
sessionStorageClient,
leaves,
withSponsorship
Expand Down Expand Up @@ -117,11 +116,12 @@ const nftMintTx: Transaction = {
}),
};

const txs = [transferTx, nftMintTx];
const txs = [nftMintTx, transferTx];
const correspondingIndexes = [1, 0]; // The order of the txs from the sessionBatch

const batchSessionParams = await getBatchSessionTxParams(
["ERC20", "ABI"],
txs,
correspondingIndexes,
session,
chain
);
Expand Down
1 change: 0 additions & 1 deletion examples/CREATE_AND_USE_A_SESSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ const policy: Policy[] = [
const { wait, session } = await createSession(
smartAccount,
policy,
sessionKeyAddress,
sessionStorageClient,
{
paymasterServiceData: { mode: PaymasterMode.SPONSORED },
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"sideEffects": false,
"name": "@biconomy/account",
"author": "Biconomy",
"version": "4.4.1",
"version": "4.4.2",
"description": "SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.",
"keywords": [
"erc-7579",
Expand Down
1 change: 1 addition & 0 deletions src/account/BiconomySmartAccountV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ export class BiconomySmartAccountV2 extends BaseSmartContractAccount {
_params?: SendUserOpParams
): Promise<UserOperationStruct> {
const params = { ..._params, ...(this.sessionData ? this.sessionData : {}) }

this.isActiveValidationModuleDefined()
const requiredFields: UserOperationKey[] = [
"sender",
Expand Down
5 changes: 3 additions & 2 deletions src/account/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ export const ERROR_MESSAGES = {
"'Amount' is required for withdrawal of native token without using a paymaster",
MISSING_RPC_URL:
"rpcUrl is required for PrivateKeyAccount signer type, please provide it in the config",
INVALID_SESSION_TYPES:
"Session types and transactions must be of the same length"
INVALID_SESSION_INDEXES:
"Session indexes and transactions must be of the same length and correspond to each other",
SIGNER_REQUIRED: "Signer is required for creating a smart account"
}

export const NATIVE_TOKEN_ALIAS: Hex =
Expand Down
3 changes: 0 additions & 3 deletions src/modules/BatchedSessionRouterModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,6 @@ export class BatchedSessionRouterModule extends BaseValidationModule {
}

const sessionDataTupleArray: SessionDataTuple[] = []

// if needed we could do mock signature over userOpHashAndModuleAddress

// signer must be the same for all the sessions
const { signer: sessionSigner } = await convertSigner(
sessionParams[0].sessionSigner,
Expand Down
3 changes: 0 additions & 3 deletions src/modules/session-storage/SessionFileStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ export class SessionFileStorage implements ISessionStorage {
data: any,
type: "sessions" | "signers"
): Promise<void> {
console.log("")
return new Promise((resolve, reject) => {
const filePath = this.getStorageFilePath(type)
// @ts-ignore
fs.writeFile(filePath, JSON.stringify(data), "utf8", (err) => {
if (err) {
// Handle errors appropriately
console.log({ err }, JSON.stringify(data))
reject(err)
} else {
resolve()
Expand Down Expand Up @@ -90,7 +88,6 @@ export class SessionFileStorage implements ISessionStorage {
return data || { merkleRoot: "", leafNodes: [] }
} catch (error) {
// Handle errors appropriately
console.log({ error })
}
}

Expand Down
52 changes: 23 additions & 29 deletions src/modules/sessions/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
type ByteArray,
type Hex,
concat,
isAddress,
pad,
slice,
toFunctionSelector,
Expand All @@ -18,6 +17,7 @@ import {
type BiconomySmartAccountV2,
type BuildUserOpOptions,
ERROR_MESSAGES,
Logger,
type Transaction
} from "../../account"
import { createSessionKeyManagerModule } from "../index"
Expand All @@ -37,7 +37,7 @@ export type Session = {
/** The storage client specific to the smartAccountAddress which stores the session keys */
sessionStorageClient: ISessionStorage
/** The relevant sessionID for the chosen session */
sessionID: string
sessionIDInfo: string[]
}

export type SessionEpoch = {
Expand Down Expand Up @@ -121,7 +121,6 @@ export type SessionGrantedPayload = UserOpResponse & { session: Session }
* valueLimit: 0n
* }
* ],
* sessionKeyAddress,
* sessionStorage,
* {
* paymasterServiceData: { mode: PaymasterMode.SPONSORED },
Expand All @@ -139,19 +138,17 @@ export type SessionGrantedPayload = UserOpResponse & { session: Session }
export const createSession = async (
smartAccount: BiconomySmartAccountV2,
policy: Policy[],
sessionKeyAddress: Hex,
sessionStorageClient: ISessionStorage,
buildUseropDto?: BuildUserOpOptions
): Promise<SessionGrantedPayload> => {
const userAccountAddress = await smartAccount.getAddress()
const smartAccountAddress = await smartAccount.getAddress()
const sessionsModule = await createSessionKeyManagerModule({
smartAccountAddress: userAccountAddress,
smartAccountAddress,
sessionStorageClient
})

const { data: policyData } = await sessionsModule.createSessionData(
policy.map(createABISessionDatum)
)
const { data: policyData, sessionIDInfo } =
await sessionsModule.createSessionData(policy.map(createABISessionDatum))

const permitTx = {
to: DEFAULT_SESSION_KEY_MANAGER_MODULE,
Expand All @@ -161,33 +158,30 @@ export const createSession = async (
const txs: Transaction[] = []

const isDeployed = await smartAccount.isAccountDeployed()
if (!isDeployed) throw new Error(ERROR_MESSAGES.ACCOUNT_NOT_DEPLOYED)

const enabled = await smartAccount.isModuleEnabled(
const enableSessionTx = await smartAccount.getEnableModuleData(
DEFAULT_SESSION_KEY_MANAGER_MODULE
)

if (!enabled) {
txs.push(
await smartAccount.getEnableModuleData(DEFAULT_SESSION_KEY_MANAGER_MODULE)
if (isDeployed) {
const enabled = await smartAccount.isModuleEnabled(
DEFAULT_SESSION_KEY_MANAGER_MODULE
)
if (!enabled) {
txs.push(enableSessionTx)
}
} else {
Logger.log(ERROR_MESSAGES.ACCOUNT_NOT_DEPLOYED)
txs.push(enableSessionTx)
}

txs.push(permitTx)

const userOpResponse = await smartAccount.sendTransaction(txs, buildUseropDto)

const sessionID =
(
await sessionStorageClient.getSessionData({
sessionPublicKey: sessionKeyAddress,
sessionValidationModule: DEFAULT_ABI_SVM_MODULE
})
).sessionID ?? ""

return {
session: {
sessionStorageClient,
sessionID
sessionIDInfo
},
...userOpResponse
}
Expand Down Expand Up @@ -290,7 +284,7 @@ export function getSessionDatum(
return sessionKeyData
}

type HardcodedReference = {
export type HardcodedReference = {
raw: Hex
}
type BaseReferenceValue = string | number | bigint | boolean | ByteArray
Expand All @@ -312,11 +306,11 @@ export function parseReferenceValue(referenceValue: AnyReferenceValue): Hex {
if ((referenceValue as HardcodedReference)?.raw) {
return (referenceValue as HardcodedReference)?.raw
}
if (isAddress(referenceValue as string)) {
return pad(referenceValue as Hex, { size: 32 })
if (typeof referenceValue === "bigint") {
return pad(toHex(referenceValue), { size: 32 }) as Hex
}
return toHex(referenceValue as BaseReferenceValue)
return pad(referenceValue as Hex, { size: 32 })
} catch (e) {
return toHex(referenceValue as BaseReferenceValue)
return pad(referenceValue as Hex, { size: 32 })
}
}
Loading
Loading