bitpay-client 2.4.0 → 2.5.1905

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +5 -1
  3. data/.travis.yml +6 -1
  4. data/CHANGELOG.md +24 -0
  5. data/GUIDE.md +233 -0
  6. data/Gemfile +0 -4
  7. data/LICENSE.md +2 -2
  8. data/README.md +8 -79
  9. data/Rakefile +57 -2
  10. data/bitpay-client.gemspec +15 -16
  11. data/config/constants.rb +12 -1
  12. data/features/creating_invoices.feature +28 -0
  13. data/features/pairing.feature +19 -0
  14. data/features/refunds.feature +23 -0
  15. data/features/retrieving_invoices.feature +11 -0
  16. data/features/step_definitions/invoice_steps.rb +31 -0
  17. data/features/step_definitions/keygen_steps.rb +56 -0
  18. data/features/step_definitions/refund_steps.rb +37 -0
  19. data/features/step_definitions/step_helpers.rb +31 -0
  20. data/lib/bitpay/cacert.pem +1295 -1760
  21. data/lib/bitpay/client.rb +175 -0
  22. data/lib/bitpay/rest_connector.rb +106 -0
  23. data/lib/bitpay/version.rb +2 -2
  24. data/lib/bitpay_sdk.rb +28 -0
  25. data/spec/client_spec.rb +153 -6
  26. data/spec/fixtures/invoices-POST.json +29 -0
  27. data/spec/fixtures/invoices_{id}-GET.json +35 -0
  28. data/spec/fixtures/invoices_{id}_refunds-GET.json +17 -0
  29. data/spec/fixtures/invoices_{id}_refunds-POST.json +9 -0
  30. data/spec/fixtures/invoices_{id}_refunds_{refund_id}-GET.json +11 -0
  31. data/spec/fixtures/response-nodata.json +10 -0
  32. data/spec/spec_helper.rb +16 -18
  33. metadata +77 -82
  34. data/bin/bitpay +0 -3
  35. data/lib/bitpay.rb +0 -21
  36. data/lib/bitpay/cli.rb +0 -64
  37. data/lib/bitpay/cli_client.rb +0 -24
  38. data/lib/bitpay/cli_key_utils.rb +0 -40
  39. data/spec/features/pair_spec.rb +0 -25
  40. data/spec/key_utils_spec.rb +0 -26
  41. data/spec/set_constants.sh +0 -4
