sc/context: add network magic into the context

See neo-project/neo#2393, we need this to be able to sign multisig
transactions.
This commit is contained in:
Roman Khimov 2021-03-12 12:59:14 +03:00
parent 4462a6a6b7
commit dc980b5847
3 changed files with 14 additions and 6 deletions

View file

@ -22,7 +22,7 @@ func InitAndSave(tx *transaction.Transaction, acc *wallet.Account, filename stri
priv := acc.PrivateKey()
pub := priv.PublicKey()
sign := priv.Sign(tx.GetSignedPart())
scCtx := context.NewParameterContext("Neo.Core.ContractTransaction", tx)
scCtx := context.NewParameterContext("Neo.Core.ContractTransaction", tx.Network, tx)
h, err := address.StringToUint160(acc.Address)
if err != nil {
return fmt.Errorf("invalid address: %s", acc.Address)

View file

@ -9,6 +9,7 @@ import (
"sort"
"strings"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
@ -24,6 +25,8 @@ import (
type ParameterContext struct {
// Type is a type of a verifiable item.
Type string
// Network is a network this context belongs to.
Network netmode.Magic
// Verifiable is an object which can be (de-)serialized.
Verifiable crypto.VerifiableDecodable
// Items is a map from script hashes to context items.
@ -32,6 +35,7 @@ type ParameterContext struct {
type paramContext struct {
Type string `json:"type"`
Net uint32 `json:"network"`
Hex []byte `json:"hex"`
Items map[string]json.RawMessage `json:"items"`
}
@ -42,9 +46,10 @@ type sigWithIndex struct {
}
// NewParameterContext returns ParameterContext with the specified type and item to sign.
func NewParameterContext(typ string, verif crypto.VerifiableDecodable) *ParameterContext {
func NewParameterContext(typ string, network netmode.Magic, verif crypto.VerifiableDecodable) *ParameterContext {
return &ParameterContext{
Type: typ,
Network: network,
Verifiable: verif,
Items: make(map[util.Uint160]*Item),
}
@ -164,6 +169,7 @@ func (c ParameterContext) MarshalJSON() ([]byte, error) {
}
pc := &paramContext{
Type: c.Type,
Net: uint32(c.Network),
Hex: verif,
Items: items,
}
@ -181,7 +187,7 @@ func (c *ParameterContext) UnmarshalJSON(data []byte) error {
switch pc.Type {
case "Neo.Core.ContractTransaction":
tx := new(transaction.Transaction)
tx.Network = 42 // temporary, neo-project/neo#2393
tx.Network = netmode.Magic(pc.Net)
verif = tx
default:
return fmt.Errorf("unsupported type: %s", c.Type)
@ -203,6 +209,7 @@ func (c *ParameterContext) UnmarshalJSON(data []byte) error {
items[u] = item
}
c.Type = pc.Type
c.Network = netmode.Magic(pc.Net)
c.Verifiable = verif
c.Items = items
return nil

View file

@ -27,7 +27,7 @@ func TestParameterContext_AddSignatureSimpleContract(t *testing.T) {
sig := priv.Sign(tx.GetSignedPart())
t.Run("invalid contract", func(t *testing.T) {
c := NewParameterContext("Neo.Core.ContractTransaction", tx)
c := NewParameterContext("Neo.Core.ContractTransaction", netmode.UnitTestNet, tx)
ctr := &wallet.Contract{
Script: pub.GetVerificationScript(),
Parameters: []wallet.ContractParam{
@ -47,7 +47,7 @@ func TestParameterContext_AddSignatureSimpleContract(t *testing.T) {
}
})
c := NewParameterContext("Neo.Core.ContractTransaction", tx)
c := NewParameterContext("Neo.Core.ContractTransaction", netmode.UnitTestNet, tx)
ctr := &wallet.Contract{
Script: pub.GetVerificationScript(),
Parameters: []wallet.ContractParam{newParam(smartcontract.SignatureType, "parameter0")},
@ -77,7 +77,7 @@ func TestParameterContext_AddSignatureSimpleContract(t *testing.T) {
func TestParameterContext_AddSignatureMultisig(t *testing.T) {
tx := getContractTx()
c := NewParameterContext("Neo.Core.ContractTransaction", tx)
c := NewParameterContext("Neo.Core.ContractTransaction", netmode.UnitTestNet, tx)
privs, pubs := getPrivateKeys(t, 4)
pubsCopy := keys.PublicKeys(pubs).Copy()
script, err := smartcontract.CreateMultiSigRedeemScript(3, pubsCopy)
@ -137,6 +137,7 @@ func TestParameterContext_MarshalJSON(t *testing.T) {
expected := &ParameterContext{
Type: "Neo.Core.ContractTransaction",
Network: netmode.UnitTestNet,
Verifiable: tx,
Items: map[util.Uint160]*Item{
priv.GetScriptHash(): {