SDK quickstart
Send your first encrypted mail with the 4thTech SDK 0.5.x: build the clients, listen for new mail, send.
This quickstart targets SDK 0.5.x (@4thtech-sdk/ethereum 0.5.13). It runs in a browser, because encrypted mail needs the Encryptor extension. Use a testnet while you try it out.
Before you start
A browser wallet with some testnet funds on a supported network that has OCC v1 contracts.
The Encryptor extension installed, unlocked and set up, for both the sender and the receiver. The receiver's public key must be published; the white-label app does this for you, or use
Encryptor.storePublicKey().A storage provider: a PollinationX endpoint and token. See Storage.
1. Install
npm install @4thtech-sdk/ethereum @4thtech-sdk/encryption @4thtech-sdk/encryptor @4thtech-sdk/storage @4thtech-sdk/types viem2. Create a wallet client
The SDK needs an object that implements WalletClient. This one uses viem with an injected browser wallet.
import { createWalletClient, custom } from 'viem';
import { gnosisChiado } from '@4thtech-sdk/ethereum';
import type { Address, EthereumTransactionRequest, WalletClient } from '@4thtech-sdk/types';
const chain = gnosisChiado; // a testnet from @4thtech-sdk/ethereum
const viemClient = createWalletClient({ chain, transport: custom(window.ethereum) });
const [account] = await viemClient.requestAddresses();
const walletClient: WalletClient = {
chain,
getAddress: async (): Promise<Address> => account,
sendTransaction: (request: EthereumTransactionRequest) =>
viemClient.sendTransaction({ account, chain, ...request }),
};sendTransaction must return the transaction hash. The user confirms it in their wallet.
3. Create the storage provider, Encryptor and encryption handler
4. Create the Mail client
5. Listen for new mail
6. Send an encrypted mail
initialize fetches the receiver's public key and computes the shared secret, so it fails if the receiver has not published one. send stores the encrypted envelope, then asks the wallet to sign the transaction.
What happens next
Figure: The call sequence of the quickstart: build the clients, listen for new mail, then send one encrypted mail.
The receiver's onNew callback fires with a ReceivedEnvelope, decrypted by the EncryptionHandler. See send, onNew and fetchPaginated for the full options, and SDK architecture for the call sequence.
Last updated
