From 516fdd397ad408eeaf5dbfe12ac29caefd243f1b Mon Sep 17 00:00:00 2001 From: panlilu Date: Thu, 7 Mar 2024 21:20:03 +0800 Subject: [PATCH] add addon commands --- command.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 22 ++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/command.go b/command.go index ad1414b3f..93c418c79 100644 --- a/command.go +++ b/command.go @@ -17,6 +17,7 @@ import ( "github.com/MixinNetwork/mixin/config" "github.com/MixinNetwork/mixin/crypto" "github.com/MixinNetwork/mixin/storage" + "github.com/shopspring/decimal" "github.com/urfave/cli/v2" ) @@ -758,6 +759,79 @@ func getUTXOCmd(c *cli.Context) error { return err } +func initKey(s string) crypto.Key { + var key [32]byte + decodedKey, _ := hex.DecodeString(s) + copy(key[:], decodedKey) + return key +} + +func getInputsCmd(c *cli.Context) error { + + type InputForJson struct { + Hash string `json:"hash"` + Index uint `json:"index"` + } + + addrs_str := c.String("addrs") + txs_str := c.String("txids") + + addrs := strings.Split(addrs_str, ",") + txs := strings.Split(txs_str, ",") + + for _, addr := range addrs { + sum := decimal.NewFromFloat(0) + inputs_arr := []InputForJson{} + + address, _ := common.NewAddressFromString(addr) + pubspendkey := address.PublicSpendKey.String() + priviewkey := address.PublicSpendKey.DeterministicHashDerive() + + fmt.Println(address) + + for _, txhash := range txs { + var tx common.Transaction + for retryCount := 0; retryCount < 5; retryCount++ { + data, err := callRPC(c.String("node"), "gettransaction", []interface{}{txhash}, false) + if err == nil { + err = json.Unmarshal(data, &tx) + if err == nil { + break + } + } + time.Sleep(1 * time.Second) + } + + total := decimal.NewFromFloat(0) + result := make([][]interface{}, 0) + for i, o := range tx.Outputs { + key := initKey(o.Keys[0].String()) + mask := initKey(o.Mask.String()) + amount := o.Amount.String() + if crypto.ViewGhostOutputKey(&key, &priviewkey, &mask, uint64(i)).String() == pubspendkey { + fmt.Println(txhash + "," + strconv.Itoa(i) + "," + amount) + result = append(result, []interface{}{txhash, i, amount}) + inputs_arr = append(inputs_arr, InputForJson{Hash: txhash, Index: uint(i)}) + amountDecimal, _ := decimal.NewFromString(amount) + total = total.Add(amountDecimal) + } + } + + sum = sum.Add(total) + + } + inputs_json, err := json.Marshal(inputs_arr) + if err != nil { + return err + } + fmt.Printf("inputs:\n%s\n", string(inputs_json)) + fmt.Println("sum:\n", sum) + fmt.Println("-----------------------------") + } + + return nil +} + func getKeyCmd(c *cli.Context) error { data, err := callRPC(c.String("node"), "getkey", []any{ c.String("key"), diff --git a/main.go b/main.go index af76f908d..3bc9af09f 100644 --- a/main.go +++ b/main.go @@ -643,6 +643,28 @@ func main() { Usage: "Dump the graph head", Action: dumpGraphHeadCmd, }, + { + Name: "getinputs", + Usage: "Get valid inputs by addresses and input transactions", + Action: getInputsCmd, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "asset", + Value: "a99c2e0e2b1da4d648755ef19bd95139acbbe6564cfb06dec7cd34931ca72cdc", + Usage: "Target asset, default is Xin", + }, + &cli.StringFlag{ + Name: "txids", + Value: "", + Usage: "input hash of transactions divided by comma", + }, + &cli.StringFlag{ + Name: "addrs", + Value: "", + Usage: "input addresses divided by comma", + }, + }, + }, } err := app.Run(os.Args) if err != nil {