Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
46 changes: 46 additions & 0 deletions examples/typescript/end-users/createEvmEip7702Delegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Usage: pnpm tsx end-users/createEvmEip7702Delegation.ts <USER_UUID>

import { CdpClient } from "@coinbase/cdp-sdk";
import "dotenv/config";

const userId = process.argv[2];
if (!userId) {
console.error(
"Usage: pnpm tsx end-users/createEvmEip7702Delegation.ts <USER_UUID>"
);
process.exit(1);
}

const cdp = new CdpClient();

try {
const endUser = await cdp.endUser.getEndUser({ userId });

console.log("EVM address:", endUser.evmAccountObjects[0]?.address);

// Create an EIP-7702 delegation using the client method (developer calls on behalf of end user)
const result = await cdp.endUser.createEvmEip7702Delegation({
userId: endUser.userId,
address: endUser.evmAccountObjects[0].address,
network: "base-sepolia",
enableSpendPermissions: true,
});

console.log(
"Delegation operation ID (via client):",
result.delegationOperationId
);

// Alternatively, create directly on the EndUserAccount (auto-picks first EVM address)
const result2 = await endUser.createEvmEip7702Delegation({
network: "base-sepolia",
enableSpendPermissions: true,
});

console.log(
"Delegation operation ID (via account):",
result2.delegationOperationId
);
} catch (error) {
console.error("Error: ", (error as { errorMessage: string }).errorMessage);
}
30 changes: 30 additions & 0 deletions examples/typescript/end-users/revokeDelegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Usage: pnpm tsx end-users/revokeDelegation.ts <USER_UUID>

import { CdpClient } from "@coinbase/cdp-sdk";
import "dotenv/config";

const userId = process.argv[2];
if (!userId) {
console.error("Usage: pnpm tsx end-users/revokeDelegation.ts <USER_UUID>");
process.exit(1);
}

const cdp = new CdpClient();

try {
const endUser = await cdp.endUser.getEndUser({ userId });

// Revoke all active delegations for the end user using the client method
await cdp.endUser.revokeDelegationForEndUser({
userId: endUser.userId,
});

console.log("Revoked delegation for end user via client method");

// Alternatively, revoke delegation directly on the EndUserAccount object
await endUser.revokeDelegation();

console.log("Revoked delegation for end user via account method");
} catch (error) {
console.error("Error: ", (error as { errorMessage: string }).errorMessage);
}
44 changes: 44 additions & 0 deletions examples/typescript/end-users/sendEvmAsset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Usage: pnpm tsx end-users/sendEvmAsset.ts <USER_UUID>
// Note: This example requires the end user to have an active delegation on their
// account that allows the developer to sign on their behalf.

import { CdpClient } from "@coinbase/cdp-sdk";
import "dotenv/config";

const userId = process.argv[2];
if (!userId) {
console.error("Usage: pnpm tsx end-users/sendEvmAsset.ts <USER_UUID>");
process.exit(1);
}

const cdp = new CdpClient();

try {
const endUser = await cdp.endUser.getEndUser({ userId });

console.log("EVM address:", endUser.evmAccountObjects[0]?.address);

// Send USDC using the client method (developer calls on behalf of end user)
const result = await cdp.endUser.sendEvmAsset({
userId: endUser.userId,
address: endUser.evmAccountObjects[0].address,
asset: "usdc",
to: "0x0000000000000000000000000000000000000001", // recipient address
amount: "1000000", // 1 USDC (6 decimals)
network: "base-sepolia",
});

console.log("Transaction hash (via client):", result.transactionHash);

// Alternatively, send directly on the EndUserAccount (auto-picks first EVM address)
const result2 = await endUser.sendEvmAsset({
asset: "usdc",
to: "0x0000000000000000000000000000000000000001",
amount: "1000000",
network: "base-sepolia",
});

console.log("Transaction hash (via account):", result2.transactionHash);
} catch (error) {
console.error("Error: ", (error as { errorMessage: string }).errorMessage);
}
40 changes: 40 additions & 0 deletions examples/typescript/end-users/sendEvmTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Usage: pnpm tsx end-users/sendEvmTransaction.ts <USER_UUID>
// Note: This example requires the end user to have an active delegation on their
// account that allows the developer to sign on their behalf.

import { CdpClient } from "@coinbase/cdp-sdk";
import "dotenv/config";

const userId = process.argv[2];
if (!userId) {
console.error("Usage: pnpm tsx end-users/sendEvmTransaction.ts <USER_UUID>");
process.exit(1);
}

const cdp = new CdpClient();

