{
  "version": 3,
  "sources": ["../src/store.ts"],
  "sourcesContent": ["import type { Driver } from 'iso-kv'\nimport type { CID } from 'multiformats'\nimport { parse as didParse } from 'iso-did'\nimport { KV } from 'iso-kv'\nimport merge from './merge.js'\nimport { Delegation } from './delegation.js'\nimport { validate } from './policy.js'\nimport type { StoreProofsOptions } from './types.js'\n\nconst POWERLINE = '$pwrl'\n\n/**\n * Store\n */\nexport class Store {\n    driver:Driver\n    kv:KV\n\n    constructor (driver:Driver) {\n        this.driver = driver\n\n        this.kv = new KV({\n            driver,\n        })\n    }\n\n    /**\n     * Set a delegation in the store by CID and index it by cmd and aud\n     */\n    async #set (delegation:Delegation):Promise<void> {\n        const options = {\n            expiration: delegation.envelope.payload.exp,\n        }\n        const cid = delegation.cid.toString()\n\n        // Index by CID\n        await this.kv.set([cid], delegation.toString(), options)\n\n        // Index by subject and audience\n        let sub:string | null = delegation.sub ? delegation.sub : null\n        if (sub === null) {\n            sub = POWERLINE\n        }\n        await this.kv.set([sub, delegation.aud, cid], cid, options)\n    }\n\n    async add (delegations:Delegation[]):Promise<void> {\n        for (const delegation of delegations) {\n            await this.#set(delegation)\n        }\n    }\n\n    /**\n     * Get a delegation by cid\n     */\n    async get (cid:string):Promise<Delegation | undefined> {\n        const str = await (this.kv.get as (key: string[]) => Promise<string | undefined>)([\n            cid,\n        ])\n        if (!str) {\n            return undefined\n        }\n        return await Delegation.fromString(str)\n    }\n\n    /**\n     * Resolve a proof by CID\n     */\n    async resolveProof (cid:CID):Promise<Delegation> {\n        const delegation = await this.get(cid.toString())\n        if (!delegation) {\n            throw new Error(`Delegation not found: ${cid.toString()}`)\n        }\n        return delegation\n    }\n\n    /**\n     * List proofs by sub and aud\n     */\n    async * proofs (options:Omit<StoreProofsOptions, 'args'>):AsyncGenerator<Delegation> {\n        let sub:string | null = options.sub ? options.sub.toString() : null\n        let aud:string | undefined = options.aud ? options.aud.toString() : undefined\n\n        if (!aud && !sub) {\n            throw new Error('No audience or subject provided')\n        }\n        if (!aud && sub) {\n            aud = sub\n        }\n        if (sub === null) {\n            sub = POWERLINE\n        }\n        if (!aud) {\n            throw new Error('No audience provided')\n        }\n\n        for await (const element of this.kv.list({\n            prefix: [sub, aud],\n        })) {\n            const value = await this.get(element.value as string)\n\n            if (value) {\n                yield value\n            }\n        }\n    }\n\n    /**\n     * Resolve a single chain of proofs ending with a root (subject === issuer).\n     * Returns the first such chain found (depth-first).\n     */\n    async chain ({ aud, sub, cmd, args }:StoreProofsOptions):Promise<Delegation[]> {\n    // console.log('\uD83D\uDE80 ~ aud', aud, 'sub', sub, 'cmd', cmd)\n        const parents = parentCmds(cmd)\n\n        const sources = merge(\n            this.proofs({ sub, aud, cmd }),\n            this.proofs({ sub: null, aud, cmd })\n        )\n\n        for await (const proof of sources) {\n            if (!parents.includes(proof.cmd)) continue\n            if (!validate(args, proof.pol)) continue\n\n            // If root, return this proof as the end of the path\n            if (proof.sub === didParse(proof.iss).did) {\n                // console.log('\uD83D\uDE80 ~ found root:', proof.iss, proof.sub, proof.cmd)\n                return [proof]\n            }\n\n            // Otherwise, go deeper and prepend this proof if a path is found\n            // console.log('\uD83D\uDE80 ~ trying deeper:', proof.iss, proof.sub, proof.cmd)\n            const nextPath = await this.chain({\n                aud: didParse(proof.iss).did,\n                sub: proof.sub ? proof.sub : sub,\n                cmd: proof.cmd,\n                args,\n            })\n            if (nextPath?.length) {\n                return [proof, ...nextPath]\n            }\n            // console.log('\uD83D\uDE80 ~ no path found')\n        }\n\n        // No path found\n        return []\n    }\n}\n\n/**\n * Returns all parent commands for a given command string.\n *\n * @example\n * ```ts twoslash\n * import { parentCmds } from 'iso-ucan/store'\n * parentCmds('/foo/bar/baz') // [\"/\", \"/foo\", \"/foo/bar\", \"/foo/bar/baz\"]\n * ```\n */\nfunction parentCmds (cmd:string):string[] {\n    if (!cmd || cmd === '/') return ['/']\n    const parts = cmd.split('/').filter(Boolean)\n    const result = ['/']\n    let current = ''\n    for (const part of parts) {\n        current += `/${part}`\n        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AAMS,EAAQT,EAAI,MAAM,GAAG,EAAE,OAAO,OAAO,EACrCU,EAAS,CAAC,GAAG,EACnB,IAAIC,EAAU,GACd,UAAWC,KAAQH,EACfE,GAAW,IAAIC,CAAI,GACnBF,EAAO,KAAKC,CAAO,EAEvB,OAAOD,CACX,CAVStB,EAAAe,EAAA",
  "names": ["store_exports", "__export", "Store", "__toCommonJS", "import_iso_did", "import_iso_kv", "import_merge", "import_delegation", "import_policy", "POWERLINE", "Store", "__name", "driver", "#set", "delegation", "options", "cid", "sub", "delegations", "str", "aud", "element", "value", "cmd", "args", "parents", "parentCmds", "sources", "merge", "proof", "didParse", "nextPath", "parts", "result", "current", "part"]
}
