Class: IOTA::Utils::InputValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/iota/utils/input_validator.rb

Instance Method Summary collapse

Instance Method Details

#isAddress(input) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/iota/utils/input_validator.rb', line 32

def isAddress(input)
  if input.length == 90
    if !isTrytes(input, 90)
      return false
    end
  else
    if !isTrytes(input, 81)
      return false
    end
  end

  true
end

#isAllNine(input) ⇒ Object



4
5
6
# File 'lib/iota/utils/input_validator.rb', line 4

def isAllNine(input)
  /^[9]+$/.match?(input)
end

#isArray(input) ⇒ Object



20
21
22
# File 'lib/iota/utils/input_validator.rb', line 20

def isArray(input)
  input.class == Array
end

#isArrayOfAttachedTrytes(trytes) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/iota/utils/input_validator.rb', line 70

def isArrayOfAttachedTrytes(trytes)
  return false if !isArray(trytes)

  (0...trytes.length).step(1) do |i|
    tryte = trytes[i]
    return false if !isTrytes(tryte, 2673)

    lastTrytes = tryte.slice(2673 - (3 * 81), trytes.length)
    return false if isAllNine(lastTrytes)
  end

  true
end

#isArrayOfHashes(input) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/iota/utils/input_validator.rb', line 60

def isArrayOfHashes(input)
  return false if !isArray(input)

  input.each do |entry|
    return false if !isAddress(entry)
  end

  true
end

#isArrayOfTrytes(trytesArray) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/iota/utils/input_validator.rb', line 50

def isArrayOfTrytes(trytesArray)
  return false if !isArray(trytesArray)

  trytesArray.each do |tryte|
    return false if !isTrytes(tryte, 2673)
  end

  true
end

#isArrayOfTxObjects(bundle) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/iota/utils/input_validator.rb', line 84

def isArrayOfTxObjects(bundle)
  bundle = bundle.transactions if bundle.class == IOTA::Models::Bundle

  return false if !isArray(bundle) || bundle.length == 0

  bundle.each do |txObject|
    if txObject.class != IOTA::Models::Transaction
      txObject = IOTA::Models::Transaction.new(txObject)
    end

    return false if !txObject.valid?
  end

  true
end

#isHash(input) ⇒ Object



28
29
30
# File 'lib/iota/utils/input_validator.rb', line 28

def isHash(input)
  isTrytes(input, 81)
end

#isInputs(inputs) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/iota/utils/input_validator.rb', line 114

def isInputs(inputs)
  return false if !isArray(inputs)

  inputs.each do |input|
    if input.class != IOTA::Models::Input
      input = IOTA::Models::Input.new(input)
    end

    return false if !input.valid?
  end

  true
end

#isNum(input) ⇒ Object



12
13
14
# File 'lib/iota/utils/input_validator.rb', line 12

def isNum(input)
  /^(\d+\.?\d{0,15}|\.\d{0,15})$/.match?(input.to_s)
end

#isObject(input) ⇒ Object



24
25
26
# File 'lib/iota/utils/input_validator.rb', line 24

def isObject(input)
  input.class == Hash
end

#isString(input) ⇒ Object



16
17
18
# File 'lib/iota/utils/input_validator.rb', line 16

def isString(input)
  input.class == String
end

#isTransfersArray(transfersArray) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/iota/utils/input_validator.rb', line 100

def isTransfersArray(transfersArray)
  return false if !isArray(transfersArray)

  transfersArray.each do |transfer|
    if transfer.class != IOTA::Models::Transfer
      transfer = IOTA::Models::Transfer.new(transfer)
    end

    return false if !transfer.valid?
  end

  true
end

#isTrytes(input, length = "0,") ⇒ Object



46
47
48
# File 'lib/iota/utils/input_validator.rb', line 46

def isTrytes(input, length = "0,")
  isString(input) && /^[9A-Z]{#{length}}$/.match?(input)
end

#isUri(node) ⇒ Object

Checks that a given uri is valid Valid Examples: udp://:14265 udp:// udp://8.8.8.8:14265 udp://domain.com udp://domain2.com:14265



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/iota/utils/input_validator.rb', line 135

def isUri(node)
  getInside = /^(udp|tcp):\/\/([\[][^\]\.]*[\]]|[^\[\]:]*)[:]{0,1}([0-9]{1,}$|$)/i

  stripBrackets = /[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-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))|(^\s*((?=.{1,255}$)(?=.*[A-Za-z].*)[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?)*)\s*$)/

  match = getInside.match(node)
  return false if match.nil? || match[2].nil?

  uriTest.match?(stripBrackets.match(match[2])[1])
end

#isValue(input) ⇒ Object



8
9
10
# File 'lib/iota/utils/input_validator.rb', line 8

def isValue(input)
  input && input.is_a?(Integer)
end