From 0b21d4caf643630ee0b9dd5b31ce5001da5c5ff4 Mon Sep 17 00:00:00 2001 From: Chubru Date: Fri, 20 Dec 2024 16:03:03 +0300 Subject: [PATCH] neotest: Add 'ReadNEF' method Signed-off-by: Chubru --- pkg/neotest/compile.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pkg/neotest/compile.go b/pkg/neotest/compile.go index 0408a0609..e727f1b5f 100644 --- a/pkg/neotest/compile.go +++ b/pkg/neotest/compile.go @@ -1,7 +1,9 @@ package neotest import ( + "encoding/json" "io" + "os" "testing" "github.com/nspcc-dev/neo-go/cli/smartcontract" @@ -90,3 +92,37 @@ func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath s contracts[cacheKey] = c return c } + +// ReadNEF loads a contract from the specified NEF and manifest files. +func ReadNEF(t testing.TB, sender util.Uint160, nefPath, manifestPath string) *Contract { + cacheKey := sender.StringLE() + "|" + nefPath + "|" + manifestPath + if c, ok := contracts[cacheKey]; ok { + return c + } + + nefBytes, err := os.ReadFile(nefPath) + require.NoError(t, err) + + ne, err := nef.FileFromBytes(nefBytes) + require.NoError(t, err) + + manifestBytes, err := os.ReadFile(manifestPath) + require.NoError(t, err) + + m := new(manifest.Manifest) + err = json.Unmarshal(manifestBytes, m) + require.NoError(t, err) + + hash := state.CreateContractHash(sender, ne.Checksum, m.Name) + err = m.IsValid(hash, true) + require.NoError(t, err) + + c := &Contract{ + Hash: hash, + NEF: &ne, + Manifest: m, + } + + contracts[cacheKey] = c + return c +}