neotest: allow to extract account from single signer

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

View file

@ -27,6 +27,14 @@ type Signer interface {
SignTx(netmode.Magic, *transaction.Transaction) error
}
// SingleSigner is a generic interface for simple one-signature signer.
type SingleSigner interface {
Signer
// Account returns underlying account which can be used to
// get public key and/or sign arbitrary things.
Account() *wallet.Account
}
// signer represents simple-signature signer.
type signer wallet.Account
@ -35,7 +43,7 @@ type multiSigner []*wallet.Account
// NewSingleSigner returns multi-signature signer for the provided account.
// It must contain exactly as many accounts as needed to sign the script.
func NewSingleSigner(acc *wallet.Account) Signer {
func NewSingleSigner(acc *wallet.Account) SingleSigner {
if !vm.IsSignatureContract(acc.Contract.Script) {
panic("account must have simple-signature verification script")
}
@ -63,6 +71,11 @@ func (s *signer) SignTx(magic netmode.Magic, tx *transaction.Transaction) error
return (*wallet.Account)(s).SignTx(magic, tx)
}
// Account implements SingleSigner interface.
func (s *signer) Account() *wallet.Account {
return (*wallet.Account)(s)
}
// 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 {

View file

@ -0,0 +1,16 @@
package neotest
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
)
func TestSingleSigner(t *testing.T) {
a, err := wallet.NewAccount()
require.NoError(t, err)
s := NewSingleSigner(a)
require.Equal(t, s.ScriptHash(), s.Account().Contract.ScriptHash())
}