2021-12-15 03:48:40 -05:00
|
|
|
package gokwallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/godbus/dbus/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2021-12-18 22:33:50 -05:00
|
|
|
NewWalletManager returns a WalletManager. It requires a RecurseOpts
|
|
|
|
(you can use DefaultRecurseOpts, call NewRecurseOpts, or provide your own RecurseOpts struct).
|
2021-12-15 03:48:40 -05:00
|
|
|
If appId is empty/nil, DefaultAppID will be used as the app ID.
|
|
|
|
If appId is specified, only the first string is used.
|
|
|
|
*/
|
2021-12-18 22:33:50 -05:00
|
|
|
func NewWalletManager(recursion *RecurseOpts, appID ...string) (wm *WalletManager, err error) {
|
2021-12-15 03:48:40 -05:00
|
|
|
|
|
|
|
var realAppID string
|
|
|
|
|
|
|
|
if appID != nil && len(appID) > 0 {
|
|
|
|
realAppID = appID[0]
|
|
|
|
} else {
|
|
|
|
realAppID = DefaultAppID
|
|
|
|
}
|
|
|
|
|
2021-12-18 22:33:50 -05:00
|
|
|
if wm, err = newWM(realAppID, recursion); err != nil {
|
|
|
|
return
|
2021-12-15 03:48:40 -05:00
|
|
|
}
|
|
|
|
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
NewWalletManagerFiles returns a WalletManager from one or more filePaths (*.kwl, *.tar, or *.xml exports).
|
|
|
|
Note that if the wallet file was created via an "encrypted export", it will be a .kwl file
|
|
|
|
inside a .tar.
|
|
|
|
err will contain a MultiError if any filepaths specified do not exist or cannot be opened.
|
|
|
|
It requires a RecurseOpts (you can use DefaultRecurseOpts, call NewRecurseOpts,
|
|
|
|
or provide your own RecurseOpts struct).
|
|
|
|
If appId is empty, DefaultAppID will be used as the app ID.
|
|
|
|
*/
|
2021-12-23 04:21:21 -05:00
|
|
|
/* TODO: POC this before exposing. I have NO idea if/how it'll work.
|
2021-12-18 22:33:50 -05:00
|
|
|
func NewWalletManagerFiles(recursion *RecurseOpts, appId string, filePaths ...string) (wm *WalletManager, err error) {
|
|
|
|
|
|
|
|
var exist bool
|
|
|
|
var errs []error = make([]error, 0)
|
|
|
|
var realFilePaths []string = make([]string, 0)
|
|
|
|
|
|
|
|
if appId == "" {
|
|
|
|
appId = DefaultAppID
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range filePaths {
|
|
|
|
if f == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if exist, err = paths.RealPathExists(&f); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
err = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !exist {
|
|
|
|
err = errors.New(fmt.Sprintf("%v does not exist", f))
|
|
|
|
err = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
realFilePaths = append(realFilePaths, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: do the actual newWM here.
|
|
|
|
|
|
|
|
if errs != nil && len(errs) > 0 {
|
|
|
|
err = NewErrors(errs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
CloseWallet closes a Wallet.
|
|
|
|
Unlike Wallet.Close, this closes access for ALL applications/WalletManagers
|
|
|
|
for the specified Wallet - not just this WalletManager.
|
|
|
|
*/
|
|
|
|
func (wm *WalletManager) CloseWallet(walletName string) (err error) {
|
2021-12-25 02:54:18 -05:00
|
|
|
|
|
|
|
var call *dbus.Call
|
2021-12-18 22:33:50 -05:00
|
|
|
var rslt int32
|
|
|
|
|
|
|
|
if !wm.isInit {
|
2021-12-25 02:54:18 -05:00
|
|
|
err = ErrInitWM
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using a handler allows us to close access for this particular parent WalletManager.
|
2021-12-25 02:54:18 -05:00
|
|
|
if call = wm.Dbus.Call(
|
2021-12-18 22:33:50 -05:00
|
|
|
DbusWMClose, 0, walletName, false,
|
2021-12-25 02:54:18 -05:00
|
|
|
); call.Err != nil {
|
|
|
|
err = call.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = call.Store(&rslt); err != nil {
|
2021-12-15 03:48:40 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-18 22:33:50 -05:00
|
|
|
|
|
|
|
err = resultCheck(rslt)
|
2021-12-15 03:48:40 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-18 22:33:50 -05:00
|
|
|
ForceCloseWallet is like WalletManager.CloseWallet but will still close a Wallet even if currently in use.
|
|
|
|
Unlike Wallet.ForceClose, this closes access for ALL applications/WalletManagers
|
|
|
|
for the specified Wallet - not just this WalletManager.
|
2021-12-15 03:48:40 -05:00
|
|
|
*/
|
2021-12-20 04:02:48 -05:00
|
|
|
func (wm *WalletManager) ForceCloseWallet(walletName string) (err error) {
|
2021-12-18 22:33:50 -05:00
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
var call *dbus.Call
|
2021-12-18 22:33:50 -05:00
|
|
|
var rslt int32
|
|
|
|
|
|
|
|
if !wm.isInit {
|
2021-12-25 02:54:18 -05:00
|
|
|
err = ErrInitWM
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using a handler allows us to close access for this particular parent WalletManager.
|
2021-12-25 02:54:18 -05:00
|
|
|
if call = wm.Dbus.Call(
|
2021-12-18 22:33:50 -05:00
|
|
|
DbusWMClose, 0, walletName, false,
|
2021-12-25 02:54:18 -05:00
|
|
|
); call.Err != nil {
|
|
|
|
err = call.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = call.Store(&rslt); err != nil {
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = resultCheck(rslt)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
/*
|
|
|
|
CloseAllWallets closes all Wallet objects. They do *not* need to be part of WalletManager.Wallets.
|
|
|
|
"All wallets" really means *all* wallets.
|
|
|
|
*/
|
2021-12-18 22:33:50 -05:00
|
|
|
func (wm *WalletManager) CloseAllWallets() (err error) {
|
|
|
|
|
|
|
|
var call *dbus.Call
|
|
|
|
|
|
|
|
if !wm.isInit {
|
2021-12-25 02:54:18 -05:00
|
|
|
err = ErrInitWM
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
if call = wm.Dbus.Call(
|
2021-12-18 22:33:50 -05:00
|
|
|
DbusWMCloseAllWallets, 0,
|
2021-12-25 02:54:18 -05:00
|
|
|
); call.Err != nil {
|
|
|
|
err = call.Err
|
|
|
|
return
|
|
|
|
}
|
2021-12-18 22:33:50 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEnabled returns whether KWallet is enabled or not (and also updates WalletManager.Enabled).
|
|
|
|
func (wm *WalletManager) IsEnabled() (enabled bool, err error) {
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
var call *dbus.Call
|
|
|
|
|
2021-12-18 22:33:50 -05:00
|
|
|
if !wm.isInit {
|
2021-12-25 02:54:18 -05:00
|
|
|
err = ErrInitWM
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
if call = wm.Dbus.Call(
|
2021-12-18 22:33:50 -05:00
|
|
|
DbusWMIsEnabled, 0,
|
2021-12-25 02:54:18 -05:00
|
|
|
); call.Err != nil {
|
|
|
|
err = call.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = call.Store(&wm.Enabled); err != nil {
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled = wm.Enabled
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalWallet returns the "local" wallet (and updates WalletManager.Local).
|
|
|
|
func (wm *WalletManager) LocalWallet() (w *Wallet, err error) {
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
var call *dbus.Call
|
2021-12-18 22:33:50 -05:00
|
|
|
var wn string
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
if !wm.isInit {
|
|
|
|
err = ErrInitWM
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if call = wm.Dbus.Call(
|
2021-12-18 22:33:50 -05:00
|
|
|
DbusWMLocalWallet, 0,
|
2021-12-25 02:54:18 -05:00
|
|
|
); call.Err != nil {
|
|
|
|
err = call.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = call.Store(&wn); err != nil {
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if w, err = NewWallet(wm, wn, wm.Recurse); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wm.Local = w
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkWallet returns the "network" wallet (and updates WalletManager.Network).
|
|
|
|
func (wm *WalletManager) NetworkWallet() (w *Wallet, err error) {
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
var call *dbus.Call
|
2021-12-18 22:33:50 -05:00
|
|
|
var wn string
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
if !wm.isInit {
|
|
|
|
err = ErrInitWM
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if call = wm.Dbus.Call(
|
2021-12-18 22:33:50 -05:00
|
|
|
DbusWMNetWallet, 0,
|
2021-12-25 02:54:18 -05:00
|
|
|
); call.Err != nil {
|
|
|
|
err = call.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = call.Store(&wn); err != nil {
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if w, err = NewWallet(wm, wn, wm.Recurse); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wm.Network = w
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-20 04:02:48 -05:00
|
|
|
// WalletNames returns a list of existing Wallet names.
|
2021-12-18 22:33:50 -05:00
|
|
|
func (wm *WalletManager) WalletNames() (wallets []string, err error) {
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
var call *dbus.Call
|
|
|
|
|
|
|
|
if call = wm.Dbus.Call(
|
2021-12-18 22:33:50 -05:00
|
|
|
DbusWMWallets, 0,
|
2021-12-25 02:54:18 -05:00
|
|
|
); call.Err != nil {
|
|
|
|
err = call.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = call.Store(&wallets); err != nil {
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update fetches/updates all Wallet objects in a WalletManager.
|
2021-12-15 03:48:40 -05:00
|
|
|
func (wm *WalletManager) Update() (err error) {
|
|
|
|
|
2021-12-18 22:33:50 -05:00
|
|
|
var walletNames []string
|
|
|
|
var errs []error = make([]error, 0)
|
|
|
|
|
|
|
|
if !wm.isInit {
|
2021-12-25 02:54:18 -05:00
|
|
|
err = ErrInitWM
|
2021-12-18 22:33:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if walletNames, err = wm.WalletNames(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wm.Wallets = make(map[string]*Wallet)
|
|
|
|
|
|
|
|
for _, wn := range walletNames {
|
|
|
|
|
|
|
|
if wm.Wallets[wn], err = NewWallet(wm, wn, wm.Recurse); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
err = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs) > 0 {
|
|
|
|
err = NewErrors(errs...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// newWM is what does the heavy lifting behind NewWalletManager and NewWalletManagerFiles.
|
|
|
|
func newWM(appId string, recursion *RecurseOpts, filePaths ...string) (wm *WalletManager, err error) {
|
|
|
|
|
|
|
|
wm = &WalletManager{
|
|
|
|
DbusObject: &DbusObject{
|
|
|
|
Conn: nil,
|
|
|
|
Dbus: nil,
|
|
|
|
},
|
|
|
|
AppID: appId,
|
|
|
|
Wallets: nil,
|
|
|
|
Recurse: recursion,
|
|
|
|
}
|
|
|
|
|
|
|
|
if wm.DbusObject.Conn, err = dbus.SessionBus(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wm.DbusObject.Dbus = wm.DbusObject.Conn.Object(DbusService, dbus.ObjectPath(DbusPath))
|
|
|
|
|
2021-12-24 02:25:37 -05:00
|
|
|
wm.isInit = true
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
if _, err = wm.IsEnabled(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-18 22:33:50 -05:00
|
|
|
if wm.Recurse.All || wm.Recurse.Wallets {
|
|
|
|
if err = wm.Update(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = wm.LocalWallet(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = wm.NetworkWallet(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 03:48:40 -05:00
|
|
|
|
2021-12-18 22:33:50 -05:00
|
|
|
wm.isInit = true
|
2021-12-15 03:48:40 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|