-
Notifications
You must be signed in to change notification settings - Fork 462
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
implement Actions around wallet and address commands #1682
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,11 @@ type addressResult struct { | |
Address string | ||
} | ||
|
||
// AddressLsResult is the result of running the address list command. | ||
type AddressLsResult struct { | ||
Addresses []string | ||
} | ||
|
||
var addrsNewCmd = &cmds.Command{ | ||
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error { | ||
addr, err := GetAPI(env).Address().Addrs().New(req.Context) | ||
|
@@ -62,18 +67,23 @@ var addrsLsCmd = &cmds.Command{ | |
return err | ||
} | ||
|
||
var alr AddressLsResult | ||
for _, addr := range addrs { | ||
if err := re.Emit(&addressResult{addr.String()}); err != nil { | ||
return err | ||
} | ||
alr.Addresses = append(alr.Addresses, addr.String()) | ||
} | ||
return nil | ||
|
||
return re.Emit(&alr) | ||
}, | ||
Type: &addressResult{}, | ||
Type: &AddressLsResult{}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, addr *addressResult) error { | ||
_, err := fmt.Fprintln(w, addr.Address) | ||
return err | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, addrs *AddressLsResult) error { | ||
for _, addr := range addrs.Addresses { | ||
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 assume the way commands library works is that if you return an error it errors out even if you have written some output to w? 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. Basically. If for some reason we couldn't write to |
||
_, err := fmt.Fprintln(w, addr) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}), | ||
}, | ||
} | ||
|
@@ -137,14 +147,30 @@ var walletImportCmd = &cmds.Command{ | |
return err | ||
} | ||
|
||
for _, a := range addrs { | ||
if err := re.Emit(a); err != nil { | ||
return err | ||
} | ||
var alr AddressLsResult | ||
for _, addr := range addrs { | ||
alr.Addresses = append(alr.Addresses, addr.String()) | ||
} | ||
return nil | ||
|
||
return re.Emit(&alr) | ||
}, | ||
Type: &AddressLsResult{}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, addrs *AddressLsResult) error { | ||
for _, addr := range addrs.Addresses { | ||
_, err := fmt.Fprintln(w, addr) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}), | ||
}, | ||
Type: address.Address{}, | ||
} | ||
|
||
// WalletExportResult is the resut of running the wallet export command. | ||
type WalletExportResult struct { | ||
KeyInfo []*types.KeyInfo | ||
} | ||
|
||
var walletExportCmd = &cmds.Command{ | ||
|
@@ -165,12 +191,26 @@ var walletExportCmd = &cmds.Command{ | |
if err != nil { | ||
return err | ||
} | ||
for _, ki := range kis { | ||
if err := re.Emit(ki); err != nil { | ||
return err | ||
|
||
var klr WalletExportResult | ||
klr.KeyInfo = append(klr.KeyInfo, kis...) | ||
|
||
return re.Emit(klr) | ||
}, | ||
Type: &WalletExportResult{}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, klr *WalletExportResult) error { | ||
for _, k := range klr.KeyInfo { | ||
a, err := k.Address() | ||
if err != nil { | ||
return err | ||
} | ||
_, err = fmt.Fprintf(w, "Address:\t%s\nPrivateKey:\t%x\nCurve:\t\t%s\n\n", a.String(), k.PrivateKey, k.Curve) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
return nil | ||
}), | ||
}, | ||
Type: types.KeyInfo{}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fat | ||
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. mentioned before that you should move fat to tools/ at some point, much more appropriate there. test helpers is for free functions and the like 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 have heard cases for both sides (keeping it where it is and moving it to tools), will address after completion. Issue here so we don't forget: #1695 |
||
|
||
import ( | ||
"context" | ||
"github.com/filecoin-project/go-filecoin/address" | ||
"github.com/filecoin-project/go-filecoin/commands" | ||
|
||
"gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer" | ||
) | ||
|
||
// AddressNew runs the address new command against the filecoin process. | ||
func (f *Filecoin) AddressNew(ctx context.Context) (address.Address, error) { | ||
var newAddress address.Address | ||
if err := f.RunCmdJSONWithStdin(ctx, nil, &newAddress, "go-filecoin", "address", "new"); err != nil { | ||
return address.Address{}, err | ||
} | ||
return newAddress, nil | ||
} | ||
|
||
// AddressLs runs the address ls command against the filecoin process. | ||
func (f *Filecoin) AddressLs(ctx context.Context) ([]address.Address, error) { | ||
// the command returns an AddressListResult | ||
var alr commands.AddressLsResult | ||
// we expect to interact with an array of address | ||
var out []address.Address | ||
|
||
if err := f.RunCmdJSONWithStdin(ctx, nil, &alr, "go-filecoin", "address", "ls"); err != nil { | ||
return nil, err | ||
} | ||
|
||
// transform the AddressListResult to an array of addresses | ||
for _, addr := range alr.Addresses { | ||
a, err := address.NewFromString(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out = append(out, a) | ||
} | ||
return out, nil | ||
} | ||
|
||
// AddressLookup runs the address lookup command against the filecoin process. | ||
func (f *Filecoin) AddressLookup(ctx context.Context, addr address.Address) (peer.ID, error) { | ||
var ownerPeer peer.ID | ||
if err := f.RunCmdJSONWithStdin(ctx, nil, &ownerPeer, "go-filecoin", "address", "lookup", addr.String()); err != nil { | ||
return "", err | ||
} | ||
return ownerPeer, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package fat | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files" | ||
|
||
"github.com/filecoin-project/go-filecoin/address" | ||
"github.com/filecoin-project/go-filecoin/commands" | ||
"github.com/filecoin-project/go-filecoin/types" | ||
) | ||
|
||
// WalletBalance run the wallet balance command against the filecoin process. | ||
func (f *Filecoin) WalletBalance(ctx context.Context, addr address.Address) (*types.AttoFIL, error) { | ||
var balance *types.AttoFIL | ||
if err := f.RunCmdJSONWithStdin(ctx, nil, &balance, "go-filecoin", "wallet", "balance", addr.String()); err != nil { | ||
return nil, err | ||
} | ||
return balance, nil | ||
} | ||
|
||
// WalletImport run the wallet import command against the filecoin process. | ||
func (f *Filecoin) WalletImport(ctx context.Context, file files.File) ([]address.Address, error) { | ||
// the command returns an AddressListResult | ||
var alr commands.AddressLsResult | ||
// we expect to interact with an array of address | ||
var out []address.Address | ||
|
||
if err := f.RunCmdJSONWithStdin(ctx, file, &alr, "go-filecoin", "wallet", "import"); err != nil { | ||
return nil, err | ||
} | ||
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. See comment on other PR #1687 (comment) |
||
|
||
// transform the AddressListResult to an array of addresses | ||
for _, addr := range alr.Addresses { | ||
a, err := address.NewFromString(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out = append(out, a) | ||
} | ||
return out, nil | ||
} | ||
|
||
// WalletExport run the wallet export command against the filecoin process. | ||
func (f *Filecoin) WalletExport(ctx context.Context, addrs []address.Address) ([]*types.KeyInfo, error) { | ||
// the command returns an KeyInfoListResult | ||
var klr commands.WalletExportResult | ||
// we expect to interact with an array of KeyInfo(s) | ||
var out []*types.KeyInfo | ||
var sAddrs []string | ||
for _, a := range addrs { | ||
sAddrs = append(sAddrs, a.String()) | ||
} | ||
|
||
if err := f.RunCmdJSONWithStdin(ctx, nil, &klr, "go-filecoin", "wallet", "export", strings.Join(sAddrs, " ")); err != nil { | ||
return nil, err | ||
} | ||
|
||
// transform the KeyInfoListResult to an array of KeyInfo(s) | ||
return append(out, klr.KeyInfo...), nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making an assumption here (and elsewhere in this file) that a node will never have more addresses than we can hold in memory; it makes the test methods nicer.