Class: ArkEcosystem::Crypto::Builder::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/arkecosystem/crypto/builder/transaction.rb

Overview

The base builder for transactions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransaction

Returns a new instance of Transaction.



16
17
18
19
20
21
22
23
24
25
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 16

def initialize
  @type = type
  @fee = ArkEcosystem::Crypto::Configuration::Fee.get(@type)
  @sender_public_key = nil
  @recipient_id = nil
  @amount = 0
  @vendor_field = nil
  @timestamp = seconds_after_epoch
  @asset = {}
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def amount
  @amount
end

#assetObject

Returns the value of attribute asset.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def asset
  @asset
end

#feeObject

Returns the value of attribute fee.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def fee
  @fee
end

#idObject

Returns the value of attribute id.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def id
  @id
end

#recipient_idObject

Returns the value of attribute recipient_id.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def recipient_id
  @recipient_id
end

#sender_public_keyObject

Returns the value of attribute sender_public_key.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def sender_public_key
  @sender_public_key
end

#sign_signatureObject

Returns the value of attribute sign_signature.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def sign_signature
  @sign_signature
end

#signatureObject

Returns the value of attribute signature.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def signature
  @signature
end

#timestampObject

Returns the value of attribute timestamp.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def timestamp
  @timestamp
end

#typeObject

Returns the value of attribute type.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def type
  @type
end

#vendor_fieldObject

Returns the value of attribute vendor_field.



14
15
16
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 14

def vendor_field
  @vendor_field
end

Instance Method Details

#second_sign(second_secret) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 43

def second_sign(second_secret)
  second_key = ArkEcosystem::Crypto::Identity::PrivateKey.from_secret(second_secret)

  bytes = ArkEcosystem::Crypto::Crypto.get_bytes(to_hash, false)

  @sign_signature = second_key.ecdsa_signature(Digest::SHA256.digest(bytes)).unpack('H*').first
  self
end

#second_verify(second_public_key_hex) ⇒ Object



56
57
58
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 56

def second_verify(second_public_key_hex)
  ArkEcosystem::Crypto::Crypto.second_verify(self, second_public_key_hex)
end

#sign(secret) ⇒ Object



27
28
29
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 27

def sign(secret)
  sign_and_create_id(secret)
end

#sign_and_create_id(secret) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 31

def sign_and_create_id(secret)
  private_key = ArkEcosystem::Crypto::Identity::PrivateKey.from_secret(secret)
  @sender_public_key = private_key.public_key.unpack('H*').first

  transaction_bytes = ArkEcosystem::Crypto::Crypto.get_bytes(to_hash)
  @signature = private_key.ecdsa_signature(Digest::SHA256.digest(transaction_bytes)).unpack('H*').first

  transaction_bytes = ArkEcosystem::Crypto::Crypto.get_bytes(to_hash, false, false)
  @id = Digest::SHA256.digest(transaction_bytes).unpack('H*').first
  self
end

#to_hashObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 77

def to_hash
  {
    amount: amount,
    asset: asset,
    fee: fee,
    id: id,
    recipient_id: recipient_id,
    sender_public_key: sender_public_key,
    sign_signature: sign_signature,
    signature: signature,
    timestamp: timestamp,
    type: type,
    vendor_field: vendor_field
  }
end

#to_paramsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 60

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

#verifyObject



52
53
54
# File 'lib/arkecosystem/crypto/builder/transaction.rb', line 52

def verify
  ArkEcosystem::Crypto::Crypto.verify(self)
end