Class: OpenAssets::Api

Inherits:
Object
  • Object
show all
Includes:
MethodFilter, Util
Defined in:
lib/openassets/api.rb

Constant Summary

Constants included from Util

Util::OA_NAMESPACE, Util::OA_VERSION_BYTE, Util::OA_VERSION_BYTE_TESTNET

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MethodFilter

included

Methods included from Util

#address_to_asset_id, #address_to_oa_address, #coin_to_satoshi, #decode_leb128, #encode_leb128, #generate_asset_id, #oa_address_to_address, #oa_address_to_asset_id, #pubkey_hash_to_asset_id, #satoshi_to_coin, #script_to_address, #to_bytes, #validate_address

Constructor Details

#initialize(config = nil) ⇒ Api

Returns a new instance of Api.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/openassets/api.rb', line 16

def initialize(config = nil)
  @cache = {}
  @config = {:network => 'mainnet',
             :provider => 'bitcoind',
             :dust_limit => 600, :default_fees => 10000, :min_confirmation => 1, :max_confirmation => 9999999,
             :rpc => { :host => 'localhost', :port => 8332 , :user => '', :password => '', :schema => 'https'}}
  if config
    @config.update(config)
  end
  if @config[:provider] == 'bitcoind'
    @provider = Provider::BitcoinCoreProvider.new(@config[:rpc])
  else
    raise OpenAssets::Error, 'specified unsupported provider.'
  end
end

Instance Attribute Details

#cache ⇒ Object (readonly)

Returns the value of attribute cache.



14
15
16
# File 'lib/openassets/api.rb', line 14

def cache
  @cache
end

#config ⇒ Object (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/openassets/api.rb', line 12

def config
  @config
end

#provider ⇒ Object (readonly)

Returns the value of attribute provider.



13
14
15
# File 'lib/openassets/api.rb', line 13

def provider
  @provider
end

Instance Method Details

#get_balance(address = nil) ⇒ Object

Returns the balance in both bitcoin and colored coin assets for all of the addresses available in your Bitcoin Core wallet.

Parameters:

  • address (String) (defaults to: nil) —

    The open assets address. if unspecified nil.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/openassets/api.rb', line 52

def get_balance(address = nil)
  outputs = get_unspent_outputs(address.nil? ? [] : [oa_address_to_address(address)])
  colored_outputs = outputs.map{|o|o.output}
  sorted_outputs = colored_outputs.sort_by { |o|o.script.to_string}
  groups = sorted_outputs.group_by{|o| o.script.to_string}
  result = groups.map{|k, v|
    btc_address = script_to_address(v[0].script)
    sorted_script_outputs = v.sort_by{|o|o.asset_id unless o.asset_id}
    group_assets = sorted_script_outputs.group_by{|o|o.asset_id}.select{|k,v| !k.nil?}
    assets = group_assets.map{|asset_id, outputs|
      {
          'asset_id' => asset_id,
          'quantity' => outputs.inject(0) { |sum, o| sum + o.asset_quantity }.to_s,
          'amount' => outputs.inject(0) { |sum, o| sum + o.asset_amount }.to_s,
          'asset_definition_url' => outputs[0].asset_definition_url
      }
    }
    {
        'address' => btc_address,
        'oa_address' => btc_address.nil? ? nil : address_to_oa_address(btc_address),
        'value' => satoshi_to_coin(v.inject(0) { |sum, o|sum +  o.value}),
        'assets' => assets,
        'account' => v[0].
    }
  }
  address.nil? ? result : result.select{|r|r['oa_address'] == address}
end

#get_color_outputs_from_tx(tx) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/openassets/api.rb', line 189

def get_color_outputs_from_tx(tx)
  unless tx.is_coinbase?
    tx.outputs.each_with_index { |out, i|
      marker_output_payload = OpenAssets::Protocol::MarkerOutput.parse_script(out.pk_script)
      unless marker_output_payload.nil?
        marker_output = OpenAssets::Protocol::MarkerOutput.deserialize_payload(marker_output_payload)
        inputs = tx.inputs.map {|input|
          get_output(input.previous_output, input.prev_out_index)
        }
        asset_ids = compute_asset_ids(inputs, i, tx.outputs, marker_output.asset_quantities)
        return asset_ids unless asset_ids.nil?
      end
    }
  end
  tx.outputs.map{|out| OpenAssets::Protocol::TransactionOutput.new(out.value, out.parsed_script, nil, 0, OpenAssets::Protocol::OutputType::UNCOLORED)}
