0.3.16 • Published 5 months ago

@crossmint/client-sdk-smart-wallet v0.3.16

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

Smart Wallet SDK

!WARNING Smart Wallet SDK is no longer maintained, use Wallets SDK instead.

A client-side SDK for interacting with Crossmint Smart Wallets. This SDK enables developers to create and manage smart contract wallets that support both traditional keypair-based signing and passkey authentication.

Installation

npm install @crossmint/client-sdk-smart-wallet
# or
pnpm add @crossmint/client-sdk-smart-wallet

Authentication

This SDK uses JSON Web Tokens (JWT) to authenticate your users. JWT is generated by the auth server and used to authorize users to deploy and operate their wallets.

For detailed instructions on how to obtain a JWT token, please refer to User Auth.

Quick Start

import { SmartWalletSDK } from "@crossmint/client-sdk-smart-wallet";
import { privateKeyToAccount } from "viem";

const sdk = SmartWalletSDK.init({ clientApiKey: "<YOUR_API_KEY>" });
const account = privateKeyToAccount(privateKey);

const wallet = await sdk.getOrCreateWallet(
  {
    jwt: "<USER_TOKEN>",
  },
  "base-sepolia",
  {
    signer: {
      type: "VIEM_ACCOUNT",
      account,
    },
  }
);

const address = wallet.address;

Examples

Wallet Creation

Passkey Wallets

const signer = await sdk.createPasskeySigner("My Wallet")
const wallet = await sdk.getOrCreateWallet(
  {
    jwt: "<USER_TOKEN>",
  },
  "base-sepolia",
  {
    signer,
  }
);
const address = walletClient.getAddress();

Private Key Wallets

import { privateKeyToAccount } from "viem";

const account = privateKeyToAccount(privateKey);
const wallet = await sdk.getOrCreateWallet(
  {
    jwt: "<USER_TOKEN>",
  },
  "base-sepolia",
  {
    signer: {
      type: "VIEM_ACCOUNT",
      account,
    },
  }
);
const address = walletClient.getAddress();

Browser Wallets

import type { EIP1193Provider } from "viem";

const providerSigner = window.ethereum as EIP1193Provider;
const wallet = await sdk.getOrCreateWallet(
  {
    jwt: "<USER_TOKEN>",
  },
  "base-sepolia",
  {
    signer: providerSigner,
  }
);

Sending Transactions

import { encodeFunctionData, erc20Abi } from "viem";

const walletClient = wallet.client.wallet;
const txHash = await walletClient.sendTransaction({
  to: "<TOKEN_ADDRESS>",
  data: encodeFunctionData({
    abi: erc20Abi,
    functionName: "transfer",
    args: ["<RECEIVER_ADDRESS>", "<TRANSFER_AMOUNT>"],
  })
})

Signing Messages

const walletClient = wallet.client.wallet;
const signature = await walletClient.signMessage({
  message: "Hello, Crossmint!"
})

Migrating from 0.1.x

0.2.x SDK strives to be backwards-compatible with 0.1.x as much as possible.

However, since the wallets are now powered by Crossmint API, some functionality is not available anymore.

Specifically, some functions from the SmartWalletClient object are not available anymore:

  • request
  • writeContract
  • deployContract
  • account
  • sendUserOperation
  • prepareUserOperationRequest

Additionally, the passkey creation flow is changed. To create a passkey-powered wallet, you first create a passkey signer:

const signer = await sdk.createPasskeySigner("My Wallet")

Then use that signer during wallet creation:

const wallet = await sdk.getOrCreateWallet(
  {
    jwt: "<USER_TOKEN>",
  },
  "<NETWORK>",
  {
    signer,
  }
);

API Reference

SmartWalletSDK

The main entry point for interacting with smart wallets.

Static Methods

init(params: SmartWalletSDKInitParams): SmartWalletSDK

Initializes the SDK with your client-side API key.

const sdk = SmartWalletSDK.init({ clientApiKey: "<YOUR_API_KEY>" });

Instance Methods

