sr25519 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +11 -2
- data/lib/address.rb +24 -1
- data/lib/ed25519_lib.rb +31 -1
- data/lib/sr25519/version.rb +1 -1
- data/lib/sr25519_lib.rb +44 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b113b67bbe4a743dfe19a186d10bf4e12b929037e9f11c80628205d5d20a8d1a
|
4
|
+
data.tar.gz: fd5d5f843cdd92ceba67cb1128f410425810641fc417915bfeb1cf79d41088b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a54153363c366648db12f5b10f3afa383ba368d412623c813c095033832f7c74c07b7bfd9fcb373296aa227ac9fd93d2a90a8dea2d7c74ad843691d7d408e3b0
|
7
|
+
data.tar.gz: 18cf9d2b4f91774660e598e82114ec789bca49fcf2e29ecdd17cfc5b6c751edaf915c6a75d7717d1d2c229288ade84570ac625ed47a6c985aa81d89e3383cbcc
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -54,12 +54,13 @@ address = Address.encode(public_key.to_s)
|
|
54
54
|
#### 5. Decode address
|
55
55
|
|
56
56
|
```ruby
|
57
|
-
|
57
|
+
public_key_str = Address.decode(address)
|
58
58
|
```
|
59
59
|
|
60
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
|
```
|
@@ -82,9 +83,17 @@ keypair = ED25519.keypair_from_seed(seed)
|
|
82
83
|
signature_result = ED25519.sign(message, keypair)
|
83
84
|
```
|
84
85
|
|
85
|
-
#### 10. ED25519
|
86
|
+
#### 10. ED25519 Get public key
|
86
87
|
|
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)
|
88
97
|
verify_result = ED25519.verify(address, message, signature_result)
|
89
98
|
```
|
90
99
|
|
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
|
-
|
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
|
|
data/lib/ed25519_lib.rb
CHANGED
@@ -1,11 +1,29 @@
|
|
1
1
|
require "ed25519"
|
2
2
|
class ED25519
|
3
3
|
|
4
|
-
|
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
|
+
|
5
12
|
def self.sign(message, key_pair)
|
6
13
|
"0x" + key_pair.sign(message).unpack1("H*")
|
7
14
|
end
|
8
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
|
+
|
9
27
|
def self.verify(address, message, signature_result)
|
10
28
|
if signature_result.start_with?("0x")
|
11
29
|
signature_result = signature_result.sub(/0x/, "")
|
@@ -21,6 +39,12 @@ class ED25519
|
|
21
39
|
end
|
22
40
|
end
|
23
41
|
|
42
|
+
##
|
43
|
+
# Generate ED25519 keypair
|
44
|
+
# ==== Examples
|
45
|
+
# seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
|
46
|
+
# keypair = ED25519.keypair_from_seed(seed)
|
47
|
+
|
24
48
|
def self.keypair_from_seed(seed)
|
25
49
|
if seed.start_with?("0x")
|
26
50
|
seed = seed.sub(/0x/, "")
|
@@ -30,6 +54,12 @@ class ED25519
|
|
30
54
|
return signing_key
|
31
55
|
end
|
32
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
|
+
|
33
63
|
def self.get_public_key_from_seed(seed)
|
34
64
|
signing_key = self.keypair_from_seed(seed)
|
35
65
|
return signing_key.verify_key.to_bytes.unpack1('H*')
|
data/lib/sr25519/version.rb
CHANGED
data/lib/sr25519_lib.rb
CHANGED
@@ -5,7 +5,6 @@ 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
8
|
ffi_lib File.dirname(__FILE__) + '/libsr25519crust.so' if RUBY_PLATFORM =~ /linux/
|
10
9
|
ffi_lib File.dirname(__FILE__) + '/libsr25519crust.dylib' if RUBY_PLATFORM =~ /darwin/
|
11
10
|
|
@@ -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,6 +65,12 @@ 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)
|
@@ -71,8 +80,13 @@ class SR25519
|
|
71
80
|
sig.to_s
|
72
81
|
end
|
73
82
|
|
74
|
-
|
75
|
-
#
|
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,6 +121,11 @@ class SR25519
|
|
97
121
|
verify = SR25519Lib.sr25519_verify(sig, msg, message.size, pk)
|
98
122
|
end
|
99
123
|
|
124
|
+
##
|
125
|
+
# Generate SR25519 keypair
|
126
|
+
# ==== Examples
|
127
|
+
# seed = "0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"
|
128
|
+
# keypair = SR25519.keypair_from_seed(seed)
|
100
129
|
def self.keypair_from_seed(seed)
|
101
130
|
if seed.start_with?("0x")
|
102
131
|
seed = seed.sub(/0x/, "")
|
@@ -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
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
|
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.
|
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
|
+
date: 2021-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|