end

#get_output(txid, output_index) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/openassets/api.rb', line 178

def get_output(txid, output_index)
  cached_output = @cache[txid + output_index.to_s]
  return cached_output if cached_output
  decode_tx = provider.get_transaction(txid, 0)
  raise OpenAssets::Transaction::TransactionBuildError, "txid #{txid} could not be retrieved." if decode_tx.nil?
  tx = Bitcoin::Protocol::Tx.new(decode_tx.htb)
  colored_outputs = get_color_outputs_from_tx(tx)
  colored_outputs.each_with_index { |o, index | @cache[txid + index.to_s] = o}
  colored_outputs[output_index]
end

#get_outputs_from_txid(txid) ⇒ Object

Get tx outputs. (use for debug) @param txid Transaction ID. @return Return array of the transaction output Hash with coloring information.



209
210
211
212
213
214
215
# File 'lib/openassets/api.rb', line 209

def get_outputs_from_txid(txid)
  decode_tx = provider.get_transaction(txid, 0)
  raise OpenAssets::Transaction::TransactionBuildError, "txid #{txid} could not be retrieved." if decode_tx.nil?
  tx = Bitcoin::Protocol::Tx.new(decode_tx.htb)
  outputs = get_color_outputs_from_tx(tx)
  outputs.map.with_index{|out, i|out.to_hash.merge({'txid' => tx.hash, 'vout' => i})}
end

#get_unspent_outputs(addresses) ⇒ Array[OpenAssets::Transaction::SpendableOutput]

Get unspent outputs.

Parameters:

  • addresses (Array) —

    The array of Bitcoin address.

Returns:



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/openassets/api.rb', line 164

def get_unspent_outputs(addresses)
  validate_address(addresses)
  unspent = provider.list_unspent(addresses, @config[:min_confirmation], @config[:max_confirmation])
  result = unspent.map{|item|
    output_result = get_output(item['txid'], item['vout'])
    output_result. = item['account']
    output = OpenAssets::Transaction::SpendableOutput.new(
      OpenAssets::Transaction::OutPoint.new(item['txid'], item['vout']), output_result)
    output.confirmations = item['confirmations']
    output
  }
  result
end

#is_testnet? ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/openassets/api.rb', line 36

def is_testnet?
  @config[:network] == 'testnet'
end

#issue_asset(from, amount, metadata = nil, to = nil, fees = nil, mode = 'broadcast', output_qty = 1) ⇒ Object

Creates a transaction for issuing an asset. @param from The open asset address to issue the asset from. @param amount The amount of asset units to issue. @param to The open asset address to send the asset to; if unspecified, the assets are sent back to the issuing address. @param metadata The metadata to embed in the transaction. The asset definition pointer defined by this metadata. @param fees The fess in satoshis for the transaction. @param mode Specify the following mode. 'broadcast' (default) for signing and broadcasting the transaction, 'signed' for signing the transaction without broadcasting, 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast' @param output_qty The number of divides the issue output. Default value is 1. Ex. amount = 125 and output_qty = 2, asset quantity = [62, 63] and issue TxOut is two. @return The Bitcoin::Protocol::Tx object.



93
94
95
96
97
98
99
100
101
# File 'lib/openassets/api.rb', line 93

def issue_asset(from, amount,  = nil, to = nil, fees = nil, mode = 'broadcast', output_qty = 1)
  to = from if to.nil?
  builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
  colored_outputs = get_unspent_outputs([oa_address_to_address(from)])
  issue_param = OpenAssets::Transaction::TransferParameters.new(colored_outputs, to, from, amount, output_qty)
  tx = builder.issue_asset(issue_param, , fees.nil? ? @config[:default_fees]: fees)
  tx = process_transaction(tx, mode)
  tx
end

#list_unspent(oa_address_list = []) ⇒ Array

