# All files / src/operations safety.js

82.76% Statements 48/58
68% Branches 17/25
90.32% Functions 28/31
82.14% Lines 46/56

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
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151

    64x
    64x

    64x

    2x

    62x
    62x
    3x

    59x

    118x
    3x

    115x

    8x
    8x

    2x
    1x

    1x
    1x

    3x
    1x

    2x
    1x

    1x

    2x
    2x

    4x
    2x

    2x
    2x

    3x
    2x

    1x
    1x

    3x
    2x

    1x
    1x

    4x
    4x

    3x
    3x

    1x
    1x

    3x
    53x
    53x

    1x

     /* @flow */

    import { config } from '../config'

    function isNameValid(fullyQualifiedName: ?string = '') {
      const NAME_PART_RULE = /^[a-z0-9\-_+]+$/
      const LENGTH_MAX_NAME = 37

      if (!fullyQualifiedName
          || fullyQualifiedName.length > LENGTH_MAX_NAME) {
        return Promise.resolve(false)
      }
      const nameParts = fullyQualifiedName.split('.')
      if (nameParts.length !== 2) {
        return Promise.resolve(false)
      }
      return Promise.resolve(
        nameParts.reduce(
          (agg, namePart) => {
            if (!agg) {
              return false
            } else {
              return NAME_PART_RULE.test(namePart)
            }
          }, true
        )
      )
    }

    function isNamespaceValid(namespaceID: string) {
      const NAMESPACE_RULE = /^[a-z0-9\-_]{1,19}$/
      return Promise.resolve(
        namespaceID.match(NAMESPACE_RULE) !== null
      )
    }

    function isNameAvailable(fullyQualifiedName: string) {
      return config.network.getNameInfo(fullyQualifiedName)
        .then(() => false)
        .catch((e) => {
          Eif (e.message === 'Name not found') {
            return true
          } else {
            throw e
          }
        })
    }

    function isNamespaceAvailable(namespaceID: string) {
      return config.network.getNamespaceInfo(namespaceID)
        .then(() => false)
        .catch((e) => {
          if (e.message === 'Namespace not found') {
            return true
          } else {
            throw e
          }
        })
    }

    function ownsName(fullyQualifiedName: string, ownerAddress: string) {
      return config.network.getNameInfo(fullyQualifiedName)
        .then(nameInfo => nameInfo.address === ownerAddress)
        .catch((e) => {
          if (e.message === 'Name not found') {
            return false
          } else {
            throw e
          }
        })
    }

    function revealedNamespace(namespaceID: string, revealAddress: string) {
      return config.network.getNamespaceInfo(namespaceID)
        .then(namespaceInfo => namespaceInfo.recipient_address === revealAddress)
        .catch((e) => {
          Eif (e.message === 'Namespace not found') {
            return false
          } else {
            throw e
          }
        })
    }

    function namespaceIsReady(namespaceID: string) {
      return config.network.getNamespaceInfo(namespaceID)
        .then(namespaceInfo => namespaceInfo.ready)
        .catch((e) => {
          Eif (e.message === 'Namespace not found') {
            return false
          } else {
            throw e
          }
        })
    }

    function namespaceIsRevealed(namespaceID: string) {
      return config.network.getNamespaceInfo(namespaceID)
        .then(namespaceInfo => !namespaceInfo.ready)
        .catch((e) => {
          Eif (e.message === 'Namespace not found') {
            return false
          } else {
            throw e
          }
        })
    }

    function isInGracePeriod(fullyQualifiedName: string) {
      const network = config.network
      return Promise.all([network.getNameInfo(fullyQualifiedName),
                          network.getBlockHeight(),
                          network.getGracePeriod(fullyQualifiedName)])
        .then(([nameInfo, blockHeight, gracePeriod]) => {
          const expiresAt = nameInfo.expire_block
          return (blockHeight >= expiresAt) && (blockHeight < (gracePeriod + expiresAt))
        })
        .catch((e) => {
          Eif (e.message === 'Name not found') {
            return false
          } else {
            throw e
          }
        })
    }

    function addressCanReceiveName(address: string) {
      return config.network.getNamesOwned(address)
        .then(names => (Promise.all(names.map(name => isNameValid(name)))
          .then(validNames => validNames.filter(nameValid => nameValid).length < 25)))
    }

    function isAccountSpendable(address: string, tokenType: string, blockHeight: number) {
      return config.network.getAccountStatus(address, tokenType)
        .then(accountStatus => accountStatus.transfer_send_block_id >= blockHeight)
    }

    export const safety = {
      addressCanReceiveName,
      isInGracePeriod,
      ownsName,
      isNameAvailable,
      isNameValid,
      isNamespaceValid,
      isNamespaceAvailable,
      revealedNamespace,
      namespaceIsReady,
      namespaceIsRevealed,
      isAccountSpendable
    }