An easy-to-use SDK for building frontend interfaces that interact with a community server and any related smart contracts that we use. This SDK is frontend framework agnostic and includes types, API calls, and state management.
Welcome to the official SDK for our platform.
We hope you find this SDK useful, and we're excited to see what you build with it!
To install the SDK, run the following command in your terminal:
Deno add jsr:@citizenwallet/sdk
To install the SDK, run the following command in your terminal:
npm install --save @citizenwallet/sdk
Config
- Main configuration interfaceConfigCommunity
- Community configurationConfigToken
- Token configurationConfigAccount
- Account configurationConfigChain
- Chain configurationConfigIPFS
- IPFS configurationConfigPlugin
- Plugin configuration
The main configuration class that provides helper methods for accessing community settings.
import { CommunityConfig } from "@citizenwallet/sdk";
const config = new CommunityConfig(configData);
// Access primary token
const token = config.primaryToken;
// Access primary network
const network = config.primaryNetwork;
// Access RPC URL
const rpcUrl = config.primaryRPCUrl;
// Access account configuration
const accountConfig = config.primaryAccountConfig;
// Access session configuration
const sessionConfig = config.primarySessionConfig;
// Access card configuration
const cardConfig = config.primaryCardConfig;
Predefined network configurations for supported blockchains:
import { NETWORKS } from "@citizenwallet/sdk";
// Available networks: 100 (Gnosis), 137 (Polygon), 8453 (Base), 42220 (CELO), 42161 (Arbitrum)
const gnosisNetwork = NETWORKS["100"];
Profile management functions for community members.
Formats profile image URLs to use the provided IPFS domain.
Converts a username to bytes32 format for smart contract interaction.
getProfileFromId(ipfsDomain: string, config: CommunityConfig, id: string): Promise<ProfileWithTokenId | null>
Retrieves a profile by its token ID.
getProfileFromAddress(ipfsDomain: string, config: CommunityConfig, address: string): Promise<ProfileWithTokenId | null>
Retrieves a profile by wallet address.
getProfileFromUsername(ipfsDomain: string, config: CommunityConfig, username: string): Promise<ProfileWithTokenId | null>
Retrieves a profile by username.
Checks if an address has profile admin role.
Checks if a username is available for registration.
Transaction log management and querying.
Service for querying transaction logs with pagination and filtering.
import { LogsService } from "@citizenwallet/sdk";
const logsService = new LogsService(config);
// Get a specific log
const log = await logsService.getLog(tokenAddress, hash);
// Get logs with pagination and filtering
const logs = await logsService.getLogs(tokenAddress, topic, {
limit: 10,
offset: 0,
maxDate: "2024-01-01T00:00:00Z",
data: { key: "value" },
});
// Get all logs
const allLogs = await logsService.getAllLogs(tokenAddress, topic);
// Get new logs since a date
const newLogs = await logsService.getNewLogs(tokenAddress, topic, {
fromDate: "2024-01-01T00:00:00Z",
});
TRANSFER_EVENT_SIGNATURE
- ERC20 transfer event signature
Smart account transaction bundling and execution.
Service for creating and submitting user operations to the bundler.
import { BundlerService } from "@citizenwallet/sdk";
const bundler = new BundlerService(config);
// Send ERC20 tokens
const txHash = await bundler.sendERC20Token(
signer,
tokenAddress,
fromAddress,
toAddress,
amount,
description
);
// Mint ERC20 tokens
const txHash = await bundler.mintERC20Token(
signer,
tokenAddress,
fromAddress,
toAddress,
amount,
description
);
// Burn ERC20 tokens
const txHash = await bundler.burnFromERC20Token(
signer,
tokenAddress,
senderAddress,
fromAddress,
amount,
description
);
// Set profile
const txHash = await bundler.setProfile(
signer,
signerAccountAddress,
profileAccountAddress,
username,
ipfsHash
);
// Burn profile
const txHash = await bundler.burnProfile(
signer,
signerAccountAddress,
profileAccountAddress
);
// Grant role
const txHash = await bundler.grantRole(
signer,
tokenAddress,
senderAddress,
role,
accountAddress
);
// Revoke role
const txHash = await bundler.revokeRole(
signer,
tokenAddress,
senderAddress,
role,
accountAddress
);
// Execute custom call
const txHash = await bundler.call(
signer,
contractAddress,
senderAddress,
calldata,
value,
userOpData,
extraData
);
IPFS integration utilities.
Downloads and parses JSON data from IPFS.
Voucher creation and parsing for token distribution.
createVoucher(config: CommunityConfig, voucherName: string, voucherCreator: string, voucherSigner: BaseWallet): Promise<{voucherLink: string, voucherAccountAddress: string}>
Creates a voucher for token distribution.
Parses a voucher from a URL or data string.
QR code parsing and deep link generation.
generateLegacyReceiveLink(baseUrl: string, config: CommunityConfig, account: string, amount?: string, description?: string): string
Generates a legacy receive link for token transfers.
Determines the format of a QR code or URI.
Parses QR code data into structured format.
Extracts message from a receive link.
Account management and verification functions.
Resolves ENS domain to address.
Gets the smart account address for a given owner and salt.
Gets the token balance for an account.
verifyAccountOwnership(config: CommunityConfig, accountAddress: string, message: string, signature: string): Promise<boolean>
Verifies account ownership through signature validation.
isSafeOwner(config: CommunityConfig, accountAddress: string, ownerAddress: string): Promise<boolean>
Checks if an address is an owner of a Safe account.
Receive link generation.
generateReceiveLink(baseUrl: string, config: CommunityConfig, account: string, amount?: string, description?: string): string
Generates a receive link for token transfers.
Transaction utilities.
Waits for a transaction to be confirmed and checks its success status.
Card management functions.
getCardAddress(config: CommunityConfig, hashedSerial: string, instanceId?: string): Promise<string | null>
Gets the card address for a given serial number and instance.
Gets the owner of a card instance.
Smart contract call data generation.
Generates calldata for ERC20 token transfers.
Generates calldata for ERC20 token minting.
createInstanceCallData(config: CommunityConfig, contracts: string[], instanceId?: string): Uint8Array
Generates calldata for creating card instances.
updateInstanceContractsCallData(config: CommunityConfig, contracts: string[], instanceId?: string): Uint8Array
Generates calldata for updating instance contracts.
updateWhitelistCallData(config: CommunityConfig, addresses: string[], instanceId?: string): Uint8Array
Generates calldata for updating whitelist.
callOnCardCallData(config: CommunityConfig, hashedSerial: string, to: string, value: bigint, data: Uint8Array, instanceId?: string): Uint8Array
Generates calldata for executing calls on cards.
addOwnerCallData(config: CommunityConfig, hashedSerial: string, newOwner: string, instanceId?: string): Uint8Array
Generates calldata for adding card owners.
generateCalldataLink(baseUrl: string, config: CommunityConfig, address: string, value: bigint, calldata: string): string
Generates a deep link with calldata for execution.
tokenTransferEventTopic
- ERC20 transfer event topictokenTransferSingleEventTopic
- ERC1155 transfer single event topicroleGrantedEventTopic
- Role granted event topicroleRevokedEventTopic
- Role revoked event topic
Cryptographic utilities and role management.
Checks if a function exists in an ABI.
hasRole(tokenAddress: string, role: string, account: string, provider: JsonRpcProvider): Promise<boolean>
Checks if an account has a specific role.
MINTER_ROLE
- ERC20 minter role hashPROFILE_ADMIN_ROLE
- Profile admin role hash
Compression utilities for voucher and deep link data.
compress(data: string): string
- Compresses data using gzipdecompress(data: string): string
- Decompresses gzipped data
Domain parsing utilities.
Extracts community alias from a domain.
Authentication and connection utilities.
generateConnectionMessage(accountAddress: string, expiryTimeStamp: string, redirectUrl?: string): string
Generates a connection message for signature authentication.
generateConnectedHeaders(signer: Signer, accountAddress: string, expiryTimeStamp: string, redirectUrl?: string): Promise<object>
Generates authentication headers for API requests.
createConnectedUrl(url: string, signer: Signer, accountAddress: string, expiryTimeStamp: string, redirectUrl?: string): Promise<string>
Creates a URL with authentication parameters.
Verifies authentication headers and returns the account address.
verifyConnectedUrl(config: CommunityConfig, options: {url?: string, params?: URLSearchParams}): Promise<string | null>
Verifies authentication parameters from a URL and returns the account address.
Session management for secure interactions.
Generates a unique salt for session identification.
generateSessionRequestHash(params: {community: CommunityConfig, sessionOwner: string, salt: string, expiry: number}): string
Generates a hash for session request verification.
Generates the final session hash with challenge.
verifySessionRequest(params: {community: CommunityConfig, sessionOwner: string, source: string, type: string, expiry: number, signature: string}): boolean
Verifies a session request signature.
verifySessionConfirm(params: {sessionOwner: string, sessionHash: string, signedSessionHash: string}): boolean
Verifies a session confirmation signature.
requestSession(params: {community: CommunityConfig, signer: Wallet, sessionSalt: string, sessionRequestHash: string, signedSessionRequestHash: string, signedSessionHash: string, sessionExpiry: number}): Promise<string>
Submits a session request to the blockchain.
verifyIncomingSessionRequest(params: {community: CommunityConfig, signer: Wallet, sessionRequestHash: string, sessionHash: string}): Promise<boolean>
Verifies an incoming session request.
confirmSession(params: {community: CommunityConfig, signer: Wallet, sessionRequestHash: string, sessionHash: string, signedSessionHash: string}): Promise<string>
Confirms a session on the blockchain.
isSessionExpired(params: {community: CommunityConfig, account: string, owner: string}): Promise<boolean>
Checks if a session has expired.
getTwoFAAddress(params: {community: CommunityConfig, source: string, type: string}): Promise<string | null>
Gets the 2FA address for a session.
revokeSession(params: {community: CommunityConfig, signer: Wallet, account: string}): Promise<string | null>
Revokes an active session.
Token metadata utilities.
Gets the decimal places of the primary token.
Gets the name of the primary token.
Gets the symbol of the primary token.
getTokenMetadata(config: CommunityConfig): Promise<{decimals: bigint | null, name: string | null, symbol: string | null} | null>
Gets all metadata for the primary token.
A voucher is actually simply a random private key which is used to generate a Smart Account that we top up and send to someone with some metadata.
You are effectively creating an account, topping it up, describing what it is and sending that over to the other person.
import { createVoucher } from "@citizenwallet/sdk";
const communityAlias = "bread"; // config.community.alias
const voucherName = "Voucher for X tokens";
const creatorAddress = "0x59D17ec3d96C52d4539eed43f33492679ae4aCf7"; // since the user who is redeeming will only see a transaction from the voucher to them, this allows displaying the original creator on the UI.
const signer = ethers.Wallet.createRandom(); // generate a random account which will be used for the voucher.
const voucher = await createVoucher(
config,
voucherName,
creatorAddress,
signer
);
Example:
{
"voucherLink": "https://app.citizenwallet.xyz/#/?voucher=H4sIAEFF7WYAAw3JyQ3AMAgEwIoiLeYy5YDBJaT-5D3vemZEhHHS83INkWm5G4uo6MJkN4f_jFiEuhZnI3sHyJkaH_18VYRDAAAA¶ms=H4sIAEFF7WYAAw3LsRLCIAwA0L_p4qIkEDIwSC3_kUI4vdNyR6nn59vlbU_eL9nD2lXKlE9H6-H6s_y4kWYo7GZrClpg1YJQAZCNIxZFmStNknM7tnEWMItjghRNrQ6i95YpMSzI0Zv7SmhIgFOZNvloGLqPy7cd-an9D-Zqgw6DAAAA",
"voucherAccountAddress": "0x32E6973FB2ff63B88597F93E49B82Ab7427a39Fd"
}
import { parseVoucher } from "@citizenwallet/sdk";
const parsed = parseVoucher(voucher.voucherLink);
Example:
{
"voucher": {
"alias": "bread",
"creator": "0x59D17ec3d96C52d4539eed43f33492679ae4aCf7",
"account": "0x32E6973FB2ff63B88597F93E49B82Ab7427a39Fd",
"name": "test voucher"
},
"signer": {
"provider": null,
"address": "0x60fab84316061E5c7C2eD09a2c4Be454B6B1fC69"
}
}
signer = type ethers.Wallet
Every community and their currency has a configuration in a json format.
The SDK provides types for accessing properties as well as a class with helper functions.
To build the SDK, run the following command in your terminal:
npm run build
This will compile the TypeScript code to JavaScript.
To automatically recompile the SDK when a file changes, run the following command in your terminal:
npm run watch
We welcome contributions! Please see our contributing guidelines for more details.
This project is licensed under the MIT license.