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() priv := acc.PrivateKey()
pub := priv.PublicKey() pub := priv.PublicKey()
sign := priv.Sign(tx.GetSignedPart()) 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) h, err := address.StringToUint160(acc.Address)
if err != nil { if err != nil {
return fmt.Errorf("invalid address: %s", acc.Address) return fmt.Errorf("invalid address: %s", acc.Address)

View file

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

View file

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