Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add command getinputs #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/storage"
"github.com/shopspring/decimal"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -758,6 +759,79 @@
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"),
Expand Down Expand Up @@ -1030,7 +1104,7 @@
return utxo, nil
}

func (raw signerInput) ReadDepositLock(deposit *common.DepositData) (crypto.Hash, error) {

Check warning on line 1107 in command.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'deposit' seems to be unused, consider removing or renaming it as _ (revive)
return crypto.Hash{}, nil
}

Expand Down
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading