Skip to content

Commit

Permalink
[models] refs #230 - Start uypgrading QML models code to use wallet v…
Browse files Browse the repository at this point in the history
…2 API
  • Loading branch information
olemis committed Feb 13, 2020
1 parent 43ace46 commit 195041b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ var (
ErrInvalidValue = errors.New("Value errors")
// ErrWalletCantSign wallet can not sign transactions
ErrWalletCantSign = errors.New("Wallet does not support transaction signing")
// ErrUnsupportedOperation operation not supported by type
ErrUnsupportedOperation = errors.New("Operation not supported")
// ErrNotImplemented feature not implemented
ErrNotImplemented = errors.New("Feature not implemented")
// ErrNotSupported feature not supported
Expand Down
4 changes: 2 additions & 2 deletions src/models/modelWallets.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func (m *ModelWallets) loadModel() {
return
}
for wallets.Next() {
addresses, err := wallets.Value().GetLoadedAddresses()
addresses, err := wallets.Value().GetAllLoadedAddresses()
if err != nil {
logWalletsModel.WithError(nil).Warn("Couldn't get loaded address")
return
}
ma := NewModelAddresses(nil)
ma.SetName(wallets.Value().GetLabel())
ma.SetName(wallets.Value().WalletLabel())
oModels := make([]*ModelOutputs, 0)

for addresses.Next() {
Expand Down
51 changes: 31 additions & 20 deletions src/models/walletsManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (walletM *WalletManager) updateAddresses(wltId string) {
logWalletManager.Info("Updating Addresses")
wlt := walletM.WalletEnv.GetWalletSet().GetWallet(wltId)
qAddresses := make([]*QAddress, 0)
it, err := wlt.GetLoadedAddresses()
it, err := wlt.GetAllLoadedAddresses()
if err != nil {
logWalletManager.WithError(err).Warn("Couldn't loaded addresses")
walletM.addresseseByWallets[wltId] = qAddresses
Expand All @@ -184,8 +184,8 @@ func (walletM *WalletManager) updateAddresses(wltId string) {
qml.QQmlEngine_SetObjectOwnership(qAddress, qml.QQmlEngine__CppOwnership)
qAddress.SetAddress(addr.String())
qAddress.SetMarked(0)
qAddress.SetWallet(wlt.GetLabel())
qAddress.SetWalletId(wlt.GetId())
qAddress.SetWallet(wlt.WalletLabel())
qAddress.SetWalletId(wlt.WalletId())
skyFl, err := addr.GetCryptoAccount().GetBalance(params.SkycoinTicker)
if err != nil {
qAddress.SetAddressSky("N/A")
Expand Down Expand Up @@ -225,7 +225,7 @@ func (walletM *WalletManager) updateAddresses(wltId string) {

func (walletM *WalletManager) updateOutputs(wltId, address string) {
outs := make([]*QOutput, 0)
addressIterator, err := walletM.WalletEnv.GetWalletSet().GetWallet(wltId).GetLoadedAddresses()
addressIterator, err := walletM.WalletEnv.GetWalletSet().GetWallet(wltId).GetAllLoadedAddresses()
if err != nil {
logWalletManager.WithError(err).Warn("Couldn't get an addresses iterator")
walletM.outputsByAddress[address] = outs
Expand Down Expand Up @@ -298,7 +298,7 @@ func (walletM *WalletManager) updateWallets() {
}
for it.Next() {

encrypted, err := walletM.WalletEnv.GetStorage().IsEncrypted(it.Value().GetId())
encrypted, err := walletM.WalletEnv.GetStorage().IsEncrypted(it.Value().WalletId())
if err != nil {
logWalletManager.WithError(err).Warn("Couldn't get wallet by id")
continue
Expand All @@ -319,7 +319,7 @@ func (walletM *WalletManager) getAllAddresses() []*QAddress {
return nil
}
for wltIter.Next() {
qAddresses = append(qAddresses, walletM.getAddresses(wltIter.Value().GetId())...)
qAddresses = append(qAddresses, walletM.getAddresses(wltIter.Value().WalletId())...)
}
return qAddresses
}
Expand Down Expand Up @@ -406,7 +406,7 @@ func (walletM *WalletManager) sendFromOutputs(wltIds []string, from, addrTo, sky
var txn core.Transaction
var err error
if len(wltCache) > 1 {
walletsOutputs := make([]core.WalletOutput, 0)
walletsOutputs := make([]core.TransactionOutput, 0)
for i, wlt := range wlts {
walletsOutputs = append(walletsOutputs, &util.SimpleWalletOutput{
Wallet: wlt,
Expand All @@ -415,7 +415,12 @@ func (walletM *WalletManager) sendFromOutputs(wltIds []string, from, addrTo, sky
}
txn, err = walletM.transactionAPI.Spend(walletsOutputs, outputsTo, &changeAddr, opt)
} else {
txn, err = wlts[0].Spend(outputsFrom, outputsTo, &changeAddr, opt)
bw, isVisor := wlts[0].(core.BlockchainVisor)
if isVisor {
txn, err = bw.Spend(outputsFrom, outputsTo, &changeAddr, opt)
} else {
err = errors.ErrUnsupportedOperation
}
}

if err != nil {
Expand Down Expand Up @@ -486,16 +491,21 @@ func (walletM *WalletManager) sendFromAddresses(wltIds []string, from, addrTo, s
var txn core.Transaction
var err error
if len(wltCache) > 1 {
walletsAddresses := make([]core.WalletAddress, 0)
walletsAddresses := make([]core.Address, 0)
for i, wlt := range wlts {
walletsAddresses = append(walletsAddresses, &util.SimpleWalletAddress{
Wallet: wlt,
UxOut: addrsFrom[i],
Wallet: wlt,
Address: addrsFrom[i],
})
}
txn, err = walletM.transactionAPI.SendFromAddress(walletsAddresses, outputsTo, changeAddr, opt)
} else {
txn, err = wlts[0].SendFromAddress(addrsFrom, outputsTo, changeAddr, opt)
bw, isVisor := wlts[0].(core.BlockchainVisor)
if isVisor {
txn, err = bw.SendFromAddress(addrsFrom, outputsTo, changeAddr, opt)
} else {
err = errors.ErrUnsupportedOperation
}
}

if err != nil {
Expand Down Expand Up @@ -524,7 +534,7 @@ func (walletM *WalletManager) getOutputs(wltId, address string) []*QOutput {
func (walletM *WalletManager) getOutputsFromWallet(wltId string) []*QOutput {
logWalletManager.Info("Getting Outputs from wallet by Id")
outs := make([]*QOutput, 0)
addrIter, err := walletM.WalletEnv.GetWalletSet().GetWallet(wltId).GetLoadedAddresses()
addrIter, err := walletM.WalletEnv.GetWalletSet().GetWallet(wltId).GetAllLoadedAddresses()
if err != nil {
logWalletManager.WithError(err).Warn("Couldn't load addresses iterator")
return nil
Expand Down Expand Up @@ -619,7 +629,7 @@ func (walletM *WalletManager) signTxn(wltIds, address []string, source string, t
logWalletManager.WithError(err).Warnf("No signer %s for wallet %v", source, wlts[0])
return nil
}
txn, err = wlts[0].Sign(qTxn.txn, signer, pwd, nil)
txn, err = signer.SignTransaction(qTxn.txn, pwd, nil)
}

if err != nil {
Expand Down Expand Up @@ -768,15 +778,16 @@ func (walletM *WalletManager) newWalletAddress(id string, n int, password string
// NOTE: No easy way to get plain passwords in memory
password = ""
wltEntriesLen := 0
it, err := wlt.GetLoadedAddresses()
it, err := wlt.GetAllLoadedAddresses()
if err != nil {
logWalletManager.WithError(err).Error("Couldn't load addresses")
return
}
for it.Next() {
wltEntriesLen++
}
wlt.GenAddresses(core.AccountAddress, uint32(wltEntriesLen), uint32(n), pwd)
// FIXME: Account=0 is a quick patch for Skycoin. Other coins may implement BIP44 accounts
wlt.GenAddresses(0, core.AccountAddress, uint32(wltEntriesLen), uint32(n), pwd)
logWalletManager.Info("New addresses created")
}

Expand All @@ -799,7 +810,7 @@ func (walletM *WalletManager) getWallets() []*QWallet {

for it.Next() {

encrypted, err := walletM.WalletEnv.GetStorage().IsEncrypted(it.Value().GetId())
encrypted, err := walletM.WalletEnv.GetStorage().IsEncrypted(it.Value().WalletId())
if err != nil {
logWalletManager.WithError(err).Error("Couldn't get wallets")
return walletM.wallets
Expand All @@ -825,7 +836,7 @@ func (walletM *WalletManager) editWallet(id, label string) *QWallet {
wlt := walletM.WalletEnv.GetWalletSet().GetWallet(id)
wlt.SetLabel(label)
wlt = walletM.WalletEnv.GetWalletSet().GetWallet(id)
encrypted, err := walletM.WalletEnv.GetStorage().IsEncrypted(wlt.GetId())
encrypted, err := walletM.WalletEnv.GetStorage().IsEncrypted(wlt.WalletId())
if err != nil {
logWalletManager.WithError(err).Error("Couldn't edit wallet")
return nil
Expand All @@ -848,10 +859,10 @@ func fromWalletToQWallet(wlt core.FullWallet, isEncrypted bool) *QWallet {

qWallet := NewQWallet(nil)
qml.QQmlEngine_SetObjectOwnership(qWallet, qml.QQmlEngine__CppOwnership)
qWallet.SetName(wlt.GetLabel())
qWallet.SetName(wlt.WalletLabel())
qWallet.SetExpand(false)

qWallet.SetFileName(wlt.GetId())
qWallet.SetFileName(wlt.WalletId())

qWallet.SetEncryptionEnabled(0)
if isEncrypted {
Expand Down

0 comments on commit 195041b

Please sign in to comment.