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
200 lines (199 loc) • 7.88 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UtxoNetworkDescriptor = void 0;
const btc = __importStar(require("@scure/btc-signer"));
const errors_1 = require("../../errors");
const messageSigning_1 = require("../../utils/messageSigning");
const NetworkDescriptor_1 = require("./NetworkDescriptor");
/** Sign header prefix per UTXO network, matching the Bitcoin message signing standard. */
const SIGN_HEADERS = {
bitcoin: '\x18Bitcoin Signed Message:\n',
bitcointestnet: '\x18Bitcoin Signed Message:\n',
litecoin: '\x19Litecoin Signed Message:\n',
dogecoin: '\x19Dogecoin Signed Message:\n',
};
/** A {@link NetworkDescriptor} whose {@link id} is a UTXO network. */
class UtxoNetworkDescriptor extends NetworkDescriptor_1.NetworkDescriptor {
/** @internal */
constructor(id, info, marketsCache, apiKey) {
super(id, info, marketsCache, apiKey);
}
/**
* Derives a UTXO address from a compressed public key.
*
* @param publicKey - Compressed (33-byte) secp256k1 public key.
* @param addressType - `'segwit'`, `'legacy'`, or `'taproot'`. Defaults to
* the network's {@link defaultAddressType}.
*/
publicKeyToAddress(publicKey, addressType) {
const type = addressType ?? this.defaultAddressType;
switch (type) {
case 'segwit': {
const payment = btc.p2wpkh(publicKey, this.networkParams);
if (!payment.address) {
throw new errors_1.UnsupportedOperationError('Failed to derive segwit address.');
}
return payment.address;
}
case 'taproot': {
// p2tr expects the x-only key (32 bytes). Strip the 0x02/0x03 prefix
// from a compressed 33-byte key.
const xOnly = publicKey.length === 33 ? publicKey.slice(1) : publicKey;
const payment = btc.p2tr(xOnly, undefined, this.networkParams);
if (!payment.address) {
throw new errors_1.UnsupportedOperationError('Failed to derive taproot address.');
}
return payment.address;
}
case 'legacy': {
const payment = btc.p2pkh(publicKey, this.networkParams);
if (!payment.address) {
throw new errors_1.UnsupportedOperationError('Failed to derive legacy address.');
}
return payment.address;
}
default:
throw new errors_1.UnsupportedOperationError(`Unsupported address type: ${type}`);
}
}
/**
* Signs a message using the Bitcoin 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).
*
* @example
* ```ts
* const key = await wallet.derive("m/84'/0'/0'/0/0");
* const sig = cg.networks.bitcoin.signMessage('Hello', key.privateKey.raw);
* ```
*/
signMessage(message, privateKey) {
const prefix = SIGN_HEADERS[this.id] ?? '\x18Bitcoin Signed Message:\n';
return (0, messageSigning_1.signUtxoMessage)(message, privateKey, prefix);
}
/**
* Verifies a Bitcoin-style 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.
* @returns `true` if the signature is valid.
*
* @example
* ```ts
* const valid = cg.networks.bitcoin.verifyMessage('Hello', sig, 'bc1q...');
* ```
*/
verifyMessage(message, signature, address) {
const prefix = SIGN_HEADERS[this.id] ?? '\x18Bitcoin Signed Message:\n';
try {
const publicKeyRaw = (0, messageSigning_1.recoverUtxoPublicKey)(message, signature, prefix);
const detailedType = this.identifyAddressType(address);
const addressTypeMap = {
'legacy-p2pkh': 'legacy',
'legacy-p2pk': 'legacy',
'segwit-p2wpkh': 'segwit',
'taproot-p2tr': 'taproot',
};
const simpleType = addressTypeMap[detailedType];
if (!simpleType)
return false;
const derivedAddress = this.publicKeyToAddress(publicKeyRaw, simpleType);
return derivedAddress === address;
}
catch {
return false;
}
}
/**
* Checks whether a string is a valid address for this UTXO network.
*
* @param address - The address string to validate.
* @returns `true` if the address is valid for this network.
*/
isValidAddress(address) {
return this.identifyAddressType(address) !== 'unknown';
}
/**
* Identifies the address type of a UTXO address by decoding it.
*
* @param address - The address to identify.
* @returns The detailed address type, or `'unknown'` if the format is not recognized.
*
* @example
* ```ts
* cg.networks.bitcoin.identifyAddressType('bc1q...');
* // → 'segwit-p2wpkh'
*
* cg.networks.bitcoin.identifyAddressType('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa');
* // → 'legacy-p2pkh'
* ```
*/
identifyAddressType(address) {
try {
const addr = btc.Address(this.networkParams).decode(address);
switch (addr.type) {
case 'pkh':
return 'legacy-p2pkh';
case 'sh':
return 'legacy-p2sh';
case 'pk':
return 'legacy-p2pk';
case 'ms':
return 'legacy-p2ms';
case 'wpkh':
return 'segwit-p2wpkh';
case 'wsh':
return 'segwit-p2wsh';
case 'tr':
return 'taproot-p2tr';
case 'tr_ns':
return 'taproot-n-of-n';
case 'tr_ms':
return 'taproot-m-of-n';
default:
return 'unknown';
}
}
catch {
return 'unknown';
}
}
}
exports.UtxoNetworkDescriptor = UtxoNetworkDescriptor;