From 6d2fa888fc74a1835491b3f2f1f99b052ab31eb0 Mon Sep 17 00:00:00 2001 From: Jacky Wang Date: Wed, 8 Jul 2020 11:18:35 -0700 Subject: [PATCH] [node.sh] adapt to new multibls.PrivateKeys --- cmd/harmony/blsloader/loader.go | 36 +++++++++++++-------------- cmd/harmony/blsloader/passProvider.go | 2 +- cmd/harmony/blsloader/utils.go | 18 +++++++++----- crypto/bls/bls.go | 13 ++++++++++ multibls/multibls.go | 15 +++++------ 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/cmd/harmony/blsloader/loader.go b/cmd/harmony/blsloader/loader.go index 9c51d936f..bf78bd987 100644 --- a/cmd/harmony/blsloader/loader.go +++ b/cmd/harmony/blsloader/loader.go @@ -6,7 +6,7 @@ import ( "os" "path/filepath" - ffibls "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/multibls" ) @@ -24,10 +24,10 @@ type Loader struct { } // LoadKeys load all keys from the input fields provided -func (loader *Loader) LoadKeys() (multibls.PrivateKey, error) { +func (loader *Loader) LoadKeys() (multibls.PrivateKeys, error) { helper, err := loader.getHelper() if err != nil { - return multibls.PrivateKey{}, err + return multibls.PrivateKeys{}, err } return helper.loadKeys() } @@ -64,7 +64,7 @@ func (loader *Loader) getHelper() (loadHelper, error) { // loadHelper defines the interface to help load bls keys type loadHelper interface { - loadKeys() (multibls.PrivateKey, error) + loadKeys() (multibls.PrivateKeys, error) } // basicSingleBlsLoader loads a single bls key file with passphrase @@ -75,14 +75,14 @@ type basicSingleBlsLoader struct { persistPassphrase bool } -func (loader *basicSingleBlsLoader) loadKeys() (multibls.PrivateKey, error) { +func (loader *basicSingleBlsLoader) loadKeys() (multibls.PrivateKeys, error) { providers, err := loader.getPassProviders() if err != nil { - return multibls.PrivateKey{}, err + return multibls.PrivateKeys{}, err } secretKey, err := loadBasicKey(loader.blsKeyFile, providers) if err != nil { - return multibls.PrivateKey{}, err + return multibls.PrivateKeys{}, err } return secretKeyToMultiPrivateKey(secretKey), nil } @@ -126,14 +126,14 @@ type kmsSingleBlsLoader struct { awsConfigFile *string } -func (loader *kmsSingleBlsLoader) loadKeys() (multibls.PrivateKey, error) { +func (loader *kmsSingleBlsLoader) loadKeys() (multibls.PrivateKeys, error) { provider, err := loader.getKmsClientProvider() if err != nil { - return multibls.PrivateKey{}, err + return multibls.PrivateKeys{}, err } secretKey, err := loadKmsKeyFromFile(loader.awsBlsKey, provider) if err != nil { - return multibls.PrivateKey{}, err + return multibls.PrivateKeys{}, err } return secretKeyToMultiPrivateKey(secretKey), nil } @@ -170,16 +170,16 @@ type blsDirLoader struct { pps []passProvider kcp kmsClientProvider // result field - secretKeys []*ffibls.SecretKey + secretKeys []*bls.SecretKey } -func (loader *blsDirLoader) loadKeys() (multibls.PrivateKey, error) { +func (loader *blsDirLoader) loadKeys() (multibls.PrivateKeys, error) { var err error if loader.pps, err = loader.getPassProviders(); err != nil { - return multibls.PrivateKey{}, err + return multibls.PrivateKeys{}, err } if loader.kcp, err = loader.getKmsClientProvider(); err != nil { - return multibls.PrivateKey{}, err + return multibls.PrivateKeys{}, err } return loader.loadKeyFiles() } @@ -231,10 +231,10 @@ func (loader *blsDirLoader) getKmsClientProvider() (kmsClientProvider, error) { } } -func (loader *blsDirLoader) loadKeyFiles() (multibls.PrivateKey, error) { +func (loader *blsDirLoader) loadKeyFiles() (multibls.PrivateKeys, error) { err := filepath.Walk(loader.dirPath, loader.processFileWalk) if err != nil { - return multibls.PrivateKey{}, err + return multibls.PrivateKeys{}, err } return secretKeyToMultiPrivateKey(loader.secretKeys...), nil } @@ -264,9 +264,9 @@ func (loader *blsDirLoader) skippingErrors() []error { } } -func (loader *blsDirLoader) loadKeyFromFile(path string, info os.FileInfo) (*ffibls.SecretKey, error) { +func (loader *blsDirLoader) loadKeyFromFile(path string, info os.FileInfo) (*bls.SecretKey, error) { var ( - key *ffibls.SecretKey + key *bls.SecretKey err error ) switch { diff --git a/cmd/harmony/blsloader/passProvider.go b/cmd/harmony/blsloader/passProvider.go index b58edc92b..51104ae97 100644 --- a/cmd/harmony/blsloader/passProvider.go +++ b/cmd/harmony/blsloader/passProvider.go @@ -28,7 +28,7 @@ type promptPassProvider struct { const pwdPromptStr = "Enter passphrase for the BLS key file %s:" func newPromptPassProvider() *promptPassProvider { - return &promptPassProvider{q} + return &promptPassProvider{} } func (provider *promptPassProvider) toStr() string { diff --git a/cmd/harmony/blsloader/utils.go b/cmd/harmony/blsloader/utils.go index 406d0854e..384311647 100644 --- a/cmd/harmony/blsloader/utils.go +++ b/cmd/harmony/blsloader/utils.go @@ -5,7 +5,8 @@ import ( "os" "strings" - ffibls "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/blsgen" "github.com/harmony-one/harmony/multibls" "github.com/pkg/errors" @@ -19,7 +20,7 @@ var ( // loadBasicKey loads a single bls key through a key file and passphrase combination. // The passphrase is provided by a slice of passProviders. -func loadBasicKey(blsKeyFile string, pps []passProvider) (*ffibls.SecretKey, error) { +func loadBasicKey(blsKeyFile string, pps []passProvider) (*bls_core.SecretKey, error) { if len(pps) == 0 { return nil, errNilPassProvider } @@ -33,7 +34,7 @@ func loadBasicKey(blsKeyFile string, pps []passProvider) (*ffibls.SecretKey, err return nil, fmt.Errorf("failed to load bls key %v", blsKeyFile) } -func loadBasicKeyWithProvider(blsKeyFile string, pp passProvider) (*ffibls.SecretKey, error) { +func loadBasicKeyWithProvider(blsKeyFile string, pp passProvider) (*bls_core.SecretKey, error) { pass, err := pp.getPassphrase(blsKeyFile) if err != nil { return nil, errors.Wrapf(err, "unable to get passphrase from %s", pp.toStr()) @@ -46,7 +47,7 @@ func loadBasicKeyWithProvider(blsKeyFile string, pp passProvider) (*ffibls.Secre } // loadKmsKeyFromFile loads a single KMS BLS key from file -func loadKmsKeyFromFile(blsKeyFile string, kcp kmsClientProvider) (*ffibls.SecretKey, error) { +func loadKmsKeyFromFile(blsKeyFile string, kcp kmsClientProvider) (*bls_core.SecretKey, error) { if kcp == nil { return nil, errNilKMSClientProvider } @@ -123,8 +124,13 @@ func promptYesNo(prompt string) (bool, error) { } } -func secretKeyToMultiPrivateKey(secretKeys ...*ffibls.SecretKey) multibls.PrivateKey { - return multibls.PrivateKey{PrivateKey: secretKeys} +func secretKeyToMultiPrivateKey(secretKeys ...*bls_core.SecretKey) multibls.PrivateKeys { + keys := make(multibls.PrivateKeys, 0, len(secretKeys)) + for _, secretKey := range secretKeys { + key := bls.WrapperFromPrivateKey(secretKey) + keys = append(keys, key) + } + return keys } func stringIsSet(val *string) bool { diff --git a/crypto/bls/bls.go b/crypto/bls/bls.go index 0058d7f76..0f291debf 100644 --- a/crypto/bls/bls.go +++ b/crypto/bls/bls.go @@ -32,6 +32,19 @@ type PublicKeyWrapper struct { Object *bls.PublicKey } +// WrapperFromPrivateKey makes a PrivateKeyWrapper from bls secret key +func WrapperFromPrivateKey(pri *bls.SecretKey) PrivateKeyWrapper { + pub := pri.GetPublicKey() + pubBytes := FromLibBLSPublicKeyUnsafe(pub) + return PrivateKeyWrapper{ + Pri: pri, + Pub: &PublicKeyWrapper{ + Bytes: *pubBytes, + Object: pub, + }, + } +} + // SerializedPublicKey defines the serialized bls public key type SerializedPublicKey [PublicKeySizeInBytes]byte diff --git a/multibls/multibls.go b/multibls/multibls.go index f1e9ed356..5c4568fa8 100644 --- a/multibls/multibls.go +++ b/multibls/multibls.go @@ -3,9 +3,8 @@ package multibls import ( "strings" - "github.com/harmony-one/harmony/crypto/bls" - bls_core "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/crypto/bls" ) // PrivateKeys stores the bls secret keys that belongs to the node @@ -47,9 +46,11 @@ func (multiKey PrivateKeys) GetPublicKeys() PublicKeys { } // GetPrivateKeys creates a multibls PrivateKeys using bls.SecretKey -func GetPrivateKeys(key *bls_core.SecretKey) PrivateKeys { - pub := key.GetPublicKey() - pubWrapper := bls.PublicKeyWrapper{Object: pub} - pubWrapper.Bytes.FromLibBLSPublicKey(pub) - return PrivateKeys{bls.PrivateKeyWrapper{Pri: key, Pub: &pubWrapper}} +func GetPrivateKeys(secretKeys ...*bls_core.SecretKey) PrivateKeys { + keys := make(PrivateKeys, 0, len(secretKeys)) + for _, secretKey := range secretKeys { + key := bls.WrapperFromPrivateKey(secretKey) + keys = append(keys, key) + } + return keys }