block_io 1.0.8 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +5 -5
  2. data/.appveyor.yml-disabled +26 -0
  3. data/.gitignore +5 -1
  4. data/.rspec +1 -0
  5. data/.travis.yml +14 -0
  6. data/LICENSE +1 -1
  7. data/README.md +23 -13
  8. data/block_io.gemspec +9 -9
  9. data/examples/basic.rb +38 -10
  10. data/examples/dtrust.rb +60 -42
  11. data/examples/proxy.rb +36 -0
  12. data/examples/sweeper.rb +24 -14
  13. data/lib/block_io.rb +16 -408
  14. data/lib/block_io/api_exception.rb +11 -0
  15. data/lib/block_io/chainparams/BTC.yml +8 -0
  16. data/lib/block_io/chainparams/BTCTEST.yml +8 -0
  17. data/lib/block_io/chainparams/DOGE.yml +8 -0
  18. data/lib/block_io/chainparams/DOGETEST.yml +8 -0
  19. data/lib/block_io/chainparams/LTC.yml +8 -0
  20. data/lib/block_io/chainparams/LTCTEST.yml +8 -0
  21. data/lib/block_io/client.rb +244 -0
  22. data/lib/block_io/extended_bitcoinrb.rb +127 -0
  23. data/lib/block_io/helper.rb +322 -0
  24. data/lib/block_io/key.rb +38 -0
  25. data/lib/block_io/version.rb +1 -1
  26. data/spec/client_misc_spec.rb +76 -0
  27. data/spec/client_spec.rb +68 -0
  28. data/spec/dtrust_spec.rb +167 -0
  29. data/spec/helper_spec.rb +154 -0
  30. data/spec/key_spec.rb +92 -0
  31. data/spec/larger_transaction_spec.rb +351 -0
  32. data/spec/spec_helper.rb +5 -0
  33. data/spec/sweep_spec.rb +115 -0
  34. data/spec/test-cases/.gitignore +2 -0
  35. data/spec/test-cases/LICENSE +21 -0
  36. data/spec/test-cases/README.md +2 -0
  37. data/spec/test-cases/json/create_and_sign_transaction_response.json +61 -0
  38. data/spec/test-cases/json/create_and_sign_transaction_response_P2WSH-over-P2SH_1of2_251inputs.json +1261 -0
  39. data/spec/test-cases/json/create_and_sign_transaction_response_P2WSH-over-P2SH_1of2_252inputs.json +1266 -0
  40. data/spec/test-cases/json/create_and_sign_transaction_response_P2WSH-over-P2SH_1of2_253inputs.json +1271 -0
  41. data/spec/test-cases/json/create_and_sign_transaction_response_P2WSH-over-P2SH_1of2_762inputs.json +3816 -0
  42. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2SH_3of5_195inputs.json +2931 -0
  43. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2SH_4of5_195inputs.json +5 -0
  44. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_3of5_251inputs.json +3771 -0
  45. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_3of5_252inputs.json +3786 -0
  46. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_3of5_253inputs.json +3801 -0
  47. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_4of5_251inputs.json +5 -0
  48. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_4of5_252inputs.json +5 -0
  49. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_4of5_253inputs.json +5 -0
  50. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_3of5_251inputs.json +3771 -0
  51. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_3of5_252inputs.json +3786 -0
  52. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_3of5_253inputs.json +3801 -0
  53. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_4of5_251inputs.json +5 -0
  54. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_4of5_252inputs.json +5 -0
  55. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_4of5_253inputs.json +5 -0
  56. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2sh_3_of_5_keys.json +21 -0
  57. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2sh_4_of_5_keys.json +5 -0
  58. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_3_of_5_keys.json +21 -0
  59. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_4_of_5_keys.json +5 -0
  60. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_3_of_5_keys.json +36 -0
  61. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_3of5_251outputs.json +591 -0
  62. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_3of5_252outputs.json +576 -0
  63. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_3of5_253outputs.json +531 -0
  64. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_4_of_5_keys.json +5 -0
  65. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_4of5_251outputs.json +5 -0
  66. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_4of5_252outputs.json +5 -0
  67. data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_4of5_253outputs.json +5 -0
  68. data/spec/test-cases/json/create_and_sign_transaction_response_sweep_p2pkh.json +5 -0
  69. data/spec/test-cases/json/create_and_sign_transaction_response_sweep_p2wpkh.json +5 -0
  70. data/spec/test-cases/json/create_and_sign_transaction_response_sweep_p2wpkh_over_p2sh.json +5 -0
  71. data/spec/test-cases/json/create_and_sign_transaction_response_with_blockio_fee_and_expected_unsigned_txid.json +21 -0
  72. data/spec/test-cases/json/get_balance_response.json +8 -0
  73. data/spec/test-cases/json/prepare_dtrust_transaction_response_P2SH_3of5_195inputs.json +1397 -0
  74. data/spec/test-cases/json/prepare_dtrust_transaction_response_P2SH_4of5_195inputs.json +1397 -0
  75. data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_3of5_251inputs.json +1795 -0
  76. data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_3of5_252inputs.json +1802 -0
  77. data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_3of5_253inputs.json +1809 -0
  78. data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_4of5_251inputs.json +1789 -0
  79. data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_4of5_252inputs.json +1802 -0
  80. data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_4of5_253inputs.json +1809 -0
  81. data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_3of5_251inputs.json +1795 -0
  82. data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_3of5_252inputs.json +1802 -0
  83. data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_3of5_253inputs.json +1809 -0
  84. data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_4of5_251inputs.json +1795 -0
  85. data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_4of5_252inputs.json +1802 -0
  86. data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_4of5_253inputs.json +1809 -0
  87. data/spec/test-cases/json/prepare_dtrust_transaction_response_p2sh.json +45 -0
  88. data/spec/test-cases/json/prepare_dtrust_transaction_response_p2wsh_over_p2sh.json +45 -0
  89. data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0.json +52 -0
  90. data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_3of5_251outputs.json +1805 -0
  91. data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_3of5_252outputs.json +1804 -0
  92. data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_3of5_253outputs.json +1789 -0
  93. data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_4of5_251outputs.json +1805 -0
  94. data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_4of5_252outputs.json +1804 -0
  95. data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_4of5_253outputs.json +1789 -0
  96. data/spec/test-cases/json/prepare_sweep_transaction_response_p2pkh.json +35 -0
  97. data/spec/test-cases/json/prepare_sweep_transaction_response_p2wpkh.json +35 -0
  98. data/spec/test-cases/json/prepare_sweep_transaction_response_p2wpkh_over_p2sh.json +35 -0
  99. data/spec/test-cases/json/prepare_transaction_response.json +164 -0
  100. data/spec/test-cases/json/prepare_transaction_response_P2WSH-over-P2SH_1of2_251inputs.json +1796 -0
  101. data/spec/test-cases/json/prepare_transaction_response_P2WSH-over-P2SH_1of2_252inputs.json +1803 -0
  102. data/spec/test-cases/json/prepare_transaction_response_P2WSH-over-P2SH_1of2_253inputs.json +1810 -0
  103. data/spec/test-cases/json/prepare_transaction_response_P2WSH-over-P2SH_1of2_762inputs.json +5367 -0
  104. data/spec/test-cases/json/prepare_transaction_response_with_blockio_fee_and_expected_unsigned_txid.json +76 -0
  105. data/spec/test-cases/json/summarize_prepared_transaction_response_with_blockio_fee_and_expected_unsigned_txid.json +6 -0
  106. metadata +264 -73
  107. data/examples/change.rb +0 -117
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f9dc992c3a7aa1fc125e1eaf8b16d531318e31fd
4
- data.tar.gz: fdf9ae723127d8688e55cdc0c1ffc9f0eb263bda
2
+ SHA256:
3
+ metadata.gz: 42de3243361e4c2a75e0357745ac507c381332d927453d6b35f81b63f30ea723
4
+ data.tar.gz: f2416231c346fe92b0f39b665390be3c511aab2b62d9c8c8e2f07c580c12e30e
5
5
  SHA512:
