2021-11-03 10:44:46 +00:00
|
|
|
package neotest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-11-28 06:23:55 +00:00
|
|
|
"errors"
|
2021-11-03 10:44:46 +00:00
|
|
|
"fmt"
|
2021-12-03 13:10:55 +00:00
|
|
|
"sort"
|
2021-11-03 12:28:29 +00:00
|
|
|
"testing"
|
2021-11-03 10:44:46 +00:00
|
|
|
|
|
|
|
"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/crypto/hash"
|
2022-10-05 07:45:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2023-11-28 06:23:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2021-11-03 10:44:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2023-11-28 06:23:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2021-11-03 10:44:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2021-11-03 12:28:29 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-11-03 10:44:46 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Signer is a generic interface which can be either a simple- or multi-signature signer.
|
2021-11-03 10:44:46 +00:00
|
|
|
type Signer interface {
|
2022-04-20 18:30:09 +00:00
|
|
|
// Script returns a signer verification script.
|
2023-12-01 06:25:41 +00:00
|
|
|
Script() []byte
|
|
|
|
// ScriptHash returns a signer script hash.
|
2021-11-03 10:44:46 +00:00
|
|
|
ScriptHash() util.Uint160
|
2022-04-20 18:30:09 +00:00
|
|
|
// SignHashable returns an invocation script for signing an item.
|
2021-11-03 10:44:46 +00:00
|
|
|
SignHashable(uint32, hash.Hashable) []byte
|
|
|
|
// SignTx signs a transaction.
|
|
|
|
SignTx(netmode.Magic, *transaction.Transaction) error
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SingleSigner is a generic interface for a simple one-signature signer.
|
2021-12-03 13:10:17 +00:00
|
|
|
type SingleSigner interface {
|
|
|
|
Signer
|
2022-04-20 18:30:09 +00:00
|
|
|
// Account returns the underlying account which can be used to
|
|
|
|
// get a public key and/or sign arbitrary things.
|
2021-12-03 13:10:17 +00:00
|
|
|
Account() *wallet.Account
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MultiSigner is an interface for multisignature signing account.
|
2021-12-03 13:10:55 +00:00
|
|
|
type MultiSigner interface {
|
|
|
|
Signer
|
2022-04-20 18:30:09 +00:00
|
|
|
// Single returns a simple-signature signer for the n-th account in a list.
|
2021-12-03 13:10:55 +00:00
|
|
|
Single(n int) SingleSigner
|
|
|
|
}
|
|
|
|
|
2023-11-28 06:23:55 +00:00
|
|
|
// ContractSigner is an interface for contract signer.
|
|
|
|
type ContractSigner interface {
|
|
|
|
Signer
|
|
|
|
// InvocationScript returns an invocation script to be used as invocation script for contract-based witness.
|
|
|
|
InvocationScript(tx *transaction.Transaction) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// signer represents a simple-signature signer.
|
2021-11-03 10:44:46 +00:00
|
|
|
type signer wallet.Account
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// multiSigner represents a single multi-signature signer consisting of the provided accounts.
|
2021-12-03 13:10:55 +00:00
|
|
|
type multiSigner struct {
|
|
|
|
accounts []*wallet.Account
|
|
|
|
m int
|
|
|
|
}
|
2021-11-03 10:44:46 +00:00
|
|
|
|
2023-09-05 09:36:22 +00:00
|
|
|
// NewSingleSigner creates a [SingleSigner] from the provided account. It has
|
|
|
|
// just one key, see [NewMultiSigner] for multisignature accounts.
|
2021-12-03 13:10:17 +00:00
|
|
|
func NewSingleSigner(acc *wallet.Account) SingleSigner {
|
2021-11-03 10:44:46 +00:00
|
|
|
if !vm.IsSignatureContract(acc.Contract.Script) {
|
|
|
|
panic("account must have simple-signature verification script")
|
|
|
|
}
|
|
|
|
return (*signer)(acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Script implements Signer interface.
|
|
|
|
func (s *signer) Script() []byte {
|
|
|
|
return (*wallet.Account)(s).Contract.Script
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScriptHash implements Signer interface.
|
|
|
|
func (s *signer) ScriptHash() util.Uint160 {
|
|
|
|
return (*wallet.Account)(s).Contract.ScriptHash()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignHashable implements Signer interface.
|
|
|
|
func (s *signer) SignHashable(magic uint32, item hash.Hashable) []byte {
|
2022-10-05 07:45:52 +00:00
|
|
|
return append([]byte{byte(opcode.PUSHDATA1), keys.SignatureLen},
|
2022-09-01 17:42:42 +00:00
|
|
|
(*wallet.Account)(s).SignHashable(netmode.Magic(magic), item)...)
|
2021-11-03 10:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignTx implements Signer interface.
|
|
|
|
func (s *signer) SignTx(magic netmode.Magic, tx *transaction.Transaction) error {
|
|
|
|
return (*wallet.Account)(s).SignTx(magic, tx)
|
|
|
|
}
|
|
|
|
|
2021-12-03 13:10:17 +00:00
|
|
|
// Account implements SingleSigner interface.
|
|
|
|
func (s *signer) Account() *wallet.Account {
|
|
|
|
return (*wallet.Account)(s)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewMultiSigner returns a multi-signature signer for the provided account.
|
2021-11-03 10:44:46 +00:00
|
|
|
// It must contain at least as many accounts as needed to sign the script.
|
2021-12-03 13:10:55 +00:00
|
|
|
func NewMultiSigner(accs ...*wallet.Account) MultiSigner {
|
2021-11-03 10:44:46 +00:00
|
|
|
if len(accs) == 0 {
|
|
|
|
panic("empty account list")
|
|
|
|
}
|
|
|
|
script := accs[0].Contract.Script
|
|
|
|
m, _, ok := vm.ParseMultiSigContract(script)
|
|
|
|
if !ok {
|
|
|
|
panic("all accounts must have multi-signature verification script")
|
|
|
|
}
|
|
|
|
if len(accs) < m {
|
|
|
|
panic(fmt.Sprintf("verification script requires %d signatures, "+
|
|
|
|
"but only %d accounts were provided", m, len(accs)))
|
|
|
|
}
|
2021-12-03 13:10:55 +00:00
|
|
|
sort.Slice(accs, func(i, j int) bool {
|
2022-09-01 14:52:44 +00:00
|
|
|
p1 := accs[i].PublicKey()
|
|
|
|
p2 := accs[j].PublicKey()
|
2021-12-03 13:10:55 +00:00
|
|
|
return p1.Cmp(p2) == -1
|
|
|
|
})
|
2021-11-03 10:44:46 +00:00
|
|
|
for _, acc := range accs {
|
|
|
|
if !bytes.Equal(script, acc.Contract.Script) {
|
|
|
|
panic("all accounts must have equal verification script")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 13:10:55 +00:00
|
|
|
return multiSigner{accounts: accs, m: m}
|
2021-11-03 10:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ScriptHash implements Signer interface.
|
|
|
|
func (m multiSigner) ScriptHash() util.Uint160 {
|
2021-12-03 13:10:55 +00:00
|
|
|
return m.accounts[0].Contract.ScriptHash()
|
2021-11-03 10:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Script implements Signer interface.
|
|
|
|
func (m multiSigner) Script() []byte {
|
2021-12-03 13:10:55 +00:00
|
|
|
return m.accounts[0].Contract.Script
|
2021-11-03 10:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignHashable implements Signer interface.
|
|
|
|
func (m multiSigner) SignHashable(magic uint32, item hash.Hashable) []byte {
|
|
|
|
var script []byte
|
2021-12-03 13:10:55 +00:00
|
|
|
for i := 0; i < m.m; i++ {
|
2022-09-01 17:42:42 +00:00
|
|
|
sign := m.accounts[i].SignHashable(netmode.Magic(magic), item)
|
2022-10-05 07:45:52 +00:00
|
|
|
script = append(script, byte(opcode.PUSHDATA1), keys.SignatureLen)
|
2021-11-03 10:44:46 +00:00
|
|
|
script = append(script, sign...)
|
|
|
|
}
|
|
|
|
return script
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignTx implements Signer interface.
|
|
|
|
func (m multiSigner) SignTx(magic netmode.Magic, tx *transaction.Transaction) error {
|
|
|
|
invoc := m.SignHashable(uint32(magic), tx)
|
|
|
|
verif := m.Script()
|
|
|
|
for i := range tx.Scripts {
|
|
|
|
if bytes.Equal(tx.Scripts[i].VerificationScript, verif) {
|
|
|
|
tx.Scripts[i].InvocationScript = invoc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Scripts = append(tx.Scripts, transaction.Witness{
|
|
|
|
InvocationScript: invoc,
|
|
|
|
VerificationScript: verif,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-03 12:28:29 +00:00
|
|
|
|
2021-12-03 13:10:55 +00:00
|
|
|
// Single implements MultiSigner interface.
|
|
|
|
func (m multiSigner) Single(n int) SingleSigner {
|
|
|
|
if len(m.accounts) <= n {
|
|
|
|
panic("invalid index")
|
|
|
|
}
|
|
|
|
return NewSingleSigner(wallet.NewAccountFromPrivateKey(m.accounts[n].PrivateKey()))
|
|
|
|
}
|
|
|
|
|
2022-03-18 15:11:24 +00:00
|
|
|
func checkMultiSigner(t testing.TB, s Signer) {
|
2021-12-03 13:10:55 +00:00
|
|
|
ms, ok := s.(multiSigner)
|
2021-11-03 12:28:29 +00:00
|
|
|
require.True(t, ok, "expected to be a multi-signer")
|
2021-12-03 13:10:55 +00:00
|
|
|
|
|
|
|
accs := ms.accounts
|
2021-11-03 12:28:29 +00:00
|
|
|
require.True(t, len(accs) > 0, "empty multi-signer")
|
|
|
|
|
|
|
|
m := len(accs[0].Contract.Parameters)
|
|
|
|
require.True(t, m <= len(accs), "honest not count is too big for a multi-signer")
|
|
|
|
|
|
|
|
h := accs[0].Contract.ScriptHash()
|
|
|
|
for i := 1; i < len(accs); i++ {
|
|
|
|
require.Equal(t, m, len(accs[i].Contract.Parameters), "inconsistent multi-signer accounts")
|
|
|
|
require.Equal(t, h, accs[i].Contract.ScriptHash(), "inconsistent multi-signer accounts")
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 06:23:55 +00:00
|
|
|
|
|
|
|
type contractSigner struct {
|
|
|
|
params func(tx *transaction.Transaction) []any
|
|
|
|
scriptHash util.Uint160
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContractSigner returns a contract signer for the provided contract hash.
|
|
|
|
// getInvParams must return params to be used as invocation script for contract-based witness.
|
|
|
|
func NewContractSigner(h util.Uint160, getInvParams func(tx *transaction.Transaction) []any) ContractSigner {
|
|
|
|
return &contractSigner{
|
|
|
|
scriptHash: h,
|
|
|
|
params: getInvParams,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvocationScript implements ContractSigner.
|
|
|
|
func (s *contractSigner) InvocationScript(tx *transaction.Transaction) ([]byte, error) {
|
|
|
|
params := s.params(tx)
|
|
|
|
script := io.NewBufBinWriter()
|
|
|
|
for i := range params {
|
|
|
|
emit.Any(script.BinWriter, params[i])
|
|
|
|
}
|
|
|
|
if script.Err != nil {
|
|
|
|
return nil, script.Err
|
|
|
|
}
|
|
|
|
return script.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Script implements ContractSigner.
|
|
|
|
func (s *contractSigner) Script() []byte {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScriptHash implements ContractSigner.
|
|
|
|
func (s *contractSigner) ScriptHash() util.Uint160 {
|
|
|
|
return s.scriptHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignHashable implements ContractSigner.
|
|
|
|
func (s *contractSigner) SignHashable(uint32, hash.Hashable) []byte {
|
|
|
|
panic("not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignTx implements ContractSigner.
|
|
|
|
func (s *contractSigner) SignTx(magic netmode.Magic, tx *transaction.Transaction) error {
|
|
|
|
pos := -1
|
|
|
|
for idx := range tx.Signers {
|
|
|
|
if tx.Signers[idx].Account.Equals(s.ScriptHash()) {
|
|
|
|
pos = idx
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pos < 0 {
|
|
|
|
return fmt.Errorf("signer %s not found", s.ScriptHash().String())
|
|
|
|
}
|
|
|
|
if len(tx.Scripts) < pos {
|
|
|
|
return errors.New("transaction is not yet signed by the previous signer")
|
|
|
|
}
|
|
|
|
invoc, err := s.InvocationScript(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(tx.Scripts) == pos {
|
|
|
|
tx.Scripts = append(tx.Scripts, transaction.Witness{})
|
|
|
|
}
|
|
|
|
tx.Scripts[pos].InvocationScript = invoc
|
|
|
|
tx.Scripts[pos].VerificationScript = s.Script()
|
|
|
|
return nil
|
|
|
|
}
|