UNPKG

hollowdb

Version:

A decentralized privacy-preserving key-value database

383 lines (364 loc) 12.5 kB
function $parcel$export(e, n, v, s) { Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); } $parcel$export(module.exports, "BaseSDK", () => $83a13e96bfc8ddef$export$f7af2da260061f5e); $parcel$export(module.exports, "SDK", () => $5e703c66837a5b12$export$f7af2da260061f5e); $parcel$export(module.exports, "SetSDK", () => $f500dc08a51cd667$export$95f8f8e92037cfa); class $28139fa08f24198e$export$ef88aa0d34c34520 { constructor(signer, contractTxId, warp){ this.signer = signer; this.warp = warp; this.contract = this.warp.contract(contractTxId).setEvaluationOptions({ allowBigInt: true, useKVStorage: true, sequencerUrl: warp.environment === "mainnet" ? "https://gw.warp.cc/" : undefined }).connect(this.signer); } /** * Return the latest contract state. * * This is a good way to trigger Warp to fetch the latest data from Arweave. * Note that if the contract has many transactions, fetching up to the latest * state may take some time. * * @returns contract state along with corresponding sort key */ async readState() { return await this.contract.readState(); } /** * A typed wrapper around `dryWrite`, which evaluates a given input * on the local state, without creating a transaction. This may provide * better UX for some use-cases. * @param input input in the form of `{function, value}` * @returns interaction result */ async dryWrite(input) { return await this.contract.dryWrite(input); } /** * A typed wrapper around `writeInteraction`, which creates a * transaction. You are likely to use this after `dryWrite`, or you * may directly call this function. * @param input input in the form of `{function, value}` * @returns interaction response */ async writeInteraction(input) { return await this.contract.writeInteraction(input); } /** * A typed wrapper around `dryWrite` followed by `writeInteraction`. This * function first executes the interaction locally via `dryWrite`, and if * there is an error, throws an error with an optional prefix in the message. * @param input input in the form of `{function, value}` * @param errorPrefix optional prefix for the error message */ async dryWriteInteraction(input) { const result = await this.dryWrite(input); if (result.type !== "ok") throw new Error(`Contract Error [${input.function}]: ${result.errorMessage}`); await this.writeInteraction(input); } /** * A typed wrapper around `viewState` followed with a repsonse type check. * If response type is not `ok`, it will throw an error. * @param input input in the form of `{function, value}` * @returns interaction result */ async safeReadInteraction(input) { const response = await this.viewState(input); if (response.type !== "ok") throw new Error(`Contract Error [${input.function}]: ${response.errorMessage}`); return response.result; } /** * A typed wrapper around `viewState`, which is a read interaction. * @param input input in the form of `{function, value}` * @returns interaction result */ async viewState(input) { return await this.contract.viewState(input); } } class $decf44cc7d6b29b3$export$c67a8869b938d43b { constructor(base){ this.base = base; } /** * Sets the owner as the given wallet address. * @param newOwner address of the new owner, make sure that this is correct! */ async updateOwner(newOwner) { await this.base.writeInteraction({ function: "updateOwner", value: { newOwner: newOwner } }); } /** * Changes the whitelist for the selected list. * @param users an array of user addresses * @param name name of the list to be updated * @param op whether to `add` the users to whitelist or `remove` them */ async updateWhitelist(users, name, op) { const add = op === "add" ? users : []; const remove = op === "remove" ? users : []; await this.base.writeInteraction({ function: "updateWhitelist", value: { add: add, remove: remove, name: name } }); } /** * Update a verification key. * @param name name of the circuit that the verification key belongs to * @param verificationKey verification key */ async updateVerificationKey(name, // eslint-disable-next-line @typescript-eslint/no-explicit-any verificationKey) { await this.base.writeInteraction({ function: "updateVerificationKey", value: { verificationKey: verificationKey, name: name } }); } /** * Disable or enable whitelist checks for a specific whitelist. * @param name name of the list * @param value a boolean */ async updateWhitelistRequirement(name, value) { await this.base.writeInteraction({ function: "updateWhitelistRequirement", value: { name: name, value: value } }); } /** * Disable or enable proof checks for a specific circuit. * @param name name of the circuit * @param value a boolean */ async updateProofRequirement(name, value) { await this.base.writeInteraction({ function: "updateProofRequirement", value: { name: name, value: value } }); } } class $83a13e96bfc8ddef$export$f7af2da260061f5e { /** * Connects to the given contract via the provided Warp instance using the provided signer. * @param signer a Signer, such as Arweave wallet or Ethereum CustomSignature * @param contractTxId contract txId to connect to * @param warp a Warp instace, such as `WarpFactory.forMainnet()` */ constructor(signer, contractTxId, warp){ this.base = new (0, $28139fa08f24198e$export$ef88aa0d34c34520)(signer, contractTxId, warp); this.admin = new (0, $decf44cc7d6b29b3$export$c67a8869b938d43b)(this.base); } /** The smart-contract that we are connected to. */ get contract() { return this.base.contract; } /** Warp instance. */ get warp() { return this.base.warp; } /** Contract transaction id. */ get contractTxId() { return this.base.contract.txId(); } /** Signer. */ get signer() { return this.base.signer; } /** * Returns the latest contract state. * * For a more fine-grained state data, use `base.readState()`. * * @returns contract state object */ async getState() { return await this.base.readState().then((s)=>s.cachedValue.state); } /** * Alternative method of getting key values. Uses the underlying `getStorageValues` * function, returns a Map instead of an array. * * @param keys an array of keys * @returns `SortKeyCacheResult` of a key-value `Map` */ async getStorageValues(keys) { return await this.contract.getStorageValues(keys); } /** * Returns keys with respect to a range option. * * If no option is provided, it will get all keys. * * @param options optional range * @returns an array of keys */ async getKeys(options) { return await this.base.safeReadInteraction({ function: "getKeys", value: { options: options } }); } /** * Returns all keys in the database. * * @returns an array of all keys */ async getAllKeys() { return this.getKeys(); } /** * Returns a mapping of keys and values with respect to a range option. * If no option is provided, all values are returned. * * @param options optional range * @returns a key-value `Map` */ async getKVMap(options) { return await this.base.safeReadInteraction({ function: "getKVMap", value: { options: options } }); } /** * Gets the value of the given key. * @param key the key of the value to be returned * @returns the value of the given key */ async get(key) { return await this.base.safeReadInteraction({ function: "get", value: { key: key } }); } /** * Gets the values at the given keys as an array. * * If a value does not exist, it is returned as `null`. * * Note that the transaction limit may become a problem for too many keys. * * @param keys an array of keys * @returns an array of corresponding values */ async getMany(keys) { return await this.base.safeReadInteraction({ function: "getMany", value: { keys: keys } }); } /** * Inserts the given value into database. * * There must not be a value at the given key. * * @param key the key of the value to be inserted * @param value the value to be inserted */ async put(key, value) { await this.base.dryWriteInteraction({ function: "put", value: { key: key, value: value } }); } /** * Inserts an array of value into database. * * There must not be a value at the given key. * * @param keys the keys of the values to be inserted * @param values the values to be inserted */ async putMany(keys, values) { await this.base.dryWriteInteraction({ function: "putMany", value: { keys: keys, values: values } }); } /** * Updates the value of given key. * @param key key of the value to be updated * @param value new value * @param proof optional zero-knowledge proof */ async update(key, value, proof) { await this.base.dryWriteInteraction({ function: "update", value: { key: key, value: value, proof: proof } }); } /** * Removes the value of given key along with the key. * Checks if the proof is valid. * @param key key of the value to be removed * @param proof optional zero-knowledge proof */ async remove(key, proof) { await this.base.dryWriteInteraction({ function: "remove", value: { key: key, proof: proof } }); } } class $5e703c66837a5b12$export$f7af2da260061f5e extends (0, $83a13e96bfc8ddef$export$f7af2da260061f5e) { } class $f500dc08a51cd667$export$95f8f8e92037cfa extends (0, $83a13e96bfc8ddef$export$f7af2da260061f5e) { /** * Inserts the given value into database. * * Overwrites the existing values at the given key. * * @param key the key of the value to be inserted * @param value the value to be inserted */ async set(key, value) { await this.base.dryWriteInteraction({ function: "set", value: { key: key, value: value } }); } /** * Inserts an array of value into database. * * Overwrites the existing values at the given keys. * * @param keys the keys of the values to be inserted * @param values the values to be inserted */ async setMany(keys, values) { await this.base.dryWriteInteraction({ function: "setMany", value: { keys: keys, values: values } }); } /** * Overwrites the contract state. * * Note that this is an owner-only operation, and a wrongfully * overwritten state may break some of the contract methods. * * @param state the key of the value to be inserted */ async setState(state) { await this.base.dryWriteInteraction({ function: "setState", value: state }); } } //# sourceMappingURL=index.cjs.map