Class: Keoken::Backend::BitcoinRuby::Transaction

Inherits:
Keoken::Backend::Base show all
Includes:
Bitcoin::Builder
Defined in:
lib/keoken/backend/bitcoin_ruby/transaction.rb

Instance Attribute Summary collapse

Attributes inherited from Keoken::Backend::Base

#bitprim_transaction, #inputs, #total_inputs_amount

Instance Method Summary collapse

Methods inherited from Keoken::Backend::Base

#initialize

Constructor Details

This class inherits a constructor from Keoken::Backend::Base

Instance Attribute Details

#rawObject

Returns the value of attribute raw.



5
6
7
# File 'lib/keoken/backend/bitcoin_ruby/transaction.rb', line 5

def raw
  @raw
end

#to_jsonObject

Returns the value of attribute to_json.



5
6
7
# File 'lib/keoken/backend/bitcoin_ruby/transaction.rb', line 5

def to_json
  @to_json
end

Instance Method Details

#build(address, addr2, key, script, type) ⇒ Keoken::Backend::BitcoinRuby::Transaction

Invoke the methods creating the transaction automatically.

Parameters:

  • Address that will contain the token.

  • Address to receive the tokens.

  • The key obtained from Bitcoin Ruby.

  • The token script.

  • :create for creation and :send to send tokens.

Returns:

  • An object instanciated with the transaction to broadcast.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/keoken/backend/bitcoin_ruby/transaction.rb', line 43

def build(address, addr2, key, script, type)
  build_inputs(address)
  total, fee = build_fee(type)
  case type
  when :create
    output_amount = total - fee.to_i
    create(@inputs, output_amount, key.addr, key, script)
  when :send
    output_amount = total - (fee.to_i * 2)
    output_amount_to_addr2 = fee.to_i
    send_amount(@inputs, output_amount, key.addr, output_amount_to_addr2, addr2, key, script)
  end
end

#build_for_creation(address, key, script) ⇒ Keoken::Backend::BitcoinRuby::Transaction

Create the transaction to broadcast in order to create tokens.

Parameters:

  • Address that will contain the token.

  • The key obtained from Bitcoin Ruby.

  • The token script.

Returns:

  • An object instanciated with the transaction to broadcast.



16
17
18
# File 'lib/keoken/backend/bitcoin_ruby/transaction.rb', line 16

def build_for_creation(address, key, script)
  build(address, nil, key, script, :create)
end

#build_for_send_amount(address, address_dest, key, script) ⇒ Keoken::Backend::BitcoinRuby::Transaction

Create the transaction to broadcast in order to send amount between tokens.

Parameters:

  • Address that will contain the token.

  • Address to receive the tokens.

  • The key obtained from Bitcoin Ruby.

  • The token script.

Returns:

  • An object instanciated with the transaction to broadcast.



29
30
31
# File 'lib/keoken/backend/bitcoin_ruby/transaction.rb', line 29

def build_for_send_amount(address, address_dest, key, script)
  build(address, address_dest, key, script, :send)
end

#create(inputs, output_amount, output_address, key, script) ⇒ Keoken::Backend::BitcoinRuby::Transaction

Create the transaction to broadcast in order to create tokens.

Parameters:

  • Inputs to sign.

  • Amount to send to output, should be enough for feed.

  • Address that will contain the token.

  • The key obtained from Bitcoin Ruby.

  • The token script.

Returns:

  • An object instanciated with the transaction to broadcast.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/keoken/backend/bitcoin_ruby/transaction.rb', line 67

def create(inputs, output_amount, output_address, key, script)
  tx = build_tx do |t|
    inputs.each do |input|
      t.input do |i|
        i.prev_out(input[:tx_id], input[:position], input[:input_script].htb, input[:input_amount], 0)
        i.signature_key(key)
      end
    end
    t.output do |o|
      o.value output_amount
      o.script do |s|
        s.recipient(output_address)
      end
    end
    t.output do |o|
      o.value 0
      o.to(script, :custom)
    end
  end
  @to_json = tx.to_json
  @raw = tx.to_payload.bth
  self
end

#send_amount(inputs, output_amount, output_address, output_amount_to_addr2, addr2, key, script) ⇒ Keoken::Backend::BitcoinRuby::Transaction

Create the transaction to broadcast in order to send amount between tokens.

Parameters:

  • Inputs to sign.

  • Amount to send to output, should be enough for feed.

  • Address that will contain the token.

  • Amount to send to the output address to receive the tokens, should be low enough for feed.

  • Address to receive the tokens.

  • The key obtained from Bitcoin Ruby.

  • The token script.

Returns:

  • An object instanciated with the transaction to broadcast.



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
# File 'lib/keoken/backend/bitcoin_ruby/transaction.rb', line 103

def send_amount(inputs, output_amount, output_address, output_amount_to_addr2, addr2, key, script)
  tx = build_tx do |t|
    inputs.each do |input|
      t.input do |i|
        i.prev_out(input[:tx_id], input[:position], input[:input_script].htb, input[:input_amount], 0)
        i.signature_key(key)
      end
    end
    t.output do |o|
      o.value output_amount_to_addr2
      o.script do |s|
        s.recipient(addr2)
      end
    end
    t.output do |o|
      o.value output_amount
      o.script do |s|
        s.recipient(output_address)
      end
    end
    t.output do |o|
      o.value 0
      o.to(script, :custom)
    end
  end
  @to_json = tx.to_json
  @raw = tx.to_payload.bth
  self
end