Merge pull request #4186 from filecoin-project/asr/default-address
Support addresses with mainnet prefixes
This commit is contained in:
commit
2bf42d5318
@ -5,6 +5,7 @@
|
|||||||
package build
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/filecoin-project/go-address"
|
||||||
"github.com/filecoin-project/go-state-types/abi"
|
"github.com/filecoin-project/go-state-types/abi"
|
||||||
"github.com/filecoin-project/lotus/chain/actors/policy"
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
||||||
|
|
||||||
@ -34,6 +35,9 @@ func init() {
|
|||||||
abi.RegisteredSealProof_StackedDrg32GiBV1,
|
abi.RegisteredSealProof_StackedDrg32GiBV1,
|
||||||
abi.RegisteredSealProof_StackedDrg64GiBV1,
|
abi.RegisteredSealProof_StackedDrg64GiBV1,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
SetAddressNetwork(address.Mainnet)
|
||||||
|
|
||||||
Devnet = false
|
Devnet = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ func (w *Wallet) findKey(addr address.Address) (*Key, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ki, err := w.keystore.Get(KNamePrefix + addr.String())
|
ki, err := w.tryFind(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if xerrors.Is(err, types.ErrKeyInfoNotFound) {
|
if xerrors.Is(err, types.ErrKeyInfoNotFound) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -96,6 +96,42 @@ func (w *Wallet) findKey(addr address.Address) (*Key, error) {
|
|||||||
return k, nil
|
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) {
|
func (w *Wallet) Export(addr address.Address) (*types.KeyInfo, error) {
|
||||||
k, err := w.findKey(addr)
|
k, err := w.findKey(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -129,6 +165,7 @@ func (w *Wallet) ListAddrs() ([]address.Address, error) {
|
|||||||
|
|
||||||
sort.Strings(all)
|
sort.Strings(all)
|
||||||
|
|
||||||
|
seen := map[address.Address]struct{}{}
|
||||||
out := make([]address.Address, 0, len(all))
|
out := make([]address.Address, 0, len(all))
|
||||||
for _, a := range all {
|
for _, a := range all {
|
||||||
if strings.HasPrefix(a, KNamePrefix) {
|
if strings.HasPrefix(a, KNamePrefix) {
|
||||||
@ -137,10 +174,19 @@ func (w *Wallet) ListAddrs() ([]address.Address, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("converting name to address: %w", err)
|
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)
|
out = append(out, addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Slice(out, func(i, j int) bool {
|
||||||
|
return out[i].String() < out[j].String()
|
||||||
|
})
|
||||||
|
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -53,8 +53,12 @@ func TestStore(t *testing.T) {
|
|||||||
addrs, err = store.ListChannels()
|
addrs, err = store.ListChannels()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Len(t, addrs, 2)
|
require.Len(t, addrs, 2)
|
||||||
require.Contains(t, addrsStrings(addrs), "t0100")
|
t0100, err := address.NewIDAddress(100)
|
||||||
require.Contains(t, addrsStrings(addrs), "t0200")
|
require.NoError(t, err)
|
||||||
|
t0200, err := address.NewIDAddress(200)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Contains(t, addrs, t0100)
|
||||||
|
require.Contains(t, addrs, t0200)
|
||||||
|
|
||||||
// Request vouchers for channel
|
// Request vouchers for channel
|
||||||
vouchers, err := store.VouchersForPaych(*ci.Channel)
|
vouchers, err := store.VouchersForPaych(*ci.Channel)
|
||||||
@ -79,11 +83,3 @@ func TestStore(t *testing.T) {
|
|||||||
_, err = store.AllocateLane(tutils.NewIDAddr(t, 300))
|
_, err = store.AllocateLane(tutils.NewIDAddr(t, 300))
|
||||||
require.Equal(t, err, ErrChannelNotTracked)
|
require.Equal(t, err, ErrChannelNotTracked)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addrsStrings(addrs []address.Address) []string {
|
|
||||||
str := make([]string, len(addrs))
|
|
||||||
for i, a := range addrs {
|
|
||||||
str[i] = a.String()
|
|
||||||
}
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user