6
- metadata.gz: 2ed068c30ce271df8745e66288f050b97f5f3ec3f68f3087c88ed3b7068059e29503edc1043521bb55d56f80dfbb57491ce709033e36ef021e850775ee94501f
7
- data.tar.gz: e9c4c14b3c2bbd292a20f8a018bd9089b20d5742115b0ebc9d6c2a707e4fac3583e1dc620fb4a580c5ab284b45a4ce3bc911dbd42ba6853930e93524d0e5e412
6
+ metadata.gz: bdd5177bade408ffcb526b09d62a177ebee4d6cef0352267c1d1578c1223bd598f7900012bfa27c0ae1cb71e9202f862180ecad91ea70b4b077a44f4d06fe080
7
+ data.tar.gz: ff6f0db6e6bc5b485de84487d805970317fa4f936d015d4fce994302a9e48fd3e37dd076ffb0da8a8fb41e1f988040397905d6b389ca7995ed0e6b88b23544bd
@@ -0,0 +1,26 @@
1
+ version: '{build}'
2
+
3
+ skip_tags: true
4
+
5
+ environment:
6
+ matrix:
7
+ - ruby_version: "24"
8
+ - ruby_version: "24-x64"
9
+ - ruby_version: "25"
10
+ - ruby_version: "25-x64"
11
+ - ruby_version: "26"
12
+ - ruby_version: "26-x64"
13
+ - ruby_version: "27"
14
+ - ruby_version: "27-x64"
15
+ - ruby_version: "30"
16
+ - ruby_version: "30-x64"
17
+
18
+ install:
19
+ - SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
20
+ - gem install bundler --no-document -v 2.1.4
21
+ - bundle install --retry=3
22
+
23
+ test_script:
24
+ - bundle exec rspec
25
+
26
+ build: off
data/.gitignore CHANGED
@@ -1,2 +1,6 @@
1
1
  Gemfile.lock