get UTXO for colored coins.

Parameters:

  • oa_address_list (Array) (defaults to: []) —

    Obtain the balance of this open assets address only, or all addresses if unspecified.

Returns:

  • (Array) —

    Return array of the unspent information Hash.



43
44
45
46
47
48
# File 'lib/openassets/api.rb', line 43

def list_unspent(oa_address_list = [])
  btc_address_list = oa_address_list.map { |oa_address| oa_address_to_address(oa_address)}
  outputs = get_unspent_outputs(btc_address_list)
  result = outputs.map{|out| out.to_hash}
  result
end

#send_asset(from, asset_id, amount, to, fees = nil, mode = 'broadcast', output_qty = 1) ⇒ Object

Creates a transaction for sending an asset from an address to another. @param from The open asset address to send the asset from. @param asset_id The asset ID identifying the asset to send. @param amount The amount of asset units to send. @param to The open asset address to send the asset to. @param fees The fess in satoshis for the transaction. @param mode 'broadcast' (default) for signing and broadcasting the transaction, 'signed' for signing the transaction without broadcasting, 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast' @return The resulting transaction.



113
114
115
116
117
118
119
120
# File 'lib/openassets/api.rb', line 113

def send_asset(from, asset_id, amount, to, fees = nil, mode = 'broadcast', output_qty = 1)
  builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
  colored_outputs = get_unspent_outputs([oa_address_to_address(from)])
  asset_transfer_spec = OpenAssets::Transaction::TransferParameters.new(colored_outputs, to, from, amount, output_qty)
  tx = builder.transfer_asset(asset_id, asset_transfer_spec, from, fees.nil? ? @config[:default_fees]: fees)
  tx = process_transaction(tx, mode)
  tx
end

#send_assets(from, send_asset_params, fees = nil, mode = 'broadcast') ⇒ Object

Creates a transaction for sending multiple asset from an address to another. @param from The open asset address to send the asset from. @param[Array[OpenAssets::SendAssetParams]] send_asset_params The send Asset information(asset_id, amount, to). @param fees The fess in satoshis for the transaction. @param mode 'broadcast' (default) for signing and broadcasting the transaction, 'signed' for signing the transaction without broadcasting, 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast' @return The resulting transaction.



130
131
132
133
134
135
136
137
138
139
# File 'lib/openassets/api.rb', line 130

def send_assets(from, send_asset_params, fees = nil, mode = 'broadcast')
  builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
  colored_outputs = get_unspent_outputs([oa_address_to_address(from)])
  transfer_specs = send_asset_params.map{|param|
    [param.asset_id, OpenAssets::Transaction::TransferParameters.new(colored_outputs, param.to, from, param.amount)]
  }
  tx = builder.transfer_assets(transfer_specs, from, fees.nil? ? @config[:default_fees]: fees)
  tx = process_transaction(tx, mode)
  tx
end

#send_bitcoin(from, amount, to, fees = nil, mode = 'broadcast', output_qty = 1) ⇒ Object

Creates a transaction for sending bitcoins from an address to another. @param from The address to send the bitcoins from. @param amount The amount of satoshis to send. @param to The address to send the bitcoins to. @param fees The fess in satoshis for the transaction. @param mode 'broadcast' (default) for signing and broadcasting the transaction, 'signed' for signing the transaction without broadcasting, 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast' Ex. amount = 125 and output_qty = 2, asset quantity = [62, 63] and issue TxOut is two. @return The resulting transaction.

Parameters:

  • output_qty (Integer) (defaults to: 1) —

    The number of divides the issue output. Default value is 1.



152
153
154
155
156
157
158
159
# File 'lib/openassets/api.rb', line 152

def send_bitcoin(from, amount, to, fees = nil, mode = 'broadcast', output_qty = 1)
  validate_address([from, to])
  builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
  colored_outputs = get_unspent_outputs([from])
  btc_transfer_spec = OpenAssets::Transaction::TransferParameters.new(colored_outputs, to, from, amount, output_qty)
  tx = builder.transfer_btc(btc_transfer_spec, fees.nil? ? @config[:default_fees]: fees)
  process_transaction(tx, mode)
end