Merge pull request #2272 from nspcc-dev/fix-sender-custom-scopes

Fix custom sender scopes in CreateTxFromScript
This commit is contained in:
Roman Khimov 2021-11-23 12:37:33 +03:00 committed by GitHub
commit a6e5c18359
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 10 deletions

View file

@ -466,9 +466,17 @@ func TestComlileAndInvokeFunction(t *testing.T) {
}) })
t.Run("with cosigner", func(t *testing.T) { t.Run("with cosigner", func(t *testing.T) {
t.Run("cosigner is sender", func(t *testing.T) { t.Run("cosigner is sender (none)", func(t *testing.T) {
e.In.WriteString("one\r") e.In.WriteString("one\r")
e.Run(t, append(cmd, hVerify.StringLE(), "verify", "--", validatorAddr+":Global")...) e.RunWithError(t, append(cmd, h.StringLE(), "checkSenderWitness", "--", validatorAddr+":None")...)
})
t.Run("cosigner is sender (customcontract)", func(t *testing.T) {
e.In.WriteString("one\r")
e.Run(t, append(cmd, h.StringLE(), "checkSenderWitness", "--", validatorAddr+":CustomContracts:"+h.StringLE())...)
})
t.Run("cosigner is sender (global)", func(t *testing.T) {
e.In.WriteString("one\r")
e.Run(t, append(cmd, h.StringLE(), "checkSenderWitness", "--", validatorAddr+":Global")...)
}) })
acc, err := wallet.NewAccount() acc, err := wallet.NewAccount()

View file

@ -40,6 +40,14 @@ func Fail() {
panic("as expected") panic("as expected")
} }
// CheckSenderWitness checks sender's witness.
func CheckSenderWitness() {
tx := runtime.GetScriptContainer()
if !runtime.CheckWitness(tx.Sender) {
panic("not witnessed")
}
}
// Update updates contract with the new one. // Update updates contract with the new one.
func Update(script, manifest []byte) { func Update(script, manifest []byte) {
ctx := storage.GetReadOnlyContext() ctx := storage.GetReadOnlyContext()

View file

@ -714,7 +714,7 @@ func getSigners(sender *wallet.Account, cosigners []SignerAccount) ([]transactio
} }
for _, c := range cosigners { for _, c := range cosigners {
if c.Signer.Account == from { if c.Signer.Account == from {
s.Scopes = c.Signer.Scopes s = c.Signer
continue continue
} }
signers = append(signers, c.Signer) signers = append(signers, c.Signer)

View file

@ -710,13 +710,39 @@ func TestCreateNEP17TransferTx(t *testing.T) {
gasContractHash, err := c.GetNativeContractHash(nativenames.Gas) gasContractHash, err := c.GetNativeContractHash(nativenames.Gas)
require.NoError(t, err) require.NoError(t, err)
tx, err := c.CreateNEP17TransferTx(acc, util.Uint160{}, gasContractHash, 1000, 0, nil, nil) t.Run("default scope", func(t *testing.T) {
require.NoError(t, err) tx, err := c.CreateNEP17TransferTx(acc, util.Uint160{}, gasContractHash, 1000, 0, nil, nil)
require.NoError(t, acc.SignTx(testchain.Network(), tx)) require.NoError(t, err)
require.NoError(t, chain.VerifyTx(tx)) require.NoError(t, acc.SignTx(testchain.Network(), tx))
v, _ := chain.GetTestVM(trigger.Application, tx, nil) require.NoError(t, chain.VerifyTx(tx))
v.LoadScriptWithFlags(tx.Script, callflag.All) v, _ := chain.GetTestVM(trigger.Application, tx, nil)
require.NoError(t, v.Run()) v.LoadScriptWithFlags(tx.Script, callflag.All)
require.NoError(t, v.Run())
})
t.Run("none scope", func(t *testing.T) {
_, err := c.CreateNEP17TransferTx(acc, util.Uint160{}, gasContractHash, 1000, 0, nil, []client.SignerAccount{{
Signer: transaction.Signer{
Account: priv.PublicKey().GetScriptHash(),
Scopes: transaction.None,
},
}})
require.Error(t, err)
})
t.Run("customcontracts scope", func(t *testing.T) {
tx, err := c.CreateNEP17TransferTx(acc, util.Uint160{}, gasContractHash, 1000, 0, nil, []client.SignerAccount{{
Signer: transaction.Signer{
Account: priv.PublicKey().GetScriptHash(),
Scopes: transaction.CustomContracts,
AllowedContracts: []util.Uint160{gasContractHash},
},
}})
require.NoError(t, err)
require.NoError(t, acc.SignTx(testchain.Network(), tx))
require.NoError(t, chain.VerifyTx(tx))
v, _ := chain.GetTestVM(trigger.Application, tx, nil)
v.LoadScriptWithFlags(tx.Script, callflag.All)
require.NoError(t, v.Run())
})
} }
func TestInvokeVerify(t *testing.T) { func TestInvokeVerify(t *testing.T) {