Skip to content

Commit

Permalink
refactor: removed permissionless package and ts docs
Browse files Browse the repository at this point in the history
  • Loading branch information
GabiDev45 committed Mar 20, 2024
1 parent ca9666b commit 9e9e1f9
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 75 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,5 @@
"simple-git-hooks": {
"pre-commit": "bun run format && bun run lint:fix",
"commit-msg": "npx --no -- commitlint --edit ${1}"
},
"dependencies": {
"permissionless": "^0.1.10"
}
}
7 changes: 3 additions & 4 deletions src/accounts/actions/prepareUserOperationRequest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { AccountOrClientNotFoundError, parseAccount } from "permissionless"
import type { Chain, Client, Transport } from "viem"
import { estimateFeesPerGas } from "viem/actions"
import type { Prettify } from "viem/chains"
import { estimateUserOperationGas } from "../../bundler/actions/estimateUserOperationGas"
import type { StateOverrides } from "../../bundler/utils/types"
import { PaymasterMode } from "../../paymaster/utils/types"
import { getAction } from "../utils/helpers"
import { getAction, parseAccount } from "../utils/helpers"
import type {
ENTRYPOINT_ADDRESS_V06_TYPE,
PrepareUserOperationRequestParameters,
Expand All @@ -27,7 +26,7 @@ async function prepareUserOperationRequestForEntryPointV06<
userOperation: partialUserOperation,
middleware
} = args
if (!account_) throw new AccountOrClientNotFoundError()
if (!account_) throw new Error("No account found")

