lotus/cli/wallet_test.go

334 lines
9.5 KiB
Go
Raw Normal View History

2022-02-22 15:00:01 +00:00
//stm: #cli
2022-02-11 15:30:28 +00:00
package cli
import (
"context"
2022-02-22 14:35:46 +00:00
"encoding/hex"
"encoding/json"
"fmt"
2022-02-11 15:30:28 +00:00
"testing"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/api"
apitypes "github.com/filecoin-project/lotus/api/types"
types "github.com/filecoin-project/lotus/chain/types"
"github.com/golang/mock/gomock"
"github.com/ipfs/go-cid"
2022-02-22 14:35:46 +00:00
"github.com/multiformats/go-multihash"
2022-02-11 15:30:28 +00:00
"github.com/stretchr/testify/assert"
)
func TestWalletNew(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletNew))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
keyType := types.KeyType("secp256k1")
2022-02-22 14:35:46 +00:00
address, err := address.NewFromString("t0123")
assert.NoError(t, err)
2022-02-11 15:30:28 +00:00
mockApi.EXPECT().WalletNew(ctx, keyType).Return(address, nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_NEW_001
2022-02-22 14:35:46 +00:00
err = app.Run([]string{"wallet", "new"})
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), address.String())
2022-02-11 15:30:28 +00:00
}
func TestWalletList(t *testing.T) {
addr, err := address.NewIDAddress(1234)
addresses := []address.Address{addr}
assert.NoError(t, err)
cid := cid.Cid{}
key := types.NewTipSetKey(cid)
actor := types.Actor{
Code: cid,
Head: cid,
Nonce: 0,
Balance: big.NewInt(100),
}
t.Run("wallet-list-addr-only", func(t *testing.T) {
2022-03-01 01:21:22 +00:00
app, mockApi, buf, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletList))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gomock.InOrder(
mockApi.EXPECT().WalletList(ctx).Return(addresses, nil),
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(addr, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_LIST_001
2022-03-01 01:21:22 +00:00
err := app.Run([]string{"wallet", "list", "--addr-only"})
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-03-01 01:21:22 +00:00
assert.Contains(t, buf.String(), addr.String())
2022-02-11 15:30:28 +00:00
})
t.Run("wallet-list-id", func(t *testing.T) {
app, mockApi, _, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletList))
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gomock.InOrder(
mockApi.EXPECT().WalletList(ctx).Return(addresses, nil),
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(addr, nil),
mockApi.EXPECT().StateGetActor(ctx, addr, key).Return(&actor, nil),
mockApi.EXPECT().StateLookupID(ctx, addr, key).Return(addr, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_LIST_002
2022-02-11 15:30:28 +00:00
err := app.Run([]string{"wallet", "list", "--id"})
assert.NoError(t, err)
})
t.Run("wallet-list-market", func(t *testing.T) {
app, mockApi, _, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletList))
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
balance := api.MarketBalance{
Escrow: big.NewInt(1234),
Locked: big.NewInt(123),
}
gomock.InOrder(
mockApi.EXPECT().WalletList(ctx).Return(addresses, nil),
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(addr, nil),
mockApi.EXPECT().StateGetActor(ctx, addr, key).Return(&actor, nil),
mockApi.EXPECT().StateMarketBalance(ctx, addr, key).Return(balance, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_LIST_003
2022-02-11 15:30:28 +00:00
err := app.Run([]string{"wallet", "list", "--market"})
assert.NoError(t, err)
})
}
func TestWalletBalance(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletBalance))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
balance := big.NewInt(1234)
mockApi.EXPECT().WalletBalance(ctx, addr).Return(balance, nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_BALANCE_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "balance", "f01234"})
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), balance.String())
2022-02-11 15:30:28 +00:00
}
func TestWalletGetDefault(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletGetDefault))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2022-02-22 14:35:46 +00:00
addr, err := address.NewFromString("t0123")
assert.NoError(t, err)
2022-02-11 15:30:28 +00:00
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(addr, nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_GET_DEFAULT_001
2022-02-22 14:35:46 +00:00
err = app.Run([]string{"wallet", "default"})
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), addr.String())
2022-02-11 15:30:28 +00:00
}
func TestWalletSetDefault(t *testing.T) {
app, mockApi, _, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletSetDefault))
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
mockApi.EXPECT().WalletSetDefault(ctx, addr).Return(nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_SET_DEFAULT_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "set-default", "f01234"})
assert.NoError(t, err)
}
func TestWalletExport(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletExport))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
keyInfo := types.KeyInfo{
Type: types.KTSecp256k1,
PrivateKey: []byte("0x000000000000000000001"),
}
2022-02-11 15:30:28 +00:00
mockApi.EXPECT().WalletExport(ctx, addr).Return(&keyInfo, nil)
2022-02-22 14:35:46 +00:00
ki, err := json.Marshal(keyInfo)
assert.NoError(t, err)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_EXPORT_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "export", "f01234"})
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), hex.EncodeToString(ki))
2022-02-11 15:30:28 +00:00
}
func TestWalletSign(t *testing.T) {
2022-03-01 01:34:52 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletSign))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2022-02-22 14:35:46 +00:00
addr, err := address.NewFromString("f01234")
assert.NoError(t, err)
msg, err := hex.DecodeString("01")
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-03-01 01:34:52 +00:00
signature := crypto.Signature{
Type: crypto.SigTypeSecp256k1,
Data: []byte{0x01},
}
2022-02-11 15:30:28 +00:00
mockApi.EXPECT().WalletSign(ctx, addr, msg).Return(&signature, nil)
2022-03-01 01:34:52 +00:00
sigBytes := append([]byte{byte(signature.Type)}, signature.Data...)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_SIGN_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "sign", "f01234", "01"})
assert.NoError(t, err)
2022-03-01 01:34:52 +00:00
assert.Contains(t, buffer.String(), hex.EncodeToString(sigBytes))
2022-02-11 15:30:28 +00:00
}
func TestWalletVerify(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletVerify))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
msg := []byte{1}
signature := crypto.Signature{
Type: crypto.SigTypeSecp256k1,
Data: []byte{},
}
mockApi.EXPECT().WalletVerify(ctx, addr, msg, &signature).Return(true, nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_VERIFY_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "verify", "f01234", "01", "01"})
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), "valid")
2022-02-11 15:30:28 +00:00
}
func TestWalletDelete(t *testing.T) {
app, mockApi, _, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletDelete))
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
mockApi.EXPECT().WalletDelete(ctx, addr).Return(nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_DELETE_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "delete", "f01234"})
assert.NoError(t, err)
}
func TestWalletMarketWithdraw(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletMarket))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
balance := api.MarketBalance{
Escrow: big.NewInt(100),
Locked: big.NewInt(10),
}
2022-02-22 14:35:46 +00:00
h, err := hex.DecodeString("12209cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47")
assert.NoError(t, err)
cid := cid.NewCidV0(multihash.Multihash(h))
2022-02-11 15:30:28 +00:00
msgLookup := api.MsgLookup{}
var networkVers apitypes.NetworkVersion
gomock.InOrder(
mockApi.EXPECT().StateMarketBalance(ctx, addr, types.TipSetKey{}).Return(balance, nil),
// mock reserve to 10
mockApi.EXPECT().MarketGetReserved(ctx, addr).Return(big.NewInt(10), nil),
// available should be 80.. escrow - locked - reserve
mockApi.EXPECT().MarketWithdraw(ctx, addr, addr, big.NewInt(80)).Return(cid, nil),
mockApi.EXPECT().StateWaitMsg(ctx, cid, uint64(5), abi.ChainEpoch(int64(-1)), true).Return(&msgLookup, nil),
mockApi.EXPECT().StateNetworkVersion(ctx, types.TipSetKey{}).Return(networkVers, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_MARKET_WITHDRAW_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "market", "withdraw", "--wallet", addr.String()})
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), fmt.Sprintf("WithdrawBalance message cid: %s", cid))
2022-02-11 15:30:28 +00:00
}
func TestWalletMarketAdd(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletMarket))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
toAddr := address.Address{}
defaultAddr := address.Address{}
2022-02-22 14:35:46 +00:00
h, err := hex.DecodeString("12209cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47")
assert.NoError(t, err)
cid := cid.NewCidV0(multihash.Multihash(h))
2022-02-11 15:30:28 +00:00
gomock.InOrder(
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(defaultAddr, nil),
mockApi.EXPECT().MarketAddBalance(ctx, defaultAddr, toAddr, big.NewInt(80)).Return(cid, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_MARKET_ADD_001
2022-02-22 14:35:46 +00:00
err = app.Run([]string{"wallet", "market", "add", "0.000000000000000080", "--address", toAddr.String()})
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), fmt.Sprintf("AddBalance message cid: %s", cid))
2022-02-11 15:30:28 +00:00
}