github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/neorpc/types_test.go (about) 1 package neorpc 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 "github.com/nspcc-dev/neo-go/internal/testserdes" 9 "github.com/nspcc-dev/neo-go/pkg/core/transaction" 10 "github.com/nspcc-dev/neo-go/pkg/crypto/keys" 11 "github.com/nspcc-dev/neo-go/pkg/util" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestSignerWithWitnessMarshalUnmarshalJSON(t *testing.T) { 16 s := &SignerWithWitness{ 17 Signer: transaction.Signer{ 18 Account: util.Uint160{1, 2, 3}, 19 Scopes: transaction.CalledByEntry | transaction.CustomContracts, 20 AllowedContracts: []util.Uint160{{1, 2, 3, 4}}, 21 }, 22 Witness: transaction.Witness{ 23 InvocationScript: []byte{1, 2, 3}, 24 VerificationScript: []byte{4, 5, 6}, 25 }, 26 } 27 testserdes.MarshalUnmarshalJSON(t, s, &SignerWithWitness{}) 28 29 // Check marshalling separately to ensure Scopes are marshalled OK. 30 expected := `{"account":"0xcadb3dc2faa3ef14a13b619c9a43124755aa2569","scopes":"CalledByEntry, CustomContracts"}` 31 acc, err := util.Uint160DecodeStringLE("cadb3dc2faa3ef14a13b619c9a43124755aa2569") 32 require.NoError(t, err) 33 s = &SignerWithWitness{ 34 Signer: transaction.Signer{ 35 Account: acc, 36 Scopes: transaction.CalledByEntry | transaction.CustomContracts, 37 }, 38 } 39 actual, err := json.Marshal(s) 40 require.NoError(t, err) 41 require.Equal(t, expected, string(actual)) 42 43 t.Run("subitems overflow", func(t *testing.T) { 44 checkSubitems := func(t *testing.T, bad any) { 45 data, err := json.Marshal(bad) 46 require.NoError(t, err) 47 err = json.Unmarshal(data, &SignerWithWitness{}) 48 49 require.Error(t, err) 50 require.Contains(t, err.Error(), fmt.Sprintf("got %d, allowed %d at max", transaction.MaxAttributes+1, transaction.MaxAttributes)) 51 } 52 53 t.Run("groups", func(t *testing.T) { 54 pk, err := keys.NewPrivateKey() 55 require.NoError(t, err) 56 bad := &SignerWithWitness{ 57 Signer: transaction.Signer{ 58 AllowedGroups: make([]*keys.PublicKey, transaction.MaxAttributes+1), 59 }, 60 } 61 for i := range bad.AllowedGroups { 62 bad.AllowedGroups[i] = pk.PublicKey() 63 } 64 65 checkSubitems(t, bad) 66 }) 67 t.Run("contracts", func(t *testing.T) { 68 bad := &SignerWithWitness{ 69 Signer: transaction.Signer{ 70 AllowedContracts: make([]util.Uint160, transaction.MaxAttributes+1), 71 }, 72 } 73 74 checkSubitems(t, bad) 75 }) 76 t.Run("rules", func(t *testing.T) { 77 bad := &SignerWithWitness{ 78 Signer: transaction.Signer{ 79 Rules: make([]transaction.WitnessRule, transaction.MaxAttributes+1), 80 }, 81 } 82 for i := range bad.Rules { 83 bad.Rules[i] = transaction.WitnessRule{ 84 Action: transaction.WitnessAllow, 85 Condition: &transaction.ConditionScriptHash{}, 86 } 87 } 88 89 checkSubitems(t, bad) 90 }) 91 }) 92 }