2
- pkg
2
+ vendor/
3
+ .bundle/
4
+ *.gem
5
+ *~
6
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ os:
3
+ - linux
4
+ rvm:
5
+ - 2.4
6
+ - 2.5
7
+ - 2.6
8
+ - 2.7
9
+ - 3.0
10
+ bundler_args: --jobs=2
11
+ script:
12
+ - bundle exec rspec
13
+ cache: bundler
14
+
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2014 BlockIo
3
+ Copyright (c) 2021 BlockIo
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # BlockIo
2
2
 
3
- This Ruby Gem is the official reference client for the Block.io payments API. To use this, you will need the Dogecoin, Bitcoin, or Litecoin API key(s) from <a href="https://block.io" target="_blank">Block.io</a>. Go ahead, sign up :)
3
+ This Ruby Gem is the official reference client for the Block.io's infrastructure APIs. To use this, you will need the Dogecoin, Bitcoin, or Litecoin API key(s) from <a href="https://block.io" target="_blank">Block.io</a>. Go ahead, sign up :)
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,33 +10,43 @@ Add this line to your application's Gemfile:
10
10
 
11
11
  And then execute:
12
12
 
13
- $ bundle
13
+ $ bundle install
14
14
 
15
15
  Or install it yourself as:
16
16
 
17
- $ gem install block_io -v=1.0.6
17
+ $ gem install block_io
18
18
 
19
19
  ## Changelog
20
+ *06/09/21*: Version 3.0.1 implements use of dynamic decryption algorithms.
21
+ *04/14/21*: BREAKING CHANGES. Version 3.0.0. Remove support for Ruby < 2.4.0, and Windows. Behavior and interfaces have changed. By upgrading you'll need to revise your code and tests.
20
22
 
21
- *01/21/15*: Added ability to sweep coins from one address to another.
22
- *11/04/14*: Fix issue with nil parameters in an API call.
23
- *11/03/14*: Reduce dependence on OpenSSL. PBKDF2 function is now Ruby-based. Should work well with Heroku's libraries.
24
- *10/18/14*: Now using deterministic signatures (RFC6979), and BIP62 to hinder transaction malleability.
25
-
23
+ ## Important Notes
24
+ * This gem depends on the bitcoinrb gem. By using this gem, your application will load the bitcoinrb gem as well with the Bitcoin namespace. It may conflict with another gem using the same namespace.
25
+ * Transaction endpoints are updated as of v3.0.0.
26
+ * See the examples/ folder for basic examples.
27
+ * Be careful to test thoroughly before production.
28
+ * Use of this software is subject to its LICENSE.
26
29
 
