Skip to content
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
11 changes: 6 additions & 5 deletions chain-abstraction/add-owner-passkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/

import * as dotenv from 'dotenv'
import * as ethers from 'ethers'
import { hexToBytes, keccak256, toBytes, numberToBytes } from 'viem'
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"
import {
SafeMultiChainSigAccount as SafeAccount,
AllowAllPaymaster,
Expand Down Expand Up @@ -64,11 +65,11 @@ async function main(): Promise<void> {
id: 'safe.global',
},
user: {
id: ethers.getBytes(ethers.id('chain-abstraction-demo')),
id: hexToBytes(keccak256(toBytes('chain-abstraction-demo'))),
name: 'demo-user',
displayName: 'Demo User',
},
challenge: ethers.toBeArray(Date.now()),
challenge: numberToBytes(Date.now()),
pubKeyCredParams: [{ type: 'public-key', alg: -7 }],
},
})
Expand All @@ -84,7 +85,7 @@ async function main(): Promise<void> {
console.log(" Public key X:", publicKey.x.toString().slice(0, 20) + "...")

// Generate a new owner address to add (using random address for demo)
const newOwnerAddress = ethers.Wallet.createRandom().address
const newOwnerAddress = privateKeyToAccount(generatePrivateKey()).address

console.log("\nPasskey owner (signer):", credential.id.slice(0, 20) + "...")
console.log("New owner to add:", newOwnerAddress)
Expand Down Expand Up @@ -157,7 +158,7 @@ async function main(): Promise<void> {
// In a browser, this would trigger device biometrics
const assertion = navigator.credentials.get({
publicKey: {
challenge: ethers.getBytes(multiChainHash),
challenge: hexToBytes(multiChainHash as `0x${string}`),
rpId: 'safe.global',
allowCredentials: [{ type: 'public-key', id: new Uint8Array(credential.rawId) }],
userVerification: UserVerificationRequirement.required,
Expand Down
8 changes: 4 additions & 4 deletions eip-7702/upgrade-eoa-to-7702-smart-account-sponsored-gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import {
createAndSignEip7702DelegationAuthorization,
CandidePaymaster,
} from "abstractionkit";
import { Wallet } from 'ethers';
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";

async function main(): Promise<void> {
//get values from .env
dotenv.config();
const chainId = BigInt(process.env.CHAIN_ID as string)
const bundlerUrl = process.env.BUNDLER_URL as string
const nodeUrl = process.env.NODE_URL as string;

const eoaDelegator = Wallet.createRandom();

const eoaDelegatorPrivateKey = generatePrivateKey();
const eoaDelegator = privateKeyToAccount(eoaDelegatorPrivateKey);
const eoaDelegatorPublicAddress = eoaDelegator.address;
const eoaDelegatorPrivateKey = eoaDelegator.privateKey;
const paymasterUrl = process.env.PAYMASTER_URL as string;
const sponsorshipPolicyId = process.env.SPONSORSHIP_POLICY_ID as string;

Expand Down
26 changes: 13 additions & 13 deletions eip-7702/upgrade-eoa-to-7702-smart-account-wallet-signed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
createEip7702DelegationAuthorizationHash,
createUserOperationHash,
} from "abstractionkit";
import { JsonRpcProvider, toBeHex, Wallet } from 'ethers';
import { generatePrivateKey, privateKeyToAccount, sign } from "viem/accounts";
import { createPublicClient, http, toHex } from "viem";

async function main(): Promise<void> {
try {
Expand All @@ -17,9 +18,10 @@ async function main(): Promise<void> {
const bundlerUrl = process.env.BUNDLER_URL as string
const nodeUrl = process.env.NODE_URL as string;

const provider = new JsonRpcProvider(nodeUrl);
const client = createPublicClient({ transport: http(nodeUrl) });

const eoaDelegator = Wallet.createRandom(provider);
const eoaDelegatorPrivateKey = generatePrivateKey();
const eoaDelegator = privateKeyToAccount(eoaDelegatorPrivateKey);
const eoaDelegatorPublicAddress = eoaDelegator.address;
const paymasterUrl = process.env.PAYMASTER_URL as string;
const sponsorshipPolicyId = process.env.SPONSORSHIP_POLICY_ID as string;
Expand Down Expand Up @@ -58,25 +60,23 @@ async function main(): Promise<void> {
}
);

const nonce = await eoaDelegator.getNonce();
const nonce = await client.getTransactionCount({ address: eoaDelegatorPublicAddress });

const eip7702DelegationAuthorizationHash = createEip7702DelegationAuthorizationHash(
chainId,
smartAccount.delegateeAddress,
BigInt(nonce)
);

const signedHash = eoaDelegator.signingKey.sign(
eip7702DelegationAuthorizationHash,
);
const delegationSig = await sign({ hash: eip7702DelegationAuthorizationHash as `0x${string}`, privateKey: eoaDelegatorPrivateKey });

userOperation.eip7702Auth = {
chainId: toBeHex(chainId),
chainId: toHex(chainId),
address: smartAccount.delegateeAddress,
nonce: toBeHex(nonce),
yParity: toBeHex(signedHash.yParity),
r: signedHash.r,
s: signedHash.s
nonce: toHex(nonce),
yParity: toHex(delegationSig.v === 27n ? 0 : 1),
r: delegationSig.r,
s: delegationSig.s
};

const paymaster = new CandidePaymaster(paymasterUrl);
Expand All @@ -91,7 +91,7 @@ async function main(): Promise<void> {
chainId,
);

userOperation.signature = eoaDelegator.signingKey.sign(userOperationHash).serialized;
userOperation.signature = await sign({ hash: userOperationHash as `0x${string}`, privateKey: eoaDelegatorPrivateKey, to: 'hex' });

let sendUserOperationResponse = await smartAccount.sendUserOperation(
userOperation, bundlerUrl
Expand Down
10 changes: 6 additions & 4 deletions multisig/multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CandidePaymaster,
} from "abstractionkit";

import { Wallet } from "ethers";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";

async function main(): Promise<void> {
//get values from .env
Expand All @@ -21,8 +21,10 @@ async function main(): Promise<void> {
const sponsorshipPolicyId = process.env.SPONSORSHIP_POLICY_ID;


const owner1 = Wallet.createRandom();
const owner2 = Wallet.createRandom();
const owner1PrivateKey = generatePrivateKey();
const owner1 = privateKeyToAccount(owner1PrivateKey);
const owner2PrivateKey = generatePrivateKey();
const owner2 = privateKeyToAccount(owner2PrivateKey);

//initializeNewAccount only needed when the smart account
//have not been deployed yet for its first useroperation.
Expand Down Expand Up @@ -98,7 +100,7 @@ async function main(): Promise<void> {
//privateKeys
userOperation.signature = smartAccount.signUserOperation(
userOperation,
[owner1.privateKey, owner2.privateKey],
[owner1PrivateKey, owner2PrivateKey],
chainId
)
console.log(userOperation)
Expand Down
20 changes: 12 additions & 8 deletions nested-safe-accounts/nested-safe-accounts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as dotenv from 'dotenv'
import * as ethers from 'ethers'
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";

import {
SafeAccountV0_3_0 as SafeAccount,
Expand All @@ -20,11 +20,15 @@ async function main(): Promise<void> {
const sponsorshipPolicyId = process.env.SPONSORSHIP_POLICY_ID as string;

/*subaccount1 signers*/
const signer1Subaccount1 = ethers.Wallet.createRandom();
const signer2Subaccount1 = ethers.Wallet.createRandom();
const signer1Subaccount1PrivateKey = generatePrivateKey();
const signer1Subaccount1 = privateKeyToAccount(signer1Subaccount1PrivateKey);
const signer2Subaccount1PrivateKey = generatePrivateKey();
const signer2Subaccount1 = privateKeyToAccount(signer2Subaccount1PrivateKey);
/*subaccount2 signers*/
const signer1Subaccount2 = ethers.Wallet.createRandom();
const signer2Subaccount2 = ethers.Wallet.createRandom();
const signer1Subaccount2PrivateKey = generatePrivateKey();
const signer1Subaccount2 = privateKeyToAccount(signer1Subaccount2PrivateKey);
const signer2Subaccount2PrivateKey = generatePrivateKey();
const signer2Subaccount2 = privateKeyToAccount(signer2Subaccount2PrivateKey);

/* initialize subaccounts */
const subAccount1 = SafeAccount.initializeNewAccount(
Expand Down Expand Up @@ -66,7 +70,7 @@ async function main(): Promise<void> {

subAccount1DeployMainAccountUserOperation.signature = subAccount1.signUserOperation(
subAccount1DeployMainAccountUserOperation,
[signer1Subaccount1.privateKey, signer2Subaccount1.privateKey],
[signer1Subaccount1PrivateKey, signer2Subaccount1PrivateKey],
chainId,
)

Expand Down Expand Up @@ -176,7 +180,7 @@ async function main(): Promise<void> {

subAccount1UserOperation.signature = subAccount1.signUserOperation(
subAccount1UserOperation,
[signer1Subaccount1.privateKey, signer2Subaccount1.privateKey],
[signer1Subaccount1PrivateKey, signer2Subaccount1PrivateKey],
chainId,
)
let subAccount2UserOperation = await subAccount2.createUserOperation(
Expand All @@ -190,7 +194,7 @@ async function main(): Promise<void> {

subAccount2UserOperation.signature = subAccount2.signUserOperation(
subAccount2UserOperation,
[signer1Subaccount2.privateKey, signer2Subaccount2.privateKey],
[signer1Subaccount2PrivateKey, signer2Subaccount2PrivateKey],
chainId,
)
/***********************************/
Expand Down
8 changes: 4 additions & 4 deletions passkeys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import * as dotenv from 'dotenv'
import * as ethers from 'ethers'
import { hexToBytes, keccak256, toBytes, numberToBytes } from 'viem'
import {
SafeAccountV0_3_0 as SafeAccount,
MetaTransaction,
Expand Down Expand Up @@ -68,11 +68,11 @@ async function main(): Promise<void> {
id: 'safe.global',
},
user: {
id: ethers.getBytes(ethers.id('chucknorris')),
id: hexToBytes(keccak256(toBytes('chucknorris'))),
name: 'chucknorris',
displayName: 'Chuck Norris',
},
challenge: ethers.toBeArray(Date.now()),
challenge: numberToBytes(Date.now()),
pubKeyCredParams: [{ type: 'public-key', alg: -7 }],
},
})
Expand Down Expand Up @@ -150,7 +150,7 @@ async function main(): Promise<void> {
// Simulate passkey authentication (biometric prompt in real browser)
const assertion = navigator.credentials.get({
publicKey: {
challenge: ethers.getBytes(safeInitOpHash),
challenge: hexToBytes(safeInitOpHash as `0x${string}`),
rpId: 'safe.global',
allowCredentials: [{ type: 'public-key', id: new Uint8Array(credential.rawId) }],
userVerification: UserVerificationRequirement.required,
Expand Down
8 changes: 4 additions & 4 deletions passkeys/passkeys-v0.2.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import * as dotenv from 'dotenv'
import * as ethers from 'ethers'
import { hexToBytes, keccak256, toBytes, numberToBytes } from 'viem'
import {
SafeAccountV0_3_0 as SafeAccount,
MetaTransaction,
Expand Down Expand Up @@ -92,11 +92,11 @@ async function main(): Promise<void> {
id: 'safe.global',
},
user: {
id: ethers.getBytes(ethers.id('chucknorris')),
id: hexToBytes(keccak256(toBytes('chucknorris'))),
name: 'chucknorris',
displayName: 'Chuck Norris',
},
challenge: ethers.toBeArray(Date.now()),
challenge: numberToBytes(Date.now()),
pubKeyCredParams: [{ type: 'public-key', alg: -7 }],
},
})
Expand Down Expand Up @@ -182,7 +182,7 @@ async function main(): Promise<void> {
// Simulate passkey authentication (biometric prompt in real browser)
const assertion = navigator.credentials.get({
publicKey: {
challenge: ethers.getBytes(safeInitOpHash),
challenge: hexToBytes(safeInitOpHash as `0x${string}`),
rpId: 'safe.global',
allowCredentials: [{ type: 'public-key', id: new Uint8Array(credential.rawId) }],
userVerification: UserVerificationRequirement.required,
Expand Down
Loading