From d4689db47e5ec765773cdf1e21cf12f994b0d367 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Fri, 3 Dec 2021 16:10:17 +0300 Subject: [PATCH] neotest: allow to extract account from single signer Signed-off-by: Evgeniy Stratonikov --- pkg/neotest/signer.go | 15 ++++++++++++++- pkg/neotest/signer_test.go | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 pkg/neotest/signer_test.go diff --git a/pkg/neotest/signer.go b/pkg/neotest/signer.go index dc3469e40..1f903a619 100644 --- a/pkg/neotest/signer.go +++ b/pkg/neotest/signer.go @@ -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 { diff --git a/pkg/neotest/signer_test.go b/pkg/neotest/signer_test.go new file mode 100644 index 000000000..c0f2d7e35 --- /dev/null +++ b/pkg/neotest/signer_test.go @@ -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()) +}