-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsig.go
122 lines (102 loc) · 2.74 KB
/
sig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package client
import (
"crypto"
goEd25519 "crypto/ed25519"
"github.com/vedhavyas/go-subkey/v2"
"github.com/vedhavyas/go-subkey/v2/ed25519"
"github.com/vedhavyas/go-subkey/v2/sr25519"
"golang.org/x/crypto/blake2b"
)
type Signer struct {
subkey.KeyPair
// Address is an SS58 address
Address string
// PublicKey
PublicKey []byte
// key type
KeyType uint8
}
func (e *Signer) Sign(msg []byte) ([]byte, error) {
if len(msg) > 256 {
h := blake2b.Sum256(msg)
msg = h[:]
}
return e.KeyPair.Sign(msg)
}
func (e *Signer) Verify(msg []byte, signature []byte) bool {
if len(msg) > 256 {
h := blake2b.Sum256(msg)
msg = h[:]
}
return e.KeyPair.Verify(msg, signature)
}
// Sr25519PairFromSecret generates a sr25519 key pair from a seed or phrase
func Sr25519PairFromSecret(seedOrPhrase string, network uint16) (Signer, error) {
scheme := sr25519.Scheme{}
kyr, err := subkey.DeriveKeyPair(scheme, seedOrPhrase)
if err != nil {
return Signer{}, err
}
ss58Address := kyr.SS58Address(network)
return Signer{
KeyPair: kyr,
Address: ss58Address,
PublicKey: kyr.Public(),
}, nil
}
// Ed25519PairFromSecret generates a ed25519 key pair from a seed or phrase
func Ed25519PairFromSecret(seedOrPhrase string, network uint16) (Signer, error) {
scheme := ed25519.Scheme{}
kyr, err := subkey.DeriveKeyPair(scheme, seedOrPhrase)
if err != nil {
return Signer{}, err
}
ss58Address := kyr.SS58Address(network)
return Signer{
KeyType: 1,
KeyPair: kyr,
Address: ss58Address,
PublicKey: kyr.Public(),
}, nil
}
// Ed25519PairFromPk generates a ed25519 key pair from golang ed25519.PrivateKey
func Ed25519PairFromPk(pk goEd25519.PrivateKey, network uint16) (Signer, error) {
pub := pk.Public().(goEd25519.PublicKey)
kyr := Ed25519Signer{
pub: &pub,
secret: &pk,
}
ss58Address := kyr.SS58Address(network)
return Signer{
KeyType: 1,
KeyPair: &kyr,
Address: ss58Address,
PublicKey: pub,
}, nil
}
type Ed25519Signer struct {
pub *goEd25519.PublicKey
secret *goEd25519.PrivateKey
}
// Public returns the pub key in bytes.
func (e *Ed25519Signer) Public() []byte {
return *e.pub
}
// Seed returns the seed for this key
func (kr *Ed25519Signer) Seed() []byte {
return kr.secret.Seed()
}
// AccountID returns the accountID for this key
func (e *Ed25519Signer) AccountID() []byte {
return e.Public()
}
// SS58Address returns the Base58 public key with checksum and network identifier.
func (e *Ed25519Signer) SS58Address(network uint16) string {
return subkey.SS58Encode(e.AccountID(), network)
}
func (e *Ed25519Signer) Sign(msg []byte) ([]byte, error) {
return e.secret.Sign(nil, msg, crypto.Hash(0))
}
func (e *Ed25519Signer) Verify(msg []byte, signature []byte) bool {
return goEd25519.Verify(*e.pub, msg, signature)
}