2021-11-03 10:44:46 +00:00
|
|
|
package neotest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"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
|
|
|
// ScriptHash returns a signer script hash.
|
2021-11-03 10:44:46 +00:00
|
|
|
Script() []byte
|
2022-04-20 18:30:09 +00:00
|
|
|
// Script returns a signer verification script.
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewSingleSigner returns a multi-signature signer for the provided account.
|
2021-11-03 10:44:46 +00:00
|
|
|
// It must contain exactly as many accounts as needed to sign the script.
|
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 {
|
|
|
|
return append([]byte{byte(opcode.PUSHDATA1), 64},
|
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)
|
2021-11-03 10:44:46 +00:00
|
|
|
script = append(script, byte(opcode.PUSHDATA1), 64)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|