UNPKG

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

55 lines (54 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XpubWallet = void 0; const bip32_1 = require("@scure/bip32"); const ViewOnlyWallet_1 = require("../ViewOnlyWallet"); const DerivedPublicKey_1 = require("../../DerivedPublicKey"); const utils_1 = require("../../../utils"); const errors_1 = require("../../errors"); /** * Read-only HD wallet from an extended public key (xpub). Supports derivation (non-hardened only). * * @example * ```ts * const wallet = new XpubWallet('xpub6CUGRUo...'); * const derived = await wallet.derive("m/0/0"); * console.log(derived.publicKeyHex); * ``` */ class XpubWallet extends ViewOnlyWallet_1.ViewOnlyWallet { /** @param xpub - The extended public key string. */ constructor(xpub) { super(); this.walletType = 'xpub'; this._xpub = xpub; const master = bip32_1.HDKey.fromExtendedKey(xpub); if (!master.publicKey) throw new errors_1.HDKeyNullError('publicKey'); this._publicKey = (0, utils_1.bytesToHex)(master.publicKey); } /** Compressed master public key as hex string. */ get publicKey() { return this._publicKey; } /** * Derives a public key at the given path (non-hardened only). * * @param derivationPath - e.g. `"m/0/0"`. */ async derive(derivationPath) { const master = bip32_1.HDKey.fromExtendedKey(this._xpub); const derived = derivationPath ? master.derive(derivationPath) : master; if (!derived.publicKey) throw new errors_1.HDKeyNullError('publicKey'); return new DerivedPublicKey_1.DerivedPublicKey({ publicKey: derived.publicKey, xpub: derived.publicExtendedKey, }); } /** @inheritdoc */ async serialize() { return { type: 'xpub', xpub: this._xpub }; } } exports.XpubWallet = XpubWallet;