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"
|
2021-03-04 11:14:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-10-02 13:13:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/context"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
)
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// validUntilBlockIncrement is the number of extra blocks to add to an exported transaction.
|
2020-10-02 13:13:17 +00:00
|
|
|
const validUntilBlockIncrement = 50
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// InitAndSave creates an incompletely signed transaction which can be used
|
|
|
|
// as an input to `multisig sign`.
|
2021-03-25 16:18:01 +00:00
|
|
|
func InitAndSave(net netmode.Magic, tx *transaction.Transaction, acc *wallet.Account, filename string) error {
|
2020-10-02 13:13:17 +00:00
|
|
|
// avoid fast transaction expiration
|
|
|
|
tx.ValidUntilBlock += validUntilBlockIncrement
|
|
|
|
priv := acc.PrivateKey()
|
|
|
|
pub := priv.PublicKey()
|
2021-03-25 16:18:01 +00:00
|
|
|
sign := priv.SignHashable(uint32(net), tx)
|
2021-07-23 08:33:51 +00:00
|
|
|
scCtx := context.NewParameterContext("Neo.Network.P2P.Payloads.Transaction", net, tx)
|
2021-03-04 11:14:25 +00:00
|
|
|
h, err := address.StringToUint160(acc.Address)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid address: %s", acc.Address)
|
|
|
|
}
|
|
|
|
if err := scCtx.AddSignature(h, acc.Contract, pub, sign); err != nil {
|
2020-10-02 13:13:17 +00:00
|
|
|
return fmt.Errorf("can't add signature: %w", err)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|