chaingate
Version:
Multi-chain cryptocurrency SDK for TypeScript — unified API for Bitcoin, Ethereum, Litecoin, Dogecoin, Bitcoin Cash, Polygon, Arbitrum, and any EVM-compatible chain. Create wallets, query balances, send transactions, and manage tokens and NFTs across UTXO
96 lines (95 loc) • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BchNetworkDescriptor = void 0;
const errors_1 = require("../../errors");
const messageSigning_1 = require("../../utils/messageSigning");
const bch_1 = require("../../Connector/UtxoConnector/BchConnector/bch");
const cashaddr_1 = require("../../Connector/UtxoConnector/BchConnector/cashaddr");
const NetworkDescriptor_1 = require("./NetworkDescriptor");
/** A {@link NetworkDescriptor} for Bitcoin Cash specifically. */
class BchNetworkDescriptor extends NetworkDescriptor_1.NetworkDescriptor {
/** @internal */
constructor(info, marketsCache, apiKey) {
super('bitcoincash', info, marketsCache, apiKey);
}
/**
* Derives a Bitcoin Cash address from a compressed public key.
*
* @param publicKey - Compressed (33-byte) secp256k1 public key.
* @param addressType - `'cashaddr'` or `'legacy'`. Defaults to the network's
* {@link defaultAddressType} (`'cashaddr'`).
*/
publicKeyToAddress(publicKey, addressType) {
const type = addressType ?? this.defaultAddressType;
// HASH160(publicKey) = RIPEMD160(SHA256(publicKey))
const hash160 = (0, bch_1.publicKeyToHash160)(publicKey);
const cashAddr = (0, cashaddr_1.hashToCashAddress)(hash160, 'p2pkh');
switch (type) {
case 'cashaddr':
return cashAddr;
case 'legacy':
return (0, cashaddr_1.toLegacyAddress)(cashAddr);
default:
throw new errors_1.UnsupportedOperationError(`Unsupported BCH address type: ${type}`);
}
}
/**
* Checks whether a string is a valid Bitcoin Cash address.
*
* Accepts both CashAddr (e.g. `bitcoincash:qq...`) and legacy Base58Check formats.
*
* @param address - The address string to validate.
* @returns `true` if the address is valid.
*/
isValidAddress(address) {
// Try CashAddr decoding (with or without prefix).
try {
(0, cashaddr_1.toLegacyAddress)(address);
return true;
}
catch {
// Not a valid CashAddr — try legacy below.
}
// Try legacy Base58Check → CashAddr conversion.
try {
(0, cashaddr_1.toCashAddress)(address);
return true;
}
catch {
return false;
}
}
/**
* Signs a message using the Bitcoin Cash message signing standard.
*
* @param message - The message to sign (string or raw bytes).
* @param privateKey - The 32-byte secp256k1 private key.
* @returns The signature as a base64 string (65 bytes).
*/
signMessage(message, privateKey) {
return (0, messageSigning_1.signUtxoMessage)(message, privateKey, '\x18Bitcoin Signed Message:\n');
}
/**
* Verifies a Bitcoin Cash signed message by recovering the public key and
* comparing the derived address against the expected address.
*
* @param message - The original message.
* @param signature - The base64-encoded signature (65 bytes).
* @param address - The expected signer address (CashAddr or legacy format).
* @returns `true` if the signature is valid.
*/
verifyMessage(message, signature, address) {
try {
const publicKeyRaw = (0, messageSigning_1.recoverUtxoPublicKey)(message, signature, '\x18Bitcoin Signed Message:\n');
// Derive CashAddr from the recovered public key
const derivedCashAddr = this.publicKeyToAddress(publicKeyRaw, 'cashaddr');
const derivedLegacy = this.publicKeyToAddress(publicKeyRaw, 'legacy');
// Normalize the input address to both formats for comparison
return derivedCashAddr === address || derivedLegacy === address;
}
catch {
return false;
}
}
}
exports.BchNetworkDescriptor = BchNetworkDescriptor;