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ACH;IAEA;;;;;;;KAOG,GACH,MAAM,SAAS,KAA4C,EAA3D;QACE,MAAM,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAgB;YACjD,UAAU;YACV,OAAO;QACR;IACH;AACD;","sources":["src/index.ts","src/base/index.ts","src/base/sdk.ts","src/base/base.ts","src/base/admin.ts","src/hollowdb.ts","src/hollowdb-set.ts"],"sourcesContent":["export {SDK as BaseSDK} from './base';\nexport {SDK} from './hollowdb';\nexport {SetSDK} from './hollowdb-set';\n","export {SDK} from './sdk';\n","import {Base} from './base';\nimport {Admin} from './admin';\nimport type {SortKeyCacheRangeOptions} from 'warp-contracts/lib/types/cache/SortKeyCacheRangeOptions';\nimport type {\n ContractMode,\n ContractState,\n GetInput,\n GetManyInput,\n GetKVMapInput,\n GetKeysInput,\n PutInput,\n PutManyInput,\n RemoveInput,\n UpdateInput,\n} from '../contracts/types';\nimport type {ArWallet, Contract, CustomSignature, SortKeyCacheResult, Warp} from 'warp-contracts';\n\nexport class SDK<V = unknown, M extends ContractMode = ContractMode> {\n readonly base: Base<M>;\n readonly admin: Admin<M>;\n\n /**\n * Connects to the given contract via the provided Warp instance using the provided signer.\n * @param signer a Signer, such as Arweave wallet or Ethereum CustomSignature\n * @param contractTxId contract txId to connect to\n * @param warp a Warp instace, such as `WarpFactory.forMainnet()`\n */\n constructor(signer: ArWallet | CustomSignature, contractTxId: string, warp: Warp) {\n this.base = new Base(signer, contractTxId, warp);\n this.admin = new Admin(this.base);\n }\n\n /** The smart-contract that we are connected to. */\n get contract(): Contract<ContractState<M>> {\n return this.base.contract;\n }\n\n /** Warp instance. */\n get warp(): Warp {\n return this.base.warp;\n }\n\n /** Contract transaction id. */\n get contractTxId(): string {\n return this.base.contract.txId();\n }\n\n /** Signer. */\n get signer(): ArWallet | CustomSignature {\n return this.base.signer;\n }\n\n /**\n * Returns the latest contract state.\n *\n * For a more fine-grained state data, use `base.readState()`.\n *\n * @returns contract state object\n */\n async getState(): Promise<ContractState<M>> {\n return await this.base.readState().then(s => s.cachedValue.state);\n }\n\n /**\n * Alternative method of getting key values. Uses the underlying `getStorageValues`\n * function, returns a Map instead of an array.\n *\n * @param keys an array of keys\n * @returns `SortKeyCacheResult` of a key-value `Map`\n */\n async getStorageValues(keys: string[]): Promise<SortKeyCacheResult<Map<string, V | null>>> {\n return (await this.contract.getStorageValues(keys)) as SortKeyCacheResult<Map<string, V | null>>;\n }\n\n /**\n * Returns keys with respect to a range option.\n *\n * If no option is provided, it will get all keys.\n *\n * @param options optional range\n * @returns an array of keys\n */\n async getKeys(options?: SortKeyCacheRangeOptions): Promise<string[]> {\n return await this.base.safeReadInteraction<GetKeysInput, string[]>({\n function: 'getKeys',\n value: {\n options,\n },\n });\n }\n\n /**\n * Returns all keys in the database.\n *\n * @returns an array of all keys\n */\n async getAllKeys(): Promise<string[]> {\n return this.getKeys();\n }\n\n /**\n * Returns a mapping of keys and values with respect to a range option.\n * If no option is provided, all values are returned.\n *\n * @param options optional range\n * @returns a key-value `Map`\n */\n async getKVMap(options?: SortKeyCacheRangeOptions): Promise<Map<string, V | null>> {\n return await this.base.safeReadInteraction<GetKVMapInput, Map<string, V>>({\n function: 'getKVMap',\n value: {\n options,\n },\n });\n }\n\n /**\n * Gets the value of the given key.\n * @param key the key of the value to be returned\n * @returns the value of the given key\n */\n async get(key: string): Promise<V | null> {\n return await this.base.safeReadInteraction<GetInput, V>({\n function: 'get',\n value: {\n key,\n },\n });\n }\n\n /**\n * Gets the values at the given keys as an array.\n *\n * If a value does not exist, it is returned as `null`.\n *\n * Note that the transaction limit may become a problem for too many keys.\n *\n * @param keys an array of keys\n * @returns an array of corresponding values\n */\n async getMany(keys: string[]): Promise<(V | null)[]> {\n return await this.base.safeReadInteraction<GetManyInput, (V | null)[]>({\n function: 'getMany',\n value: {\n keys,\n },\n });\n }\n\n /**\n * Inserts the given value into database.\n *\n * There must not be a value at the given key.\n *\n * @param key the key of the value to be inserted\n * @param value the value to be inserted\n */\n async put(key: string, value: V): Promise<void> {\n await this.base.dryWriteInteraction<PutInput<V>>({\n function: 'put',\n value: {\n key,\n value,\n },\n });\n }\n\n /**\n * Inserts an array of value into database.\n *\n * There must not be a value at the given key.\n *\n * @param keys the keys of the values to be inserted\n * @param values the values to be inserted\n */\n async putMany(keys: string[], values: V[]): Promise<void> {\n await this.base.dryWriteInteraction<PutManyInput<V>>({\n function: 'putMany',\n value: {\n keys,\n values,\n },\n });\n }\n\n /**\n * Updates the value of given key.\n * @param key key of the value to be updated\n * @param value new value\n * @param proof optional zero-knowledge proof\n */\n async update(key: string, value: V, proof?: object): Promise<void> {\n await this.base.dryWriteInteraction<UpdateInput<V>>({\n function: 'update',\n value: {\n key,\n value,\n proof,\n },\n });\n }\n\n /**\n * Removes the value of given key along with the key.\n * Checks if the proof is valid.\n * @param key key of the value to be removed\n * @param proof optional zero-knowledge proof\n */\n async remove(key: string, proof?: object): Promise<void> {\n await this.base.dryWriteInteraction<RemoveInput>({\n function: 'remove',\n value: {\n key,\n proof,\n },\n });\n }\n}\n","import type {Warp, Contract, ArWallet, CustomSignature} from 'warp-contracts';\nimport type {ContractInputGeneric, ContractMode, ContractState} from '../contracts/types/contract';\n\nexport class Base<M extends ContractMode> {\n readonly contract: Contract<ContractState<M>>;\n readonly warp: Warp;\n readonly signer: ArWallet | CustomSignature;\n\n constructor(signer: ArWallet | CustomSignature, contractTxId: string, warp: Warp) {\n this.signer = signer;\n\n this.warp = warp;\n\n this.contract = this.warp\n .contract<ContractState<M>>(contractTxId)\n .setEvaluationOptions({\n allowBigInt: true,\n useKVStorage: true,\n sequencerUrl: warp.environment === 'mainnet' ? 'https://gw.warp.cc/' : undefined,\n })\n .connect(this.signer);\n }\n\n /**\n * Return the latest contract state.\n *\n * This is a good way to trigger Warp to fetch the latest data from Arweave.\n * Note that if the contract has many transactions, fetching up to the latest\n * state may take some time.\n *\n * @returns contract state along with corresponding sort key\n */\n async readState() {\n return await this.contract.readState();\n }\n\n /**\n * A typed wrapper around `dryWrite`, which evaluates a given input\n * on the local state, without creating a transaction. This may provide\n * better UX for some use-cases.\n * @param input input in the form of `{function, value}`\n * @returns interaction result\n */\n async dryWrite<I extends ContractInputGeneric>(input: I) {\n return await this.contract.dryWrite(input);\n }\n\n /**\n * A typed wrapper around `writeInteraction`, which creates a\n * transaction. You are likely to use this after `dryWrite`, or you\n * may directly call this function.\n * @param input input in the form of `{function, value}`\n * @returns interaction response\n */\n async writeInteraction<I extends ContractInputGeneric>(input: I) {\n return await this.contract.writeInteraction(input);\n }\n\n /**\n * A typed wrapper around `dryWrite` followed by `writeInteraction`. This\n * function first executes the interaction locally via `dryWrite`, and if\n * there is an error, throws an error with an optional prefix in the message.\n * @param input input in the form of `{function, value}`\n * @param errorPrefix optional prefix for the error message\n */\n async dryWriteInteraction<I extends ContractInputGeneric>(input: I) {\n const result = await this.dryWrite(input);\n if (result.type !== 'ok') {\n throw new Error(`Contract Error [${input.function}]: ${result.errorMessage}`);\n }\n await this.writeInteraction(input);\n }\n\n /**\n * A typed wrapper around `viewState` followed with a repsonse type check.\n * If response type is not `ok`, it will throw an error.\n * @param input input in the form of `{function, value}`\n * @returns interaction result\n */\n async safeReadInteraction<I extends ContractInputGeneric, V>(input: I) {\n const response = await this.viewState<I, V>(input);\n if (response.type !== 'ok') {\n throw new Error(`Contract Error [${input.function}]: ${response.errorMessage}`);\n }\n return response.result;\n }\n\n /**\n * A typed wrapper around `viewState`, which is a read interaction.\n * @param input input in the form of `{function, value}`\n * @returns interaction result\n */\n async viewState<I extends ContractInputGeneric, R>(input: I) {\n return await this.contract.viewState<I, R>(input);\n }\n}\n","import type {\n ContractMode,\n UpdateOwnerInput,\n UpdateProofRequirementInput,\n UpdateWhitelistRequirementInput,\n UpdateVerificationKeyInput,\n UpdateWhitelistInput,\n OpitonalArray,\n} from '../contracts/types';\nimport type {Base} from './base';\n\nexport class Admin<M extends ContractMode = ContractMode> {\n constructor(private readonly base: Base<M>) {}\n\n /**\n * Sets the owner as the given wallet address.\n * @param newOwner address of the new owner, make sure that this is correct!\n */\n async updateOwner(newOwner: string) {\n await this.base.writeInteraction<UpdateOwnerInput>({\n function: 'updateOwner',\n value: {\n newOwner,\n },\n });\n }\n\n /**\n * Changes the whitelist for the selected list.\n * @param users an array of user addresses\n * @param name name of the list to be updated\n * @param op whether to `add` the users to whitelist or `remove` them\n */\n async updateWhitelist(users: string[], name: OpitonalArray<M['whitelists'], string>, op: 'add' | 'remove') {\n const add = op === 'add' ? users : [];\n const remove = op === 'remove' ? users : [];\n await this.base.writeInteraction<UpdateWhitelistInput<M['whitelists']>>({\n function: 'updateWhitelist',\n value: {\n add,\n remove,\n name,\n },\n });\n }\n\n /**\n * Update a verification key.\n * @param name name of the circuit that the verification key belongs to\n * @param verificationKey verification key\n */\n async updateVerificationKey(\n name: OpitonalArray<M['proofs'], string>,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n verificationKey: any\n ) {\n await this.base.writeInteraction<UpdateVerificationKeyInput<M['proofs']>>({\n function: 'updateVerificationKey',\n value: {\n verificationKey,\n name,\n },\n });\n }\n\n /**\n * Disable or enable whitelist checks for a specific whitelist.\n * @param name name of the list\n * @param value a boolean\n */\n async updateWhitelistRequirement(name: OpitonalArray<M['whitelists'], string>, value: boolean) {\n await this.base.writeInteraction<UpdateWhitelistRequirementInput<M['whitelists']>>({\n function: 'updateWhitelistRequirement',\n value: {\n name,\n value,\n },\n });\n }\n\n /**\n * Disable or enable proof checks for a specific circuit.\n * @param name name of the circuit\n * @param value a boolean\n */\n async updateProofRequirement(name: OpitonalArray<M['proofs'], string>, value: boolean) {\n await this.base.writeInteraction<UpdateProofRequirementInput<M['proofs']>>({\n function: 'updateProofRequirement',\n value: {\n name,\n value,\n },\n });\n }\n}\n","import {BaseSDK} from './';\n\nexport class SDK<V = unknown> extends BaseSDK<V, {proofs: ['auth']; whitelists: ['put', 'update']}> {}\n","import {BaseSDK} from '.';\nimport {SetInput, SetManyInput, SetStateInput} from './contracts/hollowdb-set.contract';\n\n/** Just like HollowDB SDK, but supports `Set` and `SetMany` operations.\n * The user must be whitelisted for `set` separately to use them.\n *\n * A `set` operation is like a `put` but the key can exist already and will be overwritten.\n */\nexport class SetSDK<V = unknown> extends BaseSDK<V, {proofs: ['auth']; whitelists: ['put', 'update', 'set']}> {\n /**\n * Inserts the given value into database.\n *\n * Overwrites the existing values at the given key.\n *\n * @param key the key of the value to be inserted\n * @param value the value to be inserted\n */\n async set(key: string, value: V): Promise<void> {\n await this.base.dryWriteInteraction<SetInput<V>>({\n function: 'set',\n value: {\n key,\n value,\n },\n });\n }\n\n /**\n * Inserts an array of value into database.\n *\n * Overwrites the existing values at the given keys.\n *\n * @param keys the keys of the values to be inserted\n * @param values the values to be inserted\n */\n async setMany(keys: string[], values: V[]): Promise<void> {\n await this.base.dryWriteInteraction<SetManyInput<V>>({\n function: 'setMany',\n value: {\n keys,\n values,\n },\n });\n }\n\n /**\n * Overwrites the contract state.\n *\n * Note that this is an owner-only operation, and a wrongfully\n * overwritten state may break some of the contract methods.\n *\n * @param state the key of the value to be inserted\n */\n async setState(state: Awaited<ReturnType<this['getState']>>): Promise<void> {\n await this.base.dryWriteInteraction<SetStateInput>({\n function: 'setState',\n value: state,\n });\n }\n}\n"],"names":[],"version":3,"file":"index.mjs.map","sourceRoot":"../"}