UNPKG

@minatokens/storage

Version:
60 lines 1.86 kB
import { Field, Struct, Encoding, Provable } from "o1js"; /** * Represents the off-chain storage information, * such as an IPFS hash. */ export class Storage extends Struct({ url: Provable.Array(Field, 2), }) { constructor(value) { super(value); } /** * Asserts that two Storage instances are equal. * @param a The first Storage instance. * @param b The second Storage instance. */ static assertEquals(a, b) { a.url[0].assertEquals(b.url[0]); a.url[1].assertEquals(b.url[1]); } /** * Checks if two Storage instances are equal. * @param a The first Storage instance. * @param b The second Storage instance. * @returns A Bool indicating whether the two instances are equal. */ static equals(a, b) { return a.url[0].equals(b.url[0]).and(a.url[1].equals(b.url[1])); } /** * Creates a Storage instance from a string. * @param url The string representing the storage URL. * @returns A new Storage instance. */ static fromString(url) { if (url === "") return Storage.empty(); const fields = Encoding.stringToFields(url); if (fields.length !== 2) throw new Error("Invalid string length"); return new Storage({ url: [fields[0], fields[1]] }); } /** * Converts the Storage instance to a string. * @returns The string representation of the storage URL. */ toString() { if (this.isEmpty().toBoolean()) { return ""; } return Encoding.stringFromFields([this.url[0], this.url[1]]); } static empty() { return new Storage({ url: [Field(0), Field(0)] }); } isEmpty() { return this.url[0].equals(Field(0)).and(this.url[1].equals(Field(0))); } } //# sourceMappingURL=storage.js.map