consensus: use wallet in service

This commit is contained in:
Evgenii Stratonikov 2020-01-15 17:05:47 +03:00
parent 36cfa5b1f4
commit aaefcdecae
6 changed files with 51 additions and 27 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/smartcontract"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
"github.com/CityOfZion/neo-go/pkg/wallet"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/dbft/block"
"github.com/nspcc-dev/dbft/crypto"
@ -56,6 +57,7 @@ type service struct {
messages chan Payload
transactions chan *transaction.Transaction
lastProposal []util.Uint256
wallet *wallet.Wallet
}
// Config is a configuration for consensus services.
@ -104,7 +106,15 @@ func NewService(cfg Config) (Service, error) {
return srv, nil
}
priv, pub := getKeyPair(cfg.Wallet)
var err error
if srv.wallet, err = wallet.NewWalletFromFile(cfg.Wallet.Path); err != nil {
return nil, err
}
defer srv.wallet.Close()
_, priv, pub := srv.getKeyPair(srv.getValidators())
srv.dbft = dbft.New(
dbft.WithLogger(srv.log),
@ -180,14 +190,23 @@ func (s *service) validatePayload(p *Payload) bool {
return p.Verify(h)
}
func getKeyPair(cfg *config.WalletConfig) (crypto.PrivateKey, crypto.PublicKey) {
// TODO: replace with wallet opening from the given path (#588)
key, err := keys.NEP2Decrypt(cfg.Path, cfg.Password)
if err != nil {
return nil, nil
func (s *service) getKeyPair(pubs []crypto.PublicKey) (int, crypto.PrivateKey, crypto.PublicKey) {
for i := range pubs {
script := pubs[i].(*publicKey).GetVerificationScript()
acc := s.wallet.GetAccount(hash.Hash160(script))
if acc == nil {
continue
}
key, err := keys.NEP2Decrypt(acc.EncryptedWIF, s.Config.Wallet.Password)
if err != nil {
continue
}
return i, &privateKey{PrivateKey: key}, &publicKey{PublicKey: key.PublicKey()}
}
return &privateKey{PrivateKey: key}, &publicKey{PublicKey: key.PublicKey()}
return -1, nil, nil
}
// OnPayload handles Payload receive.