From b0eb0834450b418e3c27a66a7d44abc6b045b4bd Mon Sep 17 00:00:00 2001 From: otobongfp Date: Fri, 9 Aug 2024 17:05:26 +0100 Subject: [PATCH] Added anchoring to forge operation --- src/App.tsx | 2 +- src/services/Ownable.service.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 780c9fa..5e6d71d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -80,7 +80,7 @@ export default function App() { }; const forge = async (pkg: TypedPackage) => { - const chain = OwnableService.create(pkg); + const chain = await OwnableService.create(pkg); setOwnables([...ownables, { chain, package: pkg.cid }]); setShowPackages(false); enqueueSnackbar(`${pkg.title} forged`, { variant: "success" }); diff --git a/src/services/Ownable.service.ts b/src/services/Ownable.service.ts index 9b8ea61..234db63 100644 --- a/src/services/Ownable.service.ts +++ b/src/services/Ownable.service.ts @@ -1,4 +1,4 @@ -import { EventChain, Event } from "@ltonetwork/lto"; +import { EventChain, Event, Binary } from "@ltonetwork/lto"; import LTOService from "./LTO.service"; import IDBService from "./IDB.service"; import TypedDict from "../interfaces/TypedDict"; @@ -11,6 +11,7 @@ import JSZip from "jszip"; import { TypedPackage } from "../interfaces/TypedPackage"; import { TypedOwnableInfo } from "../interfaces/TypedOwnableInfo"; import EventChainService from "./EventChain.service"; +import LocalStorageService from "./LocalStorage.service"; export type StateDump = Array<[ArrayLike, ArrayLike]>; @@ -55,6 +56,16 @@ export interface OwnableRPC { } export default class OwnableService { + private static _anchoring = !!LocalStorageService.get("anchoring"); + + static get anchoring(): boolean { + return this._anchoring; + } + static set anchoring(enabled: boolean) { + LocalStorageService.set("anchoring", enabled); + this._anchoring = enabled; + } + private static readonly _rpc = new Map(); static async loadAll(): Promise< @@ -82,9 +93,10 @@ export default class OwnableService { this._rpc.delete(id); } - static create(pkg: TypedPackage): EventChain { + static async create(pkg: TypedPackage): Promise { const account = LTOService.account; const chain = EventChain.create(account); + const anchors: Array<{ key: Binary; value: Binary }> = []; if (pkg.isDynamic) { const msg = { @@ -95,6 +107,16 @@ export default class OwnableService { }; new Event(msg).addTo(chain).signWith(account); } + + if (this.anchoring) { + const hash = chain.latestHash.hex; + anchors.push(...chain.startingWith(Binary.fromHex(hash)).anchorMap); + } + + if (anchors.length > 0) { + await LTOService.anchor(...anchors); + } + return chain; }