-
Notifications
You must be signed in to change notification settings - Fork 4
/
ed25519.go
86 lines (73 loc) · 2.19 KB
/
ed25519.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
package crypto
import (
"crypto/ed25519"
"crypto/rand"
"github.com/dipdup-net/go-lib/tools/encoding"
"github.com/pkg/errors"
"golang.org/x/crypto/blake2b"
)
// Ed25519 -
type Ed25519 struct {
addressPrefix []byte
publicKeyPrefix []byte
privateKeyPrefix []byte
signaturePrefix []byte
seedKey []byte
}
// NewEd25519 -
func NewEd25519() Ed25519 {
return Ed25519{
seedKey: []byte{101, 100, 50, 53, 53, 49, 57, 32, 115, 101, 101, 100},
addressPrefix: []byte(encoding.PrefixPublicKeyTZ1),
publicKeyPrefix: []byte(encoding.PrefixED25519PublicKey),
privateKeyPrefix: []byte(encoding.PrefixED25519SecretKey),
signaturePrefix: []byte(encoding.PrefixED25519Signature),
}
}
// GeneratePrivateKey -
func (curve Ed25519) GeneratePrivateKey() ([]byte, []byte, error) {
return ed25519.GenerateKey(rand.Reader)
}
// GetPublicKey -
func (curve Ed25519) GetPublicKey(privateKey []byte) ([]byte, error) {
var sk ed25519.PrivateKey
switch len(privateKey) {
case ed25519.PrivateKeySize:
sk = ed25519.PrivateKey(privateKey)
case ed25519.PublicKeySize:
sk = ed25519.NewKeyFromSeed(privateKey)
default:
return nil, errors.Errorf("invalid private key length: %d", len(privateKey))
}
pk := sk.Public()
return []byte(pk.(ed25519.PublicKey)), nil
}
// Sign -
func (curve Ed25519) Sign(data []byte, privateKey []byte) (Signature, error) {
if ed25519.PrivateKeySize != len(privateKey) {
return Signature{}, errors.Errorf("invalid private key length: %d != %d", len(privateKey), ed25519.PrivateKeySize)
}
digest := blake2b.Sum256(data)
sign := ed25519.Sign(privateKey, digest[:])
return NewSignature(sign, curve.signaturePrefix), nil
}
// Verify -
func (curve Ed25519) Verify(data []byte, signature []byte, pubKey []byte) bool {
if ed25519.PublicKeySize != len(pubKey) || ed25519.SignatureSize != len(signature) {
return false
}
digest := blake2b.Sum256(data)
return ed25519.Verify(pubKey, digest[:], signature)
}
// AddressPrefix -
func (curve Ed25519) AddressPrefix() []byte {
return curve.addressPrefix
}
// PublicKeyPrefix -
func (curve Ed25519) PublicKeyPrefix() []byte {
return curve.publicKeyPrefix
}
// Kind -
func (curve Ed25519) Kind() ECKind {
return KindEd25519
}