OSDN Git Service

add query API
[bytom/bytom.git] / cmd / bytomcli / main.go
index 4c6e5f7..12ce0db 100644 (file)
@@ -1,10 +1,8 @@
-// Command corectl provides miscellaneous control functions for a Chain Core.
 package main
 
 import (
        "bytes"
        "context"
-//     "encoding/hex"
        "flag"
        "fmt"
        "io"
@@ -12,21 +10,21 @@ import (
        "net/http"
        "os"
        "path/filepath"
-       //"strconv"
        "strings"
        "time"
+       stdjson "encoding/json"
 
        "github.com/bytom/blockchain"
 //     "chain/core/accesstoken"
        //"github.com/bytom/config"
+       "github.com/bytom/encoding/json"
        "github.com/bytom/blockchain/rpc"
        "github.com/bytom/crypto/ed25519"
        "github.com/bytom/env"
        "github.com/bytom/errors"
-//     "github.com/bytom/generated/rev"
        "github.com/bytom/log"
        "github.com/bytom/crypto/ed25519/chainkd"
-       //"github.com/bytom/protocol/bc"
+       "github.com/bytom/cmd/bytomcli/example"
 )
 
 // config vars
@@ -55,10 +53,7 @@ type grantReq struct {
 }
 
 var commands = map[string]*command{
-       //"config-generator":     {configGenerator},
        "create-block-keypair": {createBlockKeyPair},
-       //"create-token":         {createToken},
-       //"config":               {configNongenerator},
        "reset":                {reset},
        "grant":                {grant},
        "revoke":               {revoke},
@@ -67,6 +62,20 @@ var commands = map[string]*command{
        "update-account-tags":  {updateAccountTags},
        "create-asset":         {createAsset},
        "update-asset-tags":    {updateAssetTags},
+       "build-transaction": {buildTransaction},
+       "create-control-program": {createControlProgram},
+       "create-account-receiver": {createAccountReceiver},
+       "create-transaction-feed": {createTxFeed},
+       "get-transaction-feed":    {getTxFeed},
+       "update-transaction-feed": {updateTxFeed},
+        "list-accounts":           {listAccounts},
+        "list-assets":             {listAssets},
+        "list-transaction-feeds":  {listTxFeeds},
+        "list-transactions":       {listTransactions},
+        "list-balances":           {listBalances},
+        "list-unspent-outputs":    {listUnspentOutputs},
+       "delete-transaction-feed": {deleteTxFeed},
+       "issue-test": {example.IssueTest},
 }
 
 func main() {
@@ -101,97 +110,6 @@ func main() {
        cmd.f(mustRPCClient(), os.Args[2:])
 }
 
-/*
-func configGenerator(client *rpc.Client, args []string) {
-       const usage = "usage: corectl config-generator [flags] [quorum] [pubkey url]..."
-       var (
-               quorum  uint32
-               signers []*config.BlockSigner
-               err     error
-       )
-
-       var flags flag.FlagSet
-       maxIssuanceWindow := flags.Duration("w", 24*time.Hour, "the maximum issuance window `duration` for this generator")
-       flagK := flags.String("k", "", "local `pubkey` for signing blocks")
-       flagHSMURL := flags.String("hsm-url", "", "hsm `url` for signing blocks (mockhsm if empty)")
-       flagHSMToken := flags.String("hsm-token", "", "hsm `access-token` for connecting to hsm")
-
-       flags.Usage = func() {
-               fmt.Println(usage)
-               flags.PrintDefaults()
-               os.Exit(1)
-       }
-       flags.Parse(args)
-       args = flags.Args()
-
-       // not a blocksigner
-       if *flagK == "" && *flagHSMURL != "" {
-               fatalln("error: flag -hsm-url has no effect without -k")
-       }
-
-       // TODO(ameets): update when switching to x.509 authorization
-       if (*flagHSMURL == "") != (*flagHSMToken == "") {
-               fatalln("error: flags -hsm-url and -hsm-token must be given together")
-       }
-
-       if len(args) == 0 {
-               if *flagK != "" {
-                       quorum = 1
-               }
-       } else if len(args)%2 != 1 {
-               fatalln(usage)
-       } else {
-               q64, err := strconv.ParseUint(args[0], 10, 32)
-               if err != nil {
-                       fatalln(usage)
-               }
-               quorum = uint32(q64)
-
-               for i := 1; i < len(args); i += 2 {
-                       pubkey, err := hex.DecodeString(args[i])
-                       if err != nil {
-                               fatalln(usage)
-                       }
-                       if len(pubkey) != ed25519.PublicKeySize {
-                               fatalln("error:", "bad ed25519 public key length")
-                       }
-                       url := args[i+1]
-                       signers = append(signers, &config.BlockSigner{
-                               Pubkey: pubkey,
-                               Url:    url,
-                       })
-               }
-       }
-
-       var blockPub []byte
-       if *flagK != "" {
-               blockPub, err = hex.DecodeString(*flagK)
-               if err != nil {
-                       fatalln("error: unable to decode block pub")
-               }
-       }
-
-       conf := &config.Config{
-               IsGenerator:         true,
-               Quorum:              quorum,
-               Signers:             signers,
-               MaxIssuanceWindowMs: bc.DurationMillis(*maxIssuanceWindow),
-               IsSigner:            *flagK != "",
-               BlockPub:            blockPub,
-               BlockHsmUrl:         *flagHSMURL,
-               BlockHsmAccessToken: *flagHSMToken,
-       }
-
-       err = client.Call(context.Background(), "/configure", conf, nil)
-       dieOnRPCError(err)
-
-       wait(client, nil)
-       var r map[string]interface{}
-       err = client.Call(context.Background(), "/info", nil, &r)
-       dieOnRPCError(err)
-       fmt.Println(r["blockchain_id"])
-}
-*/
 
 func createBlockKeyPair(client *rpc.Client, args []string) {
        if len(args) != 0 {
@@ -205,108 +123,6 @@ func createBlockKeyPair(client *rpc.Client, args []string) {
        fmt.Printf("%x\n", pub.Pub)
 }
 
-/*
-func createToken(client *rpc.Client, args []string) {
-       const usage = "usage: corectl create-token [-net] [name] [policy]"
-       var flags flag.FlagSet
-       flagNet := flags.Bool("net", false, "DEPRECATED. create a network token instead of client")
-       flags.Usage = func() {
-               fmt.Println(usage)
-               flags.PrintDefaults()
-               os.Exit(1)
-       }
-       flags.Parse(args)
-       args = flags.Args()
-       if len(args) == 2 && *flagNet || len(args) < 1 || len(args) > 2 {
-               fatalln(usage)
-       }
-
-       req := struct{ ID string }{args[0]}
-       var tok accesstoken.Token
-       // TODO(kr): find a way to make this atomic with the grant below
-       err := client.Call(context.Background(), "/create-access-token", req, &tok)
-       dieOnRPCError(err)
-       fmt.Println(tok.Token)
-
-       grant := grantReq{
-               GuardType: "access_token",
-               GuardData: map[string]string{"id": tok.ID},
-       }
-       switch {
-       case len(args) == 2:
-               grant.Policy = args[1]
-       case *flagNet:
-               grant.Policy = "crosscore"
-               fmt.Fprintln(os.Stderr, "warning: the network flag is deprecated")
-       default:
-               grant.Policy = "client-readwrite"
-               fmt.Fprintln(os.Stderr, "warning: implicit policy name is deprecated")
-       }
-       err = client.Call(context.Background(), "/create-authorization-grant", grant, nil)
-       dieOnRPCError(err, "Auth grant error:")
-}
-*/
-
-/*
-func configNongenerator(client *rpc.Client, args []string) {
-       const usage = "usage: corectl config [flags] [blockchain-id] [generator-url]"
-       var flags flag.FlagSet
-       flagT := flags.String("t", "", "generator access `token`")
-       flagK := flags.String("k", "", "local `pubkey` for signing blocks")
-       flagHSMURL := flags.String("hsm-url", "", "hsm `url` for signing blocks (mockhsm if empty)")
-       flagHSMToken := flags.String("hsm-token", "", "hsm `acc
-ess-token` for connecting to hsm")
-
-       flags.Usage = func() {
-               fmt.Println(usage)
-               flags.PrintDefaults()
-               os.Exit(1)
-       }
-       flags.Parse(args)
-       args = flags.Args()
-       if len(args) < 2 {
-               fatalln(usage)
-       }
-
-       // not a blocksigner
-       if *flagK == "" && *flagHSMURL != "" {
-               fatalln("error: flag -hsm-url has no effect without -k")
-       }
-
-       // TODO(ameets): update when switching to x.509 authorization
-       if (*flagHSMURL == "") != (*flagHSMToken == "") {
-               fatalln("error: flags -hsm-url and -hsm-token must be given together")
-       }
-
-       var blockchainID bc.Hash
-       err := blockchainID.UnmarshalText([]byte(args[0]))
-       if err != nil {
-               fatalln("error: invalid blockchain ID:", err)
-       }
-
-       var blockPub []byte
-       if *flagK != "" {
-               blockPub, err = hex.DecodeString(*flagK)
-               if err != nil {
-                       fatalln("error: unable to decode block pub")
-               }
-       }
-
-       var conf config.Config
-       conf.BlockchainId = &blockchainID
-       conf.GeneratorUrl = args[1]
-       conf.GeneratorAccessToken = *flagT
-       conf.IsSigner = *flagK != ""
-       conf.BlockPub = blockPub
-       conf.BlockHsmUrl = *flagHSMURL
-       conf.BlockHsmAccessToken = *flagHSMToken
-
-       client.BlockchainID = blockchainID.String()
-       err = client.Call(context.Background(), "/configure", conf, nil)
-       dieOnRPCError(err)
-}
-*/
-
 // reset will attempt a reset rpc call on a remote core. If the
 // core is not configured with reset capabilities an error is returned.
 func reset(client *rpc.Client, args []string) {
@@ -504,7 +320,7 @@ func createAccount(client *rpc.Client, args []string) {
        var ins Ins
        ins.RootXPubs = []chainkd.XPub{xpub}
        ins.Quorum = 1
-       ins.Alias = "aa"
+       ins.Alias = "alice"
        ins.Tags = map[string]interface{}{"test_tag": "v0",}
        ins.ClientToken = args[0]
        responses := make([]interface{}, 50)
@@ -535,9 +351,9 @@ func createAsset(client *rpc.Client, args []string) {
        var ins Ins
        ins.RootXPubs = []chainkd.XPub{xpub}
        ins.Quorum = 1
-       ins.Alias = "aa"
+       ins.Alias = "bob"
        ins.Tags = map[string]interface{}{"test_tag": "v0",}
-       ins.Definition = map[string]interface{}{"test_definition": "v0"}
+       ins.Definition = map[string]interface{}{}
        ins.ClientToken = args[0]
        responses := make([]interface{}, 50)
        client.Call(context.Background(), "/create-asset", &[]Ins{ins,}, &responses)
@@ -560,27 +376,275 @@ func updateAccountTags(client *rpc.Client,args []string){
        ins.ID = &aa
        ins.Alias = &alias
        ins.Tags = map[string]interface{}{"test_tag": "v0",}
-        responses := make([]interface{}, 50)
-        client.Call(context.Background(), "/update-account-tags", &[]Ins{ins,}, &responses)
-        fmt.Printf("responses:%v\n", responses)
+       responses := make([]interface{}, 50)
+       client.Call(context.Background(), "/update-account-tags", &[]Ins{ins,}, &responses)
+       fmt.Printf("responses:%v\n", responses)
 }
 
 func updateAssetTags(client *rpc.Client, args []string){
-        if len(args) != 0{
-                fatalln("error:updateAccountTags not use args")
-        }
+       if len(args) != 0{
+                       fatalln("error:updateAccountTags not use args")
+       }
        type Ins struct {
        ID    *string
        Alias *string
        Tags  map[string]interface{} `json:"tags"`
-}
+       }
        var ins Ins
        id := "123456"
        alias := "asdfg"
        ins.ID = &id
        ins.Alias = &alias
-        ins.Tags = map[string]interface{}{"test_tag": "v0",}
-        responses := make([]interface{}, 50)
-        client.Call(context.Background(), "/update-asset-tags", &[]Ins{ins,}, &responses)
+       ins.Tags = map[string]interface{}{"test_tag": "v0",}
+       responses := make([]interface{}, 50)
+       client.Call(context.Background(), "/update-asset-tags", &[]Ins{ins,}, &responses)
+       fmt.Printf("responses:%v\n", responses)
+}
+
+func buildTransaction(client *rpc.Client, args []string) {
+       if len(args) != 0 {
+               fatalln("error:updateAccountTags not use args")
+       }
+}
+
+func createControlProgram(client *rpc.Client, args []string){
+        if len(args) != 0{
+                fatalln("error:createControlProgram not use args")
+        }
+       type Ins struct {
+       Type   string
+       Params stdjson.RawMessage
+}
+       var ins Ins
+       //TODO:undefined arguments to ins
+       responses := make([]interface{},50)
+        client.Call(context.Background(),"/create-control-program", &[]Ins{ins,}, &responses)
+        fmt.Printf("responses:%v\n", responses)
+}
+
+func createAccountReceiver(client *rpc.Client, args []string){
+        if len(args) != 0{
+                fatalln("error:createAccountReceiver not use args")
+        }
+       type Ins struct {
+       AccountID    string    `json:"account_id"`
+       AccountAlias string    `json:"account_alias"`
+       ExpiresAt    time.Time `json:"expires_at"`
+}
+       var ins Ins
+       //TODO:undefined argument to ExpiresAt
+       ins.AccountID = "123456"
+       ins.AccountAlias = "zxcvbn"
+       responses := make([]interface{},50)
+        client.Call(context.Background(),"/create-Account-Receiver", &[]Ins{ins,}, &responses)
         fmt.Printf("responses:%v\n", responses)
 }
+
+func createTxFeed(client *rpc.Client, args []string){
+        if len(args) != 1{
+                fatalln("error:createTxFeed take no arguments")
+        }
+       type In struct {
+       Alias  string
+       Filter string
+       ClientToken string `json:"client_token"`
+}
+       var in In
+       in.Alias = "asdfgh"
+       in.Filter = "zxcvbn"
+       in.ClientToken = args[0]
+       client.Call(context.Background(),"/create-transaction-feed",&[]In{in,},nil)
+}
+
+func getTxFeed(client *rpc.Client, args []string){
+       if len(args) != 0{
+               fatalln("error:getTxFeed not use args")
+       }
+       type In struct {
+       ID    string `json:"id,omitempty"`
+       Alias string `json:"alias,omitempty"`
+}
+       var in In
+       in.Alias = "qwerty"
+       in.ID = "123456"
+       client.Call(context.Background(),"/get-transaction-feed",&[]In{in,},nil)
+}
+
+func updateTxFeed(client *rpc.Client, args []string){
+        if len(args) != 0{
+                fatalln("error:updateTxFeed not use args")
+        }
+        type In struct {
+       ID    string `json:"id,omitempty"`
+       Alias string `json:"alias,omitempty"`
+}
+       var in In
+       in.ID = "123456"
+       in.Alias = "qwerty"
+       client.Call(context.Background(),"/update-transaction-feed",&[]In{in,},nil)
+}
+
+func deleteTxFeed(client *rpc.Client, args []string){
+       if len(args) != 0{
+               fatalln("error:deleteTxFeed not use args")
+       }
+       type In struct {
+       ID    string `json:"id,omitempty"`
+       Alias string `json:"alias,omitempty"`
+}
+       var in In
+        in.ID = "123456"
+        in.Alias = "qwerty"
+        client.Call(context.Background(),"/delete-transaction-feed",&[]In{in,},nil)
+}
+
+func listAccounts(client *rpc.Client, args []string){
+        if len(args) != 0{
+                fatalln("error:listAccounts not use args")
+        }
+       type requestQuery struct {
+                Filter       string        `json:"filter,omitempty"`
+                FilterParams []interface{} `json:"filter_params,omitempty"`
+                SumBy        []string      `json:"sum_by,omitempty"`
+                PageSize     int           `json:"page_size"`
+                AscLongPoll bool          `json:"ascending_with_long_poll,omitempty"`
+                Timeout     json.Duration `json:"timeout"`
+                After string `json:"after"`
+                StartTimeMS uint64 `json:"start_time,omitempty"`
+                EndTimeMS   uint64 `json:"end_time,omitempty"`
+                TimestampMS uint64 `json:"timestamp,omitempty"`
+                Type string `json:"type"`
+                Aliases []string `json:"aliases,omitempty"`
+}
+       var in requestQuery
+       after := in.After
+       out := in
+       out.After = after
+        client.Call(context.Background(),"/list-accounts",&[]requestQuery{in,},nil)
+}
+
+func listAssets(client *rpc.Client, args []string){
+        if len(args) != 0{
+                fatalln("error:listAssets not use args")
+        }
+       type requestQuery struct {
+                Filter       string        `json:"filter,omitempty"`
+                FilterParams []interface{} `json:"filter_params,omitempty"`
+                SumBy        []string      `json:"sum_by,omitempty"`
+                PageSize     int           `json:"page_size"`
+                AscLongPoll bool          `json:"ascending_with_long_poll,omitempty"`
+                Timeout     json.Duration `json:"timeout"`
+                After string `json:"after"`
+                StartTimeMS uint64 `json:"start_time,omitempty"`
+                EndTimeMS   uint64 `json:"end_time,omitempty"`
+                TimestampMS uint64 `json:"timestamp,omitempty"`
+                Type string `json:"type"`
+                Aliases []string `json:"aliases,omitempty"`
+}
+       var in requestQuery
+       after := in.After
+       out := in
+       out.After = after
+       client.Call(context.Background(),"/list-assets",&[]requestQuery{in,},nil)
+}
+
+func listTxFeeds(client *rpc.Client, args []string){
+        if len(args) != 0{
+                fatalln("error:listTxFeeds not use args")
+        }
+       type requestQuery struct {
+                Filter       string        `json:"filter,omitempty"`
+                FilterParams []interface{} `json:"filter_params,omitempty"`
+                SumBy        []string      `json:"sum_by,omitempty"`
+                PageSize     int           `json:"page_size"`
+                AscLongPoll bool          `json:"ascending_with_long_poll,omitempty"`
+                Timeout     json.Duration `json:"timeout"`
+                After string `json:"after"`
+                StartTimeMS uint64 `json:"start_time,omitempty"`
+                EndTimeMS   uint64 `json:"end_time,omitempty"`
+                TimestampMS uint64 `json:"timestamp,omitempty"`
+                Type string `json:"type"`
+                Aliases []string `json:"aliases,omitempty"`
+}
+       var in requestQuery
+       after := in.After
+       out := in
+       out.After = after
+               client.Call(context.Background(),"/list-transactions-feeds",&[]requestQuery{in,},nil)
+}
+
+func listTransactions(client *rpc.Client, args []string){
+        if len(args) != 0{
+                fatalln("error:listTransactions not use args")
+        }
+       type requestQuery struct {
+                Filter       string        `json:"filter,omitempty"`
+                FilterParams []interface{} `json:"filter_params,omitempty"`
+                SumBy        []string      `json:"sum_by,omitempty"`
+                PageSize     int           `json:"page_size"`
+                AscLongPoll bool          `json:"ascending_with_long_poll,omitempty"`
+                Timeout     json.Duration `json:"timeout"`
+                After string `json:"after"`
+                StartTimeMS uint64 `json:"start_time,omitempty"`
+                EndTimeMS   uint64 `json:"end_time,omitempty"`
+                TimestampMS uint64 `json:"timestamp,omitempty"`
+                Type string `json:"type"`
+                Aliases []string `json:"aliases,omitempty"`
+}
+       var in requestQuery
+       after := in.After
+       out := in
+       out.After = after
+        client.Call(context.Background(),"/list-transactions",&[]requestQuery{in,},nil)
+}
+
+func listBalances(client *rpc.Client, args []string){
+        if len(args) != 0{
+                fatalln("error:listBalances not use args")
+        }
+type requestQuery struct {
+                Filter       string        `json:"filter,omitempty"`
+                FilterParams []interface{} `json:"filter_params,omitempty"`
+                SumBy        []string      `json:"sum_by,omitempty"`
+                PageSize     int           `json:"page_size"`
+                AscLongPoll bool          `json:"ascending_with_long_poll,omitempty"`
+                Timeout     json.Duration `json:"timeout"`
+                After string `json:"after"`
+                StartTimeMS uint64 `json:"start_time,omitempty"`
+                EndTimeMS   uint64 `json:"end_time,omitempty"`
+                TimestampMS uint64 `json:"timestamp,omitempty"`
+                Type string `json:"type"`
+                Aliases []string `json:"aliases,omitempty"`
+}
+
+       var in requestQuery
+       after := in.After
+       out := in
+       out.After = after
+        client.Call(context.Background(),"/list-balance",&[]requestQuery{in,},nil)
+}
+
+func listUnspentOutputs(client *rpc.Client, args []string){
+        if len(args) != 0{
+                fatalln("error:listUnspentOutputs not use args")
+        }
+type requestQuery struct {
+                Filter       string        `json:"filter,omitempty"`
+                FilterParams []interface{} `json:"filter_params,omitempty"`
+                SumBy        []string      `json:"sum_by,omitempty"`
+                PageSize     int           `json:"page_size"`
+                AscLongPoll bool          `json:"ascending_with_long_poll,omitempty"`
+                Timeout     json.Duration `json:"timeout"`
+                After string `json:"after"`
+                StartTimeMS uint64 `json:"start_time,omitempty"`
+                EndTimeMS   uint64 `json:"end_time,omitempty"`
+                TimestampMS uint64 `json:"timestamp,omitempty"`
+                Type string `json:"type"`
+                Aliases []string `json:"aliases,omitempty"`
+}
+       var in requestQuery
+       after := in.After
+       out := in
+       out.After = after
+        client.Call(context.Background(),"/list-unspent-outputs",&[]requestQuery{in,},nil)
+}