hdkey 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec2d5a6b0b2dbb4996ef8a09211ac1daa588ee43
4
+ data.tar.gz: c7ac1c704f541c88f52d996b143f084288f1e656
5
+ SHA512:
6
+ metadata.gz: e8ecd70d2430be25213e409d31155bb16524acbb0e618091a98ef55ffb55466959053341afd702ae57fc2d3b962eb4339dcf8fdd6a46dc2634e7df08137e206f
7
+ data.tar.gz: eee98f266773d1d3f3dbcad9b25a989050942875dd611ff52d67322308cde88fbaccfa6c07b9834a23c0a5ebe59bc40a25117f633183c0afdbb48cf86441bbc5
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ hdkey
@@ -0,0 +1 @@
1
+ 2.3.0
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.2.2
5
+ - 2.3.0
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at azuchi@haw.co.jp. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hdkey.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 azuchi
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,87 @@
1
+ # Hierarchical Deterministic key derivation for Ruby
2
+ [![Build Status](https://travis-ci.org/azuchi/hdkey.svg?branch=master)](https://travis-ci.org/azuchi/hdkey) [![Gem Version](https://badge.fury.io/rb/hdkey.svg)](https://badge.fury.io/rb/hdkey) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
3
+
4
+ The implementation of Bitcoin hierarchical deterministic key for Ruby.
5
+
6
+ Bitcoin hierarchical deterministic key is defined by [BIP32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki).
7
+
8
+ ## Requirements
9
+
10
+ This gem depends on [bitcoin-ruby](https://github.com/lian/bitcoin-ruby).
11
+
12
+ This gem run with following ruby version.
13
+
14
+ * 2.1
15
+ * 2.2
16
+ * 2.3
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'hdkey'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install hdkey
33
+
34
+ ## Usage
35
+
36
+ ### generate master key
37
+
38
+ ```ruby
39
+ require 'hdkey'
40
+
41
+ seed = '000102030405060708090a0b0c0d0e0f'
42
+
43
+ @master_key = Bitcoin::ExtKey.generate_master(seed.htb)
44
+ ```
45
+
46
+ ### derive key
47
+
48
+ ```ruby
49
+ # derive `m/0H` key
50
+ key = @master_key.derive(2**31)
51
+
52
+ key.depth
53
+ >= 1
54
+
55
+ key.fingerprint
56
+ => '5c1bd648'
57
+
58
+ key.chain_code
59
+ => '47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141'
60
+
61
+ key.priv_key.priv
62
+ => 'edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea'
63
+
64
+ key.to_base58
65
+ => 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7'
66
+
67
+ key.ext_pubkey.to_base58
68
+ => 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw'
69
+
70
+ # derive child(m/0H/1)
71
+ child_key = key.derive(1)
72
+ ```
73
+
74
+ ### import key
75
+
76
+ ```ruby
77
+ # import privkey
78
+ ext_privkey = Bitcoin::ExtKey.from_base58('xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs')
79
+
80
+ # import pubkey
81
+ ext_pubkey = key = Bitcoin::ExtPubkey.from_base58('xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ')
82
+ ```
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
87
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hdkey"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "hdkey"
7
+ spec.version = "1.0"
8
+ spec.authors = ["Shigeyuki Azuchi"]
9
+ spec.email = ["azuchi@haw.co.jp"]
10
+
11
+ spec.summary = %q{The implementation of Bitcoin hierarchical deterministic key for Ruby.}
12
+ spec.description = %q{The implementation of Bitcoin hierarchical deterministic key for Ruby.}
13
+ spec.homepage = "https://github.com/azuchi/hdkey"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "bitcoin-ruby", "~> 0.0.10"
22
+ spec.add_runtime_dependency "ffi"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,4 @@
1
+ require 'bitcoin'
2
+ module Bitcoin
3
+ autoload :ExtKey, 'hdkey/ext_key'
4
+ end
@@ -0,0 +1,189 @@
1
+ module Bitcoin
2
+
3
+ def self.hmac_sha512(key, data)
4
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new('SHA512'), key, data)
5
+ end
6
+
7
+ # Integers modulo the order of the curve(secp256k1)
8
+ CURVE_ORDER = 115792089237316195423570985008687907852837564279074904382605163141518161494337
9
+
10
+ # BIP32 Extended private key
11
+ class ExtKey
12
+
13
+ attr_accessor :depth
14
+ attr_accessor :number
15
+ attr_accessor :chain_code
16
+ attr_accessor :priv_key
17
+ attr_accessor :parent_fingerprint
18
+
19
+ # generate master key from seed.
20
+ def self.generate_master(seed)
21
+ key = ExtKey.new
22
+ key.depth = key.number = 0
23
+ key.parent_fingerprint = '00000000'
24
+ l = Bitcoin.hmac_sha512('Bitcoin seed', seed)
25
+ left = OpenSSL::BN.from_hex(l[0..31].bth).to_i
26
+ raise 'invalid key' if left >= CURVE_ORDER || left == 0
27
+ key.priv_key = Bitcoin::Key.new(l[0..31].bth)
28
+ key.chain_code = l[32..-1]
29
+ key
30
+ end
31
+
32
+ # get ExtPubkey from priv_key
33
+ def ext_pubkey
34
+ k = ExtPubkey.new
35
+ k.depth = depth
36
+ k.number = number
37
+ k.parent_fingerprint = parent_fingerprint
38
+ k.chain_code = chain_code
39
+ key = Bitcoin::Key.new(nil, priv_key.pub, compressed: true)
40
+ k.pub_key = key.key.public_key
41
+ k
42
+ end
43
+
44
+ # serialize extended private key
45
+ def to_payload
46
+ Bitcoin.network[:extended_privkey_version].htb << [depth].pack('C') << parent_fingerprint.htb << [number].pack('N') << chain_code << [0x00].pack('C') << priv_key.priv.htb
47
+ end
48
+
49
+ # Base58 encoded extended private key
50
+ def to_base58
51
+ h = to_payload.bth
52
+ hex = h + Bitcoin.checksum(h)
53
+ Bitcoin.encode_base58(hex)
54
+ end
55
+
56
+ # get private key(hex)
57
+ def priv
58
+ priv_key.priv
59
+ end
60
+
61
+ # get public key(hex)
62
+ def pub
63
+ priv_key.pub
64
+ end
65
+
66
+ # get address
67
+ def addr
68
+ priv_key.addr
69
+ end
70
+
71
+ # get key identifier
72
+ def identifier
73
+ Bitcoin.hash160(priv_key.pub)
74
+ end
75
+
76
+ # get fingerprint
77
+ def fingerprint
78
+ identifier.slice(0..7)
79
+ end
80
+
81
+ # derive new key
82
+ def derive(number)
83
+ new_key = ExtKey.new
84
+ new_key.depth = depth + 1
85
+ new_key.number = number
86
+ new_key.parent_fingerprint = fingerprint
87
+ if number > (2**31 -1)
88
+ data = [0x00].pack('C') << priv_key.priv.htb << [number].pack('N')
89
+ else
90
+ data = priv_key.pub.htb << [number].pack('N')
91
+ end
92
+ l = Bitcoin.hmac_sha512(chain_code, data)
93
+ left = OpenSSL::BN.from_hex(l[0..31].bth).to_i
94
+ raise 'invalid key' if left >= CURVE_ORDER
95
+ child_priv = OpenSSL::BN.new((left + OpenSSL::BN.from_hex(priv_key.priv).to_i) % CURVE_ORDER)
96
+ raise 'invalid key ' if child_priv.to_i >= CURVE_ORDER
97
+ new_key.priv_key = Bitcoin::Key.new(child_priv.to_hex.rjust(64, '0'))
98
+ new_key.chain_code = l[32..-1]
99
+ new_key
100
+ end
101
+
102
+ # import private key from Base58 private key address
103
+ def self.from_base58(address)
104
+ data = StringIO.new(Bitcoin.decode_base58(address).htb)
105
+ key = ExtKey.new
106
+ data.read(4).bth # version
107
+ key.depth = data.read(1).unpack('C').first
108
+ key.parent_fingerprint = data.read(4).bth
109
+ key.number = data.read(4).unpack('N').first
110
+ key.chain_code = data.read(32)
111
+ data.read(1) # 0x00
112
+ key.priv_key = Bitcoin::Key.new(data.read(32).bth)
113
+ key
114
+ end
115
+
116
+ end
117
+
118
+ # BIP-32 Extended public key
119
+ class ExtPubkey
120
+ attr_accessor :depth
121
+ attr_accessor :number
122
+ attr_accessor :chain_code
123
+ attr_accessor :pub_key
124
+ attr_accessor :parent_fingerprint
125
+
126
+ # serialize extended pubkey
127
+ def to_payload
128
+ Bitcoin.network[:extended_pubkey_version].htb << [depth].pack('C') << parent_fingerprint.htb << [number].pack('N') << chain_code << pub.htb
129
+ end
130
+
131
+ # get public key(hex)
132
+ def pub
133
+ pub_key.group.point_conversion_form = :compressed
134
+ pub_key.to_hex.rjust(66, '0')
135
+ end
136
+
137
+ # get address
138
+ def addr
139
+ Bitcoin.hash160_to_address(Bitcoin.hash160(pub))
140
+ end
141
+
142
+ # get key identifier
143
+ def identifier
144
+ Bitcoin.hash160(pub)
145
+ end
146
+
147
+ # get fingerprint
148
+ def fingerprint
149
+ identifier.slice(0..7)
150
+ end
151
+
152
+ # Base58 encoded extended pubkey
153
+ def to_base58
154
+ h = to_payload.bth
155
+ hex = h + Bitcoin.checksum(h)
156
+ Bitcoin.encode_base58(hex)
157
+ end
158
+
159
+ # derive child key
160
+ def derive(number)
161
+ new_key = ExtPubkey.new
162
+ new_key.depth = depth + 1
163
+ new_key.number = number
164
+ new_key.parent_fingerprint = fingerprint
165
+ raise 'hardened key is not support' if number > (2**31 -1)
166
+ data = pub.htb << [number].pack('N')
167
+ l = Bitcoin.hmac_sha512(chain_code, data)
168
+ left = OpenSSL::BN.from_hex(l[0..31].bth)
169
+ raise 'invalid key' if left.to_i >= CURVE_ORDER
170
+ new_key.pub_key = bitcoin_elliptic_curve.group.generator.mul(left).ec_add(pub_key)
171
+ new_key.chain_code = l[32..-1]
172
+ new_key
173
+ end
174
+
175
+ # import private key from Base58 private key address
176
+ def self.from_base58(address)
177
+ data = StringIO.new(Bitcoin.decode_base58(address).htb)
178
+ key = ExtPubkey.new
179
+ data.read(4).bth # version
180
+ key.depth = data.read(1).unpack('C').first
181
+ key.parent_fingerprint = data.read(4).bth
182
+ key.number = data.read(4).unpack('N').first
183
+ key.chain_code = data.read(32)
184
+ key.pub_key = OpenSSL::PKey::EC::Point.from_hex(bitcoin_elliptic_curve.group, data.read(33).bth)
185
+ key
186
+ end
187
+ end
188
+
189
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hdkey
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Shigeyuki Azuchi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bitcoin-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.10
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.10
27
+ - !ruby/object:Gem::Dependency
28
+ name: ffi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: The implementation of Bitcoin hierarchical deterministic key for Ruby.
70
+ email:
71
+ - azuchi@haw.co.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".ruby-gemset"
79
+ - ".ruby-version"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - hdkey.gemspec
89
+ - lib/hdkey.rb
90
+ - lib/hdkey/ext_key.rb
91
+ homepage: https://github.com/azuchi/hdkey
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: The implementation of Bitcoin hierarchical deterministic key for Ruby.
115
+ test_files: []