neotest: allow to extract simple signers from multi-signer

There is a quirk related to ordering: we store accounts in such an order that
is expected by multi-signature verification script. This was done to
speed up transaction/block signing which is done quite frequently in
tests. This commit allows to provide accounts in any order and to
extract a single signer from multi-signer based on this order.

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-12-03 16:10:55 +03:00 committed by Roman Khimov
parent d4689db47e
commit 37ecf51d13
2 changed files with 66 additions and 8 deletions

View file

@ -3,6 +3,7 @@ package neotest
import (
"bytes"
"fmt"
"sort"
"testing"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
@ -35,11 +36,21 @@ type SingleSigner interface {
Account() *wallet.Account
}
// MultiSigner is the interface for multisignature signing account.
type MultiSigner interface {
Signer
// Single returns simple-signature signer for n-th account in list.
Single(n int) SingleSigner
}
// signer represents simple-signature signer.
type signer wallet.Account
// multiSigner represents single multi-signature signer consisting of provided accounts.
type multiSigner []*wallet.Account
type multiSigner struct {
accounts []*wallet.Account
m int
}
// NewSingleSigner returns multi-signature signer for the provided account.
// It must contain exactly as many accounts as needed to sign the script.
@ -78,7 +89,7 @@ func (s *signer) Account() *wallet.Account {
// NewMultiSigner returns multi-signature signer for the provided account.
// It must contain at least as many accounts as needed to sign the script.
func NewMultiSigner(accs ...*wallet.Account) Signer {
func NewMultiSigner(accs ...*wallet.Account) MultiSigner {
if len(accs) == 0 {
panic("empty account list")
}
@ -91,30 +102,35 @@ func NewMultiSigner(accs ...*wallet.Account) Signer {
panic(fmt.Sprintf("verification script requires %d signatures, "+
"but only %d accounts were provided", m, len(accs)))
}
sort.Slice(accs, func(i, j int) bool {
p1 := accs[i].PrivateKey().PublicKey()
p2 := accs[j].PrivateKey().PublicKey()
return p1.Cmp(p2) == -1
})
for _, acc := range accs {
if !bytes.Equal(script, acc.Contract.Script) {
panic("all accounts must have equal verification script")
}
}
return multiSigner(accs[:m])
return multiSigner{accounts: accs, m: m}
}
// ScriptHash implements Signer interface.
func (m multiSigner) ScriptHash() util.Uint160 {
return m[0].Contract.ScriptHash()
return m.accounts[0].Contract.ScriptHash()
}
// Script implements Signer interface.
func (m multiSigner) Script() []byte {
return m[0].Contract.Script
return m.accounts[0].Contract.Script
}
// SignHashable implements Signer interface.
func (m multiSigner) SignHashable(magic uint32, item hash.Hashable) []byte {
var script []byte
for _, acc := range m {
sign := acc.PrivateKey().SignHashable(magic, item)
for i := 0; i < m.m; i++ {
sign := m.accounts[i].PrivateKey().SignHashable(magic, item)
script = append(script, byte(opcode.PUSHDATA1), 64)
script = append(script, sign...)
}
@ -138,9 +154,19 @@ func (m multiSigner) SignTx(magic netmode.Magic, tx *transaction.Transaction) er
return nil
}
// 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()))
}
func checkMultiSigner(t *testing.T, s Signer) {
accs, ok := s.(multiSigner)
ms, ok := s.(multiSigner)
require.True(t, ok, "expected to be a multi-signer")
accs := ms.accounts
require.True(t, len(accs) > 0, "empty multi-signer")
m := len(accs[0].Contract.Parameters)

View file

@ -1,8 +1,11 @@
package neotest
import (
"sort"
"testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
)
@ -14,3 +17,32 @@ func TestSingleSigner(t *testing.T) {
s := NewSingleSigner(a)
require.Equal(t, s.ScriptHash(), s.Account().Contract.ScriptHash())
}
func TestMultiSigner(t *testing.T) {
const size = 4
pubs := make(keys.PublicKeys, size)
accs := make([]*wallet.Account, size)
for i := range accs {
a, err := wallet.NewAccount()
require.NoError(t, err)
accs[i] = a
pubs[i] = a.PrivateKey().PublicKey()
}
sort.Sort(pubs)
m := smartcontract.GetDefaultHonestNodeCount(size)
for i := range accs {
require.NoError(t, accs[i].ConvertMultisig(m, pubs))
}
s := NewMultiSigner(accs...)
for i := range pubs {
for j := range accs {
if pub := accs[j].PrivateKey().PublicKey(); pub.Equal(pubs[i]) {
require.Equal(t, pub, s.Single(i).Account().PrivateKey().PublicKey())
}
}
}
}