bitcoinrb 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bitcoinrb.gemspec +32 -0
- data/exe/bitcoinrb-cli +5 -0
- data/exe/bitcoinrbd +49 -0
- data/lib/bitcoin.rb +121 -0
- data/lib/bitcoin/base58.rb +40 -0
- data/lib/bitcoin/block_header.rb +41 -0
- data/lib/bitcoin/chain_params.rb +57 -0
- data/lib/bitcoin/chainparams/mainnet.yml +25 -0
- data/lib/bitcoin/chainparams/regtest.yml +20 -0
- data/lib/bitcoin/chainparams/testnet.yml +24 -0
- data/lib/bitcoin/connection.rb +66 -0
- data/lib/bitcoin/ext_key.rb +205 -0
- data/lib/bitcoin/key.rb +131 -0
- data/lib/bitcoin/logger.rb +18 -0
- data/lib/bitcoin/merkle_tree.rb +120 -0
- data/lib/bitcoin/message.rb +42 -0
- data/lib/bitcoin/message/addr.rb +74 -0
- data/lib/bitcoin/message/base.rb +40 -0
- data/lib/bitcoin/message/block.rb +41 -0
- data/lib/bitcoin/message/error.rb +10 -0
- data/lib/bitcoin/message/fee_filter.rb +27 -0
- data/lib/bitcoin/message/filter_add.rb +28 -0
- data/lib/bitcoin/message/filter_clear.rb +17 -0
- data/lib/bitcoin/message/filter_load.rb +43 -0
- data/lib/bitcoin/message/get_addr.rb +17 -0
- data/lib/bitcoin/message/get_blocks.rb +29 -0
- data/lib/bitcoin/message/get_data.rb +21 -0
- data/lib/bitcoin/message/get_headers.rb +28 -0
- data/lib/bitcoin/message/handler.rb +170 -0
- data/lib/bitcoin/message/headers.rb +34 -0
- data/lib/bitcoin/message/headers_parser.rb +24 -0
- data/lib/bitcoin/message/inv.rb +21 -0
- data/lib/bitcoin/message/inventories_parser.rb +23 -0
- data/lib/bitcoin/message/inventory.rb +47 -0
- data/lib/bitcoin/message/mem_pool.rb +17 -0
- data/lib/bitcoin/message/merkle_block.rb +42 -0
- data/lib/bitcoin/message/not_found.rb +29 -0
- data/lib/bitcoin/message/ping.rb +30 -0
- data/lib/bitcoin/message/pong.rb +26 -0
- data/lib/bitcoin/message/reject.rb +46 -0
- data/lib/bitcoin/message/send_cmpct.rb +43 -0
- data/lib/bitcoin/message/send_headers.rb +16 -0
- data/lib/bitcoin/message/tx.rb +30 -0
- data/lib/bitcoin/message/ver_ack.rb +17 -0
- data/lib/bitcoin/message/version.rb +79 -0
- data/lib/bitcoin/mnemonic.rb +76 -0
- data/lib/bitcoin/mnemonic/wordlist/chinese_simplified.txt +2048 -0
- data/lib/bitcoin/mnemonic/wordlist/chinese_traditional.txt +2048 -0
- data/lib/bitcoin/mnemonic/wordlist/english.txt +2048 -0
- data/lib/bitcoin/mnemonic/wordlist/french.txt +2048 -0
- data/lib/bitcoin/mnemonic/wordlist/italian.txt +2048 -0
- data/lib/bitcoin/mnemonic/wordlist/japanese.txt +2048 -0
- data/lib/bitcoin/mnemonic/wordlist/spanish.txt +2048 -0
- data/lib/bitcoin/nodes.rb +5 -0
- data/lib/bitcoin/nodes/spv.rb +13 -0
- data/lib/bitcoin/nodes/spv/cli.rb +12 -0
- data/lib/bitcoin/nodes/spv/daemon.rb +21 -0
- data/lib/bitcoin/opcodes.rb +172 -0
- data/lib/bitcoin/out_point.rb +31 -0
- data/lib/bitcoin/script/script.rb +347 -0
- data/lib/bitcoin/script/script_error.rb +168 -0
- data/lib/bitcoin/script/script_interpreter.rb +694 -0
- data/lib/bitcoin/script/tx_checker.rb +44 -0
- data/lib/bitcoin/script_witness.rb +29 -0
- data/lib/bitcoin/secp256k1.rb +10 -0
- data/lib/bitcoin/secp256k1/native.rb +22 -0
- data/lib/bitcoin/secp256k1/ruby.rb +96 -0
- data/lib/bitcoin/tx.rb +191 -0
- data/lib/bitcoin/tx_in.rb +45 -0
- data/lib/bitcoin/tx_out.rb +32 -0
- data/lib/bitcoin/util.rb +105 -0
- data/lib/bitcoin/version.rb +3 -0
- metadata +256 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 96f0a34fb5dcdaef395f91d69a4d9b23b0ef0e9a
|
4
|
+
data.tar.gz: 54cb847c45147461350da5bf2f6f7aa88e129858
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a151846d7ea230bd203dab08181dec34052cce1700c505a123b8220d80f1f7ff7fa296b90bbd5c639a725bb9e756661a0c77449b1731843e60526c793b8436c0
|
7
|
+
data.tar.gz: 87dbd90b24338fb0088f92e5bc1083e2749cdffa5729395433371808e20e74126ed395e01f3eaa5b73e3a35d7211ddd1137c3e7ca77a35c951531f809f32c3f3
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
bitcoinrb
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 HAW International, 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
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Bitcoinrb
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bitcoinrb`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'bitcoinrb'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install bitcoinrb
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bitcoinrb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bitcoin"
|
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
|
data/bin/setup
ADDED
data/bitcoinrb.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bitcoin/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bitcoinrb"
|
8
|
+
spec.version = Bitcoin::VERSION
|
9
|
+
spec.authors = ["azuchi"]
|
10
|
+
spec.email = ["azuchi@haw.co.jp"]
|
11
|
+
|
12
|
+
spec.summary = %q{[WIP]The implementation of Bitcoin Protocol for Ruby.}
|
13
|
+
spec.description = %q{[WIP]The implementation of Bitcoin Protocol for Ruby.}
|
14
|
+
spec.homepage = 'https://github.com/haw-itn/bitcoinrb'
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency 'ecdsa'
|
23
|
+
spec.add_runtime_dependency 'eventmachine'
|
24
|
+
spec.add_runtime_dependency 'murmurhash3'
|
25
|
+
spec.add_runtime_dependency 'bech32', '~> 1.0.1'
|
26
|
+
spec.add_runtime_dependency 'daemon-spawn'
|
27
|
+
spec.add_runtime_dependency 'thor'
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
end
|
data/exe/bitcoinrb-cli
ADDED
data/exe/bitcoinrbd
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'thor'
|
3
|
+
require 'bitcoin'
|
4
|
+
require 'bitcoin/nodes/spv/daemon'
|
5
|
+
|
6
|
+
class Bitcoinrbd < Thor
|
7
|
+
|
8
|
+
option :mode, default: 'spv'
|
9
|
+
option :network, default: 'mainnet'
|
10
|
+
desc 'start', 'start bitcoinrbd daemon process'
|
11
|
+
def start
|
12
|
+
raise ArgumentError, 'currently only support spv mode.' unless options[:mode] == 'spv'
|
13
|
+
execute_daemon(setup_base_dir(options[:network]), ['start'])
|
14
|
+
end
|
15
|
+
|
16
|
+
option :mode, default: 'spv'
|
17
|
+
option :network, default: 'mainnet'
|
18
|
+
desc 'stop', 'start bitcoinrbd daemon process'
|
19
|
+
def stop
|
20
|
+
execute_daemon(setup_base_dir(options[:network]), ['stop'])
|
21
|
+
end
|
22
|
+
|
23
|
+
def execute_daemon(base_dir, cmd_args)
|
24
|
+
Bitcoin::Nodes::SPV::Daemon.spawn!({working_dir: base_dir,
|
25
|
+
log_file: "#{base_dir}/bitcoinrbd.log",
|
26
|
+
pid_file: "#{base_dir}/bitcoinrbd.pid",
|
27
|
+
sync_log: true,
|
28
|
+
singleton: true}, cmd_args)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def setup_base_dir(network)
|
34
|
+
case network
|
35
|
+
when 'mainnet'
|
36
|
+
base_dir = Bitcoin.base_dir
|
37
|
+
when 'testnet'
|
38
|
+
base_dir = "#{Bitcoin.base_dir}/testnet3"
|
39
|
+
when 'regtest'
|
40
|
+
base_dir = "#{Bitcoin.base_dir}/regtest"
|
41
|
+
else
|
42
|
+
raise ArgumentError, 'Unsupported network specified.'
|
43
|
+
end
|
44
|
+
FileUtils.mkdir_p(base_dir)
|
45
|
+
base_dir
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
Bitcoinrbd.start(ARGV)
|
data/lib/bitcoin.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'bitcoin/version'
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'ecdsa'
|
4
|
+
require 'securerandom'
|
5
|
+
require 'json'
|
6
|
+
require 'bech32'
|
7
|
+
|
8
|
+
module Bitcoin
|
9
|
+
|
10
|
+
autoload :Util, 'bitcoin/util'
|
11
|
+
autoload :ChainParams, 'bitcoin/chain_params'
|
12
|
+
autoload :Message, 'bitcoin/message'
|
13
|
+
autoload :Connection, 'bitcoin/connection'
|
14
|
+
autoload :Logger, 'bitcoin/logger'
|
15
|
+
autoload :BlockHeader, 'bitcoin/block_header'
|
16
|
+
autoload :Tx, 'bitcoin/tx'
|
17
|
+
autoload :Script, 'bitcoin/script/script'
|
18
|
+
autoload :ScriptInterpreter, 'bitcoin/script/script_interpreter'
|
19
|
+
autoload :ScriptError, 'bitcoin/script/script_error'
|
20
|
+
autoload :TxChecker, 'bitcoin/script/tx_checker'
|
21
|
+
autoload :TxIn, 'bitcoin/tx_in'
|
22
|
+
autoload :TxOut, 'bitcoin/tx_out'
|
23
|
+
autoload :OutPoint, 'bitcoin/out_point'
|
24
|
+
autoload :ScriptWitness, 'bitcoin/script_witness'
|
25
|
+
autoload :MerkleTree, 'bitcoin/merkle_tree'
|
26
|
+
autoload :Key, 'bitcoin/key'
|
27
|
+
autoload :ExtKey, 'bitcoin/ext_key'
|
28
|
+
autoload :Opcodes, 'bitcoin/opcodes'
|
29
|
+
autoload :Node, 'bitcoin/nodes'
|
30
|
+
autoload :Base58, 'bitcoin/base58'
|
31
|
+
autoload :Secp256k1, 'bitcoin/secp256k1'
|
32
|
+
autoload :Mnemonic, 'bitcoin/mnemonic'
|
33
|
+
|
34
|
+
extend Util
|
35
|
+
|
36
|
+
@chain_param = :mainnet
|
37
|
+
|
38
|
+
# set bitcoin network chain params
|
39
|
+
def self.chain_params=(name)
|
40
|
+
raise "chain params for #{name} is not defined." unless %i(mainnet testnet regtest).include?(name.to_sym)
|
41
|
+
@current_chain = nil
|
42
|
+
@chain_param = name.to_sym
|
43
|
+
end
|
44
|
+
|
45
|
+
# current bitcoin network chain params.
|
46
|
+
def self.chain_params
|
47
|
+
return @current_chain if @current_chain
|
48
|
+
case @chain_param
|
49
|
+
when :mainnet
|
50
|
+
Bitcoin::ChainParams.mainnet
|
51
|
+
when :testnet
|
52
|
+
Bitcoin::ChainParams.testnet
|
53
|
+
when :regtest
|
54
|
+
Bitcoin::ChainParams.regtest
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# base dir path that store blockchain data and wallet data
|
59
|
+
def self.base_dir
|
60
|
+
"#{Dir.home}/.bitcoinrb"
|
61
|
+
end
|
62
|
+
|
63
|
+
# get secp implementation module
|
64
|
+
def self.secp_impl
|
65
|
+
path = ENV['SECP256K1_LIB_PATH']
|
66
|
+
(path && File.exist?(path)) ? Bitcoin::Secp256k1::Native : Bitcoin::Secp256k1::Ruby
|
67
|
+
end
|
68
|
+
|
69
|
+
class ::String
|
70
|
+
# binary convert to hex string
|
71
|
+
def bth
|
72
|
+
unpack('H*').first
|
73
|
+
end
|
74
|
+
|
75
|
+
# hex string convert to binary
|
76
|
+
def htb
|
77
|
+
[self].pack('H*')
|
78
|
+
end
|
79
|
+
|
80
|
+
# get opcode
|
81
|
+
def opcode
|
82
|
+
case encoding
|
83
|
+
when Encoding::ASCII_8BIT
|
84
|
+
each_byte.next
|
85
|
+
when Encoding::US_ASCII
|
86
|
+
ord
|
87
|
+
else
|
88
|
+
to_i
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def opcode?
|
93
|
+
!pushdata?
|
94
|
+
end
|
95
|
+
|
96
|
+
def push_opcode?
|
97
|
+
[Bitcoin::Opcodes::OP_PUSHDATA1, Bitcoin::Opcodes::OP_PUSHDATA2, Bitcoin::Opcodes::OP_PUSHDATA4].include?(opcode)
|
98
|
+
end
|
99
|
+
|
100
|
+
# whether data push only?
|
101
|
+
def pushdata?
|
102
|
+
opcode <= Bitcoin::Opcodes::OP_PUSHDATA4
|
103
|
+
end
|
104
|
+
|
105
|
+
def pushed_data
|
106
|
+
return nil unless pushdata?
|
107
|
+
offset = 1
|
108
|
+
case opcode
|
109
|
+
when Bitcoin::Opcodes::OP_PUSHDATA1
|
110
|
+
offset += 1
|
111
|
+
when Bitcoin::Opcodes::OP_PUSHDATA2
|
112
|
+
offset += 2
|
113
|
+
when Bitcoin::Opcodes::OP_PUSHDATA4
|
114
|
+
offset += 4
|
115
|
+
end
|
116
|
+
self[offset..-1]
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Bitcoin
|
2
|
+
|
3
|
+
# Base58Check encoding
|
4
|
+
# https://en.bitcoin.it/wiki/Base58Check_encoding
|
5
|
+
module Base58
|
6
|
+
|
7
|
+
module_function
|
8
|
+
|
9
|
+
ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
10
|
+
SIZE = ALPHABET.size
|
11
|
+
|
12
|
+
# encode hex value to base58 string.
|
13
|
+
def encode(hex)
|
14
|
+
leading_zero_bytes = (hex.match(/^([0]+)/) ? $1 : '').size / 2
|
15
|
+
int_val = hex.to_i(16)
|
16
|
+
base58_val = ''
|
17
|
+
while int_val > 0
|
18
|
+
int_val, remainder = int_val.divmod(SIZE)
|
19
|
+
base58_val = ALPHABET[remainder] + base58_val
|
20
|
+
end
|
21
|
+
('1' * leading_zero_bytes) + base58_val
|
22
|
+
end
|
23
|
+
|
24
|
+
# decode base58 string to hex value.
|
25
|
+
def decode(base58_val)
|
26
|
+
int_val = 0
|
27
|
+
base58_val.reverse.split(//).each_with_index do |char,index|
|
28
|
+
raise ArgumentError, 'Value passed not a valid Base58 String.' if (char_index = ALPHABET.index(char)).nil?
|
29
|
+
int_val += char_index * (SIZE**index)
|
30
|
+
end
|
31
|
+
s = int_val.to_s(16)
|
32
|
+
s = (s.bytesize.odd? ? '0' + s : s)
|
33
|
+
s = '' if s == '00'
|
34
|
+
leading_zero_bytes = (base58_val.match(/^([1]+)/) ? $1 : '').size
|
35
|
+
s = ('00' * leading_zero_bytes) + s if leading_zero_bytes > 0
|
36
|
+
s
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|