All files lookups.js

78.38% Statements 29/37
87.5% Branches 14/16
42.86% Functions 3/7
80.56% Lines 29/36

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                42x 42x 42x 41x 41x 10x 10x   31x     1x   1x     1x           1x                         33x 1x 1x       32x 2x 2x       30x 30x 1x 1x   29x 1x       28x 28x 28x 3x   28x            
import { config as bskConfig, validateProofs, resolveZoneFileToProfile } from 'blockstack'
import { validateStacksAddress } from '@stacks/transactions'
import fetch from 'node-fetch'
 
import logger from 'winston'
 
export async function isSubdomainRegistered(fullyQualifiedAddress: String) {
 
  try {
    const nameInfoUrl = bskConfig.network.blockstackAPIUrl + '/v1/names/' + fullyQualifiedAddress
    const nameInfoRequest = await fetch(nameInfoUrl)
    const { status } = nameInfoRequest
    if (status == 200) {
      const nameInfo = await nameInfoRequest.json()
      return (nameInfo.status === 'registered_subdomain')
    } else {
      return false
    }
  } catch (err) {
    Iif (err.message === 'Name not found') {
      return false
    } else Iif (err.message === 'Bad response status: 500') {
      return false // currently, the blockstack api returns 500 on subdomain lookup errors.
    } else {
      throw err
    }
  }
}
 
export function validlySignedUpdate() {
  throw new Error('Not implemented')
}
 
export function checkProofs(owner, zonefile) {
  return resolveZoneFileToProfile(zonefile, owner)
    .then((profile) => validateProofs(profile, owner))
    .then((proofs) => proofs.filter(x => x.valid))
}
 
export async function isRegistrationValid(
  subdomainName: String, domainName: String,
  owner: String, sequenceNumber: Number, checkCore: boolean) {
  // currently, only support *new* subdomains
  if (sequenceNumber !== 0) {
    logger.debug(`seqn: ${sequenceNumber} failed validation`)
    return false
  }
 
  // owner should be a stacks address
  if (!validateStacksAddress(owner)) {
    logger.debug(`owner: ${owner} failed validation`)
    return false
  }
 
  // subdomain name should be a legal name
  const subdomainRegex = /^[a-z0-9\-_+]{1,37}$/
  if (!subdomainRegex.test(subdomainName)) {
    logger.debug(`subdomainName: ${subdomainName} failed validation`)
    return false
  }
  if (!checkCore) {
    return true
  }
 
  // shouldn't already exist
  try {
    const isRegistered = await isSubdomainRegistered(`${subdomainName}.${domainName}`)
    if (isRegistered) {
      logger.warn(`${subdomainName}.${domainName} already exists`)
    }
    return !isRegistered
  } catch (e) {
    logger.error(e)
    return false
  }
}