Class: IOTA::Crypto::Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/iota/crypto/bundle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bundle = []) ⇒ Bundle

Returns a new instance of Bundle.


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/iota/crypto/bundle.rb', line 6

def initialize(bundle = [])
  bundle = bundle.map do |b|
    if b.class != IOTA::Models::Transaction
      IOTA::Models::Transaction.new(b)
    else
      b
    end
  end

  @bundle = bundle
end

Instance Attribute Details

#bundleObject (readonly)

Returns the value of attribute bundle


4
5
6
# File 'lib/iota/crypto/bundle.rb', line 4

def bundle
  @bundle
end

Instance Method Details

#addEntry(signatureMessageLength, address, value, tag, timestamp) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/iota/crypto/bundle.rb', line 18

def addEntry(signatureMessageLength, address, value, tag, timestamp)
  (0...signatureMessageLength).step(1) do |i|
    transactionObject = IOTA::Models::Transaction.new(
      address: address,
      value: i==0 ? value : 0,
      obsoleteTag: tag,
      tag: tag,
      timestamp: timestamp
    )

    @bundle << transactionObject
  end
end

#addTrytes(signatureFragments) ⇒ Object


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
# File 'lib/iota/crypto/bundle.rb', line 32

def addTrytes(signatureFragments)
  emptySignatureFragment = ''
  emptyHash = '9' * 81
  emptyTag = '9' * 27
  emptyTimestamp = '9' * 9

  while emptySignatureFragment.length < 2187
    emptySignatureFragment += '9'
  end

  (0...@bundle.length).step(1) do |i|
    # Fill empty signatureMessageFragment
    @bundle[i].signatureMessageFragment = signatureFragments[i] ? signatureFragments[i] : emptySignatureFragment

    # Fill empty trunkTransaction
    @bundle[i].trunkTransaction = emptyHash

    # Fill empty branchTransaction
    @bundle[i].branchTransaction = emptyHash

    @bundle[i].attachmentTimestamp = emptyTimestamp
    @bundle[i].attachmentTimestampLowerBound = emptyTimestamp
    @bundle[i].attachmentTimestampUpperBound = emptyTimestamp

    # Fill empty nonce
    @bundle[i].nonce = emptyTag
  end
end

#finalizeObject


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
# File 'lib/iota/crypto/bundle.rb', line 61

def finalize
  validBundle = false

  while !validBundle

    kerl = Kerl.new

    (0...@bundle.length).step(1) do |i|
      valueTrits = Converter.trits(@bundle[i].value)
      while valueTrits.length < 81
        valueTrits << 0
      end

      timestampTrits = Converter.trits(@bundle[i].timestamp)
      while timestampTrits.length < 27
        timestampTrits << 0
      end

      @bundle[i].currentIndex = i
      currentIndexTrits = Converter.trits(@bundle[i].currentIndex)
      while currentIndexTrits.length < 27
        currentIndexTrits << 0
      end

      @bundle[i].lastIndex = @bundle.length - 1
      lastIndexTrits = Converter.trits(@bundle[i].lastIndex)
      while lastIndexTrits.length < 27
        lastIndexTrits << 0
      end

      bundleEssence = Converter.trits(@bundle[i].address + Converter.trytes(valueTrits) + @bundle[i].obsoleteTag + Converter.trytes(timestampTrits) + Converter.trytes(currentIndexTrits) + Converter.trytes(lastIndexTrits))
      kerl.absorb(bundleEssence, 0, bundleEssence.length)
    end

    hash = []
    kerl.squeeze(hash, 0, Kerl::HASH_LENGTH)
    hash = Converter.trytes(hash)

    (0...@bundle.length).step(1) do |i|
      @bundle[i].bundle = hash
    end

    normalizedHash = normalizedBundle(hash)
    if !normalizedHash.index(13).nil?      # Insecure bundle. Increment Tag and recompute bundle hash.

      increasedTagTrits = Converter.trits(@bundle[0].obsoleteTag)

      # Adder implementation with 1 round
      (0...increasedTagTrits.length).step(1) do |j|
        increasedTagTrits[j] += 1

        if increasedTagTrits[j] > 1
          increasedTagTrits[j] = -1
        else
          break
        end
      end

      @bundle[0].obsoleteTag = Converter.trytes(increasedTagTrits)
    else
      validBundle = true
    end
  end
end

#normalizedBundle(bundleHash) ⇒ Object


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
152
153
154
155
156
157
158
159
160
# File 'lib/iota/crypto/bundle.rb', line 126

def normalizedBundle(bundleHash)
  normalizedBundleArr = []

  (0...3).step(1) do |i|
    sum = 0
    (0...27).step(1) do |j|
      normalizedBundleArr[i * 27 + j] = Converter.value(Converter.trits(bundleHash[i * 27 + j]))
      sum += normalizedBundleArr[i * 27 + j]
    end

    if sum >= 0
      while sum > 0
        sum -= 1
        (0...27).step(1) do |j|
          if normalizedBundleArr[i * 27 + j] > -13
            normalizedBundleArr[i * 27 + j] -= 1
            break
          end
        end
      end
    else
      while sum < 0
        sum += 1
        (0...27).step(1) do |j|
          if normalizedBundleArr[i * 27 + j] < 13
            normalizedBundleArr[i * 27 + j] += 1
            break
          end
        end
      end
    end
  end

  normalizedBundleArr
end