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
124 lines (123 loc) • 4.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvmRpcNetworkDescriptor = void 0;
const Amount_1 = require("../../utils/Amount");
const crypto_1 = require("../../utils/crypto");
const messageSigning_1 = require("../../utils/messageSigning");
/**
* Describes a custom EVM network that communicates directly with a JSON-RPC
* endpoint. Created via `cg.networks.evmRpc({ ... })`.
*
* This descriptor carries the same metadata as a regular
* {@link NetworkDescriptor} but is **not** tied to the ChainGate API. Features
* that require an indexer (transaction history, token balances, etc.) are not
* available.
*
* @example
* ```ts
* const cg = new ChainGate();
* const bsc = cg.networks.evmRpc({
* rpcUrl: 'https://bsc-dataseed.binance.org',
* chainId: 56,
* name: 'BNB Smart Chain',
* symbol: 'BNB',
* });
*
* const conn = cg.connect(bsc, wallet);
* ```
*/
class EvmRpcNetworkDescriptor {
/** @internal */
constructor(config, marketsCache) {
this.type = 'evm';
this.hasOwnToken = true;
this.defaultAddressType = 'eoa';
this.rpcUrl = config.rpcUrl;
this.name = config.name;
this.symbol = config.symbol;
this.decimals = config.decimals ?? 18;
this.isTestnet = config.isTestnet ?? false;
this.chainId = config.chainId;
this.nativeToken = {
symbol: config.symbol,
name: config.nativeTokenName ?? config.name,
};
this.addressTypes = {
eoa: { derivationPath: "m/44'/60'/0'/0" },
};
this.marketsCache = marketsCache;
}
/**
* Derives an EIP-55 checksummed Ethereum address from a public key.
*
* @param publicKey - Compressed or uncompressed secp256k1 public key.
*/
publicKeyToAddress(publicKey) {
return (0, crypto_1.publicKeyToEthAddress)(publicKey);
}
/**
* Checks whether a string is a valid EVM address.
*
* Accepts both checksummed and all-lowercase/all-uppercase forms.
* When the address uses mixed case, the EIP-55 checksum is verified.
*
* @param address - The address string to validate.
* @returns `true` if the address is valid.
*/
isValidAddress(address) {
return (0, crypto_1.isValidEvmAddress)(address);
}
/**
* Creates an {@link Amount} in base units of the native coin.
*
* @example
* ```ts
* const bsc = cg.networks.evmRpc({ ... });
* const amount = bsc.amount('0.1');
* ```
*/
amount(value) {
return Amount_1.Amount.fromDecimal(value, this.decimals, this.nativeAmountData(), this.marketsCache);
}
/**
* Creates an {@link Amount} from the smallest unit (e.g. wei).
* @internal
*/
amountFromSmallestUnit(value) {
return new Amount_1.Amount(value, this.decimals, this.nativeAmountData(), this.marketsCache);
}
/**
* Signs a message using EIP-191 personal sign.
*
* @param message - The message to sign (string or raw bytes).
* @param privateKey - The 32-byte secp256k1 private key.
* @returns The 65-byte signature as a hex string with `0x` prefix.
*/
signMessage(message, privateKey) {
return (0, messageSigning_1.signEvmMessage)(message, privateKey);
}
/**
* Verifies an EIP-191 personal sign signature.
*
* @param message - The original message.
* @param signature - The signature hex string (with `0x` prefix).
* @param address - The expected signer address.
* @returns `true` if the signature is valid.
*/
verifyMessage(message, signature, address) {
return (0, messageSigning_1.verifyEvmMessage)(message, signature, address);
}
/** Returns the network name. */
toString() {
return this.name;
}
/** @internal */
nativeAmountData() {
return {
symbol: this.nativeToken.symbol,
name: this.nativeToken.name,
network: this.name,
};
}
}
exports.EvmRpcNetworkDescriptor = EvmRpcNetworkDescriptor;