data/bin/bitpay DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require_relative '../lib/bitpay/cli'
3
- require_relative '../lib/bitpay/cli_client'
@@ -1,21 +0,0 @@
1
- # license Copyright 2011-2014 BitPay, Inc., MIT License
2
- # see http://opensource.org/licenses/MIT
3
- # or https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
4
-
5
- libdir = File.dirname(__FILE__)
6
- $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
7
- require 'bitpay_sdk'
8
- require 'bitpay/cli_client'
9
- require 'bitpay/cli_key_utils'
10
- require 'bitpay/version'
11
-
12
- module BitPay
13
-
14
- BITPAY_CREDENTIALS_DIR = ENV['RCBITPAYROOT'] || File.expand_path("~") + "/.bitpay/"
15
- PRIVATE_KEY_FILE = 'bitpay.pem'
16
- PRIVATE_KEY_PATH = File.join(BITPAY_CREDENTIALS_DIR, PRIVATE_KEY_FILE)
17
- TOKEN_FILE = 'tokens.json'
18
- TOKEN_FILE_PATH = File.join(BITPAY_CREDENTIALS_DIR, TOKEN_FILE)
19
-
20
- MISSING_KEY = 'No Private Key specified. Pass priv_key or set ENV variable PRIV_KEY'
21
- end
@@ -1,64 +0,0 @@
1
- # license Copyright 2011-2014 BitPay, Inc., MIT License
2
- # see http://opensource.org/licenses/MIT
3
- # or https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
4
-
5
- require 'rubygems'
6
- require 'commander/import'
7
- require_relative '../bitpay'
8
-
9
- program :name, 'BitPay Ruby Library CLI'
10
- program :version, BitPay::VERSION
11
- program :description, 'Official BitPay Ruby API Client. Use to securely register your client with the BitPay API endpoint. '
12
- program :help_formatter, :compact
13
-
14
- command :pair do |c|
15
- c.syntax = 'bitpay pair <code>'
16
- c.summary = "Pair the local keys to a bitpay account."
17
- c.option '--test', "Use the bitpay test server"
18
- c.option '--custom <custom>', "Use a custom bitpay URI"
19
- c.option '--insecure <insecure>', "Use an insecure custom bitpay URI"
20
- c.action do |args, options|
21
- raise ArgumentError, "Pairing failed, please call argument as 'bitpay pair <code> [options]'" unless args.first
22
- case
23
- when options.test
24
- client = BitPay::Client.new(api_uri: "https://test.bitpay.com")
25
- message = "Paired with test.bitpay.com"
26
- when options.custom
27
- client = BitPay::Client.new(api_uri: options.custom)
28
- message = "Paired with #{options.custom}"
29
- when options.insecure
30
- client = BitPay::Client.new(insecure: true, api_uri: options.insecure)
31
- message = "Paired with #{options.insecure}"
32
- else
33
- client = BitPay::Client.new
34
- message = "Paired with bitpay.com"
35
- end
36
-
37
- begin
38
- client.pair_pos_client args.first
39
- puts message
40
- rescue Exception => e
41
- puts e.message
42
- end
43
- end
44
- end
45
-
46
- command :show_keys do |c|
47
- c.syntax = 'bitpay show_keys'
48
- c.summary = "Read current environment's key information to STDOUT"
49
- c.description = ''
50
- c.example 'description', 'command example'
51
- c.action do |args, options|
52
-
53
- pem = BitPay::KeyUtils.get_local_pem_file
54
- private_key = BitPay::KeyUtils.get_private_key_from_pem pem
55
- public_key = BitPay::KeyUtils.get_public_key_from_pem pem
56
- client_id = BitPay::KeyUtils.generate_sin_from_pem pem
57
-
58
- puts "Current BitPay Client Keys:\n"
59
- puts "Private Key: #{private_key}"
60
- puts "Public Key: #{public_key}"
61
- puts "Client ID: #{client_id}"
62
-
63
- end
64
- end
@@ -1,24 +0,0 @@
1
- module BitPay
2
- class Client < BitPay::SDK::Client
3
- def initialize opts={}
4
- pem = File.read(PRIVATE_KEY_PATH) if File.exists?(PRIVATE_KEY_PATH)
5
- pem = BitPay::KeyUtils.generate_pem unless File.exists?(PRIVATE_KEY_PATH)
6
- pem = opts[:pem] if opts[:pem]
7
- File.write(PRIVATE_KEY_PATH, pem)
8
- opts[:pem] = pem
9
- super opts
10
- end
11
-
12
- def pair_pos_client claim_code
13
- response = super
14
- token = get_token_from_response response
15
- File.write(TOKEN_FILE_PATH, token)
16
- return response
17
- end
18
-
19
- private
20
- def get_token_from_response response
21
- ({response[0]["facade"] => response[0]["token"]}).to_json
22
- end
23
- end
24
- end
@@ -1,40 +0,0 @@
1
- # license Copyright 2011-2014 BitPay, Inc., MIT License
2
- # see http://opensource.org/licenses/MIT
3
- # or https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
4
-
5
- require 'uri'
6
- require 'net/https'
7
- require 'json'
8
- require 'openssl'
9
- require 'ecdsa'
10
- require 'securerandom'
11
- require 'digest/sha2'
12
- require 'cgi'
13
-
14
- module BitPay
15
- class KeyUtils
16
- class << self
17
- ## Generates a new private key and writes to local FS
18
- #
19
- def retrieve_or_generate_pem
20
- begin
21
- pem = get_local_pem_file
22
- rescue
23
- pem = generate_pem
24
- write_pem_file pem
25
- end
26
- pem
27
- end
28
-
29
- def write_pem_file pem
30
- FileUtils.mkdir_p(BITPAY_CREDENTIALS_DIR)
31
- File.open(PRIVATE_KEY_PATH, 'w') { |file| file.write(pem) }
32
- end
33
- ## Gets private key from ENV variable or local FS
34
- #
35
- def get_local_pem_file
36
- File.read(PRIVATE_KEY_PATH) || (raise BitPayError, MISSING_KEY)
37
- end
38
- end
39
- end
40
- end
@@ -1,25 +0,0 @@
1
- require_relative '../spec_helper.rb'
2
-
3
- describe "pairing a token", javascript: true, type: :feature do
4
- let(:claimCode) do
5
- File.delete(BitPay::TOKEN_FILE_PATH) if File.exists?(BitPay::TOKEN_FILE_PATH)
6
- client = BitPay::Client.new(api_uri: ROOT_ADDRESS, insecure: true)
7
- token = client.get(path: "tokens")["data"].select{|tuple| tuple["merchant"]}.first.values.first
8
- client.post(path: "tokens", token: token, params: {facade: "pos"})["data"][0]["pairingCode"]
9
- end
10
-
11
- context "when a pem file exists" do
12
- before do
13
- sleep(5)
14
- `./bin/bitpay pair #{claimCode} --insecure #{ROOT_ADDRESS}`
15
- end
16
-
17
- it "should save a pem file when pairing" do
18
- expect(File.exists?(BitPay::PRIVATE_KEY_PATH)).to eq true
19
- end
20
-
21
- it "should save a token when pairing" do
22
- expect(JSON.parse(File.read(BitPay::TOKEN_FILE_PATH))["pos"]).to_not be_nil
23
- end
24
- end
25
- end
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe BitPay::KeyUtils do
4
- let(:key_utils) {BitPay::KeyUtils}
5
-
6
-
7
- describe '.get_local_private_key' do
8
- it 'should get the key from ~/.bitpay/bitpay.pem if env variable is not set' do
9
- allow(File).to receive(:read).with(BitPay::PRIVATE_KEY_PATH) {PEM}
10
- expect(key_utils.get_local_pem_file).to eq(PEM)
11
- end
12
-
13
- end
14
-
15
- describe '.retrieve_or_generate_pem' do
16
- it 'should write a new key to ~/.bitpay/bitpay.pem if there is no existing file' do
17
- file = class_double("File").as_stubbed_const
18
- fileutils = class_double("FileUtils").as_stubbed_const
19
- allow(file).to receive(:read).with(BitPay::PRIVATE_KEY_PATH).and_throw(StandardError)
20
- allow(fileutils).to receive(:mkdir_p).with(BitPay::BITPAY_CREDENTIALS_DIR).and_return(nil)
21
- expect(file).to receive(:open).with(BitPay::PRIVATE_KEY_PATH, 'w')
22
- key_utils.retrieve_or_generate_pem
23
- end
24
- end
25
-
26
- end
@@ -1,4 +0,0 @@
1
- #!/bin/bash
2
-
3
- export RCROOTADDRESS=$1
4
- echo $RCROOTADDRESS