forked from goat-systems/go-tezos
/
curve.go
54 lines (44 loc) · 1.3 KB
/
curve.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
package keys
import (
"fmt"
)
// ECKind is the key type
type ECKind string
const (
// Ed25519 https://tools.ietf.org/html/rfc8032
Ed25519 ECKind = "Ed25519"
// Secp256k1 https://tools.ietf.org/html/rfc4492
Secp256k1 ECKind = "Secp256k1"
// NistP256 https://tools.ietf.org/html/rfc5656
NistP256 ECKind = "NistP256"
)
type iCurve interface {
addressPrefix() []byte
publicKeyPrefix() []byte
privateKeyPrefix() []byte
signaturePrefix() []byte
getECKind() ECKind
getPrivateKey(v []byte) []byte
getPublicKey(privateKey []byte) ([]byte, error)
sign(msg []byte, privateKey []byte) (Signature, error)
}
func getCurve(kind ECKind) iCurve {
if kind == Secp256k1 {
return &secp256k1Curve{}
} else if kind == NistP256 {
return &nistP256Curve{}
}
return &ed25519Curve{}
}
func getCurveByPrefix(prefix string) (iCurve, error) {
if prefix == "edpk" || prefix == "edsk" || prefix == "tz1" || prefix == "edesk" || prefix == "edsig" {
return &ed25519Curve{}, nil
}
if prefix == "sppk" || prefix == "spsk" || prefix == "tz2" || prefix == "spesk" || prefix == "spsig" {
return &secp256k1Curve{}, nil
}
if prefix == "p2pk" || prefix == "p2sk" || prefix == "tz3" || prefix == "p2esk" || prefix == "p2sig" {
return &nistP256Curve{}, nil
}
return nil, fmt.Errorf("failed to find curve with prefix '%s'", prefix)
}