All files / src/services anchoringHandler.ts

44.44% Statements 16/36
20% Branches 2/10
60% Functions 3/5
42.85% Lines 15/35

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 971x               1x                                 1x                                                 1x 55x     1x                           1x 55x 55x         55x                 1x 55x 55x 55x             55x    
import {
  ELEM_ANCHORED_DID_METHOD,
  ELEM_DID_METHOD,
  JOLO_DID_METHOD,
  POLYGON_DID_METHOD,
  POLYGON_TESTNET_DID_METHOD,
} from '../_defaultConfig'
import { RegistryApiService } from '@affinidi/internal-api-clients'
import { KeysService, LocalKeyVault, PolygonDidDocumentService } from '@affinidi/common'
import { DidMethod } from '../dto/shared.dto'
 
type AnchoringParams = {
  registry: RegistryApiService
  keysService: KeysService
  didMethod: DidMethod
  did: string
  nonce: number
  anchoredDidElem: boolean
  additionalJoloParams?: {
    didDocument: any
    seedHex: string
  }
  origin?: string
}
 
const computePreparedJoloParams = async ({ registry, keysService, nonce, additionalJoloParams }: AnchoringParams) => {
  if (!additionalJoloParams) {
    throw new Error('missing jolo params')
  }
 
  const { didDocument, seedHex } = additionalJoloParams
 
  const did = didDocument.id
  const signedDidDocument = await keysService.signDidDocument(didDocument)
 
  const { body: bodyDidDocument } = await registry.putDocumentInIpfs({ document: signedDidDocument })
  const didDocumentAddress = bodyDidDocument.hash
 
  const {
    body: { digestHex },
  } = await registry.createAnchorTransaction({ nonce, did, didDocumentAddress })
 
  const transactionSignatureJson = digestHex ? await keysService.createTransactionSignature(digestHex, seedHex) : ''
 
  const transactionPublicKey = KeysService.getAnchorTransactionPublicKey(seedHex, JOLO_DID_METHOD)
  const ethereumPublicKeyHex = transactionPublicKey.toString('hex')
 
  return { did, didDocumentAddress, ethereumPublicKeyHex, transactionSignatureJson, nonce }
}
 
const computePreparedElemParams = async ({ did }: AnchoringParams) => {
  return { did, didDocumentAddress: '', ethereumPublicKeyHex: '', transactionSignatureJson: '' }
}
 
const computePreparedPolygonParams = async ({ did, keysService, didMethod, registry }: AnchoringParams) => {
  const didService = new PolygonDidDocumentService(new LocalKeyVault(keysService), {
    isTestnet: didMethod === 'polygon:testnet',
  })
  const publicKeyBase58 = didService.getMyPubKeyBase58()
  const {
    body: { digestHex },
  } = await registry.createAnchorTransaction({ did, publicKeyBase58 })
 
  const transactionSignatureJson = await keysService.createTransactionSignature(digestHex)
 
  return { did, publicKeyBase58, transactionSignatureJson }
}
 
const computePreparedAnchoringParams = async (params: AnchoringParams) => {
  const { didMethod } = params
  switch (didMethod) {
    case JOLO_DID_METHOD:
      return computePreparedJoloParams(params)
    case ELEM_DID_METHOD:
    case ELEM_ANCHORED_DID_METHOD:
      return computePreparedElemParams(params)
    case POLYGON_DID_METHOD:
    case POLYGON_TESTNET_DID_METHOD:
      return computePreparedPolygonParams(params)
    default:
      throw new Error(`did method: "${didMethod}" is not supported`)
  }
}
 
export const anchorDid = async (params: AnchoringParams): Promise<{ did: string }> => {
  const { registry, anchoredDidElem, nonce } = params
  const preparedParams = await computePreparedAnchoringParams(params)
  const response = await registry.anchorDid({
    ...preparedParams,
    nonce,
    anchoredDidElem,
    origin: params.origin,
  })
 
  return { did: response.body.did }
}