Browse Source

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).
pull/637/head
Duncan Jones 7 years ago
committed by Ethan Buchman
parent
commit
0d392a0442
2 changed files with 19 additions and 17 deletions
  1. +9
    -10
      cmd/hsm/main.go
  2. +10
    -7
      types/priv_validator.go

+ 9
- 10
cmd/hsm/main.go 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"))


+ 10
- 7
types/priv_validator.go 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
}


Loading…
Cancel
Save