mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 19:42:23 +00:00
core: implement System.Contract.CreateStandardAccount syscall
This commit is contained in:
parent
5b4f38d717
commit
52a27e9be4
3 changed files with 35 additions and 0 deletions
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||
|
@ -486,3 +487,14 @@ func contractIsStandard(ic *interop.Context, v *vm.VM) error {
|
|||
v.Estack().PushVal(result)
|
||||
return nil
|
||||
}
|
||||
|
||||
// contractCreateStandardAccount calculates contract scripthash for a given public key.
|
||||
func contractCreateStandardAccount(ic *interop.Context, v *vm.VM) error {
|
||||
h := v.Estack().Pop().Bytes()
|
||||
p, err := keys.NewPublicKeyFromBytes(h)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.Estack().PushVal(p.GetScriptHash().BytesBE())
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/nspcc-dev/dbft/crypto"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -175,3 +176,24 @@ func TestContractIsStandard(t *testing.T) {
|
|||
require.False(t, v.Estack().Pop().Bool())
|
||||
})
|
||||
}
|
||||
|
||||
func TestContractCreateAccount(t *testing.T) {
|
||||
v, ic, chain := createVM(t)
|
||||
defer chain.Close()
|
||||
t.Run("Good", func(t *testing.T) {
|
||||
priv, err := keys.NewPrivateKey()
|
||||
require.NoError(t, err)
|
||||
pub := priv.PublicKey()
|
||||
v.Estack().PushVal(pub.Bytes())
|
||||
require.NoError(t, contractCreateStandardAccount(ic, v))
|
||||
|
||||
value := v.Estack().Pop().Bytes()
|
||||
u, err := util.Uint160DecodeBytesBE(value)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, pub.GetScriptHash(), u)
|
||||
})
|
||||
t.Run("InvalidKey", func(t *testing.T) {
|
||||
v.Estack().PushVal([]byte{1, 2, 3})
|
||||
require.Error(t, contractCreateStandardAccount(ic, v))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -89,6 +89,7 @@ var systemInterops = []interop.Function{
|
|||
AllowedTriggers: trigger.System | trigger.Application, RequiredFlags: smartcontract.AllowCall},
|
||||
{Name: "System.Contract.Create", Func: contractCreate, Price: 0,
|
||||
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates},
|
||||
{Name: "System.Contract.CreateStandardAccount", Func: contractCreateStandardAccount, Price: 10000},
|
||||
{Name: "System.Contract.Destroy", Func: contractDestroy, Price: 1000000,
|
||||
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates},
|
||||
{Name: "System.Contract.IsStandard", Func: contractIsStandard, Price: 30000},
|
||||
|
|
Loading…
Reference in a new issue