Module: BlockIo

Defined in:
lib/block_io.rb,
lib/block_io/version.rb

Defined Under Namespace

Modules: Helper Classes: Key

Constant Summary collapse

VERSION =
"1.0.3"

Class Method Summary collapse

Class Method Details

.method_missing(m, *args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/block_io.rb', line 33

def self.method_missing(m, *args, &block)      

  method_name = m.to_s

  if ['withdraw', 'withdraw_from_address', 'withdraw_from_addresses', 'withdraw_from_user', 'withdraw_from_users', 'withdraw_from_label', 'withdraw_from_labels'].include?(m.to_s) then

    self.withdraw(args.first, m.to_s)

  else
    params = get_params(args.first)
    self.api_call([method_name, params])
  end
  
end

.set_options(args = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/block_io.rb', line 20

def self.set_options(args = {})
  # initialize BlockIo
  @api_key = args[:api_key]
  @pin = args[:pin]
  @encryptionKey = Helper.pinToAesKey(@pin) if !@pin.nil?

  @conn_pool = ConnectionPool.new(size: 5, timeout: 300) { HTTPClient.new }
  
  @version = args[:version] || 2 # default version is 2
  
  self.api_call(['get_balance',""])
end

.withdraw(args = {}, method_name = 'withdraw') ⇒ Object

Raises:

  • (Exception)


48
49
50
51
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/block_io.rb', line 48

def self.withdraw(args = {}, method_name = 'withdraw')
  # validate arguments for withdrawal of funds TODO

  raise Exception.new("PIN not set. Use BlockIo.set_options(:api_key=>'API KEY',:pin=>'SECRET PIN',:version=>'API VERSION')") if @pin.nil?

  params = get_params(args)

  params += "&pin=#{@pin}" if @version == 1 # Block.io handles the Secret PIN in the legacy API (v1)

  response = self.api_call([method_name, params])
  
  if response['data'].has_key?('reference_id') then
    # Block.io's asking us to provide some client-side signatures, let's get to it

    # extract the passphrase
    encrypted_passphrase = response['data']['encrypted_passphrase']['passphrase']

    # let's get our private key
    key = Helper.extractKey(encrypted_passphrase, @encryptionKey)

    raise Exception.new('Public key mismatch for requested signer and ourselves. Invalid Secret PIN detected.') if key.public_key != response['data']['encrypted_passphrase']['signer_public_key']

    # let's sign all the inputs we can
    inputs = response['data']['inputs']

    inputs.each do |input|
      # iterate over all signers
      
      input['signers'].each do |signer|
        # if our public key matches this signer's public key, sign the data

        signer['signed_data'] = key.sign(input['data_to_sign']) if signer['signer_public_key'] == key.public_key

      end
      
    end

    # the response object is now signed, let's stringify it and finalize this withdrawal

    response = self.api_call(['sign_and_finalize_withdrawal',{:signature_data => response['data'].to_json}])

    # if we provided all the required signatures, this transaction went through
    # otherwise Block.io responded with data asking for more signatures
    # the latter will be the case for dTrust addresses
  end

  return response

end