wallet: drop publicKey from Account

It's not very useful and it's only available when we have a private key
anyway.
This commit is contained in:
Roman Khimov 2022-09-01 16:44:50 +03:00
parent 53edbd569f
commit a30e73a0d7
3 changed files with 13 additions and 11 deletions

View file

@ -1,7 +1,6 @@
package wallet package wallet
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
@ -21,9 +20,6 @@ type Account struct {
// NEO private key. // NEO private key.
privateKey *keys.PrivateKey privateKey *keys.PrivateKey
// NEO public key.
publicKey []byte
// NEO public address. // NEO public address.
Address string `json:"address"` Address string `json:"address"`
@ -160,8 +156,6 @@ func (a *Account) Decrypt(passphrase string, scrypt keys.ScryptParams) error {
return err return err
} }
a.publicKey = a.privateKey.PublicKey().Bytes()
return nil return nil
} }
@ -208,9 +202,13 @@ func (a *Account) ConvertMultisig(m int, pubs []*keys.PublicKey) error {
if a.Locked { if a.Locked {
return errors.New("account is locked") return errors.New("account is locked")
} }
if a.privateKey == nil {
return errors.New("account key is not available (need to decrypt?)")
}
var found bool var found bool
accKey := a.privateKey.PublicKey()
for i := range pubs { for i := range pubs {
if bytes.Equal(a.publicKey, pubs[i].Bytes()) { if accKey.Equal(pubs[i]) {
found = true found = true
break break
} }
@ -240,7 +238,6 @@ func NewAccountFromPrivateKey(p *keys.PrivateKey) *Account {
pubAddr := p.Address() pubAddr := p.Address()
a := &Account{ a := &Account{
publicKey: pubKey.Bytes(),
privateKey: p, privateKey: p,
Address: pubAddr, Address: pubAddr,
Contract: &Contract{ Contract: &Contract{

View file

@ -185,6 +185,13 @@ func TestAccount_ConvertMultisig(t *testing.T) {
require.Error(t, a.ConvertMultisig(1, pubs)) require.Error(t, a.ConvertMultisig(1, pubs))
a.Locked = false a.Locked = false
}) })
t.Run("no private key", func(t *testing.T) {
pk := a.privateKey
a.privateKey = nil
pubs := convertPubs(t, hexs)
require.Error(t, a.ConvertMultisig(0, pubs))
a.privateKey = pk
})
t.Run("invalid number of signatures", func(t *testing.T) { t.Run("invalid number of signatures", func(t *testing.T) {
pubs := convertPubs(t, hexs) pubs := convertPubs(t, hexs)
require.Error(t, a.ConvertMultisig(0, pubs)) require.Error(t, a.ConvertMultisig(0, pubs))
@ -223,7 +230,7 @@ func compareFields(t *testing.T, tk keytestcases.Ktype, acc *Account) {
require.Equalf(t, want, have, "expected address %s got %s", want, have) require.Equalf(t, want, have, "expected address %s got %s", want, have)
want, have = tk.Wif, acc.privateKey.WIF() want, have = tk.Wif, acc.privateKey.WIF()
require.Equalf(t, want, have, "expected wif %s got %s", want, have) require.Equalf(t, want, have, "expected wif %s got %s", want, have)
want, have = tk.PublicKey, hex.EncodeToString(acc.publicKey) want, have = tk.PublicKey, hex.EncodeToString(acc.privateKey.PublicKey().Bytes())
require.Equalf(t, want, have, "expected pub key %s got %s", want, have) require.Equalf(t, want, have, "expected pub key %s got %s", want, have)
want, have = tk.PrivateKey, acc.privateKey.String() want, have = tk.PrivateKey, acc.privateKey.String()
require.Equalf(t, want, have, "expected priv key %s got %s", want, have) require.Equalf(t, want, have, "expected priv key %s got %s", want, have)

View file

@ -48,7 +48,6 @@ func TestAddAccount(t *testing.T) {
wallet.AddAccount(&Account{ wallet.AddAccount(&Account{
privateKey: nil, privateKey: nil,
publicKey: nil,
Address: "real", Address: "real",
EncryptedWIF: "", EncryptedWIF: "",
Label: "", Label: "",
@ -77,7 +76,6 @@ func TestSave(t *testing.T) {
wallet.AddAccount(&Account{ wallet.AddAccount(&Account{
privateKey: nil, privateKey: nil,
publicKey: nil,
Address: "", Address: "",
EncryptedWIF: "", EncryptedWIF: "",
Label: "", Label: "",