Work with the client
To interact with the Chromia blockchain using the bridge client, you'll need to manage an active Session
that handles
all Chromia-related queries and operations. However, the bridge client can be initialized without a session at the
start. Here's a step-by-step guide to set up your application using @chromia/ft4
.
// 1: Setup a connection to postchain through postchain-client
const pcl = await createClient({
nodeUrlPool: "YOU_NODE_URL_POOL",
blockchainRid: "YOUR_BLOCKCHAIN_RID",
});
// 2: Create an account
const evmKeyStore = await createWeb3ProviderEvmKeyStore(window.ethereum);
const ad = createSingleSigAuthDescriptorRegistration([AuthFlag.Account, AuthFlag.Transfer], evmKeyStore.id);
const response = await registerAccount(pcl, evmKeyStore, registrationStrategy.open(ad));
// 3: Log in your user
const evmKeyStoreInteractor = createKeyStoreInteractor(pcl, evmKeyStore);
const accounts = await evmKeyStoreInteractor.getAccounts();
const session = await evmKeyStoreInteractor.getSession(accounts[0].id);
Ensure that you save your evmKeyStore
in your application state, as some methods in @chromia/bridge-client
will
require it.
With your Session
object ready, you can initialize the bridge client. You will also need to provide an EVM Provider,
either a BrowserProvider
or a JsonRpcProvider
:
const provider = new BrowserProvider(window.ethereum);
const bcl = await bridgeClient(
{ bridgeAddress: "YOUR_BRIDGE_ADDRESS", tokenAddress: "YOUR_TOKEN_ADDRESS" },
provider,
session
);
If your application setup prevents having an active Session
when instantiating the bridge client, you can still set up
the client without it:
const provider = new BrowserProvider(window.ethereum);
const bcl = await bridgeClient({ bridgeAddress: "YOUR_BRIDGE_ADDRESS", tokenAddress: "YOUR_TOKEN_ADDRESS" }, provider);
Later, when you have access to the Session
, you can set it on the client with setSession(session: Session)
:
bcl.setSession(session);