All files / src/storage/methods listCertificates.ts

85.29% Statements 29/34
44.44% Branches 4/9
100% Functions 8/8
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62        58x               15x   15x   15x 1x 1x 1x 1x 1x 1x 1x     15x 14x 14x 32x 32x 32x   54x 54x     14x   32x                       14x 10x   4x   14x     14x    
import { ListCertificatesResult, OriginatorDomainNameStringUnder250Bytes } from "@bsv/sdk"
import { StorageProvider, table } from "../index.client"
import { sdk } from "../../index.client"
 
export async function listCertificates(
    storage: StorageProvider,
    auth: sdk.AuthId,
    vargs: sdk.ValidListCertificatesArgs,
    originator?: OriginatorDomainNameStringUnder250Bytes,
)
: Promise<ListCertificatesResult>
{
    const paged: sdk.Paged = { limit: vargs.limit, offset: vargs.offset }
 
    const partial: Partial<table.Certificate> = { userId: auth.userId, isDeleted: false }
 
    if (vargs.partial) {
        const vp = vargs.partial
        Iif (vp.type) partial['type'] = vp.type;
        Iif (vp.subject) partial['subject'] = vp.subject;
        if (vp.serialNumber) partial['serialNumber'] = vp.serialNumber;
        Iif (vp.certifier) partial['certifier'] = vp.certifier;
        Iif (vp.revocationOutpoint) partial['revocationOutpoint'] = vp.revocationOutpoint;
        Iif (vp.signature) partial['signature'] = vp.signature;
    }
 
    const r = await storage.transaction(async trx => {
        const findCertsArgs: sdk.FindCertificatesArgs = { partial, certifiers: vargs.certifiers, types: vargs.types, paged, trx }
        const certs = await storage.findCertificates(findCertsArgs)
        const certsWithFields = await Promise.all(certs.map(async cert => {
            const fields = await storage.findCertificateFields({ partial: { certificateId: cert.certificateId, userId: auth.userId }, trx })
            return {
                ...cert,
                fields: Object.fromEntries(fields.map(f => ([f.fieldName, f.fieldValue]))),
                masterKeyring: Object.fromEntries(fields.map(f => ([f.fieldName, f.masterKey])))
            }
        }))
        const r: ListCertificatesResult = {
            totalCertificates: 0,
            certificates: certsWithFields.map(cwf => ({
                type: cwf.type,
                subject: cwf.subject,
                serialNumber: cwf.serialNumber,
                certifier: cwf.certifier,
                revocationOutpoint: cwf.revocationOutpoint,
                signature: cwf.signature,
                fields: cwf.fields,
                verifier: cwf.verifier,
                keyring: cwf.masterKeyring
            }))
        }
        if (r.certificates.length < paged.limit)
            r.totalCertificates = r.certificates.length
        else {
            r.totalCertificates = await storage.countCertificates(findCertsArgs)
        }
        return r
    })
 
    return r
}