Class: FlowClient::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_client/transaction.rb

Overview

A Transaction is a full transaction object containing a payload and signatures.

Constant Summary collapse

TRANSACTION_DOMAIN_TAG =
"FLOW-V0.0-transaction"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransaction

Returns a new instance of Transaction.


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/flow_client/transaction.rb', line 25

def initialize
  @authorizer_addresses = []
  @arguments = []
  @script = ""
  @gas_limit = 999
  @envelope_signatures = []
  @payload_signatures = []
  @address_aliases = {}
  @signers = {}
  @proposal_key = ProposalKey.new
end

Instance Attribute Details

#address_aliasesObject

Returns the value of attribute address_aliases.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def address_aliases
  @address_aliases
end

#argumentsObject

Returns the value of attribute arguments.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def arguments
  @arguments
end

#authorizer_addressesObject

Returns the value of attribute authorizer_addresses.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def authorizer_addresses
  @authorizer_addresses
end

#envelope_signaturesObject

Returns the value of attribute envelope_signatures.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def envelope_signatures
  @envelope_signatures
end

#gas_limitObject

Returns the value of attribute gas_limit.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def gas_limit
  @gas_limit
end

#payer_addressObject

Returns the value of attribute payer_address.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def payer_address
  @payer_address
end

#payload_signaturesObject

Returns the value of attribute payload_signatures.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def payload_signatures
  @payload_signatures
end

#proposal_keyObject

Returns the value of attribute proposal_key.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def proposal_key
  @proposal_key
end

#reference_block_idObject

Returns the value of attribute reference_block_id.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def reference_block_id
  @reference_block_id
end

#scriptObject

Returns the value of attribute script.


12
13
14
# File 'lib/flow_client/transaction.rb', line 12

def script
  @script
end

Class Method Details

.padded_transaction_domain_tagObject


56
57
58
# File 'lib/flow_client/transaction.rb', line 56

def self.padded_transaction_domain_tag
  Utils.right_pad_bytes(TRANSACTION_DOMAIN_TAG.bytes, 32).pack("c*")
end

.parse_grpc_type(type) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/flow_client/transaction.rb', line 95

def self.parse_grpc_type(type)
  tx = Transaction.new
  tx.script = type.script
  tx.arguments = type.arguments
  tx.reference_block_id = type.reference_block_id.unpack1("H*")
  tx.gas_limit = type.gas_limit
  tx.authorizer_addresses = type.authorizers.map { |address| address.unpack1("H*") }
  tx.envelope_signatures = type.envelope_signatures.map { |sig| Signature.parse_grpc_type(sig) }
  tx.payload_signatures = type.payload_signatures.map { |sig| Signature.parse_grpc_type(sig) }
  tx.proposal_key = ProposalKey.parse_grpc_type(type.proposal_key)
  tx.payer_address = type.payer.unpack1("H*")
  tx
end

Instance Method Details

#add_envelope_signature(signer_address, key_index, signer) ⇒ Object


60
61
62
63
64
65
66
67
68
# File 'lib/flow_client/transaction.rb', line 60

def add_envelope_signature(signer_address, key_index, signer)
  domain_tagged_envelope = (Transaction.padded_transaction_domain_tag.bytes + envelope_message.bytes).pack("C*")

  @envelope_signatures << Entities::Transaction::Signature.new(
    address: padded_address(signer_address),
    key_id: key_index,
    signature: signer.sign(domain_tagged_envelope)
  )
end

#add_payload_signature(signer_address, key_index, signer) ⇒ Object


70
71
72
73
74
75
76
77
78
# File 'lib/flow_client/transaction.rb', line 70

def add_payload_signature(signer_address, key_index, signer)
  domain_tagged_payload = (Transaction.padded_transaction_domain_tag.bytes + payload_message.bytes).pack("C*")

  @payload_signatures << Entities::Transaction::Signature.new(
    address: padded_address(signer_address),
    key_id: key_index,
    signature: signer.sign(domain_tagged_payload)
  )
end

#proposer_address=(address) ⇒ Object


44
45
46
# File 'lib/flow_client/transaction.rb', line 44

def proposer_address=(address)
  @proposal_key.address = address
end

#proposer_key_index=(index) ⇒ Object


48
49
50
# File 'lib/flow_client/transaction.rb', line 48

def proposer_key_index=(index)
  @proposal_key.key_id = index
end

#proposer_key_sequence_number=(sequence_number) ⇒ Object


52
53
54
# File 'lib/flow_client/transaction.rb', line 52

def proposer_key_sequence_number=(sequence_number)
  @proposal_key.sequence_number = sequence_number
end

#to_protobuf_messageObject


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/flow_client/transaction.rb', line 80

def to_protobuf_message
  payload = payload_canonical_form

  proposal_key = Entities::Transaction::ProposalKey.new(
    address: payload[4],
    key_id: payload[5],
    sequence_number: payload[6]
  )

  Entities::Transaction.new(script: payload[0], arguments: payload[1],
                            reference_block_id: payload[2], gas_limit: payload[3], proposal_key: proposal_key,
                            payer: payload[7], authorizers: payload[8], payload_signatures: @payload_signatures,
                            envelope_signatures: @envelope_signatures)
end