A dapp can use CashConnect to ask a wallet to send 25 loyalty points. The app supplies a transaction template with a named action and sends the amount and recipient as parameters. The wallet resolves the template, gets user approval, executes the action, and returns its result. The current @cashconnect-js/nostr SDK carries these requests over Nostr.
Prepare the template
- Define the contract scripts, actions and variables with
@cashconnect-js/core. The@cashconnect-js/templates-sdkpackage provides template development helpers. - Test a single named action first. Define its required parameters and expected transaction outputs, token balance changes and return data.
- Review the template and the wallet’s preview using the same pinned version. A wallet approves a template and session scope before it accepts an action.
Start a session
Pass the reviewed template, app metadata, target chain and requested token allowlist to the Nostr SDK’s Dapp. After start(), call createInviteUrl() to render the invite or provide a pairing dialog to pair(). Enable wallet actions when the SDK reports sessionEstablished.
import { Dapp } from '@cashconnect-js/nostr';
const dapp = new Dapp({
info: { name: 'BCH demo', url: location.origin },
chain: networkId,
template: reviewedTemplate,
allowedTokens: requestedTokenCategories,
});
dapp.on('sessionEstablished', ({ session }) => showConnected(session));
await dapp.start();
if (!dapp.isSessionActive()) showPairingQr(dapp.createInviteUrl());
networkId, the template, token category list, and UI callbacks belong to the app. Keep them aligned with the wallet and the action under review.
Request an action
Call dapp.executeAction(actionName, params) with a name and fields defined in the approved template. Read the returned transaction results, consumed and emitted UTXOs, and reported balance changes. A connection or response alone does not establish on-chain confirmation. Look up the transaction when the app needs confirmation.
Handle cancellation, wallet rejection, and an unreachable peer. A stored session may resume when the wallet comes back online. Use forgetSession() when the user disconnects and stop() when tearing down the local client.
CashConnect protocol reference · First template tutorial · CashConnect SDK
An application that uses a decentralized system such as BCH and can ask a wallet to authorize an action or payment.Software or a device that manages addresses and tracks UTXOs, with signing wallets also holding private keys to authorize payments.A Bitcoin Cash record that defines new outputs and, for a payment, identifies the UTXOs being spent.A record created by a transaction with a satoshi amount, a locking script, and optional CashTokens data.Descriptive information such as a derived key’s depth and index or a token’s name and icon.A 32-byte CashTokens identifier that groups related tokens and defines which fungible units are interchangeable.The basic unit of spendable value on Bitcoin Cash, holding satoshis and any attached tokens under the spending rules defined by its locking bytecode.A transaction has one confirmation when included in a block on the active chain and gains another for each subsequent block.