Skip to content

Commit

Permalink
mv commit/sign to wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero committed May 31, 2024
1 parent 78b22a9 commit d89192f
Show file tree
Hide file tree
Showing 4 changed files with 391 additions and 281 deletions.
16 changes: 14 additions & 2 deletions keychain/keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ava-labs/avalanche-tooling-sdk-go/ledger"
"github.com/ava-labs/avalanche-tooling-sdk-go/utils"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/keychain"

"golang.org/x/exp/maps"
Expand Down Expand Up @@ -44,7 +45,18 @@ func (kc *Keychain) AddLedgerIndices(indices []uint32) error {
return fmt.Errorf("keychain is not ledger enabled")
}

func (kc *Keychain) AddLedgerAddresses(addresses []string) error {
func (kc *Keychain) AddLedgerStrAddresses(addresses []string) error {
if kc.LedgerEnabled() {
indices, err := kc.ledgerDevice.FindStrAddresses(addresses, 0)
if err != nil {
return err
}
return kc.AddLedgerIndices(maps.Values(indices))
}
return fmt.Errorf("keychain is not ledger enabled")
}

func (kc *Keychain) AddLedgerAddresses(addresses []ids.ShortID) error {
if kc.LedgerEnabled() {
indices, err := kc.ledgerDevice.FindAddresses(addresses, 0)
if err != nil {
Expand Down Expand Up @@ -93,7 +105,7 @@ func NewKeychain(
}
}
if len(ledgerAddresses) > 0 {
if err := kc.AddLedgerAddresses(ledgerAddresses); err != nil {
if err := kc.AddLedgerStrAddresses(ledgerAddresses); err != nil {
return nil, err
}
}
Expand Down
28 changes: 27 additions & 1 deletion ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ava-labs/avalanche-tooling-sdk-go/avalanche"
"github.com/ava-labs/avalanche-tooling-sdk-go/utils"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/keychain"
"github.com/ava-labs/avalanchego/utils/crypto/ledger"
"github.com/ava-labs/avalanchego/utils/formatting/address"
Expand Down Expand Up @@ -42,7 +43,7 @@ func (dev *LedgerDevice) P(network avalanche.Network, indices []uint32) ([]strin
return utils.P(network.HRP(), addresses)
}

func (dev *LedgerDevice) FindAddresses(addresses []string, maxIndex uint32) (map[string]uint32, error) {
func (dev *LedgerDevice) FindStrAddresses(addresses []string, maxIndex uint32) (map[string]uint32, error) {
addressesIDs, err := address.ParseToIDs(addresses)
if err != nil {
return nil, fmt.Errorf("failure parsing ledger addresses: %w", err)
Expand Down Expand Up @@ -71,6 +72,31 @@ func (dev *LedgerDevice) FindAddresses(addresses []string, maxIndex uint32) (map
return indices, nil
}

func (dev *LedgerDevice) FindAddresses(addresses []ids.ShortID, maxIndex uint32) (map[ids.ShortID]uint32, error) {
// for all ledger indices to search for, find if the ledger address belongs to the input
// addresses and, if so, add an index association to indexMap.
// breaks the loop if all addresses were found
if maxIndex == 0 {
maxIndex = maxIndexToSearch
}
indices := map[ids.ShortID]uint32{}
for index := uint32(0); index < maxIndex; index++ {
ledgerAddress, err := dev.Addresses([]uint32{index})
if err != nil {
return nil, err
}
for addressIndex, addr := range addresses {
if addr == ledgerAddress[0] {
indices[addresses[addressIndex]] = index
}
}
if len(indices) == len(addresses) {
break
}
}
return indices, nil
}

// search for a set of indices that pay a given amount
func (dev *LedgerDevice) FindFunds(
network avalanche.Network,
Expand Down
Loading

0 comments on commit d89192f

Please sign in to comment.