Skip to content

Commit 0b21d4c

Browse files
committed
neotest: Add 'ReadNEF' method
Signed-off-by: Chubru <chubru.alexander@gmail.com>
1 parent 6d20772 commit 0b21d4c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pkg/neotest/compile.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package neotest
22

33
import (
4+
"encoding/json"
45
"io"
6+
"os"
57
"testing"
68

79
"github.com/nspcc-dev/neo-go/cli/smartcontract"
@@ -90,3 +92,37 @@ func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath s
9092
contracts[cacheKey] = c
9193
return c
9294
}
95+
96+
// ReadNEF loads a contract from the specified NEF and manifest files.
97+
func ReadNEF(t testing.TB, sender util.Uint160, nefPath, manifestPath string) *Contract {
98+
cacheKey := sender.StringLE() + "|" + nefPath + "|" + manifestPath
99+
if c, ok := contracts[cacheKey]; ok {
100+
return c
101+
}
102+
103+
nefBytes, err := os.ReadFile(nefPath)
104+
require.NoError(t, err)
105+
106+
ne, err := nef.FileFromBytes(nefBytes)
107+
require.NoError(t, err)
108+
109+
manifestBytes, err := os.ReadFile(manifestPath)
110+
require.NoError(t, err)
111+
112+
m := new(manifest.Manifest)
113+
err = json.Unmarshal(manifestBytes, m)
114+
require.NoError(t, err)
115+
116+
hash := state.CreateContractHash(sender, ne.Checksum, m.Name)
117+
err = m.IsValid(hash, true)
118+
require.NoError(t, err)
119+
120+
c := &Contract{
121+
Hash: hash,
122+
NEF: &ne,
123+
Manifest: m,
124+
}
125+
126+
contracts[cacheKey] = c
127+
return c
128+
}

0 commit comments

Comments
 (0)