|
|
@ -18,57 +18,72 @@ var ( |
|
|
|
|
|
|
|
// Generator determines the type of private key the keystore creates
|
|
|
|
type Generator interface { |
|
|
|
Generate(secret []byte) crypto.PrivKey |
|
|
|
Generate(secret []byte) (crypto.PrivKey, error) |
|
|
|
} |
|
|
|
|
|
|
|
// GenFunc is a helper to transform a function into a Generator
|
|
|
|
type GenFunc func(secret []byte) crypto.PrivKey |
|
|
|
type GenFunc func(secret []byte) (crypto.PrivKey, error) |
|
|
|
|
|
|
|
func (f GenFunc) Generate(secret []byte) crypto.PrivKey { |
|
|
|
func (f GenFunc) Generate(secret []byte) (crypto.PrivKey, error) { |
|
|
|
return f(secret) |
|
|
|
} |
|
|
|
|
|
|
|
func genEd25519(secret []byte) crypto.PrivKey { |
|
|
|
return crypto.GenPrivKeyEd25519FromSecret(secret).Wrap() |
|
|
|
func genEd25519(secret []byte) (crypto.PrivKey, error) { |
|
|
|
key := crypto.GenPrivKeyEd25519FromSecret(secret).Wrap() |
|
|
|
return key, nil |
|
|
|
} |
|
|
|
|
|
|
|
func genSecp256(secret []byte) crypto.PrivKey { |
|
|
|
return crypto.GenPrivKeySecp256k1FromSecret(secret).Wrap() |
|
|
|
func genSecp256(secret []byte) (crypto.PrivKey, error) { |
|
|
|
key := crypto.GenPrivKeySecp256k1FromSecret(secret).Wrap() |
|
|
|
return key, nil |
|
|
|
} |
|
|
|
|
|
|
|
// secret is completely ignored for the ledger...
|
|
|
|
// just for interface compatibility
|
|
|
|
func genLedger(secret []byte) crypto.PrivKey { |
|
|
|
key, err := nano.NewPrivKeyLedger() |
|
|
|
if err != nil { |
|
|
|
// TODO: cleaner error handling
|
|
|
|
panic(err) |
|
|
|
} |
|
|
|
return key |
|
|
|
func genLedger(secret []byte) (crypto.PrivKey, error) { |
|
|
|
return nano.NewPrivKeyLedger() |
|
|
|
} |
|
|
|
|
|
|
|
type genInvalidByte struct { |
|
|
|
typ byte |
|
|
|
} |
|
|
|
|
|
|
|
func (g genInvalidByte) Generate(secret []byte) (crypto.PrivKey, error) { |
|
|
|
err := errors.Errorf("Cannot generate keys for algorithm: %X", g.typ) |
|
|
|
return crypto.PrivKey{}, err |
|
|
|
} |
|
|
|
|
|
|
|
type genInvalidAlgo struct { |
|
|
|
algo string |
|
|
|
} |
|
|
|
|
|
|
|
func (g genInvalidAlgo) Generate(secret []byte) (crypto.PrivKey, error) { |
|
|
|
err := errors.Errorf("Cannot generate keys for algorithm: %s", g.algo) |
|
|
|
return crypto.PrivKey{}, err |
|
|
|
} |
|
|
|
|
|
|
|
func getGenerator(algo string) (Generator, error) { |
|
|
|
func getGenerator(algo string) Generator { |
|
|
|
switch algo { |
|
|
|
case crypto.NameEd25519: |
|
|
|
return GenEd25519, nil |
|
|
|
return GenEd25519 |
|
|
|
case crypto.NameSecp256k1: |
|
|
|
return GenSecp256k1, nil |
|
|
|
return GenSecp256k1 |
|
|
|
case nano.NameLedger: |
|
|
|
return GenLedger, nil |
|
|
|
return GenLedger |
|
|
|
default: |
|
|
|
return nil, errors.Errorf("Cannot generate keys for algorithm: %s", algo) |
|
|
|
return genInvalidAlgo{algo} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func getGeneratorByType(typ byte) (Generator, error) { |
|
|
|
func getGeneratorByType(typ byte) Generator { |
|
|
|
switch typ { |
|
|
|
case crypto.TypeEd25519: |
|
|
|
return GenEd25519, nil |
|
|
|
return GenEd25519 |
|
|
|
case crypto.TypeSecp256k1: |
|
|
|
return GenSecp256k1, nil |
|
|
|
return GenSecp256k1 |
|
|
|
case nano.TypeLedger: |
|
|
|
return GenLedger, nil |
|
|
|
return GenLedger |
|
|
|
default: |
|
|
|
return nil, errors.Errorf("Cannot generate keys for algorithm: %X", typ) |
|
|
|
return genInvalidByte{typ} |
|
|
|
} |
|
|
|
} |