bitpay-key-utils 2.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 57e7c32f43dd65ee8f2978c596ceae25ab5554c7
4
+ data.tar.gz: 1d9d8a9921b01e5c79ee85ad190eb8c443248d8f
5
+ SHA512:
6
+ metadata.gz: 23b464c38bf539f3a80f2cde38dbe2b9c6b6397ec16a7a74cb5005fb9324120f5eb9ad0ee6807dade17a6ec40f14bee974fa3f306bc67a3c089993dda8cdb354
7
+ data.tar.gz: 400893ec62fdfa9cdc9766784dd06968ff2d3a3f60d38eb8c33ba598ca4160bfd734ef5498aefba660eefdea58ccd69b847077a5b54174a7d5076ec3a854bd2d
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bitpay-key-utils (2.0.1)
5
+ ecdsa (~> 1.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ byebug (3.5.1)
11
+ columnize (~> 0.8)
12
+ debugger-linecache (~> 1.2)
13
+ slop (~> 3.6)
14
+ coderay (1.1.0)
15
+ columnize (0.9.0)
16
+ debugger-linecache (1.2.0)
17
+ diff-lcs (1.2.5)
18
+ ecdsa (1.2.0)
19
+ interception (0.5)
20
+ method_source (0.8.2)
21
+ pry (0.10.1)
22
+ coderay (~> 1.1.0)
23
+ method_source (~> 0.8.1)
24
+ slop (~> 3.4)
25
+ pry-byebug (2.0.0)
26
+ byebug (~> 3.4)
27
+ pry (~> 0.10)
28
+ pry-rescue (1.4.1)
29
+ interception (>= 0.5)
30
+ pry
31
+ rake (10.3.2)
32
+ rspec (3.1.0)
33
+ rspec-core (~> 3.1.0)
34
+ rspec-expectations (~> 3.1.0)
35
+ rspec-mocks (~> 3.1.0)
36
+ rspec-core (3.1.7)
37
+ rspec-support (~> 3.1.0)
38
+ rspec-expectations (3.1.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.1.0)
41
+ rspec-mocks (3.1.3)
42
+ rspec-support (~> 3.1.0)
43
+ rspec-support (3.1.2)
44
+ slop (3.6.0)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bitpay-key-utils!
51
+ pry (= 0.10.1)
52
+ pry-byebug (= 2.0.0)
53
+ pry-rescue (= 1.4.1)
54
+ rake (= 10.3.2)
55
+ rspec (= 3.1.0)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 BitPay, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,2 @@
1
+ # bitpay-ruby-keyutils
2
+ Crypto methods
@@ -0,0 +1,27 @@
1
+ require './lib/bitpay/key_version.rb'
2
+ Gem::Specification.new do |s|
3
+ s.name = 'bitpay-key-utils'
4
+ s.version = BitPay::KEY_UTILS_VERSION
5
+ s.licenses = ['MIT']
6
+ s.authors = 'Bitpay, Inc.'
7
+ s.email = 'info@bitpay.com'
8
+ s.homepage = 'https://github.com/bitpay/ruby-key-utils'
9
+ s.summary = 'Official Ruby library for the BitPay API'
10
+ s.description = 'Powerful, flexible, lightweight, thread-safe interface to the BitPay developers API'
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.require_paths = ["lib"]
14
+ s.rubyforge_project = s.name
15
+ s.required_rubygems_version = '>= 1.3.4'
16
+ s.required_ruby_version = '~> 2.1'
17
+ s.bindir = 'bin'
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+
20
+ s.add_dependency 'ecdsa', '~>1.2'
21
+
22
+ s.add_development_dependency 'rake', '10.3.2'
23
+ s.add_development_dependency 'pry', '0.10.1'
24
+ s.add_development_dependency 'pry-byebug', '2.0.0'
25
+ s.add_development_dependency 'pry-rescue', '1.4.1'
26
+ s.add_development_dependency 'rspec', '3.1.0'
27
+ end
@@ -0,0 +1,114 @@
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 'openssl'
6
+ require 'ecdsa'
7
+ require 'securerandom'
8
+ require 'digest/sha2'
9
+
10
+ module BitPay
11
+ class KeyUtils
12
+ class << self
13
+
14
+ def generate_pem
15
+ key = OpenSSL::PKey::EC.new("secp256k1")
16
+ key.generate_key
17
+ key.to_pem
18
+ end
19
+
20
+ def create_key pem
21
+ OpenSSL::PKey::EC.new(pem)
22
+ end
23
+
24
+ def create_new_key
25
+ key = OpenSSL::PKey::EC.new("secp256k1")
26
+ key.generate_key
27
+ key
28
+ end
29
+
30
+ def get_private_key key
31
+ key.private_key.to_int.to_s(16)
32
+ end
33
+
34
+ def get_public_key key
35
+ key.public_key.group.point_conversion_form = :compressed
36
+ key.public_key.to_bn.to_s(16).downcase
37
+ end
38
+
39
+ def get_private_key_from_pem pem
40
+ raise BitPayError, MISSING_PEM unless pem
41
+ key = OpenSSL::PKey::EC.new(pem)
42
+ get_private_key key
43
+ end
44
+
45
+ def get_public_key_from_pem pem
46
+ raise BitPayError, MISSING_PEM unless pem
47
+ key = OpenSSL::PKey::EC.new(pem)
48
+ get_public_key key
49
+ end
50
+
51
+ def generate_sin_from_pem pem
52
+ #http://blog.bitpay.com/2014/07/01/bitauth-for-decentralized-authentication.html
53
+ #https://en.bitcoin.it/wiki/Identity_protocol_v1
54
+
55
+ # NOTE: All Digests are calculated against the binary representation,
56
+ # hence the requirement to use [].pack("H*") to convert to binary for each step
57
+
58
+ #Generate Private Key
59
+ key = OpenSSL::PKey::EC.new pem
60
+ key.public_key.group.point_conversion_form = :compressed
61
+ public_key = key.public_key.to_bn.to_s(2)
62
+ step_one = Digest::SHA256.hexdigest(public_key)
63
+ step_two = Digest::RMD160.hexdigest([step_one].pack("H*"))
64
+ step_three = "0F02" + step_two
65
+ step_four_a = Digest::SHA256.hexdigest([step_three].pack("H*"))
66
+ step_four = Digest::SHA256.hexdigest([step_four_a].pack("H*"))
67
+ step_five = step_four[0..7]
68
+ step_six = step_three + step_five
69
+ encode_base58(step_six)
70
+ end
71
+
72
+
73
+ ## Generate ECDSA signature
74
+ # This is the last method that requires the ecdsa gem, which we would like to replace
75
+
76
+ def sign(message, privkey)
77
+ group = ECDSA::Group::Secp256k1
78
+ digest = Digest::SHA256.digest(message)
79
+ signature = nil
80
+ while signature.nil?
81
+ temp_key = 1 + SecureRandom.random_number(group.order - 1)
82
+ signature = ECDSA.sign(group, privkey.to_i(16), digest, temp_key)
83
+ return ECDSA::Format::SignatureDerString.encode(signature).unpack("H*").first
84
+ end
85
+ end
86
+
87
+ ########## Private Class Methods ################
88
+
89
+ ## Base58 Encoding Method
90
+ #
91
+ private
92
+ def encode_base58 (data)
93
+ code_string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
94
+ base = 58
95
+ x = data.hex
96
+ output_string = ""
97
+
98
+ while x > 0 do
99
+ remainder = x % base
100
+ x = x / base
101
+ output_string << code_string[remainder]
102
+ end
103
+
104
+ pos = 0
105
+ while data[pos,2] == "00" do
106
+ output_string << code_string[0]
107
+ pos += 2
108
+ end
109
+
110
+ output_string.reverse()
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,3 @@
1
+ module BitPay
2
+ KEY_UTILS_VERSION="2.0.0"
3
+ end
@@ -0,0 +1,15 @@
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/key_utils'
8
+ require 'bitpay/key_version'
9
+
10
+ module BitPay
11
+ MISSING_PEM = 'No pem file specified. Pass pem string'
12
+ class BitPayError < StandardError; end
13
+ class ArgumentError < ArgumentError; end
14
+ class ConnectionError < Errno::ECONNREFUSED; end
15
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe BitPay::KeyUtils do
4
+ let(:key_utils) {BitPay::KeyUtils}
5
+
6
+ describe '.generate_pem' do
7
+ it 'should generate a pem string' do
8
+ regex = /BEGIN\ EC\ PRIVATE\ KEY/
9
+ expect(regex.match(key_utils.generate_pem)).to be_truthy
10
+ end
11
+ end
12
+
13
+ describe '.get_public_key_from_pem' do
14
+ it 'should generate the right public key' do
15
+ expect(key_utils.get_public_key_from_pem(PEM)).to eq(PUB_KEY)
16
+ end
17
+
18
+ it 'should get pem from the env if none is passed' do
19
+ expect(key_utils.get_public_key_from_pem(PEM)).to eq(PUB_KEY)
20
+ end
21
+
22
+ end
23
+
24
+ describe '.generate_sin_from_pem' do
25
+ let(:pem){PEM}
26
+ let(:sin){CLIENT_ID}
27
+
28
+ it 'will return the right sin for the right pem' do
29
+ expect(key_utils.generate_sin_from_pem(pem)).to eq sin
30
+ end
31
+
32
+ end
33
+
34
+ context "errors when priv_key is not provided" do
35
+ it 'will not retrieve public key' do
36
+ expect{key_utils.get_public_key_from_pem(nil)}.to raise_error(BitPay::BitPayError)
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,12 @@
1
+ require 'pry'
2
+
3
+ require File.join File.dirname(__FILE__), '..', 'lib', 'bitpay_key_utils.rb'
4
+
5
+ #
6
+ ## Test Variables
7
+ #
8
+ PEM = "-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEICg7E4NN53YkaWuAwpoqjfAofjzKI7Jq1f532dX+0O6QoAcGBSuBBAAK\noUQDQgAEjZcNa6Kdz6GQwXcUD9iJ+t1tJZCx7hpqBuJV2/IrQBfue8jh8H7Q/4vX\nfAArmNMaGotTpjdnymWlMfszzXJhlw==\n-----END EC PRIVATE KEY-----\n"
9
+
10
+ PUB_KEY = '038d970d6ba29dcfa190c177140fd889fadd6d2590b1ee1a6a06e255dbf22b4017'
11
+ CLIENT_ID = "TeyN4LPrXiG5t2yuSamKqP3ynVk3F52iHrX"
12
+
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitpay-key-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bitpay, Inc.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ecdsa
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 10.3.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 10.3.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-rescue
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.4.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.4.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 3.1.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 3.1.0
97
+ description: Powerful, flexible, lightweight, thread-safe interface to the BitPay
98
+ developers API
99
+ email: info@bitpay.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".ruby-version"
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE
109
+ - README.md
110
+ - bitpay-key-utils.gemspec
111
+ - lib/bitpay/key_utils.rb
112
+ - lib/bitpay/key_version.rb
113
+ - lib/bitpay_key_utils.rb
114
+ - spec/key_utils_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/bitpay/ruby-key-utils
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - "~>"
127
+ - !ruby/object:Gem::Version
128
+ version: '2.1'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: 1.3.4
134
+ requirements: []
135
+ rubyforge_project: bitpay-key-utils
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Official Ruby library for the BitPay API
140
+ test_files: []