Also save events in legacy UI format

This commit is contained in:
Tornado Contrib 2024-05-07 16:41:52 +00:00
parent b5d469fd16
commit 796b847d71
Signed by: tornadocontrib
GPG Key ID: 60B4DF1A076C64B1
4 changed files with 88 additions and 4 deletions

@ -65,7 +65,7 @@ You can apply those values with two options
### How to start ### How to start
1. `yarn start --help` 1. `yarn start --help` or `yarn run help`
2. If you want to use a secure, anonymous tor connection add `--tor-port <torPort>` behind the command or add `TOR_PORT` at the `.env` file. 2. If you want to use a secure, anonymous tor connection add `--tor-port <torPort>` behind the command or add `TOR_PORT` at the `.env` file.
3. Add `PRIVATE_KEY` to `.env` file (optional, only if you want to use it for many operations) - open `.env.example` file, add private key after `PRIVATE_KEY=` and rename file to `.env`. 3. Add `PRIVATE_KEY` to `.env` file (optional, only if you want to use it for many operations) - open `.env.example` file, add private key after `PRIVATE_KEY=` and rename file to `.env`.

@ -15,8 +15,8 @@
"build:node": "ts-node scripts/fflate.ts && webpack", "build:node": "ts-node scripts/fflate.ts && webpack",
"build": "yarn types && yarn build:node", "build": "yarn types && yarn build:node",
"start": "ts-node src/cli.ts", "start": "ts-node src/cli.ts",
"startHelp": "ts-node src/cli.ts help", "help": "ts-node src/cli.ts help",
"createDeposit": "ts-node src/cli.ts create", "create": "ts-node src/cli.ts create",
"deposit": "ts-node src/cli.ts deposit", "deposit": "ts-node src/cli.ts deposit",
"depositInvoice": "ts-node src/cli.ts depositInvoice", "depositInvoice": "ts-node src/cli.ts depositInvoice",
"withdraw": "ts-node src/cli.ts withdraw", "withdraw": "ts-node src/cli.ts withdraw",

@ -1,4 +1,5 @@
import path from 'path'; import path from 'path';
import { deflate, constants } from 'zlib';
import { stat, mkdir, readFile, writeFile } from 'fs/promises'; import { stat, mkdir, readFile, writeFile } from 'fs/promises';
import { zip, unzip, AsyncZippable, Unzipped } from 'fflate'; import { zip, unzip, AsyncZippable, Unzipped } from 'fflate';
import { BaseEvents, MinimalEvents } from '@tornado/core'; import { BaseEvents, MinimalEvents } from '@tornado/core';
@ -13,6 +14,28 @@ export async function existsAsync(fileOrDir: string): Promise<boolean> {
} }
} }
/**
* Supports legacy gz format for legacy UI
*/
export function deflateAsync(data: Uint8Array): Promise<Buffer> {
return new Promise((resolve, reject) => {
deflate(
data,
{
level: constants.Z_BEST_COMPRESSION,
strategy: constants.Z_FILTERED,
},
(err, buffer) => {
if (!err) {
resolve(buffer);
} else {
reject(err);
}
},
);
});
}
export function zipAsync(file: AsyncZippable): Promise<Uint8Array> { export function zipAsync(file: AsyncZippable): Promise<Uint8Array> {
return new Promise((res, rej) => { return new Promise((res, rej) => {
zip(file, { mtime: new Date('1/1/1980') }, (err, data) => { zip(file, { mtime: new Date('1/1/1980') }, (err, data) => {
@ -37,6 +60,28 @@ export function unzipAsync(data: Uint8Array): Promise<Unzipped> {
}); });
} }
export async function saveLegacyFile({
fileName,
userDirectory,
dataString,
}: {
fileName: string;
userDirectory: string;
dataString: string;
}) {
fileName = fileName.toLowerCase();
const filePath = path.join(userDirectory, fileName);
const payload = await deflateAsync(new TextEncoder().encode(dataString));
if (!(await existsAsync(userDirectory))) {
await mkdir(userDirectory, { recursive: true });
}
await writeFile(filePath + '.gz', payload);
}
export async function saveUserFile({ export async function saveUserFile({
fileName, fileName,
userDirectory, userDirectory,

@ -13,6 +13,7 @@ import {
BaseRegistryServiceConstructor, BaseRegistryServiceConstructor,
BaseEchoServiceConstructor, BaseEchoServiceConstructor,
BaseEchoService, BaseEchoService,
DEPOSIT,
} from '@tornado/core'; } from '@tornado/core';
import type { import type {
BaseEvents, BaseEvents,
@ -23,7 +24,7 @@ import type {
AllGovernanceEvents, AllGovernanceEvents,
EchoEvents, EchoEvents,
} from '@tornado/core'; } from '@tornado/core';
import { saveUserFile, loadSavedEvents, loadCachedEvents } from './data'; import { saveUserFile, loadSavedEvents, loadCachedEvents, saveLegacyFile } from './data';
export type NodeTornadoServiceConstructor = BaseTornadoServiceConstructor & { export type NodeTornadoServiceConstructor = BaseTornadoServiceConstructor & {
cacheDirectory?: string; cacheDirectory?: string;
@ -190,6 +191,32 @@ export class NodeTornadoService extends BaseTornadoService {
userDirectory: this.userDirectory, userDirectory: this.userDirectory,
dataString: JSON.stringify(events, null, 2) + '\n', dataString: JSON.stringify(events, null, 2) + '\n',
}); });
if (this.getType().toLowerCase() === DEPOSIT) {
const legacyEvents = (events as DepositsEvents[]).map(
({ blockNumber, transactionHash, commitment, leafIndex, timestamp }) => ({
blockNumber,
transactionHash,
commitment,
leafIndex,
timestamp: String(timestamp),
}),
);
await saveLegacyFile({
fileName: instanceName + '.json',
userDirectory: this.userDirectory,
dataString: JSON.stringify(legacyEvents, null, 2) + '\n',
});
} else {
const legacyEvents = events as WithdrawalsEvents[];
await saveLegacyFile({
fileName: instanceName + '.json',
userDirectory: this.userDirectory,
dataString: JSON.stringify(legacyEvents, null, 2) + '\n',
});
}
} }
} }
} }
@ -480,6 +507,18 @@ export class NodeEncryptedNotesService extends BaseEncryptedNotesService {
userDirectory: this.userDirectory, userDirectory: this.userDirectory,
dataString: JSON.stringify(events, null, 2) + '\n', dataString: JSON.stringify(events, null, 2) + '\n',
}); });
const legacyEvents = events.map(({ transactionHash, blockNumber, encryptedNote }) => ({
txHash: transactionHash,
blockNumber,
encryptedNote,
}));
await saveLegacyFile({
fileName: instanceName + '.json',
userDirectory: this.userDirectory,
dataString: JSON.stringify(legacyEvents, null, 2) + '\n',
});
} }
} }
} }