substrate_common.rb 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f72ffe22e9039554feee750f0d5eef00f6cc95f63df3e20780174f1926b46530
4
+ data.tar.gz: 67ba8ed682462fc23ad1439982dada786d63527310c96a567ec2107c6eec8969
5
+ SHA512:
6
+ metadata.gz: 906b043f2735ab30950fbbf1204d28d31b769d550660b1fffafd6eaa42e90460b308c409dc332642dd7aafa01810d225a6ff33b9e057a9a6fefe3f8c0be4b1a3
7
+ data.tar.gz: 4167d6c20c335b432b9ef6a77e0348bb452ea085c06f96c5badaabf31f7a6ac31c20b9700eb30c308d7453ff4e219dedf8a283debdc892c413ccc8084e5112ad
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in substrate-common.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ substrate_common.rb (0.1.0)
5
+ base58
6
+ blake2b
7
+ xxhash
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ base58 (0.2.3)
13
+ blake2b (0.10.0)
14
+ coderay (1.1.2)
15
+ method_source (0.9.2)
16
+ pry (0.12.2)
17
+ coderay (~> 1.1.0)
18
+ method_source (~> 0.9.0)
19
+ rake (10.5.0)
20
+ xxhash (0.4.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.17)
27
+ pry
28
+ rake (~> 10.0)
29
+ substrate_common.rb!
30
+
31
+ BUNDLED WITH
32
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Wu Minzhe
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,39 @@
1
+ # substrate_common.rb
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/substrate/common`. 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 'substrate_common.rb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install substrate_common.rb
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]/substrate-common.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "substrate_common"
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(__FILE__)
data/bin/setup ADDED
@@ -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,3 @@
1
+ module SubstrateCommon
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,73 @@
1
+ require "substrate_common/version"
2
+ require 'xxhash'
3
+ require 'blake2b'
4
+ require 'base58'
5
+
6
+ class Array
7
+ def to_hex_string
8
+ raise "Not a byte array" unless self.is_byte_array?
9
+ '0x' + self.map { |b| b.to_s(16).rjust(2, '0') }.join
10
+ end
11
+
12
+ def to_bin_string
13
+ raise "Not a byte array" unless self.is_byte_array?
14
+ '0b' + self.map { |b| b.to_s(2).rjust(8, '0') }.join
15
+ end
16
+
17
+ def to_hex_array
18
+ raise "Not a byte array" unless self.is_byte_array?
19
+ self.map { |b| b.to_s(16).rjust(2, '0') }
20
+ end
21
+
22
+ def to_bin_array
23
+ raise "Not a byte array" unless self.is_byte_array?
24
+ self.map { |b| b.to_s(2).rjust(8, '0') }
25
+ end
26
+
27
+ def to_utf8
28
+ raise "Not a byte array" unless self.is_byte_array?
29
+ self.pack('C*').force_encoding('utf-8')
30
+ end
31
+
32
+ def is_byte_array?
33
+ self.all? {|e| e >= 0 and e <= 255 }
34
+ end
35
+ end
36
+
37
+ class String
38
+ def constantize
39
+ Object.const_get(self)
40
+ end
41
+
42
+ def to_byte_array
43
+ data = self.start_with?('0x') ? self[2..] : self
44
+ raise "Not valid hex string" if data.length % 2 != 0
45
+ data.scan(/../).map(&:hex)
46
+ end
47
+ end
48
+
49
+ def xxhash128(data)
50
+ bytes = []
51
+ 2.times do |i|
52
+ result = XXhash.xxh64 data, i
53
+ bytes = bytes + result.to_s(16).rjust(16, '0').to_byte_array.reverse
54
+ end
55
+ bytes.to_hex_string
56
+ end
57
+
58
+ # https://github.com/paritytech/substrate/wiki/External-Address-Format-(SS58)
59
+ # base58encode ( concat ( <address-type>, <address>, <checksum> ) )
60
+ def ss58_encode(address, address_type=42)
61
+ address_bytes = address.to_byte_array
62
+ checksum_length = address_bytes.length == 32 ? 2 : 1
63
+ input = [address_type] + address_bytes
64
+ checksum = sshash(input)
65
+ content = input + checksum[0...checksum_length]
66
+ Base58.encode(content.to_hex_string.to_i(16), :bitcoin)
67
+ end
68
+
69
+ def sshash(input)
70
+ checksum_prefix = 'SS58PRE'.unpack('C*')
71
+ data = checksum_prefix + input
72
+ Blake2b.bytes(input.to_utf8, Blake2b::Key.none, 64)
73
+ end
@@ -0,0 +1,44 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "substrate_common/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "substrate_common.rb"
8
+ spec.version = SubstrateCommon::VERSION
9
+ spec.authors = ["Wu Minzhe"]
10
+ spec.email = ["wuminzhe@gmail.com"]
11
+
12
+ spec.summary = %q{Common utilities and libraries for substrate in ruby}
13
+ spec.description = %q{Common utilities and libraries for substrate in ruby}
14
+ spec.homepage = "https://github.com/wuminzhe/substrate_common.rb"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_dependency "blake2b"
38
+ spec.add_dependency "xxhash"
39
+ spec.add_dependency "base58"
40
+
41
+ spec.add_development_dependency "bundler", "~> 1.17"
42
+ spec.add_development_dependency "rake", "~> 10.0"
43
+ spec.add_development_dependency "pry"
44
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: substrate_common.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wu Minzhe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: blake2b
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: xxhash
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: base58
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Common utilities and libraries for substrate in ruby
98
+ email:
99
+ - wuminzhe@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - lib/substrate_common.rb
113
+ - lib/substrate_common/version.rb
114
+ - substrate_common.gemspec
115
+ homepage: https://github.com/wuminzhe/substrate_common.rb
116
+ licenses:
117
+ - MIT
118
+ metadata:
119
+ allowed_push_host: https://rubygems.org
120
+ homepage_uri: https://github.com/wuminzhe/substrate_common.rb
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubygems_version: 3.0.6
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Common utilities and libraries for substrate in ruby
140
+ test_files: []