Class: MixinBot::Transaction
- Inherits:
-
Object
- Object
- MixinBot::Transaction
- Defined in:
- lib/mixin_bot/transaction.rb
Constant Summary collapse
- REFERENCES_TX_VERSION =
0x04
- SAFE_TX_VERSION =
0x05
- DEAULT_VERSION =
5
- MAGIC =
[0x77, 0x77].freeze
- TX_VERSION =
2
- MAX_ENCODE_INT =
0xFFFF
- MAX_EXTRA_SIZE =
512
- NULL_BYTES =
[0x00, 0x00].freeze
- AGGREGATED_SIGNATURE_PREFIX =
0xFF01
- AGGREGATED_SIGNATURE_ORDINAY_MASK =
[0x00].freeze
- AGGREGATED_SIGNATURE_SPARSE_MASK =
[0x01].freeze
Instance Attribute Summary collapse
-
#aggregated ⇒ Object
Returns the value of attribute aggregated.
-
#asset ⇒ Object
Returns the value of attribute asset.
-
#extra ⇒ Object
Returns the value of attribute extra.
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#hex ⇒ Object
Returns the value of attribute hex.
-
#inputs ⇒ Object
Returns the value of attribute inputs.
-
#outputs ⇒ Object
Returns the value of attribute outputs.
-
#references ⇒ Object
Returns the value of attribute references.
-
#signatures ⇒ Object
Returns the value of attribute signatures.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #decode ⇒ Object
- #encode ⇒ Object
-
#initialize(**kwargs) ⇒ Transaction
constructor
A new instance of Transaction.
- #to_h ⇒ Object
Constructor Details
#initialize(**kwargs) ⇒ Transaction
Returns a new instance of Transaction.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mixin_bot/transaction.rb', line 19 def initialize(**kwargs) @version = kwargs[:version] || DEAULT_VERSION @asset = kwargs[:asset] @inputs = kwargs[:inputs] @outputs = kwargs[:outputs] @extra = kwargs[:extra].to_s @hex = kwargs[:hex] @signatures = kwargs[:signatures] @aggregated = kwargs[:aggregated] @references = kwargs[:references] end |
Instance Attribute Details
#aggregated ⇒ Object
Returns the value of attribute aggregated.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def aggregated @aggregated end |
#asset ⇒ Object
Returns the value of attribute asset.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def asset @asset end |
#extra ⇒ Object
Returns the value of attribute extra.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def extra @extra end |
#hash ⇒ Object
Returns the value of attribute hash.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def hash @hash end |
#hex ⇒ Object
Returns the value of attribute hex.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def hex @hex end |
#inputs ⇒ Object
Returns the value of attribute inputs.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def inputs @inputs end |
#outputs ⇒ Object
Returns the value of attribute outputs.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def outputs @outputs end |
#references ⇒ Object
Returns the value of attribute references.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def references @references end |
#signatures ⇒ Object
Returns the value of attribute signatures.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def signatures @signatures end |
#version ⇒ Object
Returns the value of attribute version.
17 18 19 |
# File 'lib/mixin_bot/transaction.rb', line 17 def version @version end |
Instance Method Details
#decode ⇒ Object
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 152 153 |
# File 'lib/mixin_bot/transaction.rb', line 77 def decode @bytes = [hex].pack('H*').bytes @hash = SHA3::Digest::SHA256.hexdigest @bytes.pack('C*') magic = @bytes.shift(2) raise ArgumentError, 'Not valid raw' unless magic == MAGIC _version = @bytes.shift(2) @version = MixinBot.utils.decode_int _version asset = @bytes.shift(32) @asset = asset.pack('C*').unpack1('H*') # read inputs decode_inputs # read outputs decode_outputs # read references decode_references if version >= REFERENCES_TX_VERSION # read extra # unsigned 32 endian for extra size extra_size = MixinBot.utils.decode_uint32 @bytes.shift(4) @extra = @bytes.shift(extra_size).pack('C*') num = MixinBot.utils.decode_uint16 @bytes.shift(2) if num == MAX_ENCODE_INT # aggregated @aggregated = {} raise ArgumentError, 'invalid aggregated' unless MixinBot.utils.decode_uint16(@bytes.shift(2)) == AGGREGATED_SIGNATURE_PREFIX @aggregated['signature'] = @bytes.shift(64).pack('C*').unpack1('H*') byte = @bytes.shift case byte when AGGREGATED_SIGNATURE_ORDINAY_MASK.first @aggregated['signers'] = [] masks_size = MixinBot.utils.decode_uint16 @bytes.shift(2) masks = @bytes.shift(masks_size) masks = Array(masks) masks.each_with_index do |mask, i| 8.times do |j| k = 1 << j aggregated['signers'].push((i * 8) + j) if mask & k == k end end when AGGREGATED_SIGNATURE_SPARSE_MASK.first signers_size = MixinBot.utils.decode_uint16 @bytes.shift(2) return if signers_size.zero? aggregated['signers'] = [] signers_size.times do aggregated['signers'].push MixinBot.utils.decode_uint16(@bytes.shift(2)) end end elsif num.present? && num.positive? && @bytes.size.positive? @signatures = [] num.times do signature = {} keys_size = MixinBot.utils.decode_uint16 @bytes.shift(2) keys_size.times do index = MixinBot.utils.decode_uint16 @bytes.shift(2) signature[index] = @bytes.shift(64).pack('C*').unpack1('H*') end @signatures << signature end end self end |
#encode ⇒ Object
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 |
# File 'lib/mixin_bot/transaction.rb', line 31 def encode raise InvalidTransactionFormatError, 'asset is required' if asset.blank? raise InvalidTransactionFormatError, 'inputs is required' if inputs.blank? raise InvalidTransactionFormatError, 'outputs is required' if outputs.blank? bytes = [] # magic number bytes += MAGIC # version bytes += [0, version] # asset bytes += [asset].pack('H*').bytes # inputs bytes += encode_inputs # output bytes += encode_outputs # placeholder for `references` bytes += encode_references if version >= REFERENCES_TX_VERSION # extra extra_bytes = extra.bytes raise InvalidTransactionFormatError, 'extra is too long' if extra_bytes.size > MAX_EXTRA_SIZE bytes += MixinBot.utils.encode_uint32 extra_bytes.size bytes += extra_bytes # aggregated bytes += if aggregated.nil? # signatures encode_signatures else encode_aggregated_signature end @hash = SHA3::Digest::SHA256.hexdigest bytes.pack('C*') @hex = bytes.pack('C*').unpack1('H*') self end |
#to_h ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/mixin_bot/transaction.rb', line 155 def to_h { version:, asset:, inputs:, outputs:, extra:, signatures:, aggregated:, hash:, references: }.compact end |