27
30
  ## Usage
28
31
 
29
32
  It's super easy to get started. In your Ruby shell ($ irb), for example, do this:
30
33
 
31
34
  require 'block_io'
32
- BlockIo.set_options :api_key => 'API KEY', :pin => 'SECRET PIN', :version => 2
33
-
35
+ blockio = BlockIo::Client.new(:api_key => "API KEY", :pin => "SECRET PIN")
36
+
37
+ If you do not have your PIN, or just wish to use your private key backup(s) directly, do this instead:
38
+
39
+ blockio = BlockIo::Client.new(:api_key => "API KEY")
40
+ blockio.get_balance
41
+ blockio.prepare_transaction(..., :keys => [BlockIo::Key.from_wif("PRIVATE_KEY_BACKUP_IN_WIF").private_key_hex])
42
+
34
43
  And you're good to go:
35
44
 
36
- BlockIo.get_new_address
37
- BlockIo.get_my_addresses
45
+ blockio.get_new_address
46
+ blockio.get_my_addresses
38
47
 
39
- For more information, see https://block.io/api/simple/ruby
48
+ For other initialization options/parameters, see `lib/block_io/client.rb`.
49
+ For more information, see https://block.io/api/simple/ruby.
40
50
 
41
51
  ## Contributing
42
52
 
data/block_io.gemspec CHANGED
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'block_io/version'
@@ -16,14 +15,15 @@ Gem::Specification.new do |spec|
16
15
  spec.files = `git ls-files -z`.split("\x0")
17
16
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.required_ruby_version = '>= 2.4.0'
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake", "~> 0"
23
- spec.add_runtime_dependency "ecdsa", "~> 1.2", '>= 1.2.0'
24
- spec.add_runtime_dependency "httpclient", "~> 2.8", '>= 2.8.0'
25
- spec.add_runtime_dependency "oj", "~> 3.3", '>= 3.3.5'
26
- spec.add_runtime_dependency "oj_mimic_json"
27
- spec.add_runtime_dependency "connection_pool", "~> 2.2", '>= 2.2.1'
28
- spec.add_runtime_dependency "pbkdf2-ruby", '~> 0.2', '>= 0.2.1'
21
+ spec.add_development_dependency "bundler", ">= 1.16", "< 3.0"
22
+ spec.add_development_dependency "rake", "~> 13.0", ">= 13.0"
23
+ spec.add_development_dependency "rspec", "~> 3.6", ">= 3.6"
24
+ spec.add_development_dependency "webmock", "~> 3.12", "< 4.0"
25
+ spec.add_runtime_dependency "bitcoinrb", "~> 0.7.0", "= 0.7.0"
26
+ spec.add_runtime_dependency "http", "~> 4.4.1", ">= 4.4.1"
27
+ spec.add_runtime_dependency "oj", "~> 3.11", ">= 3.11"
28
+ spec.add_runtime_dependency "connection_pool", "~> 2.2.5", ">= 2.2"
29
29
  end
data/examples/basic.rb CHANGED
@@ -1,22 +1,50 @@
1
- # creates a new destination address, withdraws from the default label to it, gets sent transactions, and the current price
1
+ # creates a new destination address, withdraws from the default label to it, gets updated address balances, gets sent/received transactions, and the current price
2
+ #
3
+ # basic example: $ API_KEY=TESTNET_API_KEY PIN=YOUR_SECRET_PIN ruby basic.rb
4
+ # bundler example: $ API_KEY=TESTNET_API_KEY PIN=YOUR_SECRET_PIN bundle exec ruby basic.rb
5
+ #
6
+ # adjust amount below if not using the Litecoin Testnet
2
7
 
3
8
  require 'block_io'
4
9
 
5
- # please use the Dogecoin Testnet API key here
6
- puts BlockIo.set_options :api_key => 'YOURDOGECOINTESTNETAPIKEY', :pin => 'YOURSECRETPIN', :version => 2
10
+ blockio = BlockIo::Client.new(:api_key => ENV['API_KEY'], :pin => ENV['PIN'])
11
+ puts blockio.get_balance
12
+ puts blockio.network
7
13
 
