substrate_common.rb 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/Gemfile.lock +5 -4
- data/Rakefile +12 -1
- data/lib/substrate_common.rb +9 -23
- data/lib/substrate_common/address.rb +84 -0
- data/lib/substrate_common/version.rb +1 -1
- data/substrate_common.gemspec +3 -2
- metadata +13 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c601856518e431bc9a8a4623a659406c6d7a9d1826fcbb8b6cca5e0a90a06bfa
|
4
|
+
data.tar.gz: 71d880364a9777dffe1dc3f70b0dedfc2ea44c2bb12e4200d66d55ed5350f38e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3876a5c03ff8215684cf984c73ace0674045d9964c4d55f1628a66ac2c482f93f0b11ab235dabf71a6c611d15876c2d72a07e297da4bbafdf0eee31239810bd
|
7
|
+
data.tar.gz: ecefaeb3df900db65c6e48354814cd6a2792b9a144c695e39757e18557e711ea62f75956695d0694f60e1221a300850367b884d0f87b72f96f0e19ff11ea66f6
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.4
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
substrate_common.rb (0.1.
|
4
|
+
substrate_common.rb (0.1.1)
|
5
5
|
base58
|
6
6
|
blake2b
|
7
7
|
xxhash
|
@@ -13,19 +13,20 @@ GEM
|
|
13
13
|
blake2b (0.10.0)
|
14
14
|
coderay (1.1.2)
|
15
15
|
method_source (0.9.2)
|
16
|
+
minitest (5.12.2)
|
16
17
|
pry (0.12.2)
|
17
18
|
coderay (~> 1.1.0)
|
18
19
|
method_source (~> 0.9.0)
|
19
|
-
rake (
|
20
|
+
rake (12.3.1)
|
20
21
|
xxhash (0.4.0)
|
21
22
|
|
22
23
|
PLATFORMS
|
23
24
|
ruby
|
24
25
|
|
25
26
|
DEPENDENCIES
|
26
|
-
|
27
|
+
minitest
|
27
28
|
pry
|
28
|
-
rake (~>
|
29
|
+
rake (~> 12.0)
|
29
30
|
substrate_common.rb!
|
30
31
|
|
31
32
|
BUNDLED WITH
|
data/Rakefile
CHANGED
@@ -1,2 +1,13 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
-
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.test_files = Dir.glob("#{__dir__}/test/*_test.rb")
|
7
|
+
t.warning = false
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => :test
|
12
|
+
|
13
|
+
|
data/lib/substrate_common.rb
CHANGED
@@ -3,28 +3,31 @@ require 'xxhash'
|
|
3
3
|
require 'blake2b'
|
4
4
|
require 'base58'
|
5
5
|
|
6
|
+
|
7
|
+
require 'substrate_common/address'
|
8
|
+
|
6
9
|
class Array
|
7
|
-
def
|
10
|
+
def bytes_to_hex
|
8
11
|
raise "Not a byte array" unless self.is_byte_array?
|
9
12
|
'0x' + self.map { |b| b.to_s(16).rjust(2, '0') }.join
|
10
13
|
end
|
11
14
|
|
12
|
-
def
|
15
|
+
def bytes_to_bin
|
13
16
|
raise "Not a byte array" unless self.is_byte_array?
|
14
17
|
'0b' + self.map { |b| b.to_s(2).rjust(8, '0') }.join
|
15
18
|
end
|
16
19
|
|
17
|
-
def
|
20
|
+
def bytes_to_hex
|
18
21
|
raise "Not a byte array" unless self.is_byte_array?
|
19
22
|
self.map { |b| b.to_s(16).rjust(2, '0') }
|
20
23
|
end
|
21
24
|
|
22
|
-
def
|
25
|
+
def bytes_to_bin
|
23
26
|
raise "Not a byte array" unless self.is_byte_array?
|
24
27
|
self.map { |b| b.to_s(2).rjust(8, '0') }
|
25
28
|
end
|
26
29
|
|
27
|
-
def
|
30
|
+
def bytes_to_utf8
|
28
31
|
raise "Not a byte array" unless self.is_byte_array?
|
29
32
|
self.pack('C*').force_encoding('utf-8')
|
30
33
|
end
|
@@ -39,7 +42,7 @@ class String
|
|
39
42
|
Object.const_get(self)
|
40
43
|
end
|
41
44
|
|
42
|
-
def
|
45
|
+
def hex_to_bytes
|
43
46
|
data = self.start_with?('0x') ? self[2..] : self
|
44
47
|
raise "Not valid hex string" if data.length % 2 != 0
|
45
48
|
data.scan(/../).map(&:hex)
|
@@ -54,20 +57,3 @@ def xxhash128(data)
|
|
54
57
|
end
|
55
58
|
bytes.to_hex_string
|
56
59
|
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,84 @@
|
|
1
|
+
require 'blake2b'
|
2
|
+
require 'base58'
|
3
|
+
|
4
|
+
class Address
|
5
|
+
SS58_PREFIX = 'SS58PRE'
|
6
|
+
|
7
|
+
TYPES = [
|
8
|
+
# Polkadot Live (SS58, AccountId)
|
9
|
+
0, 1,
|
10
|
+
# Polkadot Canary (SS58, AccountId)
|
11
|
+
2, 3,
|
12
|
+
# Kulupu (SS58, Reserved)
|
13
|
+
16, 17,
|
14
|
+
# Dothereum (SS58, AccountId)
|
15
|
+
20, 21,
|
16
|
+
# Generic Substrate wildcard (SS58, AccountId)
|
17
|
+
42, 43,
|
18
|
+
|
19
|
+
# Schnorr/Ristretto 25519 ("S/R 25519") key
|
20
|
+
48,
|
21
|
+
# Edwards Ed25519 key
|
22
|
+
49,
|
23
|
+
# ECDSA SECP256k1 key
|
24
|
+
50,
|
25
|
+
|
26
|
+
# Reserved for future address format extensions.
|
27
|
+
*64..255
|
28
|
+
]
|
29
|
+
|
30
|
+
class << self
|
31
|
+
|
32
|
+
def array_to_hex_string(arr)
|
33
|
+
body = arr.map { |i| i.to_s(16).rjust(2, '0') }.join
|
34
|
+
"0x#{body}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def decode(address, addr_type = 42, ignore_checksum = true)
|
38
|
+
decoded = Base58.base58_to_binary(address, :bitcoin)
|
39
|
+
is_pubkey = decoded.size == 35
|
40
|
+
|
41
|
+
size = decoded.size - ( is_pubkey ? 2 : 1 )
|
42
|
+
|
43
|
+
prefix = decoded[0, 1].unpack("C*").first
|
44
|
+
|
45
|
+
raise "Invalid address type" unless TYPES.include?(addr_type)
|
46
|
+
|
47
|
+
hash_bytes = make_hash(decoded[0, size])
|
48
|
+
if is_pubkey
|
49
|
+
is_valid_checksum = decoded[-2].unpack("C*").first == hash_bytes[0] && decoded[-1].unpack("C*").first == hash_bytes[1]
|
50
|
+
else
|
51
|
+
is_valid_checksum = decoded[-1].unpack("C*").first == hash_bytes[0]
|
52
|
+
end
|
53
|
+
|
54
|
+
raise "Invalid decoded address checksum" unless is_valid_checksum && ignore_checksum
|
55
|
+
|
56
|
+
decoded[1...size].unpack("H*").first
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
def encode(pubkey, addr_type = 42)
|
61
|
+
pubkey = pubkey[2..-1] if pubkey =~ /^0x/i
|
62
|
+
key = [pubkey].pack("H*")
|
63
|
+
|
64
|
+
u8_array = key.bytes
|
65
|
+
|
66
|
+
u8_array.unshift(addr_type)
|
67
|
+
|
68
|
+
bytes = make_hash(u8_array.pack("C*"))
|
69
|
+
|
70
|
+
checksum = bytes[0, key.size == 32 ? 2 : 1]
|
71
|
+
|
72
|
+
u8_array.push(*checksum)
|
73
|
+
|
74
|
+
input = u8_array.pack("C*")
|
75
|
+
|
76
|
+
Base58.binary_to_base58(input, :bitcoin)
|
77
|
+
end
|
78
|
+
|
79
|
+
def make_hash(body)
|
80
|
+
Blake2b.bytes("#{SS58_PREFIX}#{body}", Blake2b::Key.none, 64)
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
data/substrate_common.gemspec
CHANGED
@@ -38,7 +38,8 @@ Gem::Specification.new do |spec|
|
|
38
38
|
spec.add_dependency "xxhash"
|
39
39
|
spec.add_dependency "base58"
|
40
40
|
|
41
|
-
spec.add_development_dependency "bundler", "~> 1.17"
|
42
|
-
spec.add_development_dependency "rake", "~>
|
41
|
+
# spec.add_development_dependency "bundler", "~> 1.17"
|
42
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
43
43
|
spec.add_development_dependency "pry"
|
44
|
+
spec.add_development_dependency "minitest"
|
44
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: substrate_common.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wu Minzhe
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: blake2b
|
@@ -53,35 +53,35 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '12.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '12.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: pry
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: minitest
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -102,6 +102,7 @@ extensions: []
|
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
104
|
- ".gitignore"
|
105
|
+
- ".ruby-version"
|
105
106
|
- Gemfile
|
106
107
|
- Gemfile.lock
|
107
108
|
- LICENSE.txt
|
@@ -110,6 +111,7 @@ files:
|
|
110
111
|
- bin/console
|
111
112
|
- bin/setup
|
112
113
|
- lib/substrate_common.rb
|
114
|
+
- lib/substrate_common/address.rb
|
113
115
|
- lib/substrate_common/version.rb
|
114
116
|
- substrate_common.gemspec
|
115
117
|
homepage: https://github.com/wuminzhe/substrate_common.rb
|