import { Device } from '../../../pkg/sdk_client'; import { SdkService } from '../core/sdk.service'; import Database from '../database.service'; export class WalletService { constructor(private sdk: SdkService, private db: Database) {} public isPaired(): boolean { try { return this.sdk.getClient().is_paired(); } catch (e) { return false; } } public getAmount(): BigInt { return this.sdk.getClient().get_available_amount(); } public getDeviceAddress(): string { return this.sdk.getClient().get_address(); } public getPairingProcessId(): string { return this.sdk.getClient().get_pairing_process_id(); } public async createNewDevice(chainTip: number): Promise { const spAddress = await this.sdk.getClient().create_new_device(0, 'signet'); const device = this.dumpDeviceFromMemory(); if (device.sp_wallet.birthday === 0) { device.sp_wallet.birthday = chainTip; device.sp_wallet.last_scan = chainTip; this.sdk.getClient().restore_device(device); } await this.saveDeviceInDatabase(device); return spAddress; } public dumpDeviceFromMemory(): Device { return this.sdk.getClient().dump_device(); } public dumpNeuteredDevice(): Device | null { try { return this.sdk.getClient().dump_neutered_device(); } catch (e) { return null; } } public async dumpWallet(): Promise { return await this.sdk.getClient().dump_wallet(); } public async getMemberFromDevice(): Promise { try { const device = await this.getDeviceFromDatabase(); if (device) { const pairedMember = device['paired_member']; return pairedMember.sp_addresses; } else { return null; } } catch (e) { throw new Error(`[WalletService] Échec: ${e}`); } } public async saveDeviceInDatabase(device: Device): Promise { await this.db.saveDevice(device); } public async getDeviceFromDatabase(): Promise { const db = await Database.getInstance(); const res = await db.getObject('wallet', '1'); return res ? res['device'] : null; } public restoreDevice(device: Device) { this.sdk.getClient().restore_device(device); } public pairDevice(processId: string, spAddressList: string[]): void { this.sdk.getClient().pair_device(processId, spAddressList); } public async unpairDevice(): Promise { this.sdk.getClient().unpair_device(); const newDevice = this.dumpDeviceFromMemory(); await this.saveDeviceInDatabase(newDevice); } }