-
Notifications
You must be signed in to change notification settings - Fork 13
/
scheme.go
39 lines (33 loc) · 870 Bytes
/
scheme.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
package subkey
import (
"fmt"
)
// Scheme represents a cryptography scheme.
type Scheme interface {
fmt.Stringer
Generate() (KeyPair, error)
FromSeed(seed []byte) (KeyPair, error)
FromPhrase(phrase, password string) (KeyPair, error)
Derive(pair KeyPair, djs []DeriveJunction) (KeyPair, error)
FromPublicKey([]byte) (PublicKey, error)
}
// DeriveKeyPair derives the Keypair from the URI using the provided cryptography scheme.
func DeriveKeyPair(scheme Scheme, uri string) (kp KeyPair, err error) {
phrase, path, pwd, err := splitURI(uri)
if err != nil {
return nil, err
}
if b, ok := DecodeHex(phrase); ok {
kp, err = scheme.FromSeed(b)
} else {
kp, err = scheme.FromPhrase(phrase, pwd)
}
if err != nil {
return nil, err
}
djs, err := deriveJunctions(derivePath(path))
if err != nil {
return nil, err
}
return scheme.Derive(kp, djs)
}