2020-04-30 13:00:33 +00:00
|
|
|
package transaction
|
|
|
|
|
|
|
|
import (
|
2020-07-29 16:23:43 +00:00
|
|
|
"errors"
|
2023-07-08 10:48:52 +00:00
|
|
|
"math/big"
|
2024-08-23 21:07:41 +00:00
|
|
|
"slices"
|
2020-07-29 16:23:43 +00:00
|
|
|
|
2020-04-30 13:00:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-07-08 10:48:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-04-30 13:00:33 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// The maximum number of AllowedContracts or AllowedGroups.
|
2020-04-30 13:00:33 +00:00
|
|
|
const maxSubitems = 16
|
|
|
|
|
2020-07-29 16:57:38 +00:00
|
|
|
// Signer implements a Transaction signer.
|
|
|
|
type Signer struct {
|
2020-04-30 13:00:33 +00:00
|
|
|
Account util.Uint160 `json:"account"`
|
|
|
|
Scopes WitnessScope `json:"scopes"`
|
2020-07-09 13:47:50 +00:00
|
|
|
AllowedContracts []util.Uint160 `json:"allowedcontracts,omitempty"`
|
|
|
|
AllowedGroups []*keys.PublicKey `json:"allowedgroups,omitempty"`
|
2021-11-12 10:22:35 +00:00
|
|
|
Rules []WitnessRule `json:"rules,omitempty"`
|
2020-04-30 13:00:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeBinary implements the Serializable interface.
|
2020-07-29 16:57:38 +00:00
|
|
|
func (c *Signer) EncodeBinary(bw *io.BinWriter) {
|
2020-04-30 13:00:33 +00:00
|
|
|
bw.WriteBytes(c.Account[:])
|
|
|
|
bw.WriteB(byte(c.Scopes))
|
|
|
|
if c.Scopes&CustomContracts != 0 {
|
|
|
|
bw.WriteArray(c.AllowedContracts)
|
|
|
|
}
|
|
|
|
if c.Scopes&CustomGroups != 0 {
|
|
|
|
bw.WriteArray(c.AllowedGroups)
|
|
|
|
}
|
2021-11-12 10:22:35 +00:00
|
|
|
if c.Scopes&Rules != 0 {
|
|
|
|
bw.WriteArray(c.Rules)
|
|
|
|
}
|
2020-04-30 13:00:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinary implements the Serializable interface.
|
2020-07-29 16:57:38 +00:00
|
|
|
func (c *Signer) DecodeBinary(br *io.BinReader) {
|
2020-04-30 13:00:33 +00:00
|
|
|
br.ReadBytes(c.Account[:])
|
|
|
|
c.Scopes = WitnessScope(br.ReadB())
|
2022-04-27 09:46:43 +00:00
|
|
|
if c.Scopes & ^(Global|CalledByEntry|CustomContracts|CustomGroups|Rules|None) != 0 {
|
2020-07-29 16:23:43 +00:00
|
|
|
br.Err = errors.New("unknown witness scope")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.Scopes&Global != 0 && c.Scopes != Global {
|
|
|
|
br.Err = errors.New("global scope can not be combined with other scopes")
|
|
|
|
return
|
|
|
|
}
|
2020-04-30 13:00:33 +00:00
|
|
|
if c.Scopes&CustomContracts != 0 {
|
|
|
|
br.ReadArray(&c.AllowedContracts, maxSubitems)
|
|
|
|
}
|
|
|
|
if c.Scopes&CustomGroups != 0 {
|
|
|
|
br.ReadArray(&c.AllowedGroups, maxSubitems)
|
|
|
|
}
|
2021-11-12 10:22:35 +00:00
|
|
|
if c.Scopes&Rules != 0 {
|
|
|
|
br.ReadArray(&c.Rules, maxSubitems)
|
|
|
|
}
|
2020-04-30 13:00:33 +00:00
|
|
|
}
|
2023-07-08 10:48:52 +00:00
|
|
|
|
|
|
|
// SignersToStackItem converts transaction.Signers to stackitem.Item.
|
|
|
|
func SignersToStackItem(signers []Signer) stackitem.Item {
|
|
|
|
res := make([]stackitem.Item, len(signers))
|
|
|
|
for i, s := range signers {
|
|
|
|
contracts := make([]stackitem.Item, len(s.AllowedContracts))
|
|
|
|
for j, c := range s.AllowedContracts {
|
|
|
|
contracts[j] = stackitem.NewByteArray(c.BytesBE())
|
|
|
|
}
|
|
|
|
groups := make([]stackitem.Item, len(s.AllowedGroups))
|
|
|
|
for j, g := range s.AllowedGroups {
|
|
|
|
groups[j] = stackitem.NewByteArray(g.Bytes())
|
|
|
|
}
|
|
|
|
rules := make([]stackitem.Item, len(s.Rules))
|
|
|
|
for j, r := range s.Rules {
|
|
|
|
rules[j] = r.ToStackItem()
|
|
|
|
}
|
|
|
|
res[i] = stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(s.Account.BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(s.Scopes))),
|
|
|
|
stackitem.NewArray(contracts),
|
|
|
|
stackitem.NewArray(groups),
|
|
|
|
stackitem.NewArray(rules),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return stackitem.NewArray(res)
|
|
|
|
}
|
2024-04-27 17:16:05 +00:00
|
|
|
|
|
|
|
// Copy creates a deep copy of the Signer.
|
|
|
|
func (c *Signer) Copy() *Signer {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cp := *c
|
2024-08-23 21:07:41 +00:00
|
|
|
cp.AllowedContracts = slices.Clone(c.AllowedContracts)
|
2024-04-27 17:16:05 +00:00
|
|
|
cp.AllowedGroups = keys.PublicKeys(c.AllowedGroups).Copy()
|
|
|
|
if c.Rules != nil {
|
|
|
|
cp.Rules = make([]WitnessRule, len(c.Rules))
|
|
|
|
for i, rule := range c.Rules {
|
|
|
|
cp.Rules[i] = *rule.Copy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cp
|
|
|
|
}
|