Skip to content

Example: Account Setup

Foreword

The SDK uses viem by default for all EVM signer interactions. This example assumes you are using viem.

Prepare a signer

In order to sign messages, you will need to prepare a signer instance that implements the EvmSigner interface. This example imports a private key and creates a local viem account.

client.ts
import { privateKeyToAccount } from 'viem/accounts';
import { createWalletClient, http } from 'viem';
import { avalanche } from 'viem/chains';
import { publicActions } from 'viem/actions';
 
// create local viem account using a private key
const account = privateKeyToAccount(process.env.PRIVATE_KEY as Hex);
 
// create viem client
const client = createWalletClient({
    account,
    chain: avalanche,
    transport: http(),
}).extend(publicActions);
 
// implement the EvmSigner interface for viem
const viemSigner = createViemSigner(account, client);

Create a new client

The client is for sending requests to the SilentSwap service.

client.ts
import { createSilentSwapClient } from '@silentswap/sdk';
 
// create new SilentSwap SDK client
const silentswap = createSilentSwapClient({
});

Sign in to SilentSwap service

Use EIP-4361 to sign in to the SilentSwap service, obtaining a secret authentication token in order to derive the secret entropy for generating facilitator wallets.

client.ts
import { deriveSecretEntropy } from './helper.ts';
 
// get secret auth token and derive entropy for generating wallets
const entropy = await deriveSecretEntropy(viemSigner);

Create a facilitator group

At this point, entropy is the raw bytes behind the mnemonic seed phrase for the account's unique SilentSwap super wallet. To access a group of accounts that will be used for a specific order, use a nonce to distinguish it from previous orders based on how many times the account has deposited into the Gateway contract.

client.ts
import { createHdFacilitatorGroupFromEntropy } from '@silentswap/sdk';
 
// get the number of deposits the user has made into Gateway contract
const depositCountReturn = await client.readContract({
    address: silentswap.gatewayAddress,
    abi: GATEWAY_ABI,
    functionName: 'signerCounts',
    args: [account.address],
});
 
// create a new facilitator group, using the deposit count to distinguish from previous orders
const group = await createHdFacilitatorGroupFromEntropy(hexToBytes(entropy), depositCountReturn);

Access a facilitator account

client.ts
// get the facilitator account for coin type 60 at the first output index (0)
const facilitator0Eth = await group.account('60', 0);
 
// get an EVM signer instance for the facilitator account
const facilitator0EthEvm = await facilitator0Eth.evmSigner();