14
+ # create the address if it doesn't exist
8
15
  begin
9
- puts BlockIo.get_new_address(:label => 'testDest')
10
- rescue Exception => e
11
- # if this failed, we probably created testDest label before
16
+ puts blockio.get_new_address(:label => 'testDest')
17
+ rescue BlockIo::APIException => e
12
18
  puts e.to_s
13
19
  end
20
+ puts " -- "
14
21
 
15
- puts BlockIo.withdraw_from_labels(:from_labels => 'default', :to_label => 'testDest', :amount => '3.5')
22
+ # retrieve unspent outputs and other relevant data to create and sign the transaction
23
+ # you will inspect the prepared transaction for things like network fees being paid, block.io fees being paid, validating what destination addresses receive how much, etc.
24
+ prepared_transaction = blockio.prepare_transaction(:to_label => 'testDest', :amount => '0.012345')
25
+ puts JSON.pretty_generate(prepared_transaction)
26
+ puts " -- "
16
27
 
17
- puts BlockIo.get_address_by_label(:label => 'default')
28
+ # a summary of what's in the prepared transaction
29
+ # for in-depth review of the transaction, look at the prepared_transaction object yourself
30
+ puts JSON.pretty_generate(blockio.summarize_prepared_transaction(prepared_transaction))
31
+ puts " -- "
18
32
 
19
- puts BlockIo.get_transactions(:type => 'sent') # API v2 only
33
+ # once satisfied with the prepared transaction, create and sign it
34
+ # inspect this again if you wish, it will contain the transaction payload in hexadecimal form you want Block.io to sign and broadcast
35
+ transaction_data = blockio.create_and_sign_transaction(prepared_transaction)
36
+ puts JSON.pretty_generate(transaction_data)
37
+ puts " -- "
20
38
 
21
- puts BlockIo.get_current_price(:base_price => 'BTC')
39
+ # ask Block.io to sign and broadcast the transaction
40
+ puts JSON.pretty_generate(blockio.submit_transaction(:transaction_data => transaction_data))
41
+ puts " -- "
42
+
43
+ puts blockio.get_address_balance(:labels => 'default,testDest')
44
+
45
+ puts blockio.get_transactions(:type => 'sent')
46
+
47
+ puts blockio.get_transactions(:type => 'received')
48
+
49
+ puts blockio.get_current_price(:base_price => 'BTC')
22
50
 
data/examples/dtrust.rb CHANGED
@@ -1,31 +1,51 @@
1
- # creates a new destination address, withdraws from the default label to it, gets sent transactions, and the current price
1
+ # creates a new dTrust destination address, sends coins to it, withdraws coins from it, gets sent transactions, and the current price
2
2
 
3
3
  require 'block_io'
4
+ require 'json'
4
5
 
5
- # please use the Dogecoin Testnet API key here
6
+ # please use the Litecoin Testnet API key here
6
7
  puts "*** Initialize BlockIo library: "
7
- puts JSON.pretty_generate(BlockIo.set_options :api_key => 'YourDogecoinTestnetAPIKey', :pin => 'YourSecretPIN', :version => 2)
8
+ blockio = BlockIo::Client.new(:api_key => ENV['API_KEY'], :pin => ENV['PIN'])
8
9
 
10
+ puts blockio.get_dtrust_balance
11
+ puts blockio.network
12
+
13
+ raise "Please use the LTCTEST network API Key here or modify this script for another network." unless blockio.network == "LTCTEST"
9
14
 
10
15
  # create 4 keys
