2020-03-02 12:39:02 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/options"
|
2020-03-02 12:39:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/context"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newMultisigCommands() []cli.Command {
|
2020-06-17 21:15:13 +00:00
|
|
|
signFlags := []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
outFlag,
|
|
|
|
inFlag,
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "addr",
|
|
|
|
Usage: "Address to use",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
signFlags = append(signFlags, options.RPC...)
|
2020-03-02 12:39:02 +00:00
|
|
|
return []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "sign",
|
|
|
|
Usage: "sign a transaction",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "multisig sign --wallet <path> --addr <addr> --in <file.in> --out <file.out>",
|
2020-03-02 12:39:02 +00:00
|
|
|
Action: signMultisig,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: signFlags,
|
2020-03-02 12:39:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func signMultisig(ctx *cli.Context) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-03-02 12:39:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
|
|
|
c, err := readParameterContext(ctx.String("in"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
addr := ctx.String("addr")
|
|
|
|
sh, err := address.StringToUint160(addr)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1)
|
2020-03-02 12:39:02 +00:00
|
|
|
}
|
2020-08-07 09:34:54 +00:00
|
|
|
acc, err := getDecryptedAccount(wall, sh)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
2020-03-02 12:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tx, ok := c.Verifiable.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return cli.NewExitError("verifiable item is not a transaction", 1)
|
|
|
|
}
|
|
|
|
printTxInfo(tx)
|
|
|
|
|
|
|
|
priv := acc.PrivateKey()
|
|
|
|
sign := priv.Sign(tx.GetSignedPart())
|
|
|
|
if err := c.AddSignature(acc.Contract, priv.PublicKey(), sign); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't add signature: %w", err), 1)
|
2020-03-02 12:39:02 +00:00
|
|
|
} else if err := writeParameterContext(c, ctx.String("out")); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-06-17 21:15:13 +00:00
|
|
|
if len(ctx.String(options.RPCEndpointFlag)) != 0 {
|
2020-03-02 12:39:02 +00:00
|
|
|
w, err := c.GetWitness(acc.Contract)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
tx.Scripts = append(tx.Scripts, *w)
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
2020-03-02 12:39:02 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
2020-03-02 12:39:02 +00:00
|
|
|
if err != nil {
|
2020-06-17 21:15:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-21 07:31:45 +00:00
|
|
|
res, err := c.SendRawTransaction(tx)
|
|
|
|
if err != nil {
|
2020-03-02 12:39:02 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-07-21 07:31:45 +00:00
|
|
|
fmt.Println(res.StringLE())
|
|
|
|
return nil
|
2020-03-02 12:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(tx.Hash().StringLE())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readParameterContext(filename string) (*context.ParameterContext, error) {
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return nil, fmt.Errorf("can't read input file: %w", err)
|
2020-03-02 12:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c := new(context.ParameterContext)
|
|
|
|
if err := json.Unmarshal(data, c); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return nil, fmt.Errorf("can't parse transaction: %w", err)
|
2020-03-02 12:39:02 +00:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeParameterContext(c *context.ParameterContext, filename string) error {
|
|
|
|
if data, err := json.Marshal(c); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("can't marshal transaction: %w", err)
|
2020-03-02 12:39:02 +00:00
|
|
|
} else if err := ioutil.WriteFile(filename, data, 0644); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("can't write transaction to file: %w", err)
|
2020-03-02 12:39:02 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTxInfo(t *transaction.Transaction) {
|
|
|
|
fmt.Printf("Hash: %s\n", t.Hash().StringLE())
|
|
|
|
}
|