To request a BCH signature over WalletConnect v2, pair with a wallet first. The wallet approves the network and methods the app can use in that session. The WC2-BCH specification defines the requests under the bch namespace.
Propose a session
Initialize @walletconnect/sign-client with your project ID and app metadata. Use bch:bitcoincash for mainnet. Use bch:bchtest only when the wallet and dapp agree on the same BCH test chain. The identifier is shared across BCH testnet variants.
const chainId = 'bch:bitcoincash';
const pairing = await client.connect({
requiredNamespaces: {
bch: {
chains: [chainId],
methods: ['bch_getAddresses', 'bch_signTransaction'],
events: ['addressesChanged'],
},
},
});
if (pairing.uri) showPairingQr(pairing.uri);
const session = await pairing.approval();
const addresses = await client.request({
topic: session.topic,
chainId,
request: { method: 'bch_getAddresses', params: {} },
});
Check the returned session’s approved chain and methods before enabling actions. Store only the session state your SDK requires. Refresh addresses and transaction drafts when the selected account changes or the session ends.
Request transaction approval
Send the proposed transaction plus one record in sourceOutputs per input, in input order. Each record identifies the UTXO being spent by outpoint and includes its BCH value, locking bytecode, and any token data. Use Libauth’s extended JSON encoding where needed for values such as bigint and Uint8Array.
import { stringify } from '@bitauth/libauth';
const result = await client.request({
topic: session.topic,
chainId,
request: {
method: 'bch_signTransaction',
params: {
transaction: rawUnsignedHex,
sourceOutputs: JSON.parse(stringify(sourceOutputs)),
broadcast: false,
userPrompt: 'Review payment',
},
},
});
The result of JSON.parse(stringify(sourceOutputs)) is an array containing the protocol’s tagged strings. With broadcast: false, decode the returned signedTransaction, check it against the intended spend and submit it explicitly. The specification defaults broadcast to true.
Message signing and lifecycle
Request bch_signMessage when needed and verify signatures using the BCH signed-message format and the connected address. Handle rejected requests and disconnected or expired sessions. Optional methods, including batch signing and cancellation extensions, require support from the connected wallet.
WC2-BCH protocol reference · WC2-BCH specification · Reference dapp examples
The currency unit of Bitcoin Cash, equal to 100,000,000 satoshis.A value made with a private key that lets someone with the corresponding public key verify authorization of the signed data.Software or a device that manages addresses and tracks UTXOs, with signing wallets also holding private keys to authorize payments.Descriptive information such as a derived key’s depth and index or a token’s name and icon.The public Bitcoin Cash blockchain on which payments use BCH.An application that uses a decentralized system such as BCH and can ask a wallet to authorize an action or payment.A shareable identifier a wallet decodes into a payment’s locking script, usually in CashAddr format on BCH.A Bitcoin Cash record that defines new outputs and, for a payment, identifies the UTXOs being spent.A transaction record that identifies a UTXO to spend and supplies its unlocking bytecode and sequence number.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 ID and zero-based output index that together identify the UTXO an input spends.The program in an output that specifies the conditions required to spend it.