11
- keys = [ BlockIo::Key.from_passphrase('alpha1alpha2alpha3alpha4'), BlockIo::Key.from_passphrase('alpha4alpha1alpha2alpha3'), BlockIo::Key.from_passphrase('alpha3alpha4alpha1alpha2'), BlockIo::Key.from_passphrase('alpha2alpha3alpha4alpha1') ]
16
+ # you will generate your own private keys, for instance: key = BlockIo::Key.generate.
17
+ # you will need to record key.to_wif (private key in Wallet Import Format (WIF)) somewhere safe before you use your keys to generate dTrust addresses.
18
+ # if you already have hex private keys, load the keys (see below). Ensure the key's public_key (key.public_key_hex) matches what you expect.
19
+ # WARNING: The keys below are just for demonstration, DO NOT use them on mainnets, DO NOT use insecurely generated keys
20
+ # WARNING: You must ALWAYS use compressed public keys. Use of uncompressed public keys can lead to lost coins when using SegWit addresses.
21
+
22
+ # these keys will use the appropriate coin's parameters. The library will know what network you're interacting with once you make a successful API call first, like blockio.get_dtrust_balance above
23
+
24
+ keys = [
25
+ "b515fd806a662e061b488e78e5d0c2ff46df80083a79818e166300666385c0a2", # alpha1alpha2alpha3alpha4
26
+ "1584b821c62ecdc554e185222591720d6fe651ed1b820d83f92cdc45c5e21f", # alpha2alpha3alpha4alpha1
27
+ "2f9090b8aa4ddb32c3b0b8371db1b50e19084c720c30db1d6bb9fcd3a0f78e61", # alpha3alpha4alpha1alpha2
28
+ "6c1cefdfd9187b36b36c3698c1362642083dcc1941dc76d751481d3aa29ca65" # alpha4alpha1alpha2alpha3
29
+ ].freeze
12
30
 
13
31
  dtrust_address = nil
32
+ dtrust_address_label = "dTrust1_witness_v0"
14
33
 
15
34
  begin
16
35
  # let's create a new address with all 4 keys as signers, but only 3 signers required (i.e., 4 of 5 multisig, with 1 signature being Block.io)
36
+ # you will need all 4 of your keys to use your address without interacting with Block.io
17
37
 
18
- signers = ""
19
- keys.each { |key| signers += ',' if signers.length > 0; signers += key.public_key; }
38
+ signers = keys.map{|x| BlockIo::Key.from_private_key_hex(x)}.map(&:public_key_hex).join(',')
20
39
 
21
- response = BlockIo.get_new_dtrust_address(:label => 'dTrust1', :public_keys => signers, :required_signatures => 3)
40
+ response = blockio.get_new_dtrust_address(:label => dtrust_address_label, :public_keys => signers, :required_signatures => 3, :address_type => "witness_v0")
22
41
 
23
42
  dtrust_address = response['data']['address']
24
- rescue Exception => e
43
+
44
+ rescue BlockIo::APIException => e
25
45
  # if this failed, we probably created the same label before. let's fetch the address then.
26
46
  puts e.to_s
27
47
 
28
- response = BlockIo.get_dtrust_address_by_label(:label => 'dTrust1')
48
+ response = blockio.get_dtrust_address_by_label(:label => dtrust_address_label)
29
49
 
30
50
  dtrust_address = response['data']['address']
31
51
  end
@@ -33,57 +53,55 @@ end
33
53
  puts "*** Our dTrust Address: #{dtrust_address}"
34
54
 
35
55
  # let's deposit some coins into this new address
36
- response = BlockIo.withdraw_from_labels(:from_labels => 'default', :to_address => dtrust_address, :amount => '3.5')
37
56
 
38
- puts "*** Withdrawal response:"
39
- puts JSON.pretty_generate(response)
57
+ # blockio.prepare_transaction gets the appropriate data you need to create and sign your transaction. You will need to inspect it to ensure things are as expected yourself.
58
+ prepared_transaction = blockio.prepare_transaction(:to_address => dtrust_address, :amount => '0.001')
40
59
 
60
+ puts JSON.pretty_generate(prepared_transaction)
61
+ puts " -- "
41
62
 
42
- # fetch the dtrust address' balance
43
- puts "*** dTrust1 Balance:"
44
- puts JSON.pretty_generate(BlockIo.get_dtrust_address_balance(:label => 'dTrust1'))
45
-
46
- # withdraw a few coins from dtrust1 to the default label
47
- normal_address = BlockIo.get_address_by_label(:label => 'default')['data']['address']
63
+ puts JSON.pretty_generate(blockio.summarize_prepared_transaction(prepared_transaction))
64
+ puts " -- "
48
65
 