try {
const endUser = await cdp.endUser.getEndUser({ userId });

console.log("EVM address:", endUser.evmAccountObjects[0]?.address);

// Send an EVM transaction using the client method (developer calls on behalf of end user)
const result = await cdp.endUser.sendEvmTransaction({
userId: endUser.userId,
address: endUser.evmAccountObjects[0].address,
transaction: "0x02...", // RLP-serialized EIP-1559 transaction
network: "base-sepolia",
});

console.log("Transaction hash (via client):", result.transactionHash);

// Alternatively, send directly on the EndUserAccount (auto-picks first EVM address)
const result2 = await endUser.sendEvmTransaction({
transaction: "0x02...",
network: "base-sepolia",
});

console.log("Transaction hash (via account):", result2.transactionHash);
} catch (error) {
console.error("Error: ", (error as { errorMessage: string }).errorMessage);
}
60 changes: 60 additions & 0 deletions examples/typescript/end-users/sendUserOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Usage: pnpm tsx end-users/sendUserOperation.ts <USER_UUID>
// Note: This example requires the end user to have an active delegation on their
// account that allows the developer to sign on their behalf.

import { CdpClient } from "@coinbase/cdp-sdk";
import "dotenv/config";

const userId = process.argv[2];
if (!userId) {
console.error("Usage: pnpm tsx end-users/sendUserOperation.ts <USER_UUID>");
process.exit(1);
}

const cdp = new CdpClient();

try {
const endUser = await cdp.endUser.getEndUser({ userId });

if (!endUser.evmSmartAccountObjects?.length) {
console.error("End user has no smart account. Create one first.");
process.exit(1);
}

console.log("Smart account address:", endUser.evmSmartAccountObjects[0]?.address);

// Send a user operation using the client method.
// User operations can batch multiple calls in a single transaction.
const result = await cdp.endUser.sendUserOperation({
userId: endUser.userId,
address: endUser.evmSmartAccountObjects[0].address,
network: "base-sepolia",
calls: [
{
to: "0x0000000000000000000000000000000000000000",
value: "0",
data: "0x",
},
],
useCdpPaymaster: true, // CDP sponsors the gas
});

console.log("User operation hash (via client):", result.userOpHash);

// Alternatively, send directly on the EndUserAccount (auto-picks first smart account)
const result2 = await endUser.sendUserOperation({
network: "base-sepolia",
calls: [
{
to: "0x0000000000000000000000000000000000000000",
value: "0",
data: "0x",
},
],
useCdpPaymaster: true,
});

console.log("User operation hash (via account):", result2.userOpHash);
} catch (error) {
console.error("Error: ", (error as { errorMessage: string }).errorMessage);
}
38 changes: 38 additions & 0 deletions examples/typescript/end-users/signEvmHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Usage: pnpm tsx end-users/signEvmHash.ts <USER_UUID>
// Note: This example requires the end user to have an active delegation on their
// account that allows the developer to sign on their behalf.

import { CdpClient } from "@coinbase/cdp-sdk";
import "dotenv/config";

const userId = process.argv[2];
if (!userId) {
console.error("Usage: pnpm tsx end-users/signEvmHash.ts <USER_UUID>");
process.exit(1);
}

const cdp = new CdpClient();

try {
const endUser = await cdp.endUser.getEndUser({ userId });

console.log("EVM address:", endUser.evmAccountObjects[0]?.address);

// Sign an EVM hash using the client method (developer calls on behalf of end user)
const result = await cdp.endUser.signEvmHash({
userId: endUser.userId,
hash: "0x0000000000000000000000000000000000000000000000000000000000000001",
address: endUser.evmAccountObjects[0].address,
});

console.log("Signature (via client):", result.signature);

// Alternatively, sign directly on the EndUserAccount (auto-picks first EVM address)
const result2 = await endUser.signEvmHash({
hash: "0x0000000000000000000000000000000000000000000000000000000000000002",
});

console.log("Signature (via account):", result2.signature);
} catch (error) {
console.error("Error: ", error);
}
38 changes: 38 additions & 0 deletions examples/typescript/end-users/signSolanaMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Usage: pnpm tsx end-users/signSolanaMessage.ts <USER_UUID>
// Note: This example requires the end user to have an active delegation on their
// account that allows the developer to sign on their behalf.

import { CdpClient } from "@coinbase/cdp-sdk";
import "dotenv/config";

const userId = process.argv[2];
if (!userId) {
console.error("Usage: pnpm tsx end-users/signSolanaMessage.ts <USER_UUID>");
process.exit(1);
}

const cdp = new CdpClient();

try {
const endUser = await cdp.endUser.getEndUser({ userId });

console.log("Solana address:", endUser.solanaAccountObjects[0]?.address);

// Sign a Solana message using the client method (developer calls on behalf of end user)
const result = await cdp.endUser.signSolanaMessage({
userId: endUser.userId,
address: endUser.solanaAccountObjects[0].address,
message: Buffer.from("Hello, World!").toString("base64"),
});
Comment on lines +22 to +26
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes the user has granted delegation to the developer, right? Would be good to document that in the script instructions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documented at the top


console.log("Signature (via client):", result.signature);

// Alternatively, sign directly on the EndUserAccount (auto-picks first Solana address)
const result2 = await endUser.signSolanaMessage({
message: Buffer.from("Hello again!").toString("base64"),
});

console.log("Signature (via account):", result2.signature);
} catch (error) {
console.error("Error: ", (error as { errorMessage: string }).errorMessage);
}
Loading
Loading