class Fabric::ProposedTransaction

Manages the instantiation and creation of the Gateway::ProposedTransaction Protobuf Message.

Adapted from official fabric-gateway SDK ProposalBuilder and hyperledger-fabric-sdk: github.com/hyperledger/fabric-gateway/blob/1518e03ed3d6db1b6809e23e61a92744fd18e724/node/src/proposalbuilder.ts github.com/kirshin/hyperledger-fabric-sdk/blob/95a5a1a37001852312df25946e960a9ff149207e/lib/fabric/proposal.rb

Attributes

arguments[R]
contract[R]
endorsing_organizations[R]

Specifies the set of organizations that will attempt to endorse the proposal. No other organizations’ peers will be sent this proposal. This is usually used in conjunction with transientData for private data scenarios.

proposed_transaction[R]
transaction_name[R]
transient_data[R]

Public Class Methods

new(contract, transaction_name, arguments: [], transient_data: {}, endorsing_organizations: []) click to toggle source
# File lib/fabric/entities/proposed_transaction.rb, line 26
def initialize(contract, transaction_name, arguments: [], transient_data: {}, endorsing_organizations: [])
  @contract = contract
  @transaction_name = transaction_name
  @arguments = arguments
  @transient_data = transient_data
  @endorsing_organizations = endorsing_organizations

  generate_proposed_transaction
end

Public Instance Methods

as_proto() click to toggle source

Returns the protobuf message instance

@return [Gateway::ProposedTransaction] protobuf message instance

# File lib/fabric/entities/proposed_transaction.rb, line 139
def as_proto
  proposed_transaction
end
chaincode_id() click to toggle source
# File lib/fabric/entities/proposed_transaction.rb, line 77
def chaincode_id
  Protos::ChaincodeID.new name: chaincode_name
end
chaincode_proposal_payload() click to toggle source
# File lib/fabric/entities/proposed_transaction.rb, line 81
def chaincode_proposal_payload
  chaincode_input = Protos::ChaincodeInput.new args: [transaction_name] + arguments
  chaincode_spec = Protos::ChaincodeSpec.new type: Protos::ChaincodeSpec::Type::NODE,
                                             chaincode_id: chaincode_id,
                                             input: chaincode_input
  input = Protos::ChaincodeInvocationSpec.new chaincode_spec: chaincode_spec

  Protos::ChaincodeProposalPayload.new input: input.to_proto, TransientMap: transient_data
end
channel_header() click to toggle source
# File lib/fabric/entities/proposed_transaction.rb, line 65
def channel_header
  Common::ChannelHeader.new type: Common::HeaderType::ENDORSER_TRANSACTION,
                            channel_id: network_name, tx_id: transaction_id,
                            extension: channel_header_extension.to_proto,
                            timestamp: timestamp, epoch: 0
  # version: Constants::CHANNEL_HEADER_VERSION # official SDK does not send this.
end
channel_header_extension() click to toggle source
# File lib/fabric/entities/proposed_transaction.rb, line 73
def channel_header_extension
  Protos::ChaincodeHeaderExtension.new chaincode_id: chaincode_id
end
generate_proposed_transaction() click to toggle source

Builds the proposed transaction protobuf message

@return [Gateway::ProposedTransaction]

# File lib/fabric/entities/proposed_transaction.rb, line 41
def generate_proposed_transaction
  @proposed_transaction = ::Gateway::ProposedTransaction.new(
    transaction_id: transaction_id,
    proposal: signed_proposal,
    endorsing_organizations: endorsing_organizations
  )
end
header() click to toggle source
# File lib/fabric/entities/proposed_transaction.rb, line 60
def header
  Common::Header.new channel_header: channel_header.to_proto,
                     signature_header: signature_header.to_proto
end
nonce() click to toggle source

Generates a random nonce

@return [String] random nonce

# File lib/fabric/entities/proposed_transaction.rb, line 107
def nonce
  @nonce ||= signer.crypto_suite.generate_nonce
end
proposal() click to toggle source
# File lib/fabric/entities/proposed_transaction.rb, line 55
def proposal
  @proposal ||= Protos::Proposal.new header: header.to_proto,
                                     payload: chaincode_proposal_payload.to_proto
end
signature_header() click to toggle source

Generates a SignatureHeader protobuf message from the signer and nonce

@return [Common::SignatureHeader] signature header protobuf message instance

# File lib/fabric/entities/proposed_transaction.rb, line 126
def signature_header
  Common::SignatureHeader.new creator: signer.to_proto, nonce: nonce
end
signed_proposal() click to toggle source
# File lib/fabric/entities/proposed_transaction.rb, line 49
def signed_proposal
  @signed_proposal ||= Protos::SignedProposal.new(
    proposal_bytes: proposal.to_proto
  )
end
timestamp() click to toggle source

Returns the current timestamp

@return [Google::Protobuf::Timestamp] gRPC timestamp

# File lib/fabric/entities/proposed_transaction.rb, line 96
def timestamp
  now = Time.now

  @timestamp ||= Google::Protobuf::Timestamp.new seconds: now.to_i, nanos: now.nsec
end
to_json(options = {}) click to toggle source

Returns the serialized JSON form of the proposed transaction

@param [Hash] options JSON serialization options @see ruby-doc.org/stdlib-2.6.3/libdoc/json/rdoc/JSON.html#method-i-generate

@return [String] serialized JSON form of the proposed transaction

# File lib/fabric/entities/proposed_transaction.rb, line 159
def to_json(options = {})
  proposed_transaction.to_json(options)
end
to_proto() click to toggle source

Returns the serialized Protobuf binary form of the proposed transaction

@return [String] serialized Protobuf binary form of the proposed transaction

# File lib/fabric/entities/proposed_transaction.rb, line 148
def to_proto
  proposed_transaction.to_proto
end
transaction_id() click to toggle source

Generates a unique transaction ID for the transaction based on a random number and the signer or returns the existing transaction ID if it has already been generated.

@return [String] transaction ID

# File lib/fabric/entities/proposed_transaction.rb, line 117
def transaction_id
  @transaction_id ||= signer.crypto_suite.hexdigest(nonce + signer.to_proto)
end