bitpay-client 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,143 +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
- def nonce
18
- Time.now.utc.strftime('%Y%m%d%H%M%S%L')
19
- end
20
-
21
- ## Generates a new private key and writes to local FS
22
- #
23
- def retrieve_or_generate_pem
24
- begin
25
- pem = get_local_pem_file
26
- rescue
27
- pem = generate_pem
28
- end
29
- pem
30
- end
31
-
32
- def generate_pem
33
- key = OpenSSL::PKey::EC.new("secp256k1")
34
- key.generate_key
35
- write_pem_file(key)
36
- key.to_pem
37
- end
38
-
39
- def create_key pem
40
- OpenSSL::PKey::EC.new(pem)
41
- end
42
-
43
- def create_new_key
44
- key = OpenSSL::PKey::EC.new("secp256k1")
45
- key.generate_key
46
- key
47
- end
48
-
49
- def write_pem_file key
50
- FileUtils.mkdir_p(BITPAY_CREDENTIALS_DIR)
51
- File.open(PRIVATE_KEY_PATH, 'w') { |file| file.write(key.to_pem) }
52
- end
53
- ## Gets private key from ENV variable or local FS
54
- #
55
- def get_local_pem_file
56
- ENV['BITPAY_PEM'] || File.read(PRIVATE_KEY_PATH) || (raise BitPayError, MISSING_KEY)
57
- end
58
-
59
- def get_private_key key
60
- key.private_key.to_int.to_s(16)
61
- end
62
-
63
- def get_public_key key
64
- key.public_key.group.point_conversion_form = :compressed
65
- key.public_key.to_bn.to_s(16).downcase
66
- end
67
-
68
- def get_private_key_from_pem pem
69
- raise BitPayError, MISSING_KEY unless pem
70
- key = OpenSSL::PKey::EC.new(pem)
71
- get_private_key key
72
- end
73
-
74
- def get_public_key_from_pem pem
75
- raise BitPayError, MISSING_KEY unless pem
76
- key = OpenSSL::PKey::EC.new(pem)
77
- get_public_key key
78
- end
79
-
80
- def generate_sin_from_pem(pem = nil)
81
- #http://blog.bitpay.com/2014/07/01/bitauth-for-decentralized-authentication.html
82
- #https://en.bitcoin.it/wiki/Identity_protocol_v1
83
-
84
- # NOTE: All Digests are calculated against the binary representation,
85
- # hence the requirement to use [].pack("H*") to convert to binary for each step
86
-
87
- #Generate Private Key
88
- key = OpenSSL::PKey::EC.new(pem ||= get_local_pem_file)
89
- key.public_key.group.point_conversion_form = :compressed
90
- public_key = key.public_key.to_bn.to_s(2)
91
- step_one = Digest::SHA256.hexdigest(public_key)
92
- step_two = Digest::RMD160.hexdigest([step_one].pack("H*"))
93
- step_three = "0F02" + step_two
94
- step_four_a = Digest::SHA256.hexdigest([step_three].pack("H*"))
95
- step_four = Digest::SHA256.hexdigest([step_four_a].pack("H*"))
96
- step_five = step_four[0..7]
97
- step_six = step_three + step_five
98
- encode_base58(step_six)
99
- end
100
-
101
-
102
- ## Generate ECDSA signature
103
- # This is the last method that requires the ecdsa gem, which we would like to replace
104
-
105
- def sign(message, privkey)
106
- group = ECDSA::Group::Secp256k1
107
- digest = Digest::SHA256.digest(message)
108
- signature = nil
109
- while signature.nil?
110
- temp_key = 1 + SecureRandom.random_number(group.order - 1)
111
- signature = ECDSA.sign(group, privkey.to_i(16), digest, temp_key)
112
- return ECDSA::Format::SignatureDerString.encode(signature).unpack("H*").first
113
- end
114
- end
115
-
116
- ########## Private Class Methods ################
117
-
118
- ## Base58 Encoding Method
119
- #
120
- private
121
- def encode_base58 (data)
122
- code_string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
123
- base = 58
124
- x = data.hex
125
- output_string = ""
126
-
127
- while x > 0 do
128
- remainder = x % base
129
- x = x / base
130
- output_string << code_string[remainder]
131
- end
132
-
133
- pos = 0
134
- while data[pos,2] == "00" do
135
- output_string << code_string[0]
136
- pos += 2
137
- end
138
-
139
- output_string.reverse()
140
- end
141
- end
142
- end
143
- end
data/lib/harness.rb DELETED
@@ -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_relative 'bitpay.rb'
6
- require_relative 'bitpay/key_utils.rb'
7
-
8
- # Test SIN Generation class methods
9
-
10
- # Generate SIN
11
- ENV["PRIV_KEY"] = "16d7c3508ec59773e71ae728d29f41fcf5d1f380c379b99d68fa9f552ce3ebc3"
12
- puts "privkey: #{ENV['PRIV_KEY']}"
13
- puts "target SIN: TfFVQhy2hQvchv4VVG4c7j4XPa2viJ9HrR8"
14
- puts "Derived SIN: #{BitPay::KeyUtils.get_client_id}"
15
-
16
- puts "\n\n------------------\n\n"
17
-
18
- uri = "https://localhost:8088"
19
- #name = "Ridonculous.label That shouldn't work really"
20
- name = "somethinginnocuous"
21
- facade = "pos"
22
- client_id = BitPay::KeyUtils.get_client_id
23
-
24
- BitPay::KeyUtils.generate_registration_url(uri,name,facade,client_id)
25
-
26
- puts "\n\n------------------\n\n"
27
-
28
- #### Test Invoice Creation using directly assigned keys
29
- ## (Ultimately pubkey and SIN should be derived)
30
-
31
- ENV["PRIV_KEY"] = "16d7c3508ec59773e71ae728d29f41fcf5d1f380c379b99d68fa9f552ce3ebc3"
32
- #ENV["pub_key"] = "0353a036fb495c5846f26a3727a28198da8336ae4f5aaa09e24c14a4126b5d969d"
33
- #ENV['SIN'] = "TfFVQhy2hQvchv4VVG4c7j4XPa2viJ9HrR8"
34
-
35
- client = BitPay::Client.new({insecure: true, debug: false})
36
-
37
- invoice = client.post 'invoices', {:price => 10.00, :currency => 'USD'}
38
-
39
- puts "Here's the invoice: \n" + JSON.pretty_generate(invoice)
40
-