-
Notifications
You must be signed in to change notification settings - Fork 810
wallet: remove ledger support in favor of golang SDK implementation of ledger keychain #4413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
felipemadero
wants to merge
6
commits into
master
Choose a base branch
from
remove-ledger-force-sign-hash
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9009d42
wallet: add signing options for chain context
felipemadero cd3e6be
wallet: add networkID to C-chain signer
felipemadero c0337be
wallet: remove ledger support and signHash parameter
felipemadero 0330279
Remove signing options from keychain interface
felipemadero 2688fb9
fix go.mod
felipemadero e453505
cleanup go.mod
felipemadero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -4,25 +4,12 @@ | |
package keychain | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/utils/set" | ||
) | ||
|
||
var ( | ||
_ Keychain = (*ledgerKeychain)(nil) | ||
_ Signer = (*ledgerSigner)(nil) | ||
|
||
ErrInvalidIndicesLength = errors.New("number of indices should be greater than 0") | ||
ErrInvalidNumAddrsToDerive = errors.New("number of addresses to derive should be greater than 0") | ||
ErrInvalidNumAddrsDerived = errors.New("incorrect number of ledger derived addresses") | ||
ErrInvalidNumSignatures = errors.New("incorrect number of signatures") | ||
) | ||
|
||
// Signer implements functions for a keychain to return its main address and | ||
// to sign a hash | ||
// to sign a hash or transaction | ||
type Signer interface { | ||
SignHash([]byte) ([]byte, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think not |
||
Sign([]byte) ([]byte, error) | ||
|
@@ -38,128 +25,3 @@ type Keychain interface { | |
// signer | ||
Addresses() set.Set[ids.ShortID] | ||
} | ||
|
||
// ledgerKeychain is an abstraction of the underlying ledger hardware device, | ||
// to be able to get a signer from a finite set of derived signers | ||
type ledgerKeychain struct { | ||
ledger Ledger | ||
addrs set.Set[ids.ShortID] | ||
addrToIdx map[ids.ShortID]uint32 | ||
} | ||
|
||
// ledgerSigner is an abstraction of the underlying ledger hardware device, | ||
// to be able sign for a specific address | ||
type ledgerSigner struct { | ||
ledger Ledger | ||
idx uint32 | ||
addr ids.ShortID | ||
} | ||
|
||
// NewLedgerKeychain creates a new Ledger with [numToDerive] addresses. | ||
func NewLedgerKeychain(l Ledger, numToDerive int) (Keychain, error) { | ||
if numToDerive < 1 { | ||
return nil, ErrInvalidNumAddrsToDerive | ||
} | ||
|
||
indices := make([]uint32, numToDerive) | ||
for i := range indices { | ||
indices[i] = uint32(i) | ||
} | ||
|
||
return NewLedgerKeychainFromIndices(l, indices) | ||
} | ||
|
||
// NewLedgerKeychainFromIndices creates a new Ledger with addresses taken from the given [indices]. | ||
func NewLedgerKeychainFromIndices(l Ledger, indices []uint32) (Keychain, error) { | ||
if len(indices) == 0 { | ||
return nil, ErrInvalidIndicesLength | ||
} | ||
|
||
addrs, err := l.Addresses(indices) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(addrs) != len(indices) { | ||
return nil, fmt.Errorf( | ||
"%w. expected %d, got %d", | ||
ErrInvalidNumAddrsDerived, | ||
len(indices), | ||
len(addrs), | ||
) | ||
} | ||
|
||
addrsSet := set.Of(addrs...) | ||
|
||
addrToIdx := map[ids.ShortID]uint32{} | ||
for i := range indices { | ||
addrToIdx[addrs[i]] = indices[i] | ||
} | ||
|
||
return &ledgerKeychain{ | ||
ledger: l, | ||
addrs: addrsSet, | ||
addrToIdx: addrToIdx, | ||
}, nil | ||
} | ||
|
||
func (l *ledgerKeychain) Addresses() set.Set[ids.ShortID] { | ||
return l.addrs | ||
} | ||
|
||
func (l *ledgerKeychain) Get(addr ids.ShortID) (Signer, bool) { | ||
idx, ok := l.addrToIdx[addr] | ||
if !ok { | ||
return nil, false | ||
} | ||
|
||
return &ledgerSigner{ | ||
ledger: l.ledger, | ||
idx: idx, | ||
addr: addr, | ||
}, true | ||
} | ||
|
||
// expects to receive a hash of the unsigned tx bytes | ||
func (l *ledgerSigner) SignHash(b []byte) ([]byte, error) { | ||
// Sign using the address with index l.idx on the ledger device. The number | ||
// of returned signatures should be the same length as the provided indices. | ||
sigs, err := l.ledger.SignHash(b, []uint32{l.idx}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if sigsLen := len(sigs); sigsLen != 1 { | ||
return nil, fmt.Errorf( | ||
"%w. expected 1, got %d", | ||
ErrInvalidNumSignatures, | ||
sigsLen, | ||
) | ||
} | ||
|
||
return sigs[0], nil | ||
} | ||
|
||
// expects to receive the unsigned tx bytes | ||
func (l *ledgerSigner) Sign(b []byte) ([]byte, error) { | ||
// Sign using the address with index l.idx on the ledger device. The number | ||
// of returned signatures should be the same length as the provided indices. | ||
sigs, err := l.ledger.Sign(b, []uint32{l.idx}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if sigsLen := len(sigs); sigsLen != 1 { | ||
return nil, fmt.Errorf( | ||
"%w. expected 1, got %d", | ||
ErrInvalidNumSignatures, | ||
sigsLen, | ||
) | ||
} | ||
|
||
return sigs[0], nil | ||
} | ||
|
||
func (l *ledgerSigner) Address() ids.ShortID { | ||
return l.addr | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.