// Copyright (C) 2026 Whiterun LLC, // This software is licensed under the GNU Lesser General Public License (LGPL), version 3.0 or later. // A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/lgpl-3.0.html /** * Dapp-side pubkey state manager. * * Stores xpub nodes received from the wallet and derives pubkeys on demand. * * Uses plain `number` for child indices (0=Receive, 1=Change, 7=Cauldron) * so this package has no dependency on chain-specific types. */ import { deriveHdPublicNodeChild } from "@bitauth/libauth"; import type { HdPublicNodeValid } from "@bitauth/libauth"; export class DappPubkeyStateManager { // xpub nodes for on-demand pubkey derivation private xpubNodes: Map = new Map(); /** Derive a pubkey on demand from the stored xpub node. */ getPubkey(childIndex: number, index: bigint): Uint8Array | undefined { const xpubNode = this.xpubNodes.get(childIndex); if (!xpubNode) return undefined; const child = deriveHdPublicNodeChild(xpubNode, Number(index)); if (typeof child === "string") return undefined; return child.publicKey; } /** Returns true if an xpub node is available for this child index. */ hasPath(childIndex: number): boolean { return this.xpubNodes.has(childIndex); } setXpubNode(childIndex: number, node: HdPublicNodeValid): void { this.xpubNodes.set(childIndex, node); } getXpubNode(childIndex: number): HdPublicNodeValid | undefined { return this.xpubNodes.get(childIndex); } }