2019-08-13 04:27:54 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-18 04:47:41 +00:00
|
|
|
types "github.com/filecoin-project/lotus/chain/types"
|
2019-08-13 04:27:54 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var paychCmd = &cli.Command{
|
|
|
|
Name: "paych",
|
|
|
|
Usage: "Manage payment channels",
|
|
|
|
Subcommands: []*cli.Command{
|
2019-09-16 13:46:05 +00:00
|
|
|
paychGetCmd,
|
2019-08-13 04:27:54 +00:00
|
|
|
paychListCmd,
|
|
|
|
paychVoucherCmd,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-09-16 13:46:05 +00:00
|
|
|
var paychGetCmd = &cli.Command{
|
|
|
|
Name: "get",
|
|
|
|
Usage: "Create a new payment channel or get existing one",
|
2019-08-13 04:27:54 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() != 3 {
|
2019-09-16 13:46:05 +00:00
|
|
|
return fmt.Errorf("must pass three arguments: <from> <to> <available funds>")
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
from, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse from address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
to, err := address.NewFromString(cctx.Args().Get(1))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse to address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
amt, err := types.BigFromString(cctx.Args().Get(2))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing amount failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2019-09-16 13:46:05 +00:00
|
|
|
info, err := api.PaychGet(ctx, from, to, amt)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-13 14:11:40 +00:00
|
|
|
fmt.Println(info.Channel.String())
|
2019-08-13 04:27:54 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychListCmd = &cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "List all locally registered payment channels",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
chs, err := api.PaychList(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range chs {
|
|
|
|
fmt.Println(v.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherCmd = &cli.Command{
|
|
|
|
Name: "voucher",
|
|
|
|
Usage: "Interact with payment channel vouchers",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
paychVoucherCreateCmd,
|
|
|
|
paychVoucherCheckCmd,
|
|
|
|
paychVoucherAddCmd,
|
|
|
|
paychVoucherListCmd,
|
|
|
|
paychVoucherBestSpendableCmd,
|
|
|
|
paychVoucherSubmitCmd,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherCreateCmd = &cli.Command{
|
|
|
|
Name: "create",
|
|
|
|
Usage: "Create a signed payment channel voucher",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "lane",
|
|
|
|
Value: 0,
|
|
|
|
Usage: "specify payment channel lane to use",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() != 2 {
|
|
|
|
return fmt.Errorf("must pass two arguments: <channel> <amount>")
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
amt, err := types.BigFromString(cctx.Args().Get(1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lane := cctx.Int("lane")
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
sv, err := api.PaychVoucherCreate(ctx, ch, amt, uint64(lane))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := sv.EncodedString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(enc)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherCheckCmd = &cli.Command{
|
|
|
|
Name: "check",
|
|
|
|
Usage: "Check validity of payment channel voucher",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() != 2 {
|
|
|
|
return fmt.Errorf("must pass payment channel address and voucher to validate")
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sv, err := types.DecodeSignedVoucher(cctx.Args().Get(1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
if err := api.PaychVoucherCheckValid(ctx, ch, sv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("voucher is valid")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherAddCmd = &cli.Command{
|
|
|
|
Name: "add",
|
|
|
|
Usage: "Add payment channel voucher to local datastore",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() != 2 {
|
|
|
|
return fmt.Errorf("must pass payment channel address and voucher")
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sv, err := types.DecodeSignedVoucher(cctx.Args().Get(1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2019-09-09 13:59:41 +00:00
|
|
|
// TODO: allow passing proof bytes
|
2019-09-16 21:25:23 +00:00
|
|
|
if _, err := api.PaychVoucherAdd(ctx, ch, sv, nil, types.NewInt(0)); err != nil {
|
2019-08-13 04:27:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherListCmd = &cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "List stored vouchers for a given payment channel",
|
2019-09-09 13:59:41 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2019-09-09 16:02:57 +00:00
|
|
|
Name: "export",
|
|
|
|
Usage: "Print export strings",
|
2019-09-09 13:59:41 +00:00
|
|
|
},
|
|
|
|
},
|
2019-08-13 04:27:54 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() != 1 {
|
|
|
|
return fmt.Errorf("must pass payment channel address")
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
vouchers, err := api.PaychVoucherList(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range vouchers {
|
2019-09-09 13:59:41 +00:00
|
|
|
if cctx.Bool("export") {
|
|
|
|
enc, err := v.EncodedString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Lane %d, Nonce %d: %s; %s\n", v.Lane, v.Nonce, v.Amount.String(), enc)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Lane %d, Nonce %d: %s\n", v.Lane, v.Nonce, v.Amount.String())
|
|
|
|
}
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherBestSpendableCmd = &cli.Command{
|
|
|
|
Name: "best-spendable",
|
|
|
|
Usage: "Print voucher with highest value that is currently spendable",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() != 1 {
|
|
|
|
return fmt.Errorf("must pass payment channel address")
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
vouchers, err := api.PaychVoucherList(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var best *types.SignedVoucher
|
|
|
|
for _, v := range vouchers {
|
|
|
|
spendable, err := api.PaychVoucherCheckSpendable(ctx, ch, v, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if spendable {
|
2019-09-23 17:11:44 +00:00
|
|
|
if best == nil || v.Amount.GreaterThan(best.Amount) {
|
2019-08-13 04:27:54 +00:00
|
|
|
best = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if best == nil {
|
|
|
|
return fmt.Errorf("No spendable vouchers for that channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := best.EncodedString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(enc)
|
|
|
|
fmt.Printf("Amount: %s\n", best.Amount)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherSubmitCmd = &cli.Command{
|
|
|
|
Name: "submit",
|
|
|
|
Usage: "Submit voucher to chain to update payment channel state",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() != 2 {
|
|
|
|
return fmt.Errorf("must pass payment channel address and voucher")
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sv, err := types.DecodeSignedVoucher(cctx.Args().Get(1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
mcid, err := api.PaychVoucherSubmit(ctx, ch, sv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-08 05:51:34 +00:00
|
|
|
mwait, err := api.StateWaitMsg(ctx, mcid)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if mwait.Receipt.ExitCode != 0 {
|
|
|
|
return fmt.Errorf("message execution failed (exit code %d)", mwait.Receipt.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("channel updated succesfully")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|