-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ava-labs/keychain
Key, Ledger, Keychain
- Loading branch information
Showing
14 changed files
with
373 additions
and
40 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
package keychain | ||
|
||
import ( | ||
"fmt" | ||
|
||
"avalanche-tooling-sdk-go/avalanche" | ||
"avalanche-tooling-sdk-go/key" | ||
"avalanche-tooling-sdk-go/ledger" | ||
"avalanche-tooling-sdk-go/utils" | ||
|
||
"github.com/ava-labs/avalanchego/utils/crypto/keychain" | ||
|
||
"golang.org/x/exp/maps" | ||
) | ||
|
||
type Keychain struct { | ||
keychain.Keychain | ||
network avalanche.Network | ||
ledgerDevice *ledger.LedgerDevice | ||
ledgerIndices []uint32 | ||
} | ||
|
||
func (kc *Keychain) P() ([]string, error) { | ||
return utils.P(kc.network.HRP(), kc.Addresses().List()) | ||
} | ||
|
||
func (kc *Keychain) LedgerEnabled() bool { | ||
return kc.ledgerDevice != nil | ||
} | ||
|
||
func (kc *Keychain) AddLedgerIndices(indices []uint32) error { | ||
if kc.LedgerEnabled() { | ||
kc.ledgerIndices = utils.Unique(append(kc.ledgerIndices, indices...)) | ||
utils.Uint32Sort(kc.ledgerIndices) | ||
newKc, err := keychain.NewLedgerKeychainFromIndices(kc.ledgerDevice, kc.ledgerIndices) | ||
if err != nil { | ||
return err | ||
} | ||
kc.Keychain = newKc | ||
return nil | ||
} | ||
return fmt.Errorf("keychain is not ledger enabled") | ||
} | ||
|
||
func (kc *Keychain) AddLedgerAddresses(addresses []string) error { | ||
if kc.LedgerEnabled() { | ||
indices, err := kc.ledgerDevice.FindAddresses(addresses, 0) | ||
if err != nil { | ||
return err | ||
} | ||
return kc.AddLedgerIndices(maps.Values(indices)) | ||
} | ||
return fmt.Errorf("keychain is not ledger enabled") | ||
} | ||
|
||
func (kc *Keychain) AddLedgerFunds(amount uint64) error { | ||
if kc.LedgerEnabled() { | ||
indices, err := kc.ledgerDevice.FindFunds(kc.network, amount, 0) | ||
if err != nil { | ||
return err | ||
} | ||
return kc.AddLedgerIndices(indices) | ||
} | ||
return fmt.Errorf("keychain is not ledger enabled") | ||
} | ||
|
||
func NewKeychain( | ||
network avalanche.Network, | ||
keyPath string, | ||
useEwoq bool, | ||
useLedger bool, | ||
ledgerAddresses []string, | ||
requiredFunds uint64, | ||
) (*Keychain, error) { | ||
// get keychain accessor | ||
if useLedger { | ||
dev, err := ledger.New() | ||
if err != nil { | ||
return nil, err | ||
} | ||
kc := Keychain{ | ||
ledgerDevice: dev, | ||
network: network, | ||
} | ||
if err := kc.AddLedgerIndices([]uint32{0}); err != nil { | ||
return nil, err | ||
} | ||
if requiredFunds > 0 { | ||
if err := kc.AddLedgerFunds(requiredFunds); err != nil { | ||
return nil, err | ||
} | ||
} | ||
if len(ledgerAddresses) > 0 { | ||
if err := kc.AddLedgerAddresses(ledgerAddresses); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return &kc, nil | ||
} | ||
if useEwoq { | ||
sf, err := key.LoadEwoq() | ||
if err != nil { | ||
return nil, err | ||
} | ||
kc := Keychain{ | ||
Keychain: sf.KeyChain(), | ||
network: network, | ||
} | ||
return &kc, nil | ||
} | ||
if keyPath != "" { | ||
sf, err := key.LoadSoft(keyPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
kc := Keychain{ | ||
Keychain: sf.KeyChain(), | ||
network: network, | ||
} | ||
return &kc, nil | ||
} | ||
return nil, fmt.Errorf("not keychain option defined") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
package ledger | ||
|
||
import ( | ||
"avalanche-tooling-sdk-go/avalanche" | ||
"avalanche-tooling-sdk-go/utils" | ||
"fmt" | ||
|
||
"github.com/ava-labs/avalanchego/utils/crypto/keychain" | ||
"github.com/ava-labs/avalanchego/utils/crypto/ledger" | ||
"github.com/ava-labs/avalanchego/utils/formatting/address" | ||
"github.com/ava-labs/avalanchego/vms/platformvm" | ||
) | ||
|
||
const ( | ||
maxIndexToSearch = 1000 | ||
maxIndexToSearchForBalance = 100 | ||
) | ||
|
||
type LedgerDevice struct { | ||
keychain.Ledger | ||
} | ||
|
||
func New() (*LedgerDevice, error) { | ||
avagoDev, err := ledger.New() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dev := LedgerDevice{ | ||
Ledger: avagoDev, | ||
} | ||
return &dev, nil | ||
} | ||
|
||
func (dev *LedgerDevice) P(network avalanche.Network, indices []uint32) ([]string, error) { | ||
addresses, err := dev.Addresses(indices) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return utils.P(network.HRP(), addresses) | ||
} | ||
|
||
func (dev *LedgerDevice) FindAddresses(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) | ||
} | ||
// 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[string]uint32{} | ||
for index := uint32(0); index < maxIndex; index++ { | ||
ledgerAddress, err := dev.Addresses([]uint32{index}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for addressIndex, addr := range addressesIDs { | ||
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, | ||
amount uint64, | ||
maxIndex uint32, | ||
) ([]uint32, error) { | ||
pClient := platformvm.NewClient(network.Endpoint) | ||
totalBalance := uint64(0) | ||
indices := []uint32{} | ||
if maxIndex == 0 { | ||
maxIndex = maxIndexToSearchForBalance | ||
} | ||
for index := uint32(0); index < maxIndex; index++ { | ||
ledgerAddress, err := dev.Addresses([]uint32{index}) | ||
if err != nil { | ||
return []uint32{}, err | ||
} | ||
ctx, cancel := utils.GetAPIContext() | ||
resp, err := pClient.GetBalance(ctx, ledgerAddress) | ||
cancel() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.Balance > 0 { | ||
totalBalance += uint64(resp.Balance) | ||
indices = append(indices, index) | ||
} | ||
if totalBalance >= amount { | ||
break | ||
} | ||
} | ||
if totalBalance < amount { | ||
return nil, fmt.Errorf("not enough funds on ledger") | ||
} | ||
return indices, nil | ||
} |
Oops, something went wrong.