All files / src/CommonNetworkMember NetworkMemberWithoutCognito.ts

100% Statements 17/17
100% Branches 0/0
100% Functions 3/3
100% Lines 15/15

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 781x 1x 1x 1x 1x 1x     1x   17x                                                   15x           15x 15x 15x                                     2x           2x 2x              
import { profile } from '@affinidi/tools-common'
import { KeyOptions, SdkOptions } from '../dto/shared.dto'
import { withDidData } from '../shared/getDidData'
import { getOptionsFromEnvironment, ParsedOptions } from '../shared/getOptionsFromEnvironment'
import { ParametersValidator } from '../shared/ParametersValidator'
import { BaseNetworkMember, StaticDependencies, ConstructorUserData } from './BaseNetworkMember'
 
@profile()
export class NetworkMemberWithoutCognito extends BaseNetworkMember {
  constructor(userData: ConstructorUserData, dependencies: StaticDependencies, options: ParsedOptions) {
    super(userData, dependencies, options)
  }
 
  /**
   * @description Creates DID and anchors it
   * 1. generate seed/keys (available key types rsa, bbs and ecdsa as a default)
   * 2. build DID document
   * 3. sign DID document
   * 4. store DID document in IPFS
   * 5. anchor DID with DID document ID from IPFS
   * @param dependencies - static dependencies
   * @param password - encryption key which will be used to encrypt randomly created seed/keys pair
   * @param inputOptions - optional parameter { registryUrl: 'https://affinity-registry.apse1.dev.affinidi.io' }
   * @param keyOptions - list of key types (rsa and bbs allowed, ecdsa - default)
   * @returns
   *
   * did - hash from public key (your decentralized ID)
   *
   * encryptedSeed - seed is encrypted by provided password. Seed - it's a source to derive your keys
   */
  static async createWallet(
    dependencies: StaticDependencies,
    inputOptions: SdkOptions,
    password: string,
    keyOptions?: KeyOptions,
  ) {
    await ParametersValidator.validate([
      { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions },
      { isArray: false, type: 'string', isRequired: true, value: password },
      { isArray: false, type: KeyOptions, isRequired: false, value: keyOptions },
    ])
 
    const options = getOptionsFromEnvironment(inputOptions)
    const userData = await NetworkMemberWithoutCognito._register(dependencies, options, password, keyOptions)
    return new NetworkMemberWithoutCognito({ ...userData, password }, dependencies, options)
  }
 
  /**
   * @description Initilizes instance of SDK from seed
   * @param dependencies - static dependencies
   * @param inputOptions - parameter { registryUrl: 'https://affinity-registry.apse1.dev.affinidi.io' }
   * @param encryptedSeed - seed for derive keys in string hex format
   * @param password - optional password, will be generated, if not provided
   * @param accountNumber - optional parameter
   * @returns initialized instance of SDK
   */
  static async openWalletByEncryptedSeed(
    dependencies: StaticDependencies,
    inputOptions: SdkOptions,
    encryptedSeed: string,
    password: string,
    accountNumber?: number,
  ) {
    await ParametersValidator.validate([
      { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions },
      { isArray: false, type: 'string', isRequired: true, value: encryptedSeed },
      { isArray: false, type: 'string', isRequired: true, value: password },
    ])
 
    const options = getOptionsFromEnvironment(inputOptions)
    return new NetworkMemberWithoutCognito(
      withDidData({ password, encryptedSeed, accountNumber }),
      dependencies,
      options,
    )
  }
}