import { BAP, type MasterID } from "bsv-bap"; /** * BotIdentity wrapper around BAP MasterID * Provides simplified interface for clawbook-bot */ export interface BotIdentity { bap: BAP; masterId: MasterID; } /** * Create a BAP identity from a WIF key * Uses Type 42 format (recommended over deprecated BIP32) * * @param wif - Wallet Import Format private key * @returns BotIdentity object containing BAP instance and MasterID */ export function createIdentity(wif: string): BotIdentity { const bap = new BAP({ rootPk: wif }); // Create a new identity // For a bot, we'll use a simple name like "clawbook-bot" const masterId = bap.newId("clawbook-bot"); return { bap, masterId, }; } /** * Get the BAP identity key (idKey) for this identity * * @param identity - BotIdentity object * @returns BAP identity key string */ export function getIdKey(identity: BotIdentity): string { return identity.masterId.getIdentityKey(); } /** * Sign OP_RETURN data with AIP (Author Identity Protocol) * * Takes hex-encoded data arrays and returns signed data including AIP signature. * The returned array includes the original data plus AIP protocol elements: * [AIP_PREFIX, "BITCOIN_ECDSA", address, signature, indices] * * @param identity - BotIdentity object * @param opReturnData - Array of number arrays (hex-encoded data) * @returns Signed OP_RETURN data with AIP signature appended */ export function signOpReturn( identity: BotIdentity, opReturnData: number[][], ): number[][] { return identity.masterId.signOpReturnWithAIP(opReturnData); } /** * Get the current signing address for this identity * * @param identity - BotIdentity object * @returns Bitcoin address (base58check) */ export function getCurrentAddress(identity: BotIdentity): string { return identity.masterId.getCurrentAddress(); } /** * Sign a message with this identity * Returns signature and address for verification * * @param identity - BotIdentity object * @param message - Message as number array (UTF-8 bytes or hex) * @returns Object containing address and signature */ export function signMessage( identity: BotIdentity, message: number[], ): { address: string; signature: string } { return identity.masterId.signMessage(message); }