Allow Signer to be generated with priv key

Prior to this change, a custom Signer would have no knowledge of the private
key stored in the configuration file. This changes introduces a generator
function, which creates a Signer based on the private key. This provides an
opportunity for customer Signers to adjust behaviour based on the key
contents. (E.g. imagine key contents are a key label, rather than the key
itself).
This commit is contained in:
Duncan Jones
2017-09-11 18:56:41 +01:00
committed by Ethan Buchman
parent 7e4a704bd1
commit 0d392a0442
2 changed files with 19 additions and 17 deletions

View File

@@ -1,14 +1,13 @@
package main
import (
"os"
"github.com/tendermint/tmlibs/cli"
"github.com/tendermint/tmlibs/log"
tcrypto "github.com/tendermint/go-crypto"
tc "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/tmlibs/cli"
"github.com/tendermint/tmlibs/log"
"os"
)
var (
@@ -35,12 +34,12 @@ func main() {
rootCmd.AddCommand(tc.TestnetFilesCmd)
rootCmd.AddCommand(tc.VersionCmd)
// Override with HSM implementation, otherwise nil will trigger default
// software signer:
var signer types.Signer = nil
signerGenerator := func(pk tcrypto.PrivKey) types.Signer {
// Return your own signer implementation here
return types.NewDefaultSigner(pk)
}
privValidator := types.LoadPrivValidatorWithSigner(config.PrivValidatorFile(),
signer)
privValidator := types.LoadPrivValidatorWithSigner(config.PrivValidatorFile(), signerGenerator)
rootCmd.AddCommand(tc.NewRunNodeCmd(privValidator))
cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv("$HOME/.tendermint"))

View File

@@ -55,6 +55,10 @@ type PrivValidator struct {
mtx sync.Mutex
}
type SignerGenerator func(pk crypto.PrivKey) (Signer)
// This is used to sign votes.
// It is the caller's duty to verify the msg before calling Sign,
// eg. to avoid double signing.
@@ -112,10 +116,12 @@ func GenPrivValidator() *PrivValidator {
}
func LoadPrivValidator(filePath string) *PrivValidator {
return LoadPrivValidatorWithSigner(filePath, nil)
return LoadPrivValidatorWithSigner(filePath, func(pk crypto.PrivKey) Signer {
return NewDefaultSigner(pk)
})
}
func LoadPrivValidatorWithSigner(filePath string, signer Signer) *PrivValidator {
func LoadPrivValidatorWithSigner(filePath string, generator SignerGenerator) *PrivValidator {
privValJSONBytes, err := ioutil.ReadFile(filePath)
if err != nil {
Exit(err.Error())
@@ -127,11 +133,8 @@ func LoadPrivValidatorWithSigner(filePath string, signer Signer) *PrivValidator
}
privVal.filePath = filePath
if signer == nil {
privVal.Signer = NewDefaultSigner(privVal.PrivKey)
} else {
privVal.Signer = signer
}
privVal.Signer = generator(privVal.PrivKey)
privVal.setPubKeyAndAddress()
return &privVal
}