class Counterparty::CounterResource

A base class for the purpose of extending by api result hashes

Attributes

connection[W]

The base connection object for this class

allow_unconfirmed_inputs[RW]

allow_unconfirmed_inputs (boolean): Set to true to allow this transaction to utilize unconfirmed UTXOs as inputs.

encoding[RW]

encoding (string): The encoding method to use

fee[RW]

fee (integer): If you’d like to specify a custom miners’ fee, specify it here (in satoshi). Leave as default for counterpartyd to automatically choose.

fee_per_kb[RW]

fee_per_kb (integer): The fee per kilobyte of transaction data constant that counterpartyd uses when deciding on the dynamic fee to use (in satoshi). Leave as default unless you know what you’re doing.

pubkey[RW]

pubkey (string): The pubkey hex string. Required if multisig transaction encoding is specified for a key external to counterpartyd’s local wallet.

Public Class Methods

new(attrs={}) click to toggle source
# File lib/counterparty/resource.rb, line 29
def initialize(attrs={})
  @result_attributes = attrs.keys.sort.collect(&:to_sym)
  attrs.each{|k,v| instance_variable_set '@%s' % k, v}
end

Private Class Methods

api_name() click to toggle source

Returns the counterparty-api version of this objects class name

# File lib/counterparty/resource.rb, line 116
def api_name
  to_s.split('::').last.gsub(/[^\A]([A-Z])/, '_\\1').downcase
end
bitcoin() click to toggle source

Returns the currently assigned connection object, or if one hasn’t been set, the default specified in the Counterparty module

# File lib/counterparty/resource.rb, line 128
def bitcoin
  @bitcoin || Counterparty.bitcoin
end
connection() click to toggle source

Returns the currently assigned connection object, or if one hasn’t been set, the default specified in the Counterparty module

# File lib/counterparty/resource.rb, line 122
def connection
  @connection || Counterparty.connection
end
find(params) click to toggle source

Queries the counterpartyd connection to find matching instances of this resource, given the filters provided in the params

# File lib/counterparty/resource.rb, line 144
def find(params)
  connection.request(to_get_request, params).collect{|r| new r}
end
to_create_request() click to toggle source

Returns the method name of a create_* request for this resource

# File lib/counterparty/resource.rb, line 133
def to_create_request
  'create_%s' % api_name
end
to_get_request() click to toggle source

Returns the method name of a get_* request for this resource

# File lib/counterparty/resource.rb, line 138
def to_get_request
  'get_%ss' % api_name
end

Public Instance Methods

save!(private_key) click to toggle source

Commit this object to the blockchain. If a private key is passed, the transaction is signed using this key via a create_ call and a subsequent sign_tx call.

# File lib/counterparty/resource.rb, line 57
def save!(private_key)
  bitcoin.sendrawtransaction to_signed_tx(private_key)
end
to_raw_tx() click to toggle source

This method returns the unsigned raw create transaction string. hex encoded (i.e. the same format that bitcoind returns with its raw transaction API calls).

# File lib/counterparty/resource.rb, line 44
def to_raw_tx
  connection.request self.class.to_create_request, to_params
end
to_signed_tx(private_key) click to toggle source

Given the provided private key, this method returns a signed transaction suitable for broadcasting on the network.

# File lib/counterparty/resource.rb, line 50
def to_signed_tx(private_key)
  sign_tx to_raw_tx, private_key
end

Private Instance Methods

bitcoin() click to toggle source
# File lib/counterparty/resource.rb, line 95
def bitcoin
  self.class.bitcoin
end
connection() click to toggle source
# File lib/counterparty/resource.rb, line 99
def connection
  self.class.connection
end
sign_tx(raw_tx, pkey_wif) click to toggle source

Currently this is communicating the request to the backend. This method is a stub for when we decide in the future to Use the bitcoin-client gem to perform signatures

# File lib/counterparty/resource.rb, line 66
def sign_tx(raw_tx, pkey_wif)
  # Seems like this is your quintessential reference:
  # http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html

  # I think this is the right way to do it...
  Bitcoin.network = (bitcoin.is_testing?) ? :testnet3 : :bitcoin

  # This parses the binary-encoded raw transaction:
  tx = Bitcoin::P::Tx.new [raw_tx].pack('H*')

  # This is the input transaction, which we'll need for signining:
  prev_hash = tx.in[0].prev_out.reverse_hth

  # let's parse the keys:
  key = Bitcoin::Key.from_base58 pkey_wif

  pubkey = [key.pub].pack('H*')

  # And parse the input transaction:
  prev_tx = Bitcoin::P::Tx.new [bitcoin.getrawtransaction(prev_hash)].pack('H*')

  # And, now we're ready to sign:
  subscript = tx.signature_hash_for_input 0, prev_tx
  sig = Bitcoin.sign_data Bitcoin.open_key(key.priv), subscript
  tx.in[0].script_sig = Bitcoin::Script.to_signature_pubkey_script sig, pubkey 

  tx.to_payload.unpack('H*')[0]
end
to_params() click to toggle source

This serializes self into a hash suitable for transmission via json

# File lib/counterparty/resource.rb, line 104
def to_params
  Hash[* @result_attributes.collect{|k| 
    v = self.send(k)
    (v) ? [k,self.send(k)] : nil
  }.compact.flatten]
end