neotest: reuse wallet.Account for contract signers
Refs #3245 Refs #3233 Signed-off-by: Evgenii Stratonikov <fyfyrchik@runbox.com>
This commit is contained in:
parent
962b161652
commit
c8ff44560e
2 changed files with 30 additions and 54 deletions
|
@ -301,8 +301,9 @@ func AddNetworkFee(t testing.TB, bc *core.Blockchain, tx *transaction.Transactio
|
||||||
baseFee := bc.GetBaseExecFee()
|
baseFee := bc.GetBaseExecFee()
|
||||||
size := io.GetVarSize(tx)
|
size := io.GetVarSize(tx)
|
||||||
for _, sgr := range signers {
|
for _, sgr := range signers {
|
||||||
if csgr, ok := sgr.(ContractSigner); ok {
|
csgr, ok := sgr.(SingleSigner)
|
||||||
sc, err := csgr.InvocationScript(tx)
|
if ok && csgr.Account().Contract.InvocationBuilder != nil {
|
||||||
|
sc, err := csgr.Account().Contract.InvocationBuilder(tx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
txCopy := *tx
|
txCopy := *tx
|
||||||
|
|
|
@ -2,7 +2,6 @@ package neotest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -11,6 +10,7 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
"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/crypto/hash"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||||
|
@ -47,13 +47,6 @@ type MultiSigner interface {
|
||||||
Single(n int) SingleSigner
|
Single(n int) SingleSigner
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// signer represents a simple-signature signer.
|
// signer represents a simple-signature signer.
|
||||||
type signer wallet.Account
|
type signer wallet.Account
|
||||||
|
|
||||||
|
@ -190,23 +183,17 @@ func checkMultiSigner(t testing.TB, s Signer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type contractSigner struct {
|
type contractSigner wallet.Account
|
||||||
params func(tx *transaction.Transaction) []any
|
|
||||||
scriptHash util.Uint160
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewContractSigner returns a contract signer for the provided contract hash.
|
// NewContractSigner returns a contract signer for the provided contract hash.
|
||||||
// getInvParams must return params to be used as invocation script for contract-based witness.
|
// 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 {
|
func NewContractSigner(h util.Uint160, getInvParams func(tx *transaction.Transaction) []any) SingleSigner {
|
||||||
return &contractSigner{
|
return &contractSigner{
|
||||||
scriptHash: h,
|
Address: address.Uint160ToString(h),
|
||||||
params: getInvParams,
|
Contract: &wallet.Contract{
|
||||||
}
|
Deployed: true,
|
||||||
}
|
InvocationBuilder: func(tx *transaction.Transaction) ([]byte, error) {
|
||||||
|
params := getInvParams(tx)
|
||||||
// InvocationScript implements ContractSigner.
|
|
||||||
func (s *contractSigner) InvocationScript(tx *transaction.Transaction) ([]byte, error) {
|
|
||||||
params := s.params(tx)
|
|
||||||
script := io.NewBufBinWriter()
|
script := io.NewBufBinWriter()
|
||||||
for i := range params {
|
for i := range params {
|
||||||
emit.Any(script.BinWriter, params[i])
|
emit.Any(script.BinWriter, params[i])
|
||||||
|
@ -215,6 +202,9 @@ func (s *contractSigner) InvocationScript(tx *transaction.Transaction) ([]byte,
|
||||||
return nil, script.Err
|
return nil, script.Err
|
||||||
}
|
}
|
||||||
return script.Bytes(), nil
|
return script.Bytes(), nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Script implements ContractSigner.
|
// Script implements ContractSigner.
|
||||||
|
@ -224,7 +214,12 @@ func (s *contractSigner) Script() []byte {
|
||||||
|
|
||||||
// ScriptHash implements ContractSigner.
|
// ScriptHash implements ContractSigner.
|
||||||
func (s *contractSigner) ScriptHash() util.Uint160 {
|
func (s *contractSigner) ScriptHash() util.Uint160 {
|
||||||
return s.scriptHash
|
return s.Account().ScriptHash()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ScriptHash implements ContractSigner.
|
||||||
|
func (s *contractSigner) Account() *wallet.Account {
|
||||||
|
return (*wallet.Account)(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignHashable implements ContractSigner.
|
// SignHashable implements ContractSigner.
|
||||||
|
@ -234,27 +229,7 @@ func (s *contractSigner) SignHashable(uint32, hash.Hashable) []byte {
|
||||||
|
|
||||||
// SignTx implements ContractSigner.
|
// SignTx implements ContractSigner.
|
||||||
func (s *contractSigner) SignTx(magic netmode.Magic, tx *transaction.Transaction) error {
|
func (s *contractSigner) SignTx(magic netmode.Magic, tx *transaction.Transaction) error {
|
||||||
pos := -1
|
// Here we rely on `len(s.Contract.Parameters) == 0` being after the `s.Contract.InvocationBuilder != nil` check,
|
||||||
for idx := range tx.Signers {
|
// because we cannot determine the list of parameters unless we already have tx.
|
||||||
if tx.Signers[idx].Account.Equals(s.ScriptHash()) {
|
return s.Account().SignTx(magic, tx)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue