smartcontract: use script instead of address in context

This commit is contained in:
Evgeniy Stratonikov 2021-03-03 15:52:33 +03:00
parent 8ef7dd320c
commit 20d2386414
4 changed files with 30 additions and 9 deletions

View file

@ -135,7 +135,7 @@ func (c *ParameterContext) getItemForContract(ctr *wallet.Contract) *Item {
params[i].Type = ctr.Parameters[i].Type
}
item := &Item{
Script: h,
Script: ctr.Script,
Parameters: params,
Signatures: make(map[string][]byte),
}

View file

@ -2,6 +2,7 @@ package context
import (
"encoding/hex"
"encoding/json"
"testing"
"github.com/nspcc-dev/neo-go/internal/testserdes"
@ -131,7 +132,7 @@ func TestParameterContext_MarshalJSON(t *testing.T) {
Verifiable: tx,
Items: map[util.Uint160]*Item{
priv.GetScriptHash(): {
Script: priv.GetScriptHash(),
Script: priv.PublicKey().GetVerificationScript(),
Parameters: []smartcontract.Parameter{{
Type: smartcontract.SignatureType,
Value: sign,
@ -144,6 +145,22 @@ func TestParameterContext_MarshalJSON(t *testing.T) {
}
testserdes.MarshalUnmarshalJSON(t, expected, new(ParameterContext))
t.Run("invalid script", func(t *testing.T) {
js := `{
"script": "AQID",
"parameters": [
{
"type": "Signature",
"value": "QfOZLLqjMyPWMzRxMAKw7fcd8leLcpwiiTV2pUyC0pth/y7Iw7o7WzNpxeAJm5bmExmlF7g5pMhXz1xVT6KK3g=="
}
],
"signatures": {
"025c210bde738e0e646929ee04ec2ccb42a700356083f55386b5347b9b725c10b9": "a6c6d8a2334791888df559419f07209ee39e2f20688af8cc38010854b98abf77194e37f173bbc86b77dce4afa8ce3ae5170dd346b5265bcb9b723d83299a6f0f",
"035d4da640b3a39f19ed88855aeddd97725422b4230ccae56bd5544419d0056ea9": "058e577f23395f382194eebb83f66bb8903c8f3c5b6afd759c20f2518466124dcd9cbccfc029a42e9a7d5a3a060b091edc73dcac949fd894d7a9d10678296ac6"
}`
require.Error(t, json.Unmarshal([]byte(js), new(ParameterContext)))
})
}
func getPrivateKeys(t *testing.T, n int) ([]*keys.PrivateKey, []*keys.PublicKey) {

View file

@ -1,23 +1,23 @@
package context
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"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"
)
// Item represents a transaction context item.
type Item struct {
Script util.Uint160
Script []byte
Parameters []smartcontract.Parameter
Signatures map[string][]byte
}
type itemAux struct {
Script util.Uint160 `json:"script"`
Script string `json:"script"`
Parameters []smartcontract.Parameter `json:"parameters"`
Signatures map[string]string `json:"signatures"`
}
@ -36,7 +36,7 @@ func (it *Item) AddSignature(pub *keys.PublicKey, sig []byte) {
// MarshalJSON implements json.Marshaler interface.
func (it Item) MarshalJSON() ([]byte, error) {
ci := itemAux{
Script: it.Script,
Script: base64.StdEncoding.EncodeToString(it.Script),
Parameters: it.Parameters,
Signatures: make(map[string]string, len(it.Signatures)),
}
@ -55,6 +55,11 @@ func (it *Item) UnmarshalJSON(data []byte) error {
return err
}
script, err := base64.StdEncoding.DecodeString(ci.Script)
if err != nil {
return err
}
sigs := make(map[string][]byte, len(ci.Signatures))
for keyHex, sigHex := range ci.Signatures {
_, err := keys.NewPublicKeyFromString(keyHex)
@ -69,7 +74,7 @@ func (it *Item) UnmarshalJSON(data []byte) error {
}
it.Signatures = sigs
it.Script = ci.Script
it.Script = script
it.Parameters = ci.Parameters
return nil
}

View file

@ -8,7 +8,6 @@ import (
"github.com/nspcc-dev/neo-go/internal/testserdes"
"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/stretchr/testify/require"
)
@ -41,7 +40,7 @@ func TestContextItem_MarshalJSON(t *testing.T) {
require.NoError(t, err)
expected := &Item{
Script: util.Uint160{1, 2, 3},
Script: []byte{1, 2, 3},
Parameters: []smartcontract.Parameter{{
Type: smartcontract.SignatureType,
Value: random.Bytes(64),