2021-05-31 08:55:38 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2021-06-28 14:24:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-05-31 08:55:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2022-06-02 07:58:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-05-31 08:55:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// LoadAccount loads NEP-6 load, unlocks and returns the provided account.
|
2021-05-31 08:55:38 +00:00
|
|
|
func LoadAccount(path, addr, password string) (*wallet.Account, error) {
|
|
|
|
w, err := wallet.NewWalletFromFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer w.Close()
|
|
|
|
|
2022-06-02 07:58:12 +00:00
|
|
|
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
|
|
|
|
}
|
2021-05-31 08:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc := w.GetAccount(h)
|
|
|
|
if acc == nil {
|
|
|
|
return nil, errors.New("account is missing")
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:24:04 +00:00
|
|
|
if err := acc.Decrypt(password, keys.NEP2ScryptParams()); err != nil {
|
2021-05-31 08:55:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc, nil
|
|
|
|
}
|