All files / src/services registeringHandler.ts

76.92% Statements 20/26
85.71% Branches 12/14
100% Functions 1/1
76% Lines 19/25

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    1x 1x 1x   1x   1x                   59x       59x 1x       1x     59x 59x   59x 59x       59x         55x                         55x     59x                         59x 59x    
import { IPlatformCryptographyTools } from '../shared/interfaces'
import { DidMethod, KeyOptions } from '../dto/shared.dto'
import { ELEM_ANCHORED_DID_METHOD, ELEM_DID_METHOD, WEB_DID_METHOD, KEY_DID_METHOD } from '../_defaultConfig'
import { DidDocumentService, extendSeedWithDid, generateFullSeed, KeysService } from '@affinidi/common'
import { anchorDid } from './anchoringHandler'
import { RegistryApiService } from '@affinidi/internal-api-clients'
import { parseDecryptedSeed } from '@affinidi/common/dist/shared/seedTools'
 
export const register = async (
  registry: RegistryApiService,
  didMethod: DidMethod,
  platformCryptographyTools: IPlatformCryptographyTools,
  password: string,
  keyOptions?: KeyOptions,
  origin?: string,
  skipAnchoringForElemMethod?: boolean,
  webDomain?: string,
) => {
  const isAnchoredSeed = didMethod === ELEM_ANCHORED_DID_METHOD
 
  let seedMetadata
  // TODO: figure out to use proper SDK error (not regular Error)
  if (didMethod && didMethod === 'web') {
    Iif (!webDomain) {
      throw Error('webDomain option should be provided to register did:web')
    }
 
    seedMetadata = { webDomain }
  }
 
  const seedWithMethod = await generateFullSeed(platformCryptographyTools, didMethod, keyOptions, seedMetadata)
  const { keysService, encryptedSeed } = await KeysService.fromSeedAndPassword(seedWithMethod, password)
 
  const didDocumentService = DidDocumentService.createDidDocumentService(keysService)
  const { did: builtDid, didDocument, keyId } = await didDocumentService.buildDidDocumentForRegister()
 
  let anchoredInBlockchainDid
 
  if (
    didMethod !== KEY_DID_METHOD &&
    didMethod !== WEB_DID_METHOD &&
    (didMethod != ELEM_DID_METHOD || !skipAnchoringForElemMethod)
  ) {
    const response = await anchorDid({
      registry,
      keysService,
      didMethod,
      did: builtDid,
      anchoredDidElem: isAnchoredSeed,
      nonce: 0,
      additionalJoloParams: {
        didDocument,
        seedHex: parseDecryptedSeed(seedWithMethod).seed.toString('hex'),
      },
      origin,
    })
    anchoredInBlockchainDid = response.did
  }
 
  Iif (isAnchoredSeed) {
    const anchoredSeed = extendSeedWithDid(seedWithMethod, anchoredInBlockchainDid)
 
    const { encryptedSeed: anchoredEncryptedSeed, keysService } = await KeysService.fromSeedAndPassword(
      anchoredSeed,
      password,
    )
    const anchoredDocumentService = DidDocumentService.createDidDocumentService(keysService)
    const didDocumentKeyId = anchoredDocumentService.getKeyId()
 
    return { did: anchoredInBlockchainDid, encryptedSeed: anchoredEncryptedSeed, didDocumentKeyId }
  }
 
  const didDocumentKeyId = keyId
  return { did: didDocumentService.getMyDid(), didDocumentKeyId, encryptedSeed, didDocument }
}