Skip to content

Commit

Permalink
Merge pull request #3492 from nspcc-dev/actor-signers
Browse files Browse the repository at this point in the history
invoker: add Signers() API
  • Loading branch information
AnnaShaleva authored Jun 21, 2024
2 parents 3889224 + cc3f528 commit d9d9d00
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
13 changes: 13 additions & 0 deletions pkg/rpcclient/actor/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ func (a *Actor) SendUncheckedRun(script []byte, sysfee int64, attrs []transactio
return a.sendWrapper(a.MakeUncheckedRun(script, sysfee, attrs, txHook))
}

// SignerAccounts returns the array of actor's signers/accounts. It's useful in
// case you need it elsewhere like for notary-related processing. Returned slice
// is a newly allocated one with signers deeply copied, accounts however are not
// so changing received account internals is an error.
func (a *Actor) SignerAccounts() []SignerAccount {
var res = make([]SignerAccount, len(a.signers))
for i := range a.signers {
res[i].Signer = *a.signers[i].Signer.Copy()
res[i].Account = a.signers[i].Account
}
return res
}

// Sender return the sender address that will be used in transactions created
// by Actor.
func (a *Actor) Sender() util.Uint160 {
Expand Down
11 changes: 8 additions & 3 deletions pkg/rpcclient/actor/actor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,14 @@ func TestNew(t *testing.T) {
// Good simple.
a, err := NewSimple(client, acc)
require.NoError(t, err)
require.Equal(t, 1, len(a.signers))
require.Equal(t, []SignerAccount{{
Signer: transaction.Signer{
Account: acc.ScriptHash(),
Scopes: transaction.CalledByEntry,
},
Account: acc,
}}, a.SignerAccounts())
require.Equal(t, 1, len(a.txSigners))
require.Equal(t, transaction.CalledByEntry, a.signers[0].Signer.Scopes)
require.Equal(t, transaction.CalledByEntry, a.txSigners[0].Scopes)

// Contractless account.
Expand Down Expand Up @@ -160,7 +165,7 @@ func TestNew(t *testing.T) {
signers[0].Signer.Account = acc.Contract.ScriptHash()
a, err = New(client, signers)
require.NoError(t, err)
require.Equal(t, 2, len(a.signers))
require.Equal(t, signers, a.SignerAccounts())
require.Equal(t, 2, len(a.txSigners))

// Good tuned
Expand Down
14 changes: 14 additions & 0 deletions pkg/rpcclient/invoker/invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ func (h *historicConverter) TraverseIterator(sessionID, iteratorID uuid.UUID, ma
return h.client.TraverseIterator(sessionID, iteratorID, maxItemsCount)
}

// Signers returns the set of current invoker signers which is mostly useful
// when working with upper-layer actors. Returned slice is a newly allocated
// one (if this invoker has them), so it's safe to modify.
func (v *Invoker) Signers() []transaction.Signer {
if v.signers == nil {
return nil
}
var res = make([]transaction.Signer, len(v.signers))
for i := range v.signers {
res[i] = *v.signers[i].Copy()
}
return res
}

// Call invokes a method of the contract with the given parameters (and
// Invoker-specific list of signers) and returns the result as is.
func (v *Invoker) Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) {
Expand Down
16 changes: 16 additions & 0 deletions pkg/rpcclient/invoker/invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,19 @@ func TestInvoker(t *testing.T) {
}
})
}

func TestInvokerSigners(t *testing.T) {
resExp := &result.Invoke{State: "HALT"}
ri := &rpcInv{resExp, true, nil, nil}
inv := New(ri, nil)

require.Nil(t, inv.Signers())

s := []transaction.Signer{}
inv = New(ri, s)
require.Equal(t, s, inv.Signers())

s = append(s, transaction.Signer{Account: util.Uint160{1, 2, 3}, Scopes: transaction.CalledByEntry})
inv = New(ri, s)
require.Equal(t, s, inv.Signers())
}

0 comments on commit d9d9d00

Please sign in to comment.