getOrCreateWallet(user: UserParams, chain: SmartWalletChain, walletParams: WalletParams): Promise<EVMSmartWallet>

Creates or retrieves a smart wallet for the specified user.

const wallet = await sdk.getOrCreateWallet(
  {
    jwt: "<USER_TOKEN>" 
  },
  "<NETWORK>",
  {
    signer: /* PasskeySigner or ExternalSigner */
  }
);
createPasskeySigner(name: string): Promise<PasskeySigner>

Creates a new passkey signer with the given name.

const signer = await sdk.createPasskeySigner("My Wallet");

Types

SmartWalletChain

Supported blockchain networks.

WalletParams

Configuration for wallet creation:

interface WalletParams {
  signer: ExternalSigner | PasskeySigner;
}

// Use existing Web3 wallet or private key
type ExternalSigner = EIP1193Provider | ViemAccount;

// Use private key authentication
interface ViemAccount {
  type: "VIEM_ACCOUNT";
  account: LocalAccount & { source: "custom" };
};

// Use passkey authentication
interface PasskeySigner {
  type: "PASSKEY";
  credential: WebAuthnP256.P256Credential;
}

EVMSmartWallet

The wallet instance returned by getOrCreateWallet(). Provides methods for interacting with the smart wallet:

interface EVMSmartWallet {
  // viem clients that provide an interface for core wallet functionality.
  client: {
      public: PublicClient<HttpTransport>;
      wallet: SmartWalletClient;
  };

  // The EVM chain of the smart wallet.
  chain: SmartWalletChain;

  // The address of the smart wallet.
  address(): Address;

  // Transfers tokens from the smart wallet to a specified address.
  transferToken(toAddress: Address, config: TransferType): Promise<string>;

  // A list of NFTs owned by the wallet.
  nfts(): Promise<NftResponse>;

  // Sends a contract call transaction and returns the hash of a confirmed transaction.
  executeContract(params: {
    address: Address;
    abi: Abi;
    functionName: string;
    args: unknown;
  }): Promise<Hex>;
}

SmartWalletClient

The client instance available via wallet.client.wallet. Provides methods for interacting with the wallet:

interface SmartWalletClient {
  // Get the wallet's address
  getAddress(): Address;
  
  // Sign a message
  signMessage(message: string): Promise<Hex>;
  
  // Sign typed data (EIP-712)
  signTypedData<T extends TypedData>(params: TypedDataDefinition<T>): Promise<Hex>;
  
  // Send a transaction
  sendTransaction(params: {
    to: Address;
    data?: Hex;
    value?: bigint;
  }): Promise<Hex>;
}

Full reference is available in the docs.

0.3.0

8 months ago

0.3.6

7 months ago

0.3.5

7 months ago

0.3.8

6 months ago

0.3.7

7 months ago

0.3.2

7 months ago

0.3.1

7 months ago

0.3.4

7 months ago

0.3.3

7 months ago

0.1.30

9 months ago

0.1.31

9 months ago

0.1.32

9 months ago

0.1.33

9 months ago

0.1.34

9 months ago

0.3.9

6 months ago

0.1.35

8 months ago

0.1.36

8 months ago

0.3.16

5 months ago

0.3.15

5 months ago

0.3.14

5 months ago

0.3.13

5 months ago

0.3.12

5 months ago

0.3.11

6 months ago

0.3.10

6 months ago

0.1.27

11 months ago

0.1.28

11 months ago

0.1.29

11 months ago

0.1.22

12 months ago

0.1.23

11 months ago

0.1.24

11 months ago

0.1.25

11 months ago

0.1.26

11 months ago

0.2.0

8 months ago

0.1.21

1 year ago

0.1.20

1 year ago

0.1.19

1 year ago

0.1.18

1 year ago

0.1.17

1 year ago

0.1.16

1 year ago

0.1.15

1 year ago

0.1.14

1 year ago

0.1.13

1 year ago

0.1.12

1 year ago

0.1.10

1 year ago

0.1.11

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago