sr25519 0.3.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9331720979c05775dd52810475846df3672445cf0da2d29369981d2cfe508ff2
4
- data.tar.gz: 685a8d5100fe6b15c2bf4d0f808812fda0e50b7d1ffc2572bef13788fb945578
3
+ metadata.gz: b113b67bbe4a743dfe19a186d10bf4e12b929037e9f11c80628205d5d20a8d1a
4
+ data.tar.gz: fd5d5f843cdd92ceba67cb1128f410425810641fc417915bfeb1cf79d41088b4
5
5
  SHA512:
6
- metadata.gz: c71efd717b9d1388cdc7722b7a284140fe8b846c0a172a3c2945e18507cd53298caa0a02534a1da901e600a11b7e560d875dbe8f6ae51596ce4843010a13c7e1
7
- data.tar.gz: 6188e1bfdb5fe02a824d01824163b3ea8b98584f49eea535c5f3e87fe02035f16b7669e534e7e958800ba4bb4ed3dbe1c9af21ac6e22688b66ab4e5d509e98ff
6
+ metadata.gz: a54153363c366648db12f5b10f3afa383ba368d412623c813c095033832f7c74c07b7bfd9fcb373296aa227ac9fd93d2a90a8dea2d7c74ad843691d7d408e3b0
7
+ data.tar.gz: 18cf9d2b4f91774660e598e82114ec789bca49fcf2e29ecdd17cfc5b6c751edaf915c6a75d7717d1d2c229288ade84570ac625ed47a6c985aa81d89e3383cbcc
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sr25519 (0.3.0)
4
+ sr25519 (0.7.0)
5
5
  base58
6
6
  blake2b
7
+ ed25519 (~> 1.2, >= 1.2.4)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
@@ -11,6 +12,7 @@ GEM
11
12
  base58 (0.2.3)
12
13
  blake2b (0.10.0)
13
14
  diff-lcs (1.4.4)
15
+ ed25519 (1.2.4)
14
16
  ffi (1.15.4)
15
17
  rake (13.0.6)
16
18
  rspec (3.10.0)
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 xuxh
3
+ Copyright (c) 2021 UniArts Network
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -22,21 +22,21 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- 1. Require ed25519.rb in your Ruby program:
25
+ #### 1. Require ed25519.rb in your Ruby program:
26
26
  ```ruby
27
27
  require "sr25519"
28
28
  ```
29
29
 
30
- 2. Generate keypair
30
+ #### 2. SR25519 Generate keypair
31
31
 
32
32
  ```ruby
33
33
  # seed is priviate key, is a hex string.
34
- # example: SR25519.sr25519_keypair_from_seed("0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e")
35
- keypaire = SR25519.sr25519_keypair_from_seed(seed)
34
+ # example: SR25519.keypair_from_seed("0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e")
35
+ keypair = SR25519.keypair_from_seed(seed)
36
36
 
37
37
  ```
38
38
 
39
- 3. Generate public_key
39
+ #### 3. SR25519 Generate public_key
40
40
 
41
41
  ```ruby
42
42
  public_key = SR25519.get_public_key_from_seed(seed)
@@ -46,30 +46,58 @@ public_key_str = public_key.to_s
46
46
 
47
47
  ```
48
48
 
49
- 4. Encode address
49
+ #### 4. Encode address
50
50
  ```ruby
51
51
  address = Address.encode(public_key.to_s)
52
52
  ```
53
53
 
54
- 5. Decode address
54
+ #### 5. Decode address
55
55
 
56
56
  ```ruby
57
- address = Address.decode(address)
57
+ public_key_str = Address.decode(address)
58
58
  ```
59
59
 
60
- 6. Sign message
60
+ #### 6. SR25519 Sign message
61
61
 
62
62
  ```ruby
63
+ # message = "Hello World"
63
64
  signature_result = SR25519.sign(message, keypair)
64
65
 
65
66
  ```
66
67
 
67
- 7. Verify message
68
+ #### 7. SR25519 Verify message
68
69
 
69
70
  ```ruby
70
- SR25519.verify(address, message, signature_result)
71
+ verify_result = SR25519.verify(address, message, signature_result)
71
72
  ```
72
73
 
74
+ #### 8. ED25519 Generate keypair
75
+
76
+ ```ruby
77
+ keypair = ED25519.keypair_from_seed(seed)
78
+ ```
79
+
80
+ #### 9. ED25519 Sign message
81
+
82
+ ```ruby
83
+ signature_result = ED25519.sign(message, keypair)
84
+ ```
85
+
86
+ #### 10. ED25519 Get public key
87
+
88
+ ```ruby
89
+ pulick_key_str = ED25519.get_public_key_from_seed(seed)
90
+ ```
91
+
92
+ #### 11. ED25519 Verify message
93
+
94
+ ```ruby
95
+ # public_key_str = ED25519.get_public_key_from_seed(seed)
96
+ # address = Address.encode(public_key_str)
97
+ verify_result = ED25519.verify(address, message, signature_result)
98
+ ```
99
+
100
+
73
101
  ## Running tests
74
102
  1. Run all tests
75
103
 
data/lib/address.rb CHANGED
@@ -1,7 +1,30 @@
1
+
2
+ # The MIT License (MIT)
3
+
4
+ # Copyright (c) 2019 Wu Minzhe
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
1
24
  require 'base58'
2
25
  require 'blake2b'
3
26
 
4
- # The code is copy from https://github.com/itering/scale.rb/blob/develop/lib/common.rb
27
+ # The code is copy from https://github.com/itering/scale.rb/blob/develop/lib/common.rb
5
28
  class Address
6
29
  SS58_PREFIX = 'SS58PRE'
7
30
 
@@ -0,0 +1,68 @@
1
+ require "ed25519"
2
+ class ED25519
3
+
4
+ ##
5
+ # Sign the given message, Return sign result as hex string
6
+ # ==== Examples
7
+ # message = "Hello world"
8
+ # seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
9
+ # keypair = ED25519.keypair_from_seed(seed)
10
+ # signature_result = ED25519.sign(message, keypair)
11
+
12
+ def self.sign(message, key_pair)
13
+ "0x" + key_pair.sign(message).unpack1("H*")
14
+ end
15
+
16
+ ##
17
+ # Verify the sign result, Return true or false
18
+ # ==== Examples
19
+ # message = "Hello world"
20
+ # seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
21
+ # public_key = ED25519.get_public_key_from_seed(seed)
22
+ # address = Address.encode(public_key)
23
+ # keypair = ED25519.keypair_from_seed(seed)
24
+ # signature_result = ED25519.sign(message, keypair)
25
+ # verify_result = ED25519.verify(address, message, signature_result)
26
+
27
+ def self.verify(address, message, signature_result)
28
+ if signature_result.start_with?("0x")
29
+ signature_result = signature_result.sub(/0x/, "")
30
+ end
31
+ signature = [signature_result].pack("H*")
32
+ public_key = Address.decode(address)
33
+ verify_key_bytes = [public_key].pack("H*")
34
+ verify_key = Ed25519::VerifyKey.new(verify_key_bytes)
35
+ begin
36
+ verify_key.verify(signature, message)
37
+ rescue
38
+ return false
39
+ end
40
+ end
41
+
42
+ ##
43
+ # Generate ED25519 keypair
44
+ # ==== Examples
45
+ # seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
46
+ # keypair = ED25519.keypair_from_seed(seed)
47
+
48
+ def self.keypair_from_seed(seed)
49
+ if seed.start_with?("0x")
50
+ seed = seed.sub(/0x/, "")
51
+ end
52
+ seed = "".tap { |binary| seed.scan(/../) { |hn| binary << hn.to_i(16).chr } }
53
+ signing_key = Ed25519::SigningKey.new(seed)
54
+ return signing_key
55
+ end
56
+
57
+ ##
58
+ # Get ED25519 public key, return as hex string
59
+ # ==== Examples
60
+ # seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
61
+ # public_key = ED25519.get_public_key_from_seed(seed)
62
+
63
+ def self.get_public_key_from_seed(seed)
64
+ signing_key = self.keypair_from_seed(seed)
65
+ return signing_key.verify_key.to_bytes.unpack1('H*')
66
+ end
67
+
68
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sr25519
4
- VERSION = "0.3.0"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/sr25519.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative "sr25519/version"
4
4
  require "address"
5
5
  require "sr25519_lib"
6
+ require "ed25519_lib"
6
7
 
7
8
  module Sr25519
8
9
  class Error < StandardError; end
data/lib/sr25519_lib.rb CHANGED
@@ -5,9 +5,8 @@ module SR25519Lib
5
5
 
6
6
  ffi_lib FFI::Library::LIBC
7
7
 
8
- # The library is compile from https://github.com/Warchant/sr25519-crust
9
- ffi_lib [File.expand_path('lib/libsr25519crust.so')] if RUBY_PLATFORM =~ /linux/
10
- ffi_lib [File.expand_path('lib/libsr25519crust.dylib')] if RUBY_PLATFORM =~ /darwin/
8
+ ffi_lib File.dirname(__FILE__) + '/libsr25519crust.so' if RUBY_PLATFORM =~ /linux/
9
+ ffi_lib File.dirname(__FILE__) + '/libsr25519crust.dylib' if RUBY_PLATFORM =~ /darwin/
11
10
 
12
11
  attach_function :sr25519_keypair_from_seed, [:pointer, :pointer], :void
13
12
  attach_function :sr25519_verify, [:pointer, :pointer, :uint, :pointer], :bool
@@ -19,6 +18,8 @@ class KeyPair < FFI::Struct
19
18
  # [32b key | 32b nonce | 32b public]
20
19
  layout :String, [:uint8, 96]
21
20
 
21
+ ##
22
+ # Return the keypair public key
22
23
  def public_key
23
24
  pub_key = self[:String].to_a[64..96]
24
25
  public_key = PublicKey.new
@@ -55,6 +56,8 @@ end
55
56
  class SigMessage < FFI::Struct
56
57
  layout :String, [:uint8, 64]
57
58
 
59
+ ##
60
+ # Return the sign message as hex string
58
61
  def to_s
59
62
  self[:String].to_a.pack("c*").unpack1("H*")
60
63
  end
@@ -62,17 +65,28 @@ end
62
65
 
63
66
  class SR25519
64
67
 
68
+ ##
69
+ # Return the sign message result as hex string
70
+ # ==== Examples
71
+ # message = "Hello World"
72
+ # private_key = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
73
+ # signature_result = SR25519.sr25519_sign(message, private_key)
65
74
  def self.sr25519_sign(message, private_key)
66
75
  sig = SigMessage.new
67
76
  msg = FFI::MemoryPointer.from_string(message)
68
- key_pair = self.sr25519_keypair_from_seed(private_key)
77
+ key_pair = self.keypair_from_seed(private_key)
69
78
  public_key = key_pair.public_key
70
79
  SR25519Lib.sr25519_sign(sig, public_key, key_pair, msg, message.length)
71
80
  sig.to_s
72
81
  end
73
82
 
74
-
75
- # Creates a signature for given data
83
+ ##
84
+ # Return the sign message result as hex string
85
+ # ==== Examples
86
+ # message = "Hello World"
87
+ # seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
88
+ # keypair = SR25519.keypair_from_seed(seed)
89
+ # signature_result = SR25519.sign(message, keypair)
76
90
  def self.sign(message, key_pair)
77
91
  sig = SigMessage.new
78
92
  msg = FFI::MemoryPointer.from_string(message)
@@ -81,6 +95,16 @@ class SR25519
81
95
  sig.to_s
82
96
  end
83
97
 
98
+ ##
99
+ # Verify the sign result, Return true or false
100
+ # ==== Examples
101
+ # message = "Hello World"
102
+ # seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
103
+ # keypair = SR25519.keypair_from_seed(seed)
104
+ # public_key = SR25519.get_public_key_from_seed(seed)
105
+ # address = Address.encode(public_key.to_s)
106
+ # signature_result = SR25519.sign(message, keypair)
107
+ # verify_result = SR25519.verify(address, message, signature_result)
84
108
  def self.verify(address, message, signature_result)
85
109
  pk = PublicKey.new
86
110
  public_key = self.decode_address(address)
@@ -97,7 +121,12 @@ class SR25519
97
121
  verify = SR25519Lib.sr25519_verify(sig, msg, message.size, pk)
98
122
  end
99
123
 
100
- def self.sr25519_keypair_from_seed(seed)
124
+ ##
125
+ # Generate SR25519 keypair
126
+ # ==== Examples
127
+ # seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
128
+ # keypair = SR25519.keypair_from_seed(seed)
129
+ def self.keypair_from_seed(seed)
101
130
  if seed.start_with?("0x")
102
131
  seed = seed.sub(/0x/, "")
103
132
  end
@@ -109,11 +138,23 @@ class SR25519
109
138
  return key_pair
110
139
  end
111
140
 
141
+ ##
142
+ # Get SR25519 public key, need call to_s to get hex string
143
+ # ==== Examples
144
+ # seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
145
+ # public_key = SR25519.get_public_key_from_seed(seed)
112
146
  def self.get_public_key_from_seed(seed)
113
- key_pair = self.sr25519_keypair_from_seed(seed)
147
+ key_pair = self.keypair_from_seed(seed)
114
148
  key_pair.public_key
115
149
  end
116
150
 
151
+ ##
152
+ # Get public from address
153
+ # ==== Examples
154
+ # seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
155
+ # public_key = SR25519.get_public_key_from_seed(seed)
156
+ # address = Address.encode(public_key.to_s)
157
+ # public_key = SR25519.decode_address(address)
117
158
  def self.decode_address(address,addr_type=42)
118
159
  public_address = Address.decode(address,addr_type)
119
160
  end
data/sr25519.gemspec CHANGED
@@ -34,5 +34,5 @@ Gem::Specification.new do |spec|
34
34
  spec.add_dependency "blake2b"
35
35
  spec.add_dependency "base58"
36
36
  spec.add_development_dependency "ffi", "~> 1.15.0"
37
-
37
+ spec.add_dependency "ed25519", '~> 1.2', '>= 1.2.4'
38
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sr25519
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - xuxh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-11 00:00:00.000000000 Z
11
+ date: 2021-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -80,6 +80,26 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.15.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: ed25519
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 1.2.4
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '1.2'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 1.2.4
83
103
  description: Sign and verify message with sr25519
84
104
  email:
85
105
  - xxh2611@gmail.com
@@ -100,6 +120,7 @@ files:
100
120
  - bin/console
101
121
  - bin/setup
102
122
  - lib/address.rb
123
+ - lib/ed25519_lib.rb
103
124
  - lib/libsr25519crust.dylib
104
125
  - lib/libsr25519crust.so
105
126
  - lib/sr25519.rb