Compare commits

...

6 Commits

Author SHA1 Message Date
Sosthene
046eef18e6 Merge branch 'scan_blocks' into dev 2025-08-23 16:05:03 +02:00
Sosthene
2ba7be8dbb Call updateDeviceBlockHeight from router 2025-08-23 16:04:34 +02:00
Sosthene
77d9c1ad43 Add updateDeviceBlockHeight 2025-08-23 16:03:49 +02:00
Sosthene
3ce412d814 Keep currentBlockHeight in services 2025-08-23 16:02:31 +02:00
Sosthene
7100eda272 [bug] update parse_new_tx 2025-08-23 16:02:06 +02:00
Sosthene
1a3a2dbef1 Remove pdf code 2025-08-23 16:00:35 +02:00
2 changed files with 68 additions and 100 deletions

View File

@ -156,13 +156,15 @@ export async function init(): Promise<void> {
// We connect to all relays now
await services.connectAllRelays();
await services.updateDeviceBlockHeight();
// We register all the event listeners if we run in an iframe
if (window.self !== window.top) {
await registerAllListeners();
}
if (services.isPaired()) {
await navigate('account');
await navigate('process');
} else {
await navigate('home');
}

View File

@ -1,19 +1,19 @@
import { INotification } from '~/models/notification.model';
import { IProcess } from '~/models/process.model';
import { initWebsocket, sendMessage } from '../websockets';
import { ApiReturn, Device, HandshakeMessage, Member, MerkleProofResult, OutPointProcessMap, Process, ProcessState, RoleDefinition, SecretsStore, UserDiff } from '../../pkg/sdk_client';
import { ApiReturn, Device, HandshakeMessage, Member, MerkleProofResult, NewTxMessage, OutPointProcessMap, Process, ProcessState, RoleDefinition, SecretsStore, UserDiff } from '../../pkg/sdk_client';
import ModalService from './modal.service';
import Database from './database.service';
import { navigate } from '../router';
import { storeData, retrieveData, testData } from './storage.service';
import { BackUp } from '~/models/backup.model';
import { PDFDocument, rgb, StandardFonts } from 'pdf-lib';
export const U32_MAX = 4294967295;
const BASEURL = `https://demo.4nkweb.com`;
const BOOTSTRAPURL = [`${BASEURL}/ws/`];
const STORAGEURL = `${BASEURL}/storage`
const BASEURL = `http://localhost`;
const BOOTSTRAPURL = [`${BASEURL}:8090`];
const STORAGEURL = `${BASEURL}:8081`
const BLINDBITURL = `${BASEURL}:8000`
const DEFAULTAMOUNT = 1000n;
const EMPTY32BYTES = String('').padStart(64, '0');
@ -31,6 +31,7 @@ export default class Services {
private routingInstance!: ModalService;
private relayAddresses: { [wsurl: string]: string } = {};
private membersList: Record<string, Member> = {};
private currentBlockHeight: number = -1;
// Private constructor to prevent direct instantiation from outside
private constructor() {}
@ -560,7 +561,7 @@ export default class Services {
const newTip = this.sdkClient.get_txid(parsedMsg.transaction);
console.log('Transaction', newTip, 'spends the tip of process', processId);
// We take the data out of the output
const newStateId = this.sdkClient.get_new_state_id(parsedMsg.transaction);
const newStateId = this.sdkClient.get_opreturn(parsedMsg.transaction);
console.log('newStateId:', newStateId);
// We update the relevant process
const updatedProcess = this.sdkClient.process_commit_new_state(process, newStateId, newTip);
@ -574,7 +575,7 @@ export default class Services {
}
try {
const parsedTx = this.sdkClient.parse_new_tx(parsedMsg.transaction, 0, membersList);
const parsedTx = this.sdkClient.parse_new_tx(newTxMsg, 0, membersList);
if (parsedTx) {
try {
await this.handleApiReturn(parsedTx);
@ -934,6 +935,7 @@ export default class Services {
async createNewDevice() {
let spAddress = '';
try {
// We set birthday later when we have the chain tip from relay
spAddress = await this.sdkClient.create_new_device(0, 'signet');
const device = this.dumpDeviceFromMemory();
await this.saveDeviceInDatabase(device);
@ -952,6 +954,61 @@ export default class Services {
}
}
public async updateDeviceBlockHeight(): Promise<void> {
if (this.currentBlockHeight === -1) {
throw new Error('Current block height not set');
}
let device: Device | null = null;
try {
device = await this.getDeviceFromDatabase();
} catch (e) {
throw new Error(`Failed to get device from database: ${e}`);
}
if (!device) {
throw new Error('Device not found');
}
const birthday = device.sp_wallet.birthday;
if (birthday === undefined || birthday === null) {
throw new Error('Birthday not found');
}
if (birthday === 0) {
// This is a new device, so current chain tip is its birthday
device.sp_wallet.birthday = this.currentBlockHeight;
// We also set last_scan, impossible that we need to scan earlier than this
device.sp_wallet.last_scan = this.currentBlockHeight;
try {
// First set the updated device in memory
this.sdkClient.restore_device(device);
// Then save it to database
await this.saveDeviceInDatabase(device);
} catch (e) {
throw new Error(`Failed to save updated device: ${e}`);
}
} else {
// For testing, set last_scan and birthday a few blocks ago
device.sp_wallet.last_scan = this.currentBlockHeight - 10;
device.sp_wallet.birthday = this.currentBlockHeight - 10;
try {
this.sdkClient.restore_device(device);
} catch (e) {
throw new Error(`Failed to restore device: ${e}`);
}
await this.saveDeviceInDatabase(device);
// This is existing device, we need to catch up if last_scan is lagging behind chain_tip
if (device.sp_wallet.last_scan < this.currentBlockHeight) {
// We need to catch up
await this.sdkClient.scan_blocks(this.currentBlockHeight, BLINDBITURL);
} else {
// Up to date, just returns
return;
}
}
}
private async removeProcess(processId: string): Promise<void> {
const db = await Database.getInstance();
const storeName = 'processes';
@ -1322,6 +1379,7 @@ export default class Services {
try {
const handshakeMsg: HandshakeMessage = JSON.parse(parsedMsg);
this.updateRelay(url, handshakeMsg.sp_address);
this.currentBlockHeight = handshakeMsg.chain_tip;
if (this.membersList && Object.keys(this.membersList).length === 0) {
// We start from an empty list, just copy it over
this.membersList = handshakeMsg.peers_list;
@ -1699,96 +1757,4 @@ export default class Services {
return await this.updateProcess(process, {}, publicData, null);
}
public async generateProcessPdf(processId: string, processState: ProcessState): Promise<void> {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage([595.28, 841.89]);
const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
const fontBold = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
const drawText = (text: string, x: number, y: number, opts: { size?: number, bold?: boolean } = {}) => {
const fontSize = opts.size || 12;
const usedFont = opts.bold ? fontBold : font;
page.drawText(text, { x, y, size: fontSize, font: usedFont, color: rgb(0, 0, 0) });
};
let y = 800;
// Header
drawText("Cabinet de Maître Jean Dupont", 50, y, { bold: true });
drawText("Notaire à Paris", 50, y -= 15);
drawText("12 rue des Archives", 50, y -= 15);
drawText("75003 Paris", 50, y -= 15);
drawText("Téléphone : 01 23 45 67 89", 50, y -= 15);
drawText("Email : contact@notairedupont.fr", 50, y -= 15);
// Client
y -= 30;
drawText("Client : Mme Sophie Martin", 50, y);
drawText("8 avenue de la Liberté", 50, y -= 15);
drawText("69003 Lyon", 50, y -= 15);
drawText("Email : sophie.martin@email.com", 50, y -= 15);
// Title
y -= 40;
drawText("Certificat de Validation de Données", 50, y, { size: 14, bold: true });
// Certification paragraph
y -= 40;
const certText = `Je soussigné, Maître Jean Dupont, notaire à Paris, certifie par la présente que les données suivantes ont été vérifiées et horodatées à laide dune empreinte cryptographique enregistrée sur la blockchain Bitcoin.`;
page.drawText(certText, {
x: 50, y,
size: 11,
font,
lineHeight: 14,
maxWidth: 500
});
// Dossier number
y -= 60;
drawText("Numéro de dossier : N-2025-0456-PAR", 50, y);
// Process ID
y -= 40;
drawText(`Identifiant du process: ${processId.split(':')[0]}`, 50, y);
// Hash table
y -= 40;
drawText("Nom", 50, y -= 20, { bold: true });
drawText("Empreinte cryptographique (SHA-256)", 150, y, { bold: true });
for (const [label, hash] of Object.entries(processState.pcd_commitment)) {
drawText(label, 50, y -= 18);
drawText(hash, 150, y);
}
// Add the state id as hash total
drawText('Ensemble', 50, y -= 18);
drawText(processState.state_id, 150, y);
// Transaction
y -= 40;
drawText("Transaction enregistrée sur la blockchain Bitcoin :", 50, y);
drawText(processState.commited_in, 50, y -= 15);
// Date & signature
y -= 50;
drawText("Fait à Paris, le 10 juin 2025.", 50, y);
drawText("Généré automatiquement par lecoffre.io", 50, y -= 30);
// drawText("Signature du notaire : ___________________________", 50, y -= 30);
const pdfBytes = await pdfDoc.save();
// Download
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `Certificat_Validation_${processId.slice(0,8)}-${processState.state_id.slice(0,8)}.pdf`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
}