-
Notifications
You must be signed in to change notification settings - Fork 56
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 #31 from pimlicolabs/feat/account-system
Create simple account client
- Loading branch information
Showing
61 changed files
with
2,807 additions
and
389 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 support for SimpleAccount management |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { | ||
type PrivateKeySimpleSmartAccount, | ||
SignTransactionNotSupportedBySmartAccount, | ||
privateKeyToSimpleSmartAccount | ||
} from "./privateKeyToSimpleSmartAccount.js" | ||
|
||
import { type SmartAccount } from "./types.js" | ||
|
||
export { | ||
SignTransactionNotSupportedBySmartAccount, | ||
type PrivateKeySimpleSmartAccount, | ||
privateKeyToSimpleSmartAccount, | ||
type SmartAccount | ||
} |
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,220 @@ | ||
import { | ||
type Address, | ||
BaseError, | ||
type Chain, | ||
type Client, | ||
type Hex, | ||
type Transport, | ||
concatHex, | ||
encodeFunctionData | ||
} from "viem" | ||
import { privateKeyToAccount, toAccount } from "viem/accounts" | ||
import { getBytecode, getChainId } from "viem/actions" | ||
import { getAccountNonce } from "../actions/public/getAccountNonce.js" | ||
import { getSenderAddress } from "../actions/public/getSenderAddress.js" | ||
import { getUserOperationHash } from "../utils/getUserOperationHash.js" | ||
import { type SmartAccount } from "./types.js" | ||
|
||
export class SignTransactionNotSupportedBySmartAccount extends BaseError { | ||
override name = "SignTransactionNotSupportedBySmartAccount" | ||
constructor({ docsPath }: { docsPath?: string } = {}) { | ||
super( | ||
[ | ||
"A smart account cannot sign or send transaction, it can only sign message or userOperation.", | ||
"Please send user operation instead." | ||
].join("\n"), | ||
{ | ||
docsPath, | ||
docsSlug: "account" | ||
} | ||
) | ||
} | ||
} | ||
|
||
export type PrivateKeySimpleSmartAccount< | ||
transport extends Transport = Transport, | ||
chain extends Chain | undefined = Chain | undefined | ||
> = SmartAccount<"privateKeySimpleSmartAccount", transport, chain> | ||
|
||
const getAccountInitCode = async ( | ||
factoryAddress: Address, | ||
owner: Address, | ||
index = 0n | ||
): Promise<Hex> => { | ||
if (!owner) throw new Error("Owner account not found") | ||
|
||
return concatHex([ | ||
factoryAddress, | ||
encodeFunctionData({ | ||
abi: [ | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: "address", | ||
name: "owner", | ||
type: "address" | ||
}, | ||
{ | ||
internalType: "uint256", | ||
name: "salt", | ||
type: "uint256" | ||
} | ||
], | ||
name: "createAccount", | ||
outputs: [ | ||
{ | ||
internalType: "contract SimpleAccount", | ||
name: "ret", | ||
type: "address" | ||
} | ||
], | ||
stateMutability: "nonpayable", | ||
type: "function" | ||
} | ||
], | ||
functionName: "createAccount", | ||
args: [owner, index] | ||
}) as Hex | ||
]) | ||
} | ||
|
||
const getAccountAddress = async < | ||
TTransport extends Transport = Transport, | ||
TChain extends Chain | undefined = Chain | undefined | ||
>({ | ||
client, | ||
factoryAddress, | ||
entryPoint, | ||
owner | ||
}: { | ||
client: Client<TTransport, TChain> | ||
factoryAddress: Address | ||
owner: Address | ||
entryPoint: Address | ||
}): Promise<Address> => { | ||
const initCode = await getAccountInitCode(factoryAddress, owner) | ||
|
||
return getSenderAddress(client, { | ||
initCode, | ||
entryPoint | ||
}) | ||
} | ||
|
||
/** | ||
* @description Creates an Simple Account from a private key. | ||
* | ||
* @returns A Private Key Simple Account. | ||
*/ | ||
export async function privateKeyToSimpleSmartAccount< | ||
TTransport extends Transport = Transport, | ||
TChain extends Chain | undefined = Chain | undefined | ||
>( | ||
client: Client<TTransport, TChain>, | ||
{ | ||
privateKey, | ||
factoryAddress, | ||
entryPoint | ||
}: { | ||
privateKey: Hex | ||
factoryAddress: Address | ||
entryPoint: Address | ||
} | ||
): Promise<PrivateKeySimpleSmartAccount<TTransport, TChain>> { | ||
const privateKeyAccount = privateKeyToAccount(privateKey) | ||
|
||
const [accountAddress, chainId] = await Promise.all([ | ||
getAccountAddress<TTransport, TChain>({ | ||
client, | ||
factoryAddress, | ||
entryPoint, | ||
owner: privateKeyAccount.address | ||
}), | ||
getChainId(client) | ||
]) | ||
|
||
if (!accountAddress) throw new Error("Account address not found") | ||
|
||
const account = toAccount({ | ||
address: accountAddress, | ||
async signMessage({ message }) { | ||
return privateKeyAccount.signMessage({ message }) | ||
}, | ||
async signTransaction(_, __) { | ||
throw new SignTransactionNotSupportedBySmartAccount() | ||
}, | ||
async signTypedData(typedData) { | ||
return privateKeyAccount.signTypedData({ ...typedData, privateKey }) | ||
} | ||
}) | ||
|
||
return { | ||
...account, | ||
client: client, | ||
publicKey: accountAddress, | ||
entryPoint: entryPoint, | ||
source: "privateKeySimpleSmartAccount", | ||
async getNonce() { | ||
return getAccountNonce(client, { | ||
sender: accountAddress, | ||
entryPoint: entryPoint | ||
}) | ||
}, | ||
async signUserOperation(userOperation) { | ||
return account.signMessage({ | ||
message: { | ||
raw: getUserOperationHash({ | ||
userOperation, | ||
entryPoint: entryPoint, | ||
chainId: chainId | ||
}) | ||
} | ||
}) | ||
}, | ||
async getInitCode() { | ||
const contractCode = await getBytecode(client, { | ||
address: accountAddress | ||
}) | ||
|
||
if ((contractCode?.length ?? 0) > 2) return "0x" | ||
|
||
return getAccountInitCode(factoryAddress, privateKeyAccount.address) | ||
}, | ||
async encodeDeployCallData(_) { | ||
throw new Error("Simple account doesn't support account deployment") | ||
}, | ||
async encodeCallData({ to, value, data }) { | ||
return encodeFunctionData({ | ||
abi: [ | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: "address", | ||
name: "dest", | ||
type: "address" | ||
}, | ||
{ | ||
internalType: "uint256", | ||
name: "value", | ||
type: "uint256" | ||
}, | ||
{ | ||
internalType: "bytes", | ||
name: "func", | ||
type: "bytes" | ||
} | ||
], | ||
name: "execute", | ||
outputs: [], | ||
stateMutability: "nonpayable", | ||
type: "function" | ||
} | ||
], | ||
functionName: "execute", | ||
args: [to, value, data] | ||
}) | ||
}, | ||
async getDummySignature() { | ||
return "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c" | ||
} | ||
} | ||
} |
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,33 @@ | ||
import type { | ||
Abi, | ||
Address, | ||
Client, | ||
GetConstructorArgs, | ||
Hex, | ||
LocalAccount | ||
} from "viem" | ||
import type { Chain, Transport } from "viem" | ||
import { type UserOperation } from "../types/index.js" | ||
|
||
export type SmartAccount< | ||
Name extends string = string, | ||
transport extends Transport = Transport, | ||
chain extends Chain | undefined = Chain | undefined | ||
> = LocalAccount<Name> & { | ||
client: Client<transport, chain> | ||
entryPoint: Address | ||
getNonce: () => Promise<bigint> | ||
getInitCode: () => Promise<Hex> | ||
encodeCallData: ({ | ||
to, | ||
value, | ||
data | ||
}: { to: Address; value: bigint; data: Hex }) => Promise<Hex> | ||
getDummySignature(): Promise<Hex> | ||
encodeDeployCallData: <TAbi extends Abi | readonly unknown[] = Abi>({ | ||
abi, | ||
args, | ||
bytecode | ||
}: { abi: TAbi; bytecode: Hex } & GetConstructorArgs<TAbi>) => Promise<Hex> | ||
signUserOperation: (UserOperation: UserOperation) => Promise<Hex> | ||
} |
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
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
Oops, something went wrong.