cryptocurrency 0.1.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
+ SHA256:
3
+ metadata.gz: 8804fd6a806b75b67944aeb1fda504d91b4f015010eafe047aa79aa93db7c5c6
4
+ data.tar.gz: f560e00dc4a575993b8462ce43f8dd6730d1e3904795bec081b7225cb9d48a3f
5
+ SHA512:
6
+ metadata.gz: 9257c94b52e53bfc366e79fd5112aeef3767e5438574cf8be02eaaa16e5325107de31ac0e74331286d7627539f7818bb164d0521cf414c281466d19476c3a724
7
+ data.tar.gz: 7939dbd5b5f2bc7d59365d366558475e5066653aeadc413915fc495eff850a0132a61299ec86fc4bfda3e644146b8405d6938fbcdb1457ff5b0f118d910f1829
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,17 @@
1
+ image: ruby:latest
2
+
3
+ rspec:
4
+ stage: test
5
+ script:
6
+ - apt-get update && apt-get install -yqq libsecp256k1-dev
7
+ - gem install bundler
8
+ - bundle install
9
+ - bundle exec rspec
10
+
11
+ publish:
12
+ stage: deploy
13
+ only:
14
+ - master
15
+ script:
16
+ - gem build cryptocurrency.gemspec
17
+ - gem push *.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.0
4
+
5
+ Style/FrozenStringLiteralComment:
6
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake', '~> 13.0'
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cryptocurrency (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (13.0.3)
11
+ rspec (3.10.0)
12
+ rspec-core (~> 3.10.0)
13
+ rspec-expectations (~> 3.10.0)
14
+ rspec-mocks (~> 3.10.0)
15
+ rspec-core (3.10.1)
16
+ rspec-support (~> 3.10.0)
17
+ rspec-expectations (3.10.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.10.0)
20
+ rspec-mocks (3.10.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-support (3.10.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ cryptocurrency!
30
+ rake (~> 13.0)
31
+ rspec
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
@@ -0,0 +1,7 @@
1
+ Copyright 2021 Crypto Jane
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # Cryptocurrency
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/cryptocurrency`. 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 'cryptocurrency'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cryptocurrency
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. 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]/cryptocurrency.
36
+
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: %i[]
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ #!/usr/bin/env ruby
4
+
5
+ require "bundler/setup"
6
+ require "cryptocurrency"
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require "irb"
16
+ IRB.start(__FILE__)
@@ -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,29 @@
1
+ require_relative 'lib/cryptocurrency/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'cryptocurrency'
5
+ spec.version = Cryptocurrency::VERSION
6
+ spec.authors = ['Crypto Jane']
7
+ spec.email = ['cryptojanedoe@protonmail.com']
8
+
9
+ spec.summary = 'Utility classes to securely work with cryptocurrencies.'
10
+ spec.description = 'Generate and verify addresses, sign transactions.'
11
+ spec.homepage = 'https://gitlab.com/cryptojane/cryptocurrency'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = spec.homepage
17
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_development_dependency 'rspec'
29
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'cryptocurrency/address'
2
+ require_relative 'cryptocurrency/base58'
3
+ require_relative 'cryptocurrency/bech32'
4
+ require_relative 'cryptocurrency/utils'
5
+ require_relative 'cryptocurrency/version'
6
+
7
+ module Cryptocurrency
8
+ end
@@ -0,0 +1,66 @@
1
+ require 'byebug'
2
+
3
+ module Cryptocurrency
4
+ module Address
5
+ def self.for(public_key, currency, kind = :p2pkh)
6
+ case kind
7
+ when :p2pkh
8
+ data = version(currency, kind) + hash(public_key, :bitcoin)
9
+ Base58.encode58_check(data)
10
+ when :segwit
11
+ script = "\x00\x14" + hash(public_key, :bitcoin)
12
+ data = version(currency, :p2sh) + Utils.hash160(script)
13
+ Base58.encode58_check(data)
14
+ when :segwit_native
15
+ Bech32.encode(BECH_PREFIXES[currency], "\x00" + hash(public_key, :bitcoin))
16
+ end
17
+ end
18
+
19
+ def self.hash(public_key, kind = :bitcoin)
20
+ case kind
21
+ when :bitcoin
22
+ Utils.hash160(public_key.to_s)
23
+ when :ethereum
24
+ Utils.kekkak(public_key.uncompressed)[-20..]
25
+ end
26
+ end
27
+
28
+ BECH_PREFIXES = {
29
+ btc: 'bc',
30
+ tbtc: 'tc',
31
+ ltc: 'ltc',
32
+ tltc: 'tltc'
33
+ }.freeze
34
+
35
+ PREFIXES = {
36
+ btc: {
37
+ p2pkh: "\x00",
38
+ p2sh: "\x05"
39
+ },
40
+ ltc: {
41
+ p2pkh: "\x30",
42
+ p2sh: "\x32"
43
+ },
44
+ doge: {
45
+ p2pkh: "\x1e",
46
+ p2sh: "\x16"
47
+ },
48
+ tbtc: {
49
+ p2pkh: "\x6f",
50
+ p2sh: "\xc4"
51
+ },
52
+ tltc: {
53
+ p2pkh: "\x6f",
54
+ p2sh: "\x3a"
55
+ },
56
+ tdoge: {
57
+ p2pkh: "\x71",
58
+ p2sh: "\xc4"
59
+ }
60
+ }.freeze
61
+
62
+ def self.version(currency, type)
63
+ PREFIXES[currency][type]
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,59 @@
1
+ require_relative './utils'
2
+
3
+ module Cryptocurrency
4
+ class Base58
5
+ ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.freeze
6
+
7
+ def self.encode58(data)
8
+ return '' if data.empty?
9
+
10
+ n = Utils.bytes_to_integer(data)
11
+
12
+ b58_bytes = []
13
+ while n > 0
14
+ b58_bytes << n % 58
15
+ n /= 58
16
+ end
17
+
18
+ zeroes = data.bytes.find_index { |b| b != 0 } || data.length
19
+ '1' * zeroes + b58_bytes.map do |v|
20
+ ALPHABET[v]
21
+ end.join.reverse
22
+ end
23
+
24
+ def self.decode58(data)
25
+ prefix = ''
26
+ in_prefix = true
27
+
28
+ value = 0
29
+
30
+ data.chars.each do |ch|
31
+ if ch == '1' && in_prefix
32
+ prefix += "\x00"
33
+ else
34
+ in_prefix = false
35
+
36
+ n = ALPHABET.index(ch)
37
+ value = (value * 58) + n
38
+ end
39
+ end
40
+
41
+ prefix + Utils.integer_to_bytes(value)
42
+ end
43
+
44
+ def self.encode58_check(data)
45
+ checksum = Utils.double_sha256(data)[0...4]
46
+ encode58(data + checksum)
47
+ end
48
+
49
+ def self.decode58_check(data)
50
+ bin = decode58(data)
51
+ value = bin[0...-4]
52
+ checksum = bin[-4..]
53
+
54
+ return unless checksum == Utils.double_sha256(value)[0...4]
55
+
56
+ value
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,107 @@
1
+ module Cryptocurrency
2
+ module Bech32
3
+ SEPARATOR = '1'.freeze
4
+ CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'.freeze
5
+
6
+ class << self
7
+ # Encode Bech32 string
8
+ def encode(hrp, data)
9
+ blocks = split_5bits(data)
10
+
11
+ checksummed = blocks + create_checksum(hrp, blocks)
12
+ hrp + SEPARATOR + checksummed.map { |i| CHARSET[i] }.join
13
+ end
14
+
15
+ # Decode a Bech32 string and determine hrp and data
16
+ def decode(bech)
17
+ # check invalid bytes
18
+ return nil if bech.scrub('?').include?('?')
19
+
20
+ # check uppercase/lowercase
21
+ return nil if bech.downcase != bech && bech.upcase != bech
22
+
23
+ bech.each_char { |c| return nil if c.ord < 33 || c.ord > 126 }
24
+ bech = bech.downcase
25
+
26
+ # check data length
27
+ pos = bech.rindex(SEPARATOR)
28
+ return nil if pos.nil? || pos < 1 || pos + 7 > bech.length || bech.length > 90
29
+
30
+ # check valid charset
31
+ bech[pos + 1..-1].each_char { |c| return nil unless CHARSET.include?(c) }
32
+
33
+ # split hrp and data
34
+ hrp = bech[0..pos - 1]
35
+ data = bech[pos + 1..-1].each_char.map { |c| CHARSET.index(c) }
36
+
37
+ # check checksum
38
+ return nil unless verify_checksum(hrp, data)
39
+
40
+ [hrp, join_5bits(data[0..-7])]
41
+ end
42
+
43
+ # Compute the checksum values given hrp and data.
44
+ def create_checksum(hrp, data)
45
+ values = expand_hrp(hrp) + data
46
+ polymod = polymod(values + [0, 0, 0, 0, 0, 0]) ^ 1
47
+ (0..5).map { |i| (polymod >> 5 * (5 - i)) & 31 }
48
+ end
49
+
50
+ # Verify a checksum given Bech32 string
51
+ def verify_checksum(hrp, data)
52
+ polymod(expand_hrp(hrp) + data) == 1
53
+ end
54
+
55
+ def split_5bits(data)
56
+ n = Utils.bytes_to_integer(data)
57
+
58
+ chunks = []
59
+ while n > 0
60
+ chunks << n % 32
61
+ n /= 32
62
+ end
63
+
64
+ zeroes = data.bytes.find_index { |b| b != 0 } || data.length
65
+
66
+ [0] * zeroes + chunks.reverse
67
+ end
68
+
69
+ def join_5bits(data)
70
+ n = 0
71
+ in_prefix = true
72
+ prefix = ''
73
+
74
+ data.each do |i|
75
+ if in_prefix && i == 0
76
+ prefix += "\x00"
77
+ else
78
+ in_prefix = false
79
+ end
80
+
81
+ n = n * 32 + i
82
+ end
83
+
84
+ prefix + Utils.integer_to_bytes(n)
85
+ end
86
+
87
+ private
88
+
89
+ # Expand the hrp into values for checksum computation.
90
+ def expand_hrp(hrp)
91
+ hrp.each_char.map { |c| c.ord >> 5 } + [0] + hrp.each_char.map { |c| c.ord & 31 }
92
+ end
93
+
94
+ # Compute Bech32 checksum
95
+ def polymod(values)
96
+ generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
97
+ chk = 1
98
+ values.each do |v|
99
+ top = chk >> 25
100
+ chk = (chk & 0x1ffffff) << 5 ^ v
101
+ (0..4).each { |i| chk ^= ((top >> i) & 1) == 0 ? 0 : generator[i] }
102
+ end
103
+ chk
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,46 @@
1
+ require 'digest'
2
+
3
+ module Cryptocurrency
4
+ module Utils
5
+ class << self
6
+ def ripemd160(data)
7
+ Digest::RMD160.digest(data)
8
+ end
9
+
10
+ def sha256(data)
11
+ Digest::SHA2.new(256).digest(data)
12
+ end
13
+
14
+ def double_sha256(data)
15
+ sha256(sha256(data))
16
+ end
17
+
18
+ def hash160(data)
19
+ ripemd160(sha256(data))
20
+ end
21
+
22
+ def hmac_sha512(key, data)
23
+ digest = OpenSSL::Digest.new('sha512')
24
+ OpenSSL::HMAC.digest(digest, key, data)
25
+ end
26
+
27
+ def bech32_encode(hrp, data)
28
+ Bech32.encode(hrp, data)
29
+ end
30
+
31
+ def bech32_decode(data)
32
+ Bech32.decode(data)
33
+ end
34
+
35
+ def bytes_to_integer(bytes)
36
+ bytes.unpack1('H*').to_i(16)
37
+ end
38
+
39
+ def integer_to_bytes(integer)
40
+ hex = integer.to_s(16)
41
+ hex = "0#{hex}" if hex.length.odd?
42
+ [hex].pack('H*')
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Cryptocurrency
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cryptocurrency
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Crypto Jane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Generate and verify addresses, sign transactions.
28
+ email:
29
+ - cryptojanedoe@protonmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".gitlab-ci.yml"
36
+ - ".rspec"
37
+ - ".rubocop.yml"
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.md
41
+ - README.md
42
+ - Rakefile
43
+ - bin/console
44
+ - bin/setup
45
+ - cryptocurrency.gemspec
46
+ - lib/cryptocurrency.rb
47
+ - lib/cryptocurrency/address.rb
48
+ - lib/cryptocurrency/base58.rb
49
+ - lib/cryptocurrency/bech32.rb
50
+ - lib/cryptocurrency/utils.rb
51
+ - lib/cryptocurrency/version.rb
52
+ homepage: https://gitlab.com/cryptojane/cryptocurrency
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://gitlab.com/cryptojane/cryptocurrency
57
+ source_code_uri: https://gitlab.com/cryptojane/cryptocurrency
58
+ allowed_push_host: https://rubygems.org
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.3.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.2.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Utility classes to securely work with cryptocurrencies.
78
+ test_files: []