2. Build a persistent store for artifact downloads
The RAILGUN proof generation system uses large artifact files. Because of the size and number of these files, which cover a variety of use cases, we recommend against packaging them directly into your application. This would increase the application size by 50MB+.
Instead, Wallet SDK will automatically download these artifact files when required and persist them for further use. The ArtifactStore module manages downloads and persistence of these assets.
As a dApp developer, you only provide a file storage mechanism that the ArtifactStore will use to (1) check file existence, (2) store assets, (3) retrieve assets.
import { ArtifactStore } from '@railgun-community/wallet';
import localforage from 'localforage';
const artifactStore = new ArtifactStore(
async (path: string) => {
return localforage.getItem(path);
},
async (dir: string, path: string, item: string | Buffer) => {
await localforage.setItem(path, item);
},
async (path: string) => (await localforage.getItem(path)) != null,
);
import { ArtifactStore } from '@railgun-community/wallet';
import fs from 'fs';
const createDownloadDirPath = (documentsDir: string, path: string) => {
return `${documentsDir}/${path}`;
};
const createArtifactStore = (documentsDir: string): ArtifactStore => {
const getFile = async (path: string) => {
return fs.promises.readFile(createDownloadDirPath(documentsDir, path));
};
const storeFile = async (
dir: string,
path: string,
item: string | Buffer,
) => {
await fs.promises.mkdir(createDownloadDirPath(documentsDir, dir), {
recursive: true,
});
await fs.promises.writeFile(
createDownloadDirPath(documentsDir, path),
item,
);
};
const fileExists = (path: string): Promise<boolean> => {
return new Promise(resolve => {
fs.promises
.access(createDownloadDirPath(documentsDir, path))
.then(() => resolve(true))
.catch(() => resolve(false));
});
};
return new ArtifactStore(getFile, storeFile, fileExists);
};
const artifactStore = createArtifactStore('local/dir');