Class: Mnam::TransactionBuilder::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/mnam/transaction_builder.rb

Defined Under Namespace

Classes: Fee, Type

Instance Method Summary collapse

Constructor Details

#initialize(type:, fee:, sender_public_key: nil, recipient_id: nil, amount: nil, vendor_field: nil, asset: {}) ⇒ Transaction

Returns a new instance of Transaction.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mnam/transaction_builder.rb', line 39

def initialize(
  type:,
  fee:,
  sender_public_key: nil,
  recipient_id: nil,
  amount: nil,
  vendor_field: nil,
  asset: {}
)
  @type = type
  @fee = fee
  @sender_public_key = sender_public_key
  @recipient_id = recipient_id
  @amount = amount
  @vendor_field = vendor_field
  @timestamp = seconds_after_epoch
  @asset = asset
end

Instance Method Details

#sign_and_create_id(key, second_key = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/mnam/transaction_builder.rb', line 58

def sign_and_create_id(key, second_key = nil)
  transaction_bytes = to_bytes
  @signature = key.ecdsa_signature(Digest::SHA256.digest(transaction_bytes)).unpack('H*').first

  hashed_transaction_bytes_with_sig = Digest::SHA256.digest(to_bytes(false))

  @sign_signature = second_key.ecdsa_signature(hashed_transaction_bytes_with_sig).unpack('H*').first if second_key
  @id = Digest::SHA256.digest(to_bytes(false, false)).unpack('H*').first
end

#to_bytes(skip_signature = true, skip_second_signature = true) ⇒ Object



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
# File 'lib/mnam/transaction_builder.rb', line 68

def to_bytes(skip_signature = true, skip_second_signature = true)
  out = ''
  out << [type].pack('c')
  out << [timestamp].pack("V")
  out << [sender_public_key].pack('H*')
  if recipient_id
    out << BTC::Base58.data_from_base58check(recipient_id)
  else
    out << [].pack('x21')
  end

  if vendor_field
    out << vendor_field
    if vendor_field.size < 64
      out << [].pack("x#{64 - vendor_field.size}")
    end
  else
    out << [].pack("x64")
  end

  out << [amount].pack('Q<')
  out << [fee].pack('Q<')

  case type
  when Type::SECOND_SIGNATURE
    asset_signature_public_key = asset[:signature][:public_key]
    out << [asset_signature_public_key].pack('H*')
  when Type::DELEGATE
    out << asset[:delegate][:username]
  when Type::VOTE
    out << asset[:votes].join('')
  when Type::MULTISIGNATURE
    ms_asset = asset[:multisignature]
    out << [ms_asset[:min]].pack('C')
    out << [ms_asset[:lifetime]].pack('C')
    out << ms_asset[:keysgroup].join('')
  end

  if !skip_signature && signature
    out << [signature].pack('H*')
  end

  if !skip_second_signature && sign_signature
    out << [sign_signature].pack('H*')
  end

  out
end

#to_paramsObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mnam/transaction_builder.rb', line 117

def to_params
  {
    :type => type,
    :amount => amount,
    :fee => fee,
    :vendorField => vendor_field,
    :timestamp => timestamp,
    :recipientId => recipient_id,
    :senderPublicKey => sender_public_key,
    :signature => signature,
    :id => id
 }.tap do |h|
    h[:asset] = asset.deep_transform_keys {|key| snake_case_to_camel_case(key)} if asset.any?
    h[:signSignature] = sign_signature if sign_signature
  end
end