Class: Counterparty::CounterResource

Inherits:
Object
  • Object
show all
Defined in:
lib/counterparty/resource.rb

Overview

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ CounterResource

Returns a new instance of CounterResource.


29
30
31
32
# 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

Class Attribute Details

.connectionObject

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


122
123
124
# File 'lib/counterparty/resource.rb', line 122

def connection
  @connection || Counterparty.connection
end

Instance Attribute Details

#allow_unconfirmed_inputsObject

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


17
18
19
# File 'lib/counterparty/resource.rb', line 17

def allow_unconfirmed_inputs
  @allow_unconfirmed_inputs
end

#encodingObject

encoding (string): The encoding method to use


9
10
11
# File 'lib/counterparty/resource.rb', line 9

def encoding
  @encoding
end

#feeObject

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.


22
23
24
# File 'lib/counterparty/resource.rb', line 22

def fee
  @fee
end

#fee_per_kbObject

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.


27
28
29
# File 'lib/counterparty/resource.rb', line 27

def fee_per_kb
  @fee_per_kb
end

#pubkeyObject

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


13
14
15
# File 'lib/counterparty/resource.rb', line 13

def pubkey
  @pubkey
end

#result_attributesObject

This is mostly used by the eq operation and indicates the attributes that this resource has


6
7
8
# File 'lib/counterparty/resource.rb', line 6

def result_attributes
  @result_attributes
end

Class Method Details

.api_nameObject

Returns the counterparty-api version of this objects class name


116
117
118
# File 'lib/counterparty/resource.rb', line 116

def api_name
  to_s.split('::').last.gsub(/[^\A]([A-Z])/, '_\\1').downcase
end

.bitcoinObject

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


128
129
130
# File 'lib/counterparty/resource.rb', line 128

def bitcoin
  @bitcoin || Counterparty.bitcoin
end

.find(params) ⇒ Object

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


144
145
146
# File 'lib/counterparty/resource.rb', line 144

def find(params)
  connection.request(to_get_request, params).collect{|r| new r}
end

.to_create_requestObject

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


133
134
135
# File 'lib/counterparty/resource.rb', line 133

def to_create_request
  'create_%s' % api_name
end

.to_get_requestObject

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


138
139
140
# File 'lib/counterparty/resource.rb', line 138

def to_get_request
  'get_%ss' % api_name
end

Instance Method Details

#==(b) ⇒ Object

Just a simple compare. No need to get crazy


35
36
37
38
39
# File 'lib/counterparty/resource.rb', line 35

def ==(b) # :nodoc:
  ( b.respond_to?(:result_attributes) &&
    result_attributes == b.result_attributes && 
    @result_attributes.all?{ |k| send(k) == b.send(k) } )
end

#save!(private_key) ⇒ Object

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.


57
58
59
# File 'lib/counterparty/resource.rb', line 57

def save!(private_key)
  bitcoin.sendrawtransaction to_signed_tx(private_key)
end

#to_raw_txObject

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).


44
45
46
# 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) ⇒ Object

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


50
51
52
# File 'lib/counterparty/resource.rb', line 50

def to_signed_tx(private_key)
  sign_tx to_raw_tx, private_key
end