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
132 lines (131 loc) • 6.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseEvmConnector = void 0;
const Connector_1 = require("../Connector");
const HDWallet_1 = require("../../Wallet/SigningWallet/HDWallet/HDWallet");
const PrivateKeyWallet_1 = require("../../Wallet/SigningWallet/PrivateKeyWallet/PrivateKeyWallet");
const XpubWallet_1 = require("../../Wallet/ViewOnlyWallet/XpubWallet/XpubWallet");
const PublicKeyWallet_1 = require("../../Wallet/ViewOnlyWallet/PublicKeyWallet/PublicKeyWallet");
const utils_1 = require("../../utils");
const errors_1 = require("../../errors");
const abiEncode_1 = require("../../utils/abiEncode");
/** Shared base for EVM-style connectors. */
class BaseEvmConnector extends Connector_1.Connector {
/** @internal */
constructor(wallet, explorer, network) {
super(wallet, explorer, network);
const typeConfig = network.addressTypes[network.defaultAddressType];
if (!typeConfig) {
throw new errors_1.UnsupportedOperationError(`Network '${network}' does not have address type configuration.`);
}
this.defaultDerivationPath = typeConfig.derivationPath;
}
/**
* Returns the EVM address for this wallet.
*
* - **HD wallets** derive at `{derivationPath}/{index}` (defaults to `m/44'/60'/0'/0/0`).
* - **Single-key wallets** return the address for the wallet's public key. Only index `0` is valid.
* - **XpubWallet** derives at `m/0/{index}`.
*/
async address(options) {
const { index = 0, derivationPath } = options ?? {};
const wallet = this.wallet;
if (wallet instanceof HDWallet_1.HDWallet) {
const basePath = derivationPath ?? this.defaultDerivationPath;
const fullPath = `${basePath}/${index}`;
const derived = await wallet.derivePublicKey(fullPath);
return this.network.publicKeyToAddress(derived.publicKey);
}
if (wallet instanceof XpubWallet_1.XpubWallet) {
const relativePath = `m/0/${index}`;
const derived = await wallet.derive(relativePath);
return this.network.publicKeyToAddress(derived.publicKey);
}
if (index !== 0) {
throw new errors_1.UnsupportedOperationError(`Wallet type '${wallet.walletType}' does not support indexed address derivation. ` +
`Only index 0 is valid for single-key wallets.`);
}
if (wallet instanceof PrivateKeyWallet_1.PrivateKeyWallet || wallet instanceof PublicKeyWallet_1.PublicKeyWallet) {
return this.network.publicKeyToAddress((0, utils_1.hexToBytes)(wallet.publicKey));
}
throw new errors_1.UnsupportedOperationError(`Unsupported wallet type: ${wallet.walletType}`);
}
/** Creates a native coin transfer transaction. */
async transfer(amount, toAddress, options) {
const { index = 0, derivationPath } = options ?? {};
const fromAddress = await this.address(options);
const valueWei = amount.min();
const getPrivateKey = this.createPrivateKeyGetter(index, derivationPath);
return this.createTransaction({
fromAddress,
toAddress,
valueWei,
getPrivateKey,
});
}
/** Creates an ERC-721 NFT transfer transaction using `safeTransferFrom`. */
async transferNft(contractAddress, tokenId, toAddress, options) {
const { index = 0, derivationPath } = options ?? {};
const fromAddress = await this.address(options);
const getPrivateKey = this.createPrivateKeyGetter(index, derivationPath);
const data = (0, abiEncode_1.encodeErc721SafeTransferFrom)(fromAddress, toAddress, BigInt(tokenId));
return this.createTransaction({
fromAddress,
toAddress: contractAddress,
valueWei: 0n,
data,
getPrivateKey,
});
}
/** Creates an ERC-1155 token transfer transaction using `safeTransferFrom`. */
async transferErc1155(contractAddress, tokenId, amount, toAddress, options) {
const { index = 0, derivationPath } = options ?? {};
const fromAddress = await this.address(options);
const getPrivateKey = this.createPrivateKeyGetter(index, derivationPath);
const data = (0, abiEncode_1.encodeErc1155SafeTransferFrom)(fromAddress, toAddress, BigInt(tokenId), amount);
return this.createTransaction({
fromAddress,
toAddress: contractAddress,
valueWei: 0n,
data,
getPrivateKey,
});
}
/** Creates a transaction that calls a smart contract with arbitrary calldata. */
async callContract(contractAddress, data, amount, options) {
const { index = 0, derivationPath } = options ?? {};
const fromAddress = await this.address(options);
const getPrivateKey = this.createPrivateKeyGetter(index, derivationPath);
return this.createTransaction({
fromAddress,
toAddress: contractAddress,
valueWei: amount.min(),
data,
getPrivateKey,
});
}
/**
* Returns a function that extracts the private key from the wallet.
* @internal
*/
createPrivateKeyGetter(index, derivationPath) {
const wallet = this.wallet;
if (wallet instanceof HDWallet_1.HDWallet) {
const basePath = derivationPath ?? this.defaultDerivationPath;
const fullPath = `${basePath}/${index}`;
return async () => {
const derived = await wallet.derive(fullPath);
return new Uint8Array(derived.privateKey.raw);
};
}
if (wallet instanceof PrivateKeyWallet_1.PrivateKeyWallet) {
return async () => {
const pk = await wallet.getPrivateKey();
return new Uint8Array(pk.raw);
};
}
throw new errors_1.UnsupportedOperationError(`Wallet type '${wallet.walletType}' does not support signing transactions. ` +
`A signing wallet (HD or private key) is required.`);
}
}
exports.BaseEvmConnector = BaseEvmConnector;