49
- puts "*** Withdrawing from dTrust1 to the 'default' label in normal multisig"
66
+ # blockio.create_and_sign_transaction creates the transaction client-side, and appends your signatures (if any)
67
+ transaction_data = blockio.create_and_sign_transaction(prepared_transaction)
50
68
 
51
- response = BlockIo.withdraw_from_dtrust_address(:from_labels => 'dTrust1', :to_addresses => normal_address, :amounts => '2.1')
69
+ # blockio.submit_transaction sends the signatures and transaction payload to Block.io so Block.io can add its signatures and broadcast the transaction to the network
70
+ response = blockio.submit_transaction(:transaction_data => transaction_data)
52
71
 
72
+ puts "*** Withdrawal response:"
53
73
  puts JSON.pretty_generate(response)
54
74
 
55
- # let's sign for the public keys specified
56
-
57
- response['data']['inputs'].each do |input|
58
- # for each input
59
75
 
60
- data_to_sign = input['data_to_sign']
76
+ # fetch the dtrust address' balance
77
+ puts "*** dtrust_address_label Balance:"
78
+ puts JSON.pretty_generate(blockio.get_dtrust_address_balance(:label => dtrust_address_label))
61
79
 
62
- input['signers'].each do |signer|
80
+ # withdraw a few coins from dtrust_address_label to the default label
81
+ normal_address = blockio.get_address_by_label(:label => 'default')['data']['address']
63
82
 
64
- # figure out if we have the public key that matches this signer
65
-
66
- keys.each do |key|
67
- # iterate over all keys till we've found the one that we need
83
+ puts "*** Withdrawing from dtrust_address_label to the 'default' label in normal multisig"
68
84
 
69
- signer['signed_data'] = key.sign(data_to_sign) if key.public_key == signer['signer_public_key']
85
+ # note use of prepare_dtrust_transaction instead of prepare_transaction, since this is a dTrust transaction
86
+ # we're not doing any inspection here, but you will in your own code
87
+ prepared_transaction = blockio.prepare_dtrust_transaction(:from_labels => dtrust_address_label, :to_addresses => normal_address, :amounts => '0.0009')
70
88
 
71
- end
89
+ # create the transaction and sign it with the keys we've provided
90
+ # we're signing the transaction partially by supplying only 3 of our 4 keys
91
+ transaction_data = blockio.create_and_sign_transaction(prepared_transaction, keys.first(3))
72
92
 
73
- end
93
+ puts "*** Submitting transaction data:"
94
+ puts JSON.pretty_generate(transaction_data)
74
95
 
75
- end
76
-
77
- puts "*** Our signed response: "
78
- puts JSON.pretty_generate(response['data']) #.to_json
96
+ # if successful, you will get a transaction ID when you submit_transaction.
97
+ response = blockio.submit_transaction(:transaction_data => transaction_data)
79
98
 
80
99
  # let's final the withdrawal
81
- puts "*** Finalize withdrawal: "
82
- puts JSON.pretty_generate(BlockIo.sign_and_finalize_withdrawal(:signature_data => response['data'].to_json))
100
+ puts "*** Submit the transaction: "
101
+ puts JSON.pretty_generate(response)
83
102
 
84
103
  # get the sent transactions for this dTrust address
85
104
 
86
- puts "*** Get transactions sent by our dTrust1 address: "
87
-
88
- puts JSON.pretty_generate(BlockIo.get_dtrust_transactions(:type => 'sent', :labels => 'dTrust1'))
105
+ puts "*** Get transactions sent by our dtrust_address_label address: "
106
+ puts JSON.pretty_generate(blockio.get_dtrust_transactions(:type => 'sent', :labels => dtrust_address_label))
89
107
 
