Skip to content

Commit

Permalink
Merge pull request #4186 from filecoin-project/asr/default-address
Browse files Browse the repository at this point in the history
Support addresses with mainnet prefixes
  • Loading branch information
magik6k authored Oct 6, 2020
2 parents 405aba4 + a179cad commit 2bf42d5
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 191 deletions.
4 changes: 4 additions & 0 deletions build/params_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package build

import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/chain/actors/policy"

Expand Down Expand Up @@ -34,6 +35,9 @@ func init() {
abi.RegisteredSealProof_StackedDrg32GiBV1,
abi.RegisteredSealProof_StackedDrg64GiBV1,
)

SetAddressNetwork(address.Mainnet)

Devnet = false
}

Expand Down
48 changes: 47 additions & 1 deletion chain/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (w *Wallet) findKey(addr address.Address) (*Key, error) {
return nil, nil
}

ki, err := w.keystore.Get(KNamePrefix + addr.String())
ki, err := w.tryFind(addr)
if err != nil {
if xerrors.Is(err, types.ErrKeyInfoNotFound) {
return nil, nil
Expand All @@ -96,6 +96,42 @@ func (w *Wallet) findKey(addr address.Address) (*Key, error) {
return k, nil
}

func (w *Wallet) tryFind(addr address.Address) (types.KeyInfo, error) {

ki, err := w.keystore.Get(KNamePrefix + addr.String())
if err == nil {
return ki, err
}

if !xerrors.Is(err, types.ErrKeyInfoNotFound) {
return types.KeyInfo{}, err
}

// We got an ErrKeyInfoNotFound error
// Try again, this time with the testnet prefix

aChars := []rune(addr.String())
prefixRunes := []rune(address.TestnetPrefix)
if len(prefixRunes) != 1 {
return types.KeyInfo{}, xerrors.Errorf("unexpected prefix length: %d", len(prefixRunes))
}

aChars[0] = prefixRunes[0]
ki, err = w.keystore.Get(KNamePrefix + string(aChars))
if err != nil {
return types.KeyInfo{}, err
}

// We found it with the testnet prefix
// Add this KeyInfo with the mainnet prefix address string
err = w.keystore.Put(KNamePrefix+addr.String(), ki)
if err != nil {
return types.KeyInfo{}, err
}

return ki, nil
}

func (w *Wallet) Export(addr address.Address) (*types.KeyInfo, error) {
k, err := w.findKey(addr)
if err != nil {
Expand Down Expand Up @@ -129,6 +165,7 @@ func (w *Wallet) ListAddrs() ([]address.Address, error) {

sort.Strings(all)

seen := map[address.Address]struct{}{}
out := make([]address.Address, 0, len(all))
for _, a := range all {
if strings.HasPrefix(a, KNamePrefix) {
Expand All @@ -137,10 +174,19 @@ func (w *Wallet) ListAddrs() ([]address.Address, error) {
if err != nil {
return nil, xerrors.Errorf("converting name to address: %w", err)
}
if _, ok := seen[addr]; ok {
continue // got duplicate with a different prefix
}
seen[addr] = struct{}{}

out = append(out, addr)
}
}

sort.Slice(out, func(i, j int) bool {
return out[i].String() < out[j].String()
})

return out, nil
}

Expand Down
Loading

0 comments on commit 2bf42d5

Please sign in to comment.