All files / src/profiles/services service.ts

86.21% Statements 25/29
57.14% Branches 4/7
81.82% Functions 9/11
86.21% Lines 25/29

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  1x 1x         1x         4x   4x 4x       1x     3x               3x 3x   3x     1x 1x 1x                         2x       3x             2x   23x         3x   3x 3x   3x 5x   2x     1x      
 
import 'cross-fetch/polyfill'
import { containsValidProofStatement, containsValidAddressProofStatement } from './serviceUtils'
 
/**
 * @ignore
 */
export class Service {
  static validateProof(proof: any,
                       ownerAddress: string,
                       name: string = null) {
    let proofUrl: string
    return Promise.resolve()
      .then(() => {
        proofUrl = this.getProofUrl(proof)
        return fetch(proofUrl)
      })
      .then((res) => {
        if (res.status !== 200) {
          throw new Error(`Proof url ${proofUrl} returned unexpected http status ${res.status}.
              Unable to validate proof.`)
        }
        return res.text()
      })
      .then((text) => {
        // Validate identity in provided proof body/tags if required
        if (this.shouldValidateIdentityInBody()
            && proof.identifier !== this.getProofIdentity(text)) {
          return proof
        }
        const proofText = this.getProofStatement(text)
        proof.valid = containsValidProofStatement(proofText, name)
          || containsValidAddressProofStatement(proofText, ownerAddress)
        return proof
      })
      .catch((error) => {
        console.error(error)
        proof.valid = false
        return proof
      })
  }
 
  static getBaseUrls(): string[] {
    return []
  }
 
  static getProofIdentity(searchText: string) {
    return searchText
  }
 
  static getProofStatement(searchText: string) {
    return searchText
  }
 
  static shouldValidateIdentityInBody() {
    return false
  }
 
  static prefixScheme(proofUrl: string) {
    if (!proofUrl.startsWith('https://') && !proofUrl.startsWith('http://')) {
      return `https://${proofUrl}`
    } else if (proofUrl.startsWith('http://')) {
      return proofUrl.replace('http://', 'https://')
    } else {
      return proofUrl
    }
  }
 
  static getProofUrl(proof: any): string {
    const baseUrls = this.getBaseUrls()
 
    let proofUrl = proof.proof_url.toLowerCase()
    proofUrl = this.prefixScheme(proofUrl)
 
    for (let i = 0; i < baseUrls.length; i++) {
      const requiredPrefix = `${baseUrls[i]}${proof.identifier}`.toLowerCase()
      if (proofUrl.startsWith(requiredPrefix)) {
        return proofUrl
      }
    }
    throw new Error(`Proof url ${proof.proof_url} is not valid for service ${proof.service}`)
  }
}