frostfs-node/pkg/util/config/crypto.go
Evgenii Stratonikov d84d52924a [#1467] pkg/util: Allow to load default account from a wallet
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2022-06-02 16:13:20 +03:00

44 lines
941 B
Go

package config
import (
"errors"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet"
)
// LoadAccount loads NEP-6 load, unlocks and returns the provided account.
func LoadAccount(path, addr, password string) (*wallet.Account, error) {
w, err := wallet.NewWalletFromFile(path)
if err != nil {
return nil, err
}
defer w.Close()
var h util.Uint160
if addr == "" {
h = w.GetChangeAddress()
if h.Equals(util.Uint160{}) {
return nil, errors.New("can't find a suitable account in the wallet")
}
} else {
h, err = address.StringToUint160(addr)
if err != nil {
return nil, err
}
}
acc := w.GetAccount(h)
if acc == nil {
return nil, errors.New("account is missing")
}
if err := acc.Decrypt(password, keys.NEP2ScryptParams()); err != nil {
return nil, err
}
return acc, nil
}