From aaefcdecaeb7386be2345ec8edb12a24c4ee9de5 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 15 Jan 2020 17:05:47 +0300 Subject: [PATCH] consensus: use wallet in service --- pkg/consensus/consensus.go | 33 ++++++++++++++++++----- pkg/consensus/consensus_test.go | 41 +++++++++++++++-------------- pkg/consensus/testdata/wallet1.json | 1 + pkg/consensus/testdata/wallet2.json | 1 + pkg/consensus/testdata/wallet3.json | 1 + pkg/consensus/testdata/wallet4.json | 1 + 6 files changed, 51 insertions(+), 27 deletions(-) create mode 100644 pkg/consensus/testdata/wallet1.json create mode 100644 pkg/consensus/testdata/wallet2.json create mode 100644 pkg/consensus/testdata/wallet3.json create mode 100644 pkg/consensus/testdata/wallet4.json diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index 6aebda543..fadb5955d 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -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. diff --git a/pkg/consensus/consensus_test.go b/pkg/consensus/consensus_test.go index 1cd450f77..2b7d0a4e4 100644 --- a/pkg/consensus/consensus_test.go +++ b/pkg/consensus/consensus_test.go @@ -7,6 +7,7 @@ import ( "github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/transaction" + "github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/util" "github.com/nspcc-dev/dbft/block" "github.com/nspcc-dev/dbft/payload" @@ -182,7 +183,7 @@ func newTestService(t *testing.T) *service { Chain: newTestChain(t), RequestTx: func(...util.Uint256) {}, Wallet: &config.WalletConfig{ - Path: "6PYLmjBYJ4wQTCEfqvnznGJwZeW9pfUcV5m5oreHxqryUgqKpTRAFt9L8Y", + Path: "./testdata/wallet1.json", Password: "one", }, }) @@ -192,35 +193,35 @@ func newTestService(t *testing.T) *service { } func getTestValidator(i int) (*privateKey, *publicKey) { - var wallet *config.WalletConfig + var wif, password string + switch i { case 0: - wallet = &config.WalletConfig{ - Path: "6PYLmjBYJ4wQTCEfqvnznGJwZeW9pfUcV5m5oreHxqryUgqKpTRAFt9L8Y", - Password: "one", - } + wif = "6PYLmjBYJ4wQTCEfqvnznGJwZeW9pfUcV5m5oreHxqryUgqKpTRAFt9L8Y" + password = "one" + case 1: - wallet = &config.WalletConfig{ - Path: "6PYXHjPaNvW8YknSXaKsTWjf9FRxo1s4naV2jdmSQEgzaqKGX368rndN3L", - Password: "two", - } + wif = "6PYXHjPaNvW8YknSXaKsTWjf9FRxo1s4naV2jdmSQEgzaqKGX368rndN3L" + password = "two" + case 2: - wallet = &config.WalletConfig{ - Path: "6PYX86vYiHfUbpD95hfN1xgnvcSxy5skxfWYKu3ztjecxk6ikYs2kcWbeh", - Password: "three", - } + wif = "6PYX86vYiHfUbpD95hfN1xgnvcSxy5skxfWYKu3ztjecxk6ikYs2kcWbeh" + password = "three" + case 3: - wallet = &config.WalletConfig{ - Path: "6PYRXVwHSqFSukL3CuXxdQ75VmsKpjeLgQLEjt83FrtHf1gCVphHzdD4nc", - Password: "four", - } + wif = "6PYRXVwHSqFSukL3CuXxdQ75VmsKpjeLgQLEjt83FrtHf1gCVphHzdD4nc" + password = "four" + default: return nil, nil } - priv, pub := getKeyPair(wallet) + key, err := keys.NEP2Decrypt(wif, password) + if err != nil { + return nil, nil + } - return priv.(*privateKey), pub.(*publicKey) + return &privateKey{PrivateKey: key}, &publicKey{PublicKey: key.PublicKey()} } func newTestChain(t *testing.T) *core.Blockchain { diff --git a/pkg/consensus/testdata/wallet1.json b/pkg/consensus/testdata/wallet1.json new file mode 100644 index 000000000..d03726fd9 --- /dev/null +++ b/pkg/consensus/testdata/wallet1.json @@ -0,0 +1 @@ +{"name":"wallet1","version":"1.0","scrypt":{"n":16384,"r":8,"p":8},"accounts":[{"address":"AKkkumHbBipZ46UMZJoFynJMXzSRnBvKcs","label":null,"isDefault":false,"lock":false,"key":"6PYLmjBYJ4wQTCEfqvnznGJwZeW9pfUcV5m5oreHxqryUgqKpTRAFt9L8Y","contract":{"script":"2102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc2ac","parameters":[{"name":"parameter0","type":"Signature"}],"deployed":false},"extra":null},{"address":"AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU","label":null,"isDefault":false,"lock":false,"key":"6PYLmjBYJ4wQTCEfqvnznGJwZeW9pfUcV5m5oreHxqryUgqKpTRAFt9L8Y","contract":{"script":"532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae","parameters":[{"name":"parameter0","type":"Signature"},{"name":"parameter1","type":"Signature"},{"name":"parameter2","type":"Signature"}],"deployed":false},"extra":null}],"extra":null} \ No newline at end of file diff --git a/pkg/consensus/testdata/wallet2.json b/pkg/consensus/testdata/wallet2.json new file mode 100644 index 000000000..ee6136680 --- /dev/null +++ b/pkg/consensus/testdata/wallet2.json @@ -0,0 +1 @@ +{"name":"wallet2","version":"1.0","scrypt":{"n":16384,"r":8,"p":8},"accounts":[{"address":"AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17","label":null,"isDefault":false,"lock":false,"key":"6PYXHjPaNvW8YknSXaKsTWjf9FRxo1s4naV2jdmSQEgzaqKGX368rndN3L","contract":{"script":"2102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406eac","parameters":[{"name":"parameter0","type":"Signature"}],"deployed":false},"extra":null},{"address":"AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU","label":null,"isDefault":false,"lock":false,"key":"6PYXHjPaNvW8YknSXaKsTWjf9FRxo1s4naV2jdmSQEgzaqKGX368rndN3L","contract":{"script":"532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae","parameters":[{"name":"parameter0","type":"Signature"},{"name":"parameter1","type":"Signature"},{"name":"parameter2","type":"Signature"}],"deployed":false},"extra":null}],"extra":null} \ No newline at end of file diff --git a/pkg/consensus/testdata/wallet3.json b/pkg/consensus/testdata/wallet3.json new file mode 100644 index 000000000..3c72d1751 --- /dev/null +++ b/pkg/consensus/testdata/wallet3.json @@ -0,0 +1 @@ +{"name":"wallet3","version":"1.0","scrypt":{"n":16384,"r":8,"p":8},"accounts":[{"address":"AR3uEnLUdfm1tPMJmiJQurAXGL7h3EXQ2F","label":null,"isDefault":false,"lock":false,"key":"6PYX86vYiHfUbpD95hfN1xgnvcSxy5skxfWYKu3ztjecxk6ikYs2kcWbeh","contract":{"script":"2103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee699ac","parameters":[{"name":"parameter0","type":"Signature"}],"deployed":false},"extra":null},{"address":"AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU","label":null,"isDefault":false,"lock":false,"key":"6PYX86vYiHfUbpD95hfN1xgnvcSxy5skxfWYKu3ztjecxk6ikYs2kcWbeh","contract":{"script":"532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae","parameters":[{"name":"parameter0","type":"Signature"},{"name":"parameter1","type":"Signature"},{"name":"parameter2","type":"Signature"}],"deployed":false},"extra":null}],"extra":null} \ No newline at end of file diff --git a/pkg/consensus/testdata/wallet4.json b/pkg/consensus/testdata/wallet4.json new file mode 100644 index 000000000..d73ab4f7d --- /dev/null +++ b/pkg/consensus/testdata/wallet4.json @@ -0,0 +1 @@ +{"name":"wallet4","version":"1.0","scrypt":{"n":16384,"r":8,"p":8},"accounts":[{"address":"AJmjUqf1jDenxYpuNS4i2NxD9FQYieDpBF","label":null,"isDefault":false,"lock":false,"key":"6PYRXVwHSqFSukL3CuXxdQ75VmsKpjeLgQLEjt83FrtHf1gCVphHzdD4nc","contract":{"script":"2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd62ac","parameters":[{"name":"parameter0","type":"Signature"}],"deployed":false},"extra":null},{"address":"AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU","label":null,"isDefault":false,"lock":false,"key":"6PYRXVwHSqFSukL3CuXxdQ75VmsKpjeLgQLEjt83FrtHf1gCVphHzdD4nc","contract":{"script":"532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae","parameters":[{"name":"parameter0","type":"Signature"},{"name":"parameter1","type":"Signature"},{"name":"parameter2","type":"Signature"}],"deployed":false},"extra":null}],"extra":null} \ No newline at end of file