2020-10-02 13:13:17 +00:00
|
|
|
package paramcontext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-02-22 16:27:32 +00:00
|
|
|
"os"
|
2020-10-02 13:13:17 +00:00
|
|
|
|
2021-03-25 16:18:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-10-02 13:13:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/context"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// InitAndSave creates an incompletely signed transaction which can be used
|
2022-08-31 19:38:35 +00:00
|
|
|
// as an input to `multisig sign`. If a wallet.Account is given and can sign,
|
|
|
|
// it's signed as well using it.
|
2021-03-25 16:18:01 +00:00
|
|
|
func InitAndSave(net netmode.Magic, tx *transaction.Transaction, acc *wallet.Account, filename string) error {
|
2021-07-23 08:33:51 +00:00
|
|
|
scCtx := context.NewParameterContext("Neo.Network.P2P.Payloads.Transaction", net, tx)
|
2022-08-31 19:38:35 +00:00
|
|
|
if acc != nil && acc.CanSign() {
|
2022-09-01 17:42:42 +00:00
|
|
|
sign := acc.SignHashable(net, tx)
|
|
|
|
if err := scCtx.AddSignature(acc.ScriptHash(), acc.Contract, acc.PublicKey(), sign); err != nil {
|
2022-08-31 19:38:35 +00:00
|
|
|
return fmt.Errorf("can't add signature: %w", err)
|
|
|
|
}
|
2020-10-02 13:13:17 +00:00
|
|
|
}
|
|
|
|
return Save(scCtx, filename)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Read reads the parameter context from the file.
|
2020-10-02 13:13:17 +00:00
|
|
|
func Read(filename string) (*context.ParameterContext, error) {
|
2022-02-22 16:27:32 +00:00
|
|
|
data, err := os.ReadFile(filename)
|
2020-10-02 13:13:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read input file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := new(context.ParameterContext)
|
|
|
|
if err := json.Unmarshal(data, c); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse transaction: %w", err)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Save writes the parameter context to the file.
|
2020-10-02 13:13:17 +00:00
|
|
|
func Save(c *context.ParameterContext, filename string) error {
|
|
|
|
if data, err := json.Marshal(c); err != nil {
|
|
|
|
return fmt.Errorf("can't marshal transaction: %w", err)
|
2022-02-22 16:27:32 +00:00
|
|
|
} else if err := os.WriteFile(filename, data, 0644); err != nil {
|
2020-10-02 13:13:17 +00:00
|
|
|
return fmt.Errorf("can't write transaction to file: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|