[node.sh] adapt to new multibls.PrivateKeys

pull/3219/head
Jacky Wang 4 years ago
parent a7c9554d8c
commit 6d2fa888fc
No known key found for this signature in database
GPG Key ID: 1085CE5F4FF5842C
  1. 36
      cmd/harmony/blsloader/loader.go
  2. 2
      cmd/harmony/blsloader/passProvider.go
  3. 18
      cmd/harmony/blsloader/utils.go
  4. 13
      crypto/bls/bls.go
  5. 15
      multibls/multibls.go

@ -6,7 +6,7 @@ import (
"os" "os"
"path/filepath" "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" "github.com/harmony-one/harmony/multibls"
) )
@ -24,10 +24,10 @@ type Loader struct {
} }
// LoadKeys load all keys from the input fields provided // 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() helper, err := loader.getHelper()
if err != nil { if err != nil {
return multibls.PrivateKey{}, err return multibls.PrivateKeys{}, err
} }
return helper.loadKeys() return helper.loadKeys()
} }
@ -64,7 +64,7 @@ func (loader *Loader) getHelper() (loadHelper, error) {
// loadHelper defines the interface to help load bls keys // loadHelper defines the interface to help load bls keys
type loadHelper interface { type loadHelper interface {
loadKeys() (multibls.PrivateKey, error) loadKeys() (multibls.PrivateKeys, error)
} }
// basicSingleBlsLoader loads a single bls key file with passphrase // basicSingleBlsLoader loads a single bls key file with passphrase
@ -75,14 +75,14 @@ type basicSingleBlsLoader struct {
persistPassphrase bool persistPassphrase bool
} }
func (loader *basicSingleBlsLoader) loadKeys() (multibls.PrivateKey, error) { func (loader *basicSingleBlsLoader) loadKeys() (multibls.PrivateKeys, error) {
providers, err := loader.getPassProviders() providers, err := loader.getPassProviders()
if err != nil { if err != nil {
return multibls.PrivateKey{}, err return multibls.PrivateKeys{}, err
} }
secretKey, err := loadBasicKey(loader.blsKeyFile, providers) secretKey, err := loadBasicKey(loader.blsKeyFile, providers)
if err != nil { if err != nil {
return multibls.PrivateKey{}, err return multibls.PrivateKeys{}, err
} }
return secretKeyToMultiPrivateKey(secretKey), nil return secretKeyToMultiPrivateKey(secretKey), nil
} }
@ -126,14 +126,14 @@ type kmsSingleBlsLoader struct {
awsConfigFile *string awsConfigFile *string
} }
func (loader *kmsSingleBlsLoader) loadKeys() (multibls.PrivateKey, error) { func (loader *kmsSingleBlsLoader) loadKeys() (multibls.PrivateKeys, error) {
provider, err := loader.getKmsClientProvider() provider, err := loader.getKmsClientProvider()
if err != nil { if err != nil {
return multibls.PrivateKey{}, err return multibls.PrivateKeys{}, err
} }
secretKey, err := loadKmsKeyFromFile(loader.awsBlsKey, provider) secretKey, err := loadKmsKeyFromFile(loader.awsBlsKey, provider)
if err != nil { if err != nil {
return multibls.PrivateKey{}, err return multibls.PrivateKeys{}, err
} }
return secretKeyToMultiPrivateKey(secretKey), nil return secretKeyToMultiPrivateKey(secretKey), nil
} }
@ -170,16 +170,16 @@ type blsDirLoader struct {
pps []passProvider pps []passProvider
kcp kmsClientProvider kcp kmsClientProvider
// result field // 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 var err error
if loader.pps, err = loader.getPassProviders(); err != nil { if loader.pps, err = loader.getPassProviders(); err != nil {
return multibls.PrivateKey{}, err return multibls.PrivateKeys{}, err
} }
if loader.kcp, err = loader.getKmsClientProvider(); err != nil { if loader.kcp, err = loader.getKmsClientProvider(); err != nil {
return multibls.PrivateKey{}, err return multibls.PrivateKeys{}, err
} }
return loader.loadKeyFiles() 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) err := filepath.Walk(loader.dirPath, loader.processFileWalk)
if err != nil { if err != nil {
return multibls.PrivateKey{}, err return multibls.PrivateKeys{}, err
} }
return secretKeyToMultiPrivateKey(loader.secretKeys...), nil 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 ( var (
key *ffibls.SecretKey key *bls.SecretKey
err error err error
) )
switch { switch {

@ -28,7 +28,7 @@ type promptPassProvider struct {
const pwdPromptStr = "Enter passphrase for the BLS key file %s:" const pwdPromptStr = "Enter passphrase for the BLS key file %s:"
func newPromptPassProvider() *promptPassProvider { func newPromptPassProvider() *promptPassProvider {
return &promptPassProvider{q} return &promptPassProvider{}
} }
func (provider *promptPassProvider) toStr() string { func (provider *promptPassProvider) toStr() string {

@ -5,7 +5,8 @@ import (
"os" "os"
"strings" "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/internal/blsgen"
"github.com/harmony-one/harmony/multibls" "github.com/harmony-one/harmony/multibls"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -19,7 +20,7 @@ var (
// loadBasicKey loads a single bls key through a key file and passphrase combination. // loadBasicKey loads a single bls key through a key file and passphrase combination.
// The passphrase is provided by a slice of passProviders. // 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 { if len(pps) == 0 {
return nil, errNilPassProvider 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) 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) pass, err := pp.getPassphrase(blsKeyFile)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "unable to get passphrase from %s", pp.toStr()) 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 // 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 { if kcp == nil {
return nil, errNilKMSClientProvider return nil, errNilKMSClientProvider
} }
@ -123,8 +124,13 @@ func promptYesNo(prompt string) (bool, error) {
} }
} }
func secretKeyToMultiPrivateKey(secretKeys ...*ffibls.SecretKey) multibls.PrivateKey { func secretKeyToMultiPrivateKey(secretKeys ...*bls_core.SecretKey) multibls.PrivateKeys {
return multibls.PrivateKey{PrivateKey: secretKeys} 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 { func stringIsSet(val *string) bool {

@ -32,6 +32,19 @@ type PublicKeyWrapper struct {
Object *bls.PublicKey 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 // SerializedPublicKey defines the serialized bls public key
type SerializedPublicKey [PublicKeySizeInBytes]byte type SerializedPublicKey [PublicKeySizeInBytes]byte

@ -3,9 +3,8 @@ package multibls
import ( import (
"strings" "strings"
"github.com/harmony-one/harmony/crypto/bls"
bls_core "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"
) )
// PrivateKeys stores the bls secret keys that belongs to the node // 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 // GetPrivateKeys creates a multibls PrivateKeys using bls.SecretKey
func GetPrivateKeys(key *bls_core.SecretKey) PrivateKeys { func GetPrivateKeys(secretKeys ...*bls_core.SecretKey) PrivateKeys {
pub := key.GetPublicKey() keys := make(PrivateKeys, 0, len(secretKeys))
pubWrapper := bls.PublicKeyWrapper{Object: pub} for _, secretKey := range secretKeys {
pubWrapper.Bytes.FromLibBLSPublicKey(pub) key := bls.WrapperFromPrivateKey(secretKey)
return PrivateKeys{bls.PrivateKeyWrapper{Pri: key, Pub: &pubWrapper}} keys = append(keys, key)
}
return keys
} }

Loading…
Cancel
Save