Skip to content

Commit

Permalink
Merge pull request #88 from ltonetwork/WithRelayService
Browse files Browse the repository at this point in the history
Event anchoring on forge
  • Loading branch information
otobongfp authored Aug 9, 2024
2 parents c3a9220 + b0eb083 commit 9324242
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
26 changes: 24 additions & 2 deletions src/services/Ownable.service.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<number>, ArrayLike<number>]>;

Expand Down Expand Up @@ -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<string, OwnableRPC>();

static async loadAll(): Promise<
Expand Down Expand Up @@ -82,9 +93,10 @@ export default class OwnableService {
this._rpc.delete(id);
}

static create(pkg: TypedPackage): EventChain {
static async create(pkg: TypedPackage): Promise<EventChain> {
const account = LTOService.account;
const chain = EventChain.create(account);
const anchors: Array<{ key: Binary; value: Binary }> = [];

if (pkg.isDynamic) {
const msg = {
Expand All @@ -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;
}

Expand Down

0 comments on commit 9324242

Please sign in to comment.