const account = parseAccount(
account_
Expand Down Expand Up @@ -145,7 +144,7 @@ export async function prepareUserOperationRequest<
stateOverrides?: StateOverrides
): Promise<UserOperationStruct> {
const { account: account_ = client.account } = args
if (!account_) throw new AccountOrClientNotFoundError()
if (!account_) throw new Error("No account found.")

return prepareUserOperationRequestForEntryPointV06(
client,
Expand Down
7 changes: 2 additions & 5 deletions src/accounts/actions/sendTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AccountOrClientNotFoundError, parseAccount } from "permissionless"
import {
type Chain,
type Client,
Expand All @@ -10,7 +9,7 @@ import {
import type { Prettify } from "viem/chains"
import { waitForUserOperationReceipt } from "../../bundler/actions/waitForUserOperationRceipt"
import type { UserOpReceipt } from "../../bundler/utils/types"
import { getAction } from "../utils/helpers"
import { getAction, parseAccount } from "../utils/helpers"
import type { Middleware, SmartAccount } from "../utils/types"
import { sendUserOperation } from "./sendUserOperation"

Expand Down Expand Up @@ -88,9 +87,7 @@ export async function sendTransaction<
} = args

if (!account_) {
throw new AccountOrClientNotFoundError({
docsPath: "/docs/actions/wallet/sendTransaction"
})
throw new Error("No account found.")
}

const account = parseAccount(account_) as SmartAccount
Expand Down
7 changes: 2 additions & 5 deletions src/accounts/actions/sendTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AccountOrClientNotFoundError, parseAccount } from "permissionless"
import {
type Address,
type Chain,
Expand All @@ -11,7 +10,7 @@ import {
} from "viem"
import type { Prettify } from "viem/chains"
import { waitForUserOperationReceipt } from "../../bundler/actions/waitForUserOperationRceipt"
import { getAction } from "../utils/helpers"
import { getAction, parseAccount } from "../utils/helpers"
import type {
GetAccountParameter,
Middleware,
Expand Down Expand Up @@ -93,9 +92,7 @@ export async function sendTransactions<
} = args

if (!account_) {
throw new AccountOrClientNotFoundError({
docsPath: "/docs/actions/wallet/sendTransaction"
})
throw new Error("No account found.")
}

const account = parseAccount(account_) as SmartAccount
Expand Down
5 changes: 2 additions & 3 deletions src/accounts/actions/sendUserOperation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { AccountOrClientNotFoundError, parseAccount } from "permissionless"
import type { Chain, Client, Hash, Transport } from "viem"
import type { PartialBy, Prettify } from "viem/chains"
import { sendUserOperation as sendUserOperationBundler } from "../../bundler/index"
import { getAction } from "../utils/helpers"
import { getAction, parseAccount } from "../utils/helpers"
import type {
GetAccountParameter,
Middleware,
Expand Down Expand Up @@ -39,7 +38,7 @@ export async function sendUserOperation<
args: Prettify<SendUserOperationParameters>
): Promise<Hash> {
const { account: account_ = client.account } = args
if (!account_) throw new AccountOrClientNotFoundError()
if (!account_) throw new Error("No account found.")

const account = parseAccount(account_) as SmartAccount

Expand Down
7 changes: 2 additions & 5 deletions src/accounts/actions/signMessage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AccountOrClientNotFoundError, parseAccount } from "permissionless"
import type {
Chain,
Client,
SignMessageParameters,
SignMessageReturnType,
Transport
} from "viem"
import { parseAccount } from "../utils/helpers"
import type { SmartAccount } from "../utils/types"

/**
Expand Down Expand Up @@ -64,10 +64,7 @@ export async function signMessage<
message
}: SignMessageParameters<SmartAccount>
): Promise<SignMessageReturnType> {
if (!account_)
throw new AccountOrClientNotFoundError({
docsPath: "/docs/actions/wallet/signMessage"
})
if (!account_) throw new Error("No account found.")

const account = parseAccount(account_)
if (account.type === "local") return account.signMessage({ message })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AccountOrClientNotFoundError, parseAccount } from "permissionless"
import {
type Chain,
type Client,
Expand All @@ -10,6 +9,7 @@ import {
getTypesForEIP712Domain,
validateTypedData
} from "viem"
import { parseAccount } from "../utils/helpers"
import type { SmartAccount } from "../utils/types"

/**
Expand Down Expand Up @@ -126,9 +126,7 @@ export async function signTypedData<
}: SignTypedDataParameters<TTypedData, TPrimaryType, SmartAccount>
): Promise<SignTypedDataReturnType> {
if (!account_) {
throw new AccountOrClientNotFoundError({
docsPath: "/docs/actions/wallet/signMessage"
})
throw new Error("No account found.")
}

const account = parseAccount(account_)
Expand Down
33 changes: 23 additions & 10 deletions src/accounts/biconomyV2/signerToSmartAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ import {
keccak256,
parseAbiParameters
} from "viem"
import { getChainId, signMessage, signTypedData } from "viem/actions"
import type { Prettify } from "viem/chains"

import { isSmartAccountDeployed } from "permissionless"
import {
SignTransactionNotSupportedBySmartAccount,
type SmartAccountSigner
} from "permissionless/accounts"
getBytecode,
getChainId,
signMessage,
signTypedData
} from "viem/actions"
import type { Prettify } from "viem/chains"

import {
type BaseValidationModule,
Expand All @@ -36,7 +35,7 @@ import {
toSmartAccount,
validateUserOp
} from "../utils/helpers.js"
import type { SmartAccount } from "../utils/types.js"
import type { SmartAccount, SmartAccountSigner } from "../utils/types.js"

export type BiconomySmartAccount<
transport extends Transport = Transport,
Expand Down Expand Up @@ -131,6 +130,20 @@ const getAccountInitCode = async ({
})
}

export const isSmartAccountDeployed = async (
client: Client,
address: Address
): Promise<boolean> => {
const contractCode = await getBytecode(client, {
address: address
})

if ((contractCode?.length ?? 0) > 2) {
return true
}
return false
}

const getAccountAddress = async ({
factoryAddress,
accountLogicAddress,
Expand Down Expand Up @@ -227,7 +240,7 @@ export async function signerToSmartAccount<
const viemSigner: LocalAccount = {
...signer,
signTransaction: (_, __) => {
throw new SignTransactionNotSupportedBySmartAccount()
throw new Error("Sign transaction not supported by smart account.")
}
} as LocalAccount

Expand Down Expand Up @@ -285,7 +298,7 @@ export async function signerToSmartAccount<
)
},
async signTransaction(_, __) {
throw new SignTransactionNotSupportedBySmartAccount()
throw new Error("Sign transaction not supported by smart account.")
},
async signTypedData<
const TTypedData extends TypedData | Record<string, unknown>,
Expand Down
48 changes: 43 additions & 5 deletions src/accounts/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
type Abi,
type Account,
type Address,
type Chain,
type Client,
Expand All @@ -9,7 +10,9 @@ import {
type Hex,
type SignableMessage,
type Transport,
type TypedData,
type TypedDataDefinition,
type WalletClient,
concat,
encodeAbiParameters,
keccak256,
Expand All @@ -20,14 +23,14 @@ import * as chains from "viem/chains"
import { toAccount } from "viem/accounts"
import type { BaseValidationModule } from "../../modules/index.js"

import { isSmartAccountDeployed } from "permissionless"
import { SignTransactionNotSupportedBySmartAccount } from "permissionless/accounts"
import type { ENTRYPOINT_ADDRESS_V06_TYPE } from "permissionless/types/entrypoint.js"

import { signTypedData } from "viem/actions"
import { isSmartAccountDeployed } from "../biconomyV2/signerToSmartAccount.js"
import { ENTRYPOINT_ADDRESS_V06 } from "./constants.js"
import type {
ENTRYPOINT_ADDRESS_V06_TYPE,
GetUserOperationHashParams,
SmartAccount,
SmartAccountSigner,
UserOperationStruct
} from "./types.js"

Expand Down Expand Up @@ -162,7 +165,7 @@ export function toSmartAccount<
return concat([abiEncodedMessage, MAGIC_BYTES])
},
async signTransaction(_, __) {
throw new SignTransactionNotSupportedBySmartAccount()
throw new Error("Sign transaction not supported by smart account")
}
})

Expand Down Expand Up @@ -313,3 +316,38 @@ export function getAction<params extends any[], returnType extends {}>(
}
)[actionName]?.(...params) ?? action(client, ...params)
}

export function parseAccount(account: Address | Account): Account {
if (typeof account === "string") return { address: account, type: "json-rpc" }
return account
}

export function walletClientToSmartAccountSigner<
TChain extends Chain | undefined = Chain | undefined
>(
walletClient: WalletClient<Transport, TChain, Account>
): SmartAccountSigner<"custom", Address> {
return {
address: walletClient.account.address,
type: "local",
source: "custom",
publicKey: walletClient.account.address,
signMessage: async ({
message
}: { message: SignableMessage }): Promise<Hex> => {
return walletClient.signMessage({ message })
},
async signTypedData<
const TTypedData extends TypedData | Record<string, unknown>,
TPrimaryType extends keyof TTypedData | "EIP712Domain" = keyof TTypedData
>(typedData: TypedDataDefinition<TTypedData, TPrimaryType>) {
return signTypedData<TTypedData, TPrimaryType, TChain, Account>(
walletClient,
{
account: walletClient.account,
...typedData
}
)
}
}
}
Loading

0 comments on commit 9e9e1f9

Please sign in to comment.