Clean up and fixes

This commit is contained in:
2026-02-08 02:32:50 +00:00
parent eb1bf9020e
commit da096af0fa
36 changed files with 2119 additions and 1751 deletions

View File

@@ -1,4 +1,9 @@
import { Engine, type XOEngineOptions } from '@xo-cash/engine';
import {
Engine,
type XOEngineOptions,
// This is temporary. Will likely be moved to where we import templates in the cli. I think that makes more sense as this is a library thing
generateTemplateIdentifier,
} from '@xo-cash/engine';
import type { XOInvitation } from '@xo-cash/types';
import { Invitation } from './invitation.js';
@@ -7,6 +12,10 @@ import { SyncServer } from '../utils/sync-server.js';
import { EventEmitter } from '../utils/event-emitter.js';
// TODO: Remove this. Exists to hash the seed for database namespace.
import { createHash } from 'crypto';
import { p2pkhTemplate } from '@xo-cash/templates';
export type AppEventMap = {
'invitation-added': Invitation;
'invitation-removed': Invitation;
@@ -26,8 +35,32 @@ export class AppService extends EventEmitter<AppEventMap> {
public invitations: Invitation[] = [];
static async create(seed: string, config: AppConfig): Promise<AppService> {
// Because of a bug that lets wallets read the unspents of other wallets, we are going to manually namespace the storage paths for the app.
// We are going to do this by computing a hash of the seed and prefixing the storage paths with it.
const seedHash = createHash('sha256').update(seed).digest('hex');
// We want to only prefix the file name
const prefixedStoragePath = `${seedHash.slice(0, 8)}-${config.engineConfig.databaseFilename}`;
console.log('Prefixed storage path:', prefixedStoragePath);
console.log('Engine config:', config.engineConfig);
// Create the engine
const engine = await Engine.create(seed, config.engineConfig);
const engine = await Engine.create(seed, {
...config.engineConfig,
databaseFilename: prefixedStoragePath,
});
// TODO: We *technically* dont want this here, but we also need some initial templates for the wallet, so im doing it here
// Import the default P2PKH template
await engine.importTemplate(p2pkhTemplate);
// Set default locking parameters for P2PKH
await engine.setDefaultLockingParameters(
generateTemplateIdentifier(p2pkhTemplate),
'receiveOutput',
'receiver',
);
// Create our own storage for the invitations
const storage = await Storage.create(config.invitationStoragePath);
@@ -67,9 +100,6 @@ export class AppService extends EventEmitter<AppEventMap> {
async addInvitation(invitation: Invitation): Promise<void> {
// Add the invitation to the invitations array
this.invitations.push(invitation);
// Make sure the invitation is started
await invitation.start();
// Emit the invitation-added event
this.emit('invitation-added', invitation);