enclavemoney-experimental
v1.0.15
Published
Enclave Money SDK
Downloads
13
Readme
Enclave SDK
The Enclave SDK provides a set of tools to interact with the Enclave API, allowing you to manage smart accounts, build and submit transactions, calculate gas fees, and retrieve smart balances.
Table of Contents
- Installation
- Getting Started
- Create a Smart Account
- Fund Your Smart Account
- Get Smart Balance
- Compute Quote
- Session Key Management
- Passkey Smart Account
- Cross-Network USDC Transfer Example
Installation
To use the Enclave SDK, you need to have Node.js installed. You can install the SDK via npm:
npm install enclavemoneyGetting Started
To get started with the Enclave SDK, you need to initialize an instance of the Enclave class with your API key.
import { Enclave, SignMode } from 'enclavemoney';
const API_KEY = 'your-api-key-here';
const enclave = new Enclave(API_KEY);Create a Smart Account
To create a smart account using the Enclave SDK, you can use the createSmartAccount method. Here's an example:
// Assuming you have already initialized the Enclave instance as shown above
const privateKey = 'your-private-key-here'; // Replace with the user's private key
const wallet = new ethers.Wallet(privateKey);
const account = await enclave.createSmartAccount(wallet.address);
console.log('Smart Account created:', account.wallet.scw_address);Fund Your Smart Account
To fund your smart account, you can deposit USDC to the wallet address on any supported network. Follow these steps:
Retrieve Your Wallet Address: Use the
createSmartAccountmethod to get your wallet address if you haven't already.Deposit USDC: Transfer USDC to your smart account's wallet address using any wallet or exchange that supports the network you are using.
Verify the Deposit: After the transfer, you can verify the deposit by checking the balance using the
getSmartBalancemethod.
Get Smart Balance
To retrieve the balance of a smart account, use the getSmartBalance method. Here's an example:
// Assuming you have already initialized the Enclave instance as shown above
const balance = await enclave.getSmartBalance(account.wallet.scw_address);
console.log('Smart Account Balance:', balance);Compute Quote
To compute a quote using the Enclave SDK, you can use the computeQuote method. Here's an example:
// Assuming you have already initialized the Enclave instance as shown above
const network = 10;
const amount = 100; // Amount to compute the quote for
const limit = 102; // Limit for the quote (optional)
const type = 'AMOUNT_OUT';
// User wants 100 USDC on OP Mainnet and is willing to spend at most 102 USDC from their balance
const quote = await enclave.computeQuote(account.wallet.scw_address, network, amount, type, limit);
console.log('Quote computed:', quote);// Assuming you have already initialized the Enclave instance as shown above
const network = 10;
const amount = 100; // Amount to compute the quote for
const limit = 99; // Limit for the quote (optional)
const type = 'AMOUNT_IN';
// User is willing to spend at 100 USDC from their balance and wants at least 99 USDC on OP Mainnet
const quote = await enclave.computeQuote(account.wallet.scw_address, network, amount, type, limit);
console.log('Quote computed:', quote);Explanation
- Order Types:
- AMOUNT_OUT: This type indicates that the user needs a specific amount on the target network. The user will be charged the specified amount plus any applicable fees.
- AMOUNT_IN: This type indicates that the user is willing to spend a specific amount, and they will receive the specified amount minus any applicable fees on the target network.
Passkey Smart Account
The Enclave SDK supports creating and managing smart accounts using passkeys (WebAuthn). This provides a secure and convenient way to manage your smart account without needing to handle private keys.
Creating Account
To create a smart account using a passkey, follow these steps:
// 1. Get registration options
const registrationOptions = await enclave.getPasskeyAccountRegistrationOptions("[email protected]");
// 2. Use client side passkey library to fetch attestation response for the options retrieved above
// ...
// 3. Submit registration response
// The response object comes from the passkey library after user completes registration
const account = await enclave.submitPasskeyAccountRegistrationResponse(username, response);
console.log('Passkey Smart Account created:', account.wallet.scw_address);
Get User
To retrieve information about a passkey account user:
// Fetch user information using their username
const user = await enclave.getUser("[email protected]");
console.log('User information:', user);Submitting Transactions
To submit transactions using a passkey smart account, follow these steps:
// 1. Prepare transaction details
const transactionDetails = [{
encodedData: "0x...", // Your encoded transaction data
targetContractAddress: "0x...", // Target contract address
value: 0
}];
// 2. Get transaction options from Enclave
const options = await enclave.getPasskeyTransactionOptions(
account.wallet.scw_address,
10,
transactionDetails,
{
amount: "1000000", // 1 USDC
type: "AMOUNT_OUT"
}
);
// 3. Use client side passkey library to fetch attestation response for the options retrieved above
// ...
// 4. Submit the signed transaction to Enclave
const result = await enclave.submitPasskeyTransactionResponse(username, response);The passkey approach provides several benefits:
- No private keys to manage
- Built-in biometric authentication
- Phishing-resistant
- Native browser, iOS, Android, MacOS, iPadOS and Windows support
Session Key Management
The Enclave SDK provides functionality to enable and disable session keys for your smart account. Session keys allow for temporary delegation of transaction signing capabilities.
Enable Session Key
To enable a session key for your smart account, use the enableSessionKey method:
import { ethers, getBytes } from 'ethers';
// Assuming you have already initialized the Enclave instance as shown above
const validAfter = Math.floor(Date.now() / 1000); // Unix timestamp when the session key becomes valid
const validUntil = validAfter + (24 * 60 * 60); // Valid for 24 hours
const network = 10; // Network ID (e.g., 10 for Optimism)
const builtTxn = await enclave.enableSessionKey(
account.wallet.scw_address,
validAfter,
validUntil,
network
);
// Sign the message
const signature = await wallet.signMessage(getBytes(builtTxn.messageToSign));
// Submit the transaction
const response = await enclave.submitTransaction(
signature,
builtTxn.userOp,
10, // Target network
account.wallet.scw_address,
builtTxn.signMode
);
console.log('Transaction submitted successfully:', response);Disable Session Key
To disable a previously enabled session key, use the disableSessionKey method:
// Assuming you have already initialized the Enclave instance as shown above
const network = 10; // Network ID (e.g., 10 for Optimism)
const builtTxn = await enclave.disableSessionKey(
account.wallet.scw_address,
network
);
// Sign the message
const signature = await wallet.signMessage(getBytes(builtTxn.messageToSign));
// Submit the transaction
const response = await enclave.submitTransaction(
signature,
builtTxn.userOp,
10, // Target network
account.wallet.scw_address,
builtTxn.signMode
);
console.log('Transaction submitted successfully:', response);Delegate Action
To execute delegated transactions through an account once session keys are enabled:
// Submit the transaction
const response = await enclave.delegateAction(
transactionDetails,
10, // Target network
account.wallet.scw_address
);
console.log('Transaction submitted successfully:', response);Cross-Network USDC Transfer Example
In this example, we'll demonstrate how to deposit USDC on Arbitrum and then transfer it to an address on Optimism using the Enclave SDK.
Step 1: Deposit USDC on Arbitrum
First, ensure you have deposited USDC into your smart account on the Arbitrum network. You can verify the deposit using the getSmartBalance method.
Step 2: Build transaction to transfer USDC to Optimism
To transfer USDC to a wallet on Optimism, you'll need to construct the calldata for the transfer and build the transaction. Here's how you can do it:
// Define the transaction details
// Define the USDC contract address on Optimism
const usdcContractAddress = '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85';
// Define the recipient address and amount to transfer
const recipientAddress = '0x...'; // Replace with the recipient's address
const amount = ethers.utils.parseUnits('1', 6); // Amount of USDC to transfer (1 USDC)
// Create the call data for the ERC20 transfer
const erc20Interface = new ethers.utils.Interface([
'function transfer(address to, uint256 amount)'
]);
const encodedData = erc20Interface.encodeFunctionData('transfer', [recipientAddress, amount]);
const transactionDetails = [{
encodedData,
targetContractAddress: usdcContractAddress,
value: 0 // Assuming no ETH is being transferred, only USDC
}];
// Define the order data - Describes how much the user wants to spend from their chain-abstracted balances
const orderData = {
amount: amount.toString(), // Amount of USDC required for the transfer
type: 'AMOUNT_OUT'
// Understanding order type
// AMOUNT_OUT: User needs 100 USDC on the target network. The user will be charged 100 + fees.
// AMOUNT_IN : User is charged 100 USDC and receives (100 - fees) on target network
};
// Build the transaction
const builtTxn = await enclave.buildTransaction(
transactionDetails,
10, // Target network
account.wallet.scw_address, // User's smart account address
orderData,
undefined,
SignMode.ECDSA // Sign mode (Pass SignMode.SimpleSessionKey for sessionKey transaction)
);
console.log('Transaction built successfully:', builtTxn);Explanation
- Transaction Details: This includes the encoded data for the USDC transfer and the target contract address on Optimism.
- Order Data: Specifies the amount of USDC to transfer and the type of operation.
- Build Transaction: The
buildTransactionmethod constructs the transaction for the specified network (Optimism in this case).
Step 3: Sign and submit the transaction
After building the transaction, you need to sign the messageToSign with the user's EOA (Externally Owned Account) and then submit the transaction using the Enclave SDK.
To sign the transaction, you can use the ethers library to sign the messageToSign with the user's private key.
import { ethers, getBytes } from 'ethers';
// Sign the message
const signature = await wallet.signMessage(getBytes(builtTxn.messageToSign));
// Submit the transaction
const response = await enclave.submitTransaction(
signature,
builtTxn.userOp,
10, // Target network
account.wallet.scw_address,
SignMode.ECDSA// Signature type (Pass SignMode.SimpleSessionKey if the signature is being generated from a sessionKey instead of the user's default EOA)
);
console.log('Transaction submitted successfully:', response);Explanation
- Sign the Message: Use the
etherslibrary to sign themessageToSignwith the user's private key. - Submit the Transaction: Call the
submitTransactionmethod on the Enclave SDK object, passing the signature and other required parameters.
