2020-03-04 10:18:49 +00:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2020-03-04 11:59:58 +00:00
|
|
|
"bytes"
|
2020-03-04 10:18:49 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-03-04 11:59:58 +00:00
|
|
|
"errors"
|
2020-03-04 10:18:49 +00:00
|
|
|
"fmt"
|
2020-03-04 11:59:58 +00:00
|
|
|
"sort"
|
2020-03-04 10:18:49 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-03-12 09:59:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-04 10:18:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-06-18 08:39:47 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto"
|
2020-03-04 11:59:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-03-04 10:18:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-03-04 11:59:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-03-04 10:18:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-03-04 11:59:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-03-04 13:30:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-03-04 11:59:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-03-04 10:18:49 +00:00
|
|
|
)
|
|
|
|
|
2022-09-08 10:07:49 +00:00
|
|
|
// TransactionType is the ParameterContext Type used for transactions.
|
|
|
|
const TransactionType = "Neo.Network.P2P.Payloads.Transaction"
|
|
|
|
|
|
|
|
// compatTransactionType is the old, 2.x type used for transactions.
|
|
|
|
const compatTransactionType = "Neo.Core.ContractTransaction"
|
|
|
|
|
2020-03-04 10:18:49 +00:00
|
|
|
// ParameterContext represents smartcontract parameter's context.
|
|
|
|
type ParameterContext struct {
|
|
|
|
// Type is a type of a verifiable item.
|
|
|
|
Type string
|
2021-03-12 09:59:14 +00:00
|
|
|
// Network is a network this context belongs to.
|
|
|
|
Network netmode.Magic
|
2020-03-04 10:18:49 +00:00
|
|
|
// Verifiable is an object which can be (de-)serialized.
|
2020-06-18 08:39:47 +00:00
|
|
|
Verifiable crypto.VerifiableDecodable
|
2020-03-04 10:18:49 +00:00
|
|
|
// Items is a map from script hashes to context items.
|
|
|
|
Items map[util.Uint160]*Item
|
|
|
|
}
|
|
|
|
|
|
|
|
type paramContext struct {
|
|
|
|
Type string `json:"type"`
|
2021-03-12 09:59:14 +00:00
|
|
|
Net uint32 `json:"network"`
|
2022-02-22 15:47:47 +00:00
|
|
|
Hash util.Uint256 `json:"hash,omitempty"`
|
2021-03-12 10:10:24 +00:00
|
|
|
Data []byte `json:"data"`
|
2020-03-04 10:18:49 +00:00
|
|
|
Items map[string]json.RawMessage `json:"items"`
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:58 +00:00
|
|
|
type sigWithIndex struct {
|
|
|
|
index int
|
|
|
|
sig []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewParameterContext returns ParameterContext with the specified type and item to sign.
|
2021-03-12 09:59:14 +00:00
|
|
|
func NewParameterContext(typ string, network netmode.Magic, verif crypto.VerifiableDecodable) *ParameterContext {
|
2020-03-04 11:59:58 +00:00
|
|
|
return &ParameterContext{
|
|
|
|
Type: typ,
|
2021-03-12 09:59:14 +00:00
|
|
|
Network: network,
|
2020-03-04 11:59:58 +00:00
|
|
|
Verifiable: verif,
|
|
|
|
Items: make(map[util.Uint160]*Item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 19:38:35 +00:00
|
|
|
func (c *ParameterContext) GetCompleteTransaction() (*transaction.Transaction, error) {
|
|
|
|
tx, ok := c.Verifiable.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("verifiable item is not a transaction")
|
|
|
|
}
|
|
|
|
if len(tx.Scripts) > 0 {
|
|
|
|
tx.Scripts = tx.Scripts[:0]
|
|
|
|
}
|
|
|
|
for i := range tx.Signers {
|
|
|
|
w, err := c.GetWitness(tx.Signers[i].Account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't create witness for signer #%d: %w", i, err)
|
|
|
|
}
|
|
|
|
tx.Scripts = append(tx.Scripts, *w)
|
|
|
|
}
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
2020-03-04 13:30:07 +00:00
|
|
|
// GetWitness returns invocation and verification scripts for the specified contract.
|
2021-03-03 12:56:08 +00:00
|
|
|
func (c *ParameterContext) GetWitness(h util.Uint160) (*transaction.Witness, error) {
|
|
|
|
item, ok := c.Items[h]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("witness not found")
|
|
|
|
}
|
2020-03-04 13:30:07 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
for i := range item.Parameters {
|
|
|
|
if item.Parameters[i].Type != smartcontract.SignatureType {
|
2022-08-31 10:02:54 +00:00
|
|
|
return nil, fmt.Errorf("unsupported %s parameter #%d", item.Parameters[i].Type.String(), i)
|
2020-03-04 13:30:07 +00:00
|
|
|
} else if item.Parameters[i].Value == nil {
|
2022-08-31 10:02:54 +00:00
|
|
|
return nil, fmt.Errorf("no value for parameter #%d (not signed yet?)", i)
|
2020-03-04 13:30:07 +00:00
|
|
|
}
|
|
|
|
emit.Bytes(bw.BinWriter, item.Parameters[i].Value.([]byte))
|
|
|
|
}
|
|
|
|
return &transaction.Witness{
|
|
|
|
InvocationScript: bw.Bytes(),
|
2021-03-03 12:56:08 +00:00
|
|
|
VerificationScript: item.Script,
|
2020-03-04 13:30:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:58 +00:00
|
|
|
// AddSignature adds a signature for the specified contract and public key.
|
2021-03-04 11:14:25 +00:00
|
|
|
func (c *ParameterContext) AddSignature(h util.Uint160, ctr *wallet.Contract, pub *keys.PublicKey, sig []byte) error {
|
|
|
|
item := c.getItemForContract(h, ctr)
|
2020-05-08 17:54:24 +00:00
|
|
|
if _, pubs, ok := vm.ParseMultiSigContract(ctr.Script); ok {
|
2020-03-04 11:59:58 +00:00
|
|
|
if item.GetSignature(pub) != nil {
|
|
|
|
return errors.New("signature is already added")
|
|
|
|
}
|
|
|
|
pubBytes := pub.Bytes()
|
|
|
|
var contained bool
|
|
|
|
for i := range pubs {
|
|
|
|
if bytes.Equal(pubBytes, pubs[i]) {
|
|
|
|
contained = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !contained {
|
|
|
|
return errors.New("public key is not present in script")
|
|
|
|
}
|
|
|
|
item.AddSignature(pub, sig)
|
2022-08-31 07:53:49 +00:00
|
|
|
if len(item.Signatures) >= len(ctr.Parameters) {
|
2020-03-04 11:59:58 +00:00
|
|
|
indexMap := map[string]int{}
|
|
|
|
for i := range pubs {
|
|
|
|
indexMap[hex.EncodeToString(pubs[i])] = i
|
|
|
|
}
|
2022-08-31 07:53:49 +00:00
|
|
|
sigs := make([]sigWithIndex, len(item.Parameters))
|
|
|
|
var i int
|
2020-03-04 11:59:58 +00:00
|
|
|
for pub, sig := range item.Signatures {
|
2022-08-31 07:53:49 +00:00
|
|
|
sigs[i] = sigWithIndex{index: indexMap[pub], sig: sig}
|
|
|
|
i++
|
|
|
|
if i == len(sigs) {
|
|
|
|
break
|
|
|
|
}
|
2020-03-04 11:59:58 +00:00
|
|
|
}
|
|
|
|
sort.Slice(sigs, func(i, j int) bool {
|
|
|
|
return sigs[i].index < sigs[j].index
|
|
|
|
})
|
|
|
|
for i := range sigs {
|
|
|
|
item.Parameters[i] = smartcontract.Parameter{
|
|
|
|
Type: smartcontract.SignatureType,
|
|
|
|
Value: sigs[i].sig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
index := -1
|
|
|
|
for i := range ctr.Parameters {
|
|
|
|
if ctr.Parameters[i].Type == smartcontract.SignatureType {
|
|
|
|
if index >= 0 {
|
|
|
|
return errors.New("multiple signature parameters in non-multisig contract")
|
|
|
|
}
|
|
|
|
index = i
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 11:14:25 +00:00
|
|
|
if index != -1 {
|
|
|
|
item.Parameters[index].Value = sig
|
|
|
|
} else if !ctr.Deployed {
|
2020-03-04 11:59:58 +00:00
|
|
|
return errors.New("missing signature parameter")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-03 12:56:08 +00:00
|
|
|
func (c *ParameterContext) getItemForContract(h util.Uint160, ctr *wallet.Contract) *Item {
|
|
|
|
item, ok := c.Items[ctr.ScriptHash()]
|
|
|
|
if ok {
|
2020-03-04 11:59:58 +00:00
|
|
|
return item
|
|
|
|
}
|
|
|
|
params := make([]smartcontract.Parameter, len(ctr.Parameters))
|
|
|
|
for i := range params {
|
|
|
|
params[i].Type = ctr.Parameters[i].Type
|
|
|
|
}
|
2021-03-04 11:14:25 +00:00
|
|
|
script := ctr.Script
|
|
|
|
if ctr.Deployed {
|
|
|
|
script = nil
|
|
|
|
}
|
2021-03-03 12:56:08 +00:00
|
|
|
item = &Item{
|
2021-03-04 11:14:25 +00:00
|
|
|
Script: script,
|
2020-03-04 11:59:58 +00:00
|
|
|
Parameters: params,
|
|
|
|
Signatures: make(map[string][]byte),
|
|
|
|
}
|
|
|
|
c.Items[h] = item
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-03-04 10:18:49 +00:00
|
|
|
func (c ParameterContext) MarshalJSON() ([]byte, error) {
|
2021-03-17 13:40:24 +00:00
|
|
|
verif, err := c.Verifiable.EncodeHashableFields()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to encode hashable fields")
|
|
|
|
}
|
2020-03-04 10:18:49 +00:00
|
|
|
items := make(map[string]json.RawMessage, len(c.Items))
|
|
|
|
for u := range c.Items {
|
|
|
|
data, err := json.Marshal(c.Items[u])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-23 09:41:09 +00:00
|
|
|
items["0x"+u.StringLE()] = data
|
2020-03-04 10:18:49 +00:00
|
|
|
}
|
|
|
|
pc := ¶mContext{
|
|
|
|
Type: c.Type,
|
2021-03-12 09:59:14 +00:00
|
|
|
Net: uint32(c.Network),
|
2022-02-22 15:47:47 +00:00
|
|
|
Hash: c.Verifiable.Hash(),
|
2021-03-12 10:10:24 +00:00
|
|
|
Data: verif,
|
2020-03-04 10:18:49 +00:00
|
|
|
Items: items,
|
|
|
|
}
|
|
|
|
return json.Marshal(pc)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-03-04 10:18:49 +00:00
|
|
|
func (c *ParameterContext) UnmarshalJSON(data []byte) error {
|
|
|
|
pc := new(paramContext)
|
|
|
|
if err := json.Unmarshal(data, pc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-18 08:39:47 +00:00
|
|
|
var verif crypto.VerifiableDecodable
|
2020-03-04 10:18:49 +00:00
|
|
|
switch pc.Type {
|
2022-09-08 10:07:49 +00:00
|
|
|
case compatTransactionType, TransactionType:
|
2021-03-12 08:27:50 +00:00
|
|
|
tx := new(transaction.Transaction)
|
|
|
|
verif = tx
|
2020-03-04 10:18:49 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported type: %s", c.Type)
|
|
|
|
}
|
2021-03-17 13:40:24 +00:00
|
|
|
err := verif.DecodeHashableFields(pc.Data)
|
2020-06-18 08:39:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-03-04 10:18:49 +00:00
|
|
|
}
|
|
|
|
items := make(map[util.Uint160]*Item, len(pc.Items))
|
|
|
|
for h := range pc.Items {
|
2021-07-23 09:41:09 +00:00
|
|
|
u, err := util.Uint160DecodeStringLE(strings.TrimPrefix(h, "0x"))
|
2020-03-04 10:18:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
item := new(Item)
|
|
|
|
if err := json.Unmarshal(pc.Items[h], item); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
items[u] = item
|
|
|
|
}
|
2022-02-22 15:47:47 +00:00
|
|
|
if !pc.Hash.Equals(util.Uint256{}) {
|
|
|
|
if !verif.Hash().Equals(pc.Hash) {
|
|
|
|
return fmt.Errorf("hash parameter doesn't match calculated verifiable hash: %s vs %s", pc.Hash.StringLE(), verif.Hash().StringLE())
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 10:18:49 +00:00
|
|
|
c.Type = pc.Type
|
2021-03-12 09:59:14 +00:00
|
|
|
c.Network = netmode.Magic(pc.Net)
|
2020-03-04 10:18:49 +00:00
|
|
|
c.Verifiable = verif
|
|
|
|
c.Items = items
|
|
|
|
return nil
|
|
|
|
}
|