diff --git a/commands/address.go b/commands/address.go index f8bd3b8866..b0346bcaf6 100644 --- a/commands/address.go +++ b/commands/address.go @@ -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 { + _, 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{}, } diff --git a/functional-tests/faucet_test.go b/functional-tests/faucet_test.go index e9af8f3c96..aeb5707aa6 100644 --- a/functional-tests/faucet_test.go +++ b/functional-tests/faucet_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/filecoin-project/go-filecoin/commands" th "github.com/filecoin-project/go-filecoin/testhelpers" iptbtester "github.com/filecoin-project/go-filecoin/testhelpers/iptbtester" ) @@ -27,10 +28,6 @@ func init() { } } -type addressResult struct { - Address string -} - func TestFaucetSendFunds(t *testing.T) { assert := assert.New(t) require := require.New(t) @@ -79,13 +76,13 @@ func TestFaucetSendFunds(t *testing.T) { defer faucetcancel() // Get address for target node - var targetAddr addressResult + var targetAddr commands.AddressLsResult node1.MustRunCmdJSON(ctx, &targetAddr, "go-filecoin", "wallet", "addrs", "ls") // Start Tests // Make request for funds - msgcid := MustSendFundsFaucet(t, "localhost:9797", targetAddr.Address) + msgcid := MustSendFundsFaucet(t, "localhost:9797", targetAddr.Addresses[0]) // Wait around for message to appear msgctx, msgcancel := context.WithTimeout(context.Background(), blockTime*3) @@ -94,7 +91,7 @@ func TestFaucetSendFunds(t *testing.T) { // Read wallet balance var balanceStr string - node1.MustRunCmdJSON(ctx, &balanceStr, "go-filecoin", "wallet", "balance", targetAddr.Address) + node1.MustRunCmdJSON(ctx, &balanceStr, "go-filecoin", "wallet", "balance", targetAddr.Addresses[0]) balance, err := strconv.ParseInt(balanceStr, 10, 64) require.NoError(err) diff --git a/functional-tests/lib/helpers.bash b/functional-tests/lib/helpers.bash index a82ce1d39f..5e40c34f70 100755 --- a/functional-tests/lib/helpers.bash +++ b/functional-tests/lib/helpers.bash @@ -42,8 +42,7 @@ function free_port { function import_private_key { ./go-filecoin wallet import ./fixtures/"$1".key \ - --repodir="$2" \ - | jq -r "" + --repodir="$2" } function init_local_daemon { diff --git a/testhelpers/fat/action_address.go b/testhelpers/fat/action_address.go new file mode 100644 index 0000000000..999f86275c --- /dev/null +++ b/testhelpers/fat/action_address.go @@ -0,0 +1,49 @@ +package fat + +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 +} diff --git a/testhelpers/fat/action_wallet.go b/testhelpers/fat/action_wallet.go new file mode 100644 index 0000000000..d0556c0ca5 --- /dev/null +++ b/testhelpers/fat/action_wallet.go @@ -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 + } + + // 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 +}