All files / src/profiles/services service.ts

91.3% Statements 21/23
66.67% Branches 10/15
100% Functions 3/3
91.3% Lines 21/23

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 651x 1x               1x           4x 4x 4x 4x 1x     3x     3x       3x 3x   3x   1x 1x 1x         3x       25x   25x 2x   23x                            
import { containsValidProofStatement, containsValidAddressProofStatement } from './serviceUtils'
import { fetchPrivate } from '../../fetchUtil'
import { AccountProofInfo } from '../profileProofs'
 
export type CheerioModuleType = typeof import('cheerio')
 
/**
 * @ignore
 */
export abstract class Service {
  async validateProof(
    proof: AccountProofInfo,
    ownerAddress: string,
    cheerio: CheerioModuleType,
    name: string = null): Promise<AccountProofInfo> {
    try {
      const proofUrl = this.getProofUrl(proof)
      const res = await fetchPrivate(proofUrl)
      if (res.status !== 200) {
        throw new Error(`Proof url ${proofUrl} returned unexpected http status ${res.status}.
            Unable to validate proof.`)
      }
      const text = await res.text()
 
      // Validate identity in provided proof body/tags if required
      Iif (this.shouldValidateIdentityInBody()
          && proof.identifier !== this.getProofIdentity(text, cheerio)) {
        return proof
      }
      const proofText = this.getProofStatement(text, cheerio)
      proof.valid = containsValidProofStatement(proofText, name)
        || containsValidAddressProofStatement(proofText, ownerAddress)
      return proof
    } catch (error) {
      console.error(error)
      proof.valid = false
      return proof
    }
  }
 
  shouldValidateIdentityInBody() {
    return false
  }
 
  prefixScheme(proofUrl: string) {
    Iif (!proofUrl.startsWith('https://') && !proofUrl.startsWith('http://')) {
      return `https://${proofUrl}`
    } else if (proofUrl.startsWith('http://')) {
      return proofUrl.replace('http://', 'https://')
    } else {
      return proofUrl
    }
  }
 
  abstract getBaseUrls(): string[];
 
  abstract getProofUrl(proof: AccountProofInfo): string;
 
  abstract getProofStatement(searchText: string, cheerio: CheerioModuleType): string;
 
  abstract normalizeUrl(proof: AccountProofInfo): string;
 
  abstract getProofIdentity(searchText: string, cheerio: CheerioModuleType): string;
}