Skip to content

Commit

Permalink
fix find Addr Index From Input
Browse files Browse the repository at this point in the history
address index for the spend output related for the input is used to find the priv/pub key pair
ref fibercrypto#139
  • Loading branch information
stdevAlDen committed Feb 14, 2020
1 parent 80c327c commit 60defd0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
47 changes: 44 additions & 3 deletions src/contrib/skywallet/sky-wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"github.com/SkycoinProject/skycoin/src/cipher"
"github.com/SkycoinProject/skycoin/src/coin"
"github.com/SkycoinProject/skycoin/src/readable"
skycoin "github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/models"
"github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/params"
"github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/skytypes"
Expand Down Expand Up @@ -108,18 +109,58 @@ func findValInSlice(val int, arr []int) bool {
return false
}

func getInputs(txn coin.Transaction, indexes []int) (inputs []*messages.SkycoinTransactionInput, err error) {
func getInputs(wlt core.Wallet, txn coin.Transaction, indexes []int) (inputs []*messages.SkycoinTransactionInput, err error) {
for idx, input := range txn.In {
var transactionInput messages.SkycoinTransactionInput
transactionInput.HashIn = proto.String(input.String())
if findValInSlice(idx, indexes) {
transactionInput.Index = proto.Uint32(uint32(idx))
inputIndex, err := findAddrIndexFromInput(wlt, txn, idx)
if err != nil {
logSkyWallet.WithFields(
logrus.Fields{"err": err, "input": txn.In[idx]},
).Errorln("unable to find address index for input")
return nil, err
}
transactionInput.Index = proto.Uint32(inputIndex)
}
inputs = append(inputs, &transactionInput)
}
return inputs, nil
}

func spendingOutputFromRemote(inputHash cipher.SHA256) (*readable.SpentOutput, error) {
logSkyWallet.Info("Getting spent outputs for transaction inputs")
c, err := skycoin.NewSkycoinApiClient(skycoin.PoolSection)
if err != nil {
return nil, err
}
defer skycoin.ReturnSkycoinClient(c)
out, err := c.UxOut(inputHash.String())
if err != nil {
logSkyWallet.WithFields(
logrus.Fields{"err": err, "in": inputHash},
).Errorln("unable to get output from backend")
return nil, err
}
return out, nil
}

// findAddrIndexFromInput return the address index for the matching output of the given input index
// FIXME: replace this implementation with precalculated data
func findAddrIndexFromInput(wlt core.Wallet, txn coin.Transaction, inputIndex int) (uint32, error) {
input := txn.In[inputIndex]
retrievedOutput, err := spendingOutputFromRemote(input)
if err != nil {
return 0, err
}
addrIndex, err := getAddrIndex(wlt, retrievedOutput.OwnerAddress)
if err != nil {
logSkyWallet.WithError(err).Errorln("unable to find address index from input")
return 0, err
}
return addrIndex, nil
}

func getAddrIndex(wlt core.Wallet, addr string) (uint32, error) {
addrs, err := wlt.GetLoadedAddresses()
if err != nil {
Expand Down Expand Up @@ -206,7 +247,7 @@ func coin2Core(txn *coin.Transaction, fee uint64) (core.Transaction, error) {
}

func (sw *SkyWallet) signTxn(txn *coin.Transaction, idxs []int, dt string) (*coin.Transaction, error) {
transactionInputs, err := getInputs(*txn, idxs)
transactionInputs, err := getInputs(sw.wlt, *txn, idxs)
if err != nil {
logSkyWallet.WithError(err).Errorln("unable to get inputs")
return nil, fce.ErrTxnSignFailure
Expand Down
2 changes: 1 addition & 1 deletion src/models/walletsModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (walletModel *WalletModel) sniffHw() {
for {
hwConnectedOn = []int{}
checkForDerivationType(skyWallet.WalletTypeDeterministic)
checkForDerivationType(skyWallet.WalletTypeBip44)
//checkForDerivationType(skyWallet.WalletTypeBip44)
time.Sleep(time.Millisecond * 500)
}
}()
Expand Down

0 comments on commit 60defd0

Please sign in to comment.