All files / src/services WalletStorageService.ts

94.44% Statements 51/54
88.88% Branches 16/18
100% Functions 13/13
94.23% Lines 49/52

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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1531x 1x 1x 1x     1x 1x 1x   1x   1x 39x                     1x         152x   152x   152x               39x   39x               3x 3x         3x 3x 3x               2x 2x 2x 2x         2x   2x       5x   5x       6x   6x   5x       11x   11x       5x   5x       5x         5x   5x       5x 5x     5x       2x   2x       5x   5x       3x   3x 3x                      
import { JwtService, KeyManager } from '@affinidi/common'
import { KeyStorageApiService } from '@affinidi/internal-api-clients'
import { profile } from '@affinidi/tools-common'
import { extractSDKVersion } from '../_helpers'
import { Env, SignedCredential } from '../dto/shared.dto'
import { DidAuthAdapter } from '../shared/DidAuthAdapter'
import SdkErrorFromCode from '../shared/SdkErrorFromCode'
import AffinidiVaultStorageService from './AffinidiVaultStorageService'
import AffinidiVaultEncryptionService from './AffinidiVaultEncryptionService'
 
const createHash = require('create-hash')
 
const sha256 = (data: unknown) => {
  return createHash('sha256').update(data).digest()
}
 
type ConstructorOptions = {
  affinidiVaultUrl: string
  storageRegion: string
  accessApiKey: string
  didAuthAdapter: DidAuthAdapter
}
 
@profile()
export default class WalletStorageService {
  private _storageRegion
  private _affinidiVaultStorageService
 
  constructor(keyManager: KeyManager, options: ConstructorOptions) {
    this._storageRegion = options.storageRegion
 
    const encryptionService = new AffinidiVaultEncryptionService(keyManager)
 
    this._affinidiVaultStorageService = new AffinidiVaultStorageService(encryptionService, {
      didAuthAdapter: options.didAuthAdapter,
      accessApiKey: options.accessApiKey,
      vaultUrl: options.affinidiVaultUrl,
    })
  }
 
  static hashFromString(data: string): string {
    const buffer = sha256(Buffer.from(data))
 
    return buffer.toString('hex')
  }
 
  static async getCredentialOffer(
    accessToken: string,
    keyStorageUrl: string,
    options: { env: Env; accessApiKey: string },
  ): Promise<string> {
    const { accessApiKey, env } = options
    const service = new KeyStorageApiService({
      keyStorageUrl,
      accessApiKey,
      sdkVersion: extractSDKVersion(),
    })
    const { body } = await service.getCredentialOffer({ accessToken, env })
    const { offerToken } = body
    return offerToken
  }
 
  static async getSignedCredentials(
    accessToken: string,
    credentialOfferResponseToken: string,
    options: { env?: Env; keyStorageUrl?: string; issuerUrl?: string; accessApiKey: string; apiKey?: string },
  ): Promise<SignedCredential[]> {
    const keyStorageUrl = options.keyStorageUrl
    const { env, issuerUrl, accessApiKey, apiKey } = options
    const service = new KeyStorageApiService({ keyStorageUrl, accessApiKey, sdkVersion: extractSDKVersion() })
    const { body } = await service.getSignedCredential(accessToken, {
      credentialOfferResponseToken,
      options: { env, issuerUrl, accessApiKey, apiKey },
    })
 
    const { signedCredentials } = body
 
    return signedCredentials as SignedCredential[]
  }
 
  private async _getCredentialsByTypes(storageRegion?: string, types?: string[][]) {
    storageRegion = storageRegion || this._storageRegion
 
    return this._affinidiVaultStorageService.searchCredentials(storageRegion, types)
  }
 
  public async saveCredentials(credentials: any[], storageRegion?: string) {
    storageRegion = storageRegion || this._storageRegion
 
    const responses = await this._affinidiVaultStorageService.saveCredentials(credentials, storageRegion)
 
    return responses
  }
 
  public async getAllCredentials(storageRegion?: string) {
    storageRegion = storageRegion || this._storageRegion
 
    return await this._affinidiVaultStorageService.searchCredentials(storageRegion)
  }
 
  public async getCredentialsByShareToken(token: string, storageRegion?: string) {
    storageRegion = storageRegion || this._storageRegion
 
    Iif (!token) {
      return this.getAllCredentials()
    }
 
    const request = JwtService.fromJWT(token)
    const {
      payload: {
        interactionToken: { credentialRequirements },
      },
    } = request
 
    Iif (!credentialRequirements) {
      return this.getAllCredentials()
    }
 
    const requirementTypes = credentialRequirements.map(
      (credentialRequirement: { type: string[] }) => credentialRequirement.type,
    )
 
    return this._getCredentialsByTypes(storageRegion, requirementTypes)
  }
 
  public async getCredentialById(credentialId: string, storageRegion?: string): Promise<any> {
    storageRegion = storageRegion || this._storageRegion
 
    return await this._affinidiVaultStorageService.getCredentialById(credentialId, storageRegion)
  }
 
  public async deleteCredentialById(credentialId: string, storageRegion?: string) {
    storageRegion = storageRegion || this._storageRegion
 
    await this._affinidiVaultStorageService.deleteCredentialById(credentialId, storageRegion)
  }
 
  public async deleteAllCredentials(storageRegion?: string): Promise<void> {
    storageRegion = storageRegion || this._storageRegion
 
    try {
      await this._affinidiVaultStorageService.deleteAllCredentials(storageRegion)
    } catch (error) {
      throw new SdkErrorFromCode('COR-0', {}, error)
    }
  }
}
 
type PaginationOptions = {
  skip: number
  limit: number
}