mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-02 09:45:50 +00:00
d9feec2be5
And DO NOT CHANGE TX in paramcontext InitAndSave, because it's really unobvious and must not be done this way.
54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
package paramcontext
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
|
"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/nspcc-dev/neo-go/pkg/wallet"
|
|
)
|
|
|
|
// InitAndSave creates an incompletely signed transaction which can be used
|
|
// as an input to `multisig sign`.
|
|
func InitAndSave(net netmode.Magic, tx *transaction.Transaction, acc *wallet.Account, filename string) error {
|
|
priv := acc.PrivateKey()
|
|
pub := priv.PublicKey()
|
|
sign := priv.SignHashable(uint32(net), tx)
|
|
scCtx := context.NewParameterContext("Neo.Network.P2P.Payloads.Transaction", net, tx)
|
|
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 {
|
|
return fmt.Errorf("can't add signature: %w", err)
|
|
}
|
|
return Save(scCtx, filename)
|
|
}
|
|
|
|
// Read reads the parameter context from the file.
|
|
func Read(filename string) (*context.ParameterContext, error) {
|
|
data, err := os.ReadFile(filename)
|
|
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
|
|
}
|
|
|
|
// Save writes the parameter context to the file.
|
|
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)
|
|
} else if err := os.WriteFile(filename, data, 0644); err != nil {
|
|
return fmt.Errorf("can't write transaction to file: %w", err)
|
|
}
|
|
return nil
|
|
}
|