This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
crypto.go
80 lines (67 loc) · 1.53 KB
/
crypto.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
package crypto
import (
"fmt"
)
type CurveType uint32
const (
CurveTypeUnset CurveType = iota
CurveTypeEd25519
CurveTypeSecp256k1
)
func (k CurveType) String() string {
switch k {
case CurveTypeSecp256k1:
return "secp256k1"
case CurveTypeEd25519:
return "ed25519"
case CurveTypeUnset:
return ""
default:
return "unknown"
}
}
func (k CurveType) ABCIType() string {
switch k {
case CurveTypeSecp256k1:
return "secp256k1"
case CurveTypeEd25519:
return "ed25519"
case CurveTypeUnset:
return ""
default:
return "unknown"
}
}
// Get this CurveType's 8 bit identifier as a byte
func (k CurveType) Byte() byte {
return byte(k)
}
func CurveTypeFromString(s string) (CurveType, error) {
switch s {
case "secp256k1":
return CurveTypeSecp256k1, nil
case "ed25519":
return CurveTypeEd25519, nil
case "":
return CurveTypeUnset, nil
default:
return CurveTypeUnset, fmt.Errorf("invalid curve name: '%s'", s)
}
}
type ErrInvalidCurve uint32
func (curveType ErrInvalidCurve) Error() string {
return fmt.Sprintf("invalid curve type: %d", curveType)
}
// The types in this file allow us to control serialisation of keys and signatures, as well as the interface
// exposed regardless of crypto library
type Signer interface {
Sign(msg []byte) (*Signature, error)
}
// Signable is an interface for all signable things.
// It typically removes signatures before serializing.
type Signable interface {
SignBytes(chainID string) ([]byte, error)
}
func (pk *PrivateKey) GetAddress() Address {
return pk.GetPublicKey().GetAddress()
}