data/examples/proxy.rb ADDED
@@ -0,0 +1,36 @@
1
+ # creates a new destination address, withdraws from the default label to it,
2
+ # gets updated address balances, gets sent/received transactions, and the
3
+ # current price
4
+ # ... through an https proxy (like Squid)
5
+ #
6
+ # Unauthenticated:
7
+ # $ PROXY_HOST=localhost PROXY_PORT=3128 API_KEY=TESTNET_API_KEY PIN=YOUR_SECRET_PIN ruby proxy.rb
8
+ #
9
+ # Authenticated:
10
+ # $ PROXY_HOST=localhost PROXY_PORT=3128 PROXY_USER=user PROXY_PASS=pass API_KEY=TESTNET_API_KEY PIN=YOUR_SECRET_PIN ruby proxy.rb
11
+ #
12
+ # adjust amount below if not using the Dogecoin Testnet
13
+
14
+ require '../lib/block_io'
15
+
16
+ blockio = BlockIo::Client.new(:api_key => ENV['API_KEY'], :pin => ENV['PIN'], :version => 2, :proxy => {
17
+ :hostname => ENV['PROXY_HOST'],
18
+ :port => ENV['PROXY_PORT'],
19
+ :username => ENV['PROXY_USER'],
20
+ :password => ENV['PROXY_PASS']
21
+ })
22
+ puts blockio.get_balance
23
+ puts blockio.network
24
+
25
+ # create the address if it doesn't exist
26
+ puts blockio.get_new_address(:label => 'testDest')
27
+
28
+ puts blockio.withdraw_from_labels(:from_labels => 'default', :to_label => 'testDest', :amount => '2.5')
29
+
30
+ puts blockio.get_address_balance(:labels => 'default,testDest')
31
+
32
+ puts blockio.get_transactions(:type => 'sent')
33
+
34
+ puts blockio.get_transactions(:type => 'received')
35
+
36
+ puts blockio.get_current_price(:base_price => 'BTC')
data/examples/sweeper.rb CHANGED
@@ -4,21 +4,31 @@
4
4
  #
5
5
  # Contact support@block.io if you have any issues
6
6
 
7
- require "block_io"
7
+ require 'block_io'
8
8
 
9
- BlockIo.set_options :api_key => 'YOUR API KEY', :pin => 'PIN NOT NEEDED', :version => 2
9
+ blockio = BlockIo::Client.new(:api_key => ENV['API_KEY'])
10
10
 
11
- to_address = 'SWEEP COINS TO THIS ADDRESS'
11
+ to_address = ENV['TO_ADDRESS'] # sweep coins into this address
12
+ private_key = ENV['PRIVATE_KEY'] # private key for the address from which you wish to sweep coins (WIF)
12
13
 
13
- from_address = 'SWEEP COINS FROM THIS ADDRESS'
14
- private_key = 'PRIVATE KEY FOR FROM_ADDRESS'
14
+ # prepare the sweep transaction
15
+ # you will inspect this data to ensure things are in order (the network fees you pay, the amount being swept, etc.)
16
+ # the private key is used to determine the public key by prepare_sweep_transaction client-side
17
+ # the private key never travels to Block.io
18
+ prepared_transaction = blockio.prepare_sweep_transaction(:to_address => to_address, :private_key => private_key)
19
+ puts JSON.pretty_generate(prepared_transaction)
20
+ puts " -- "
15
21
 
16
- begin
17
- response = BlockIo.sweep_from_address(:to_address => to_address, :private_key => private_key, :from_address => from_address)
18
-
19
- puts "Sweep Complete: #{response['data']['amount_sent']} #{response['data']['network']} swept from #{from_address} to #{to_address}."
20
- puts "Transaction ID: #{response['data']['txid']}"
21
- puts "Network Fee Incurred: #{response['data']['network_fee']} #{response['data']['network']}"
22
- rescue Exception => e
23
- puts "Sweep failed: #{e}"
24
- end
22
+ # create and sign the transaction
23
+ # the signature is from the key you provided to prepare_sweep_transaction above
24
+ transaction_data = blockio.create_and_sign_transaction(prepared_transaction)
25
+ puts JSON.pretty_generate(transaction_data)
26
+ puts " -- "
27
+
28
+ # submit the final transaction to Block.io for broadcast to the network, or
29
+ # submit the transaction payload youself elsewhere (like using sendrawtransaction RPC calls with bitcoind, dogecoind, litecoind, etc.)
30
+ response = blockio.submit_transaction(:transaction_data => transaction_data)
31
+ puts JSON.pretty_generate(response)
32
+ puts " -- "
33
+
34
+ puts "Transaction ID: #{response['data']['txid']}"