2020-04-13 12:56:41 +00:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
2020-06-09 09:27:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-04-13 12:56:41 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-04-30 13:00:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-04-13 12:56:41 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckHashedWitness checks given hash against current list of script hashes
|
|
|
|
// for verifying in the interop context.
|
2020-04-30 13:00:33 +00:00
|
|
|
func CheckHashedWitness(ic *interop.Context, v vm.ScriptHashGetter, hash util.Uint160) (bool, error) {
|
|
|
|
if tx, ok := ic.Container.(*transaction.Transaction); ok {
|
2020-06-09 09:27:53 +00:00
|
|
|
return checkScope(ic.DAO, tx, v, hash)
|
2020-04-30 13:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// only for non-Transaction types (Block, etc.)
|
2020-04-13 12:56:41 +00:00
|
|
|
hashes, err := ic.Chain.GetScriptHashesForVerifying(ic.Tx)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "failed to get script hashes")
|
|
|
|
}
|
|
|
|
for _, v := range hashes {
|
|
|
|
if hash.Equals(v) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:27:53 +00:00
|
|
|
func checkScope(d dao.DAO, tx *transaction.Transaction, v vm.ScriptHashGetter, hash util.Uint160) (bool, error) {
|
2020-04-30 13:00:33 +00:00
|
|
|
for _, c := range tx.Cosigners {
|
|
|
|
if c.Account == hash {
|
|
|
|
if c.Scopes == transaction.Global {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if c.Scopes&transaction.CalledByEntry != 0 {
|
|
|
|
callingScriptHash := v.GetCallingScriptHash()
|
|
|
|
entryScriptHash := v.GetEntryScriptHash()
|
|
|
|
if callingScriptHash == entryScriptHash {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.Scopes&transaction.CustomContracts != 0 {
|
|
|
|
currentScriptHash := v.GetCurrentScriptHash()
|
|
|
|
for _, allowedContract := range c.AllowedContracts {
|
|
|
|
if allowedContract == currentScriptHash {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.Scopes&transaction.CustomGroups != 0 {
|
2020-06-09 09:27:53 +00:00
|
|
|
callingScriptHash := v.GetCallingScriptHash()
|
|
|
|
if callingScriptHash.Equals(util.Uint160{}) {
|
2020-04-30 13:00:33 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2020-06-09 09:27:53 +00:00
|
|
|
cs, err := d.GetContractState(callingScriptHash)
|
2020-04-30 13:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// check if the current group is the required one
|
|
|
|
for _, allowedGroup := range c.AllowedGroups {
|
|
|
|
for _, group := range cs.Manifest.Groups {
|
|
|
|
if group.PublicKey.Equal(allowedGroup) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 09:27:53 +00:00
|
|
|
}
|
2020-04-30 13:00:33 +00:00
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-04-13 12:56:41 +00:00
|
|
|
// CheckKeyedWitness checks hash of signature check contract with a given public
|
|
|
|
// key against current list of script hashes for verifying in the interop context.
|
2020-04-30 13:00:33 +00:00
|
|
|
func CheckKeyedWitness(ic *interop.Context, v vm.ScriptHashGetter, key *keys.PublicKey) (bool, error) {
|
|
|
|
return CheckHashedWitness(ic, v, key.GetScriptHash())
|
2020-04-13 12:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckWitness checks witnesses.
|
|
|
|
func CheckWitness(ic *interop.Context, v *vm.VM) error {
|
|
|
|
var res bool
|
|
|
|
var err error
|
|
|
|
|
|
|
|
hashOrKey := v.Estack().Pop().Bytes()
|
|
|
|
hash, err := util.Uint160DecodeBytesBE(hashOrKey)
|
|
|
|
if err != nil {
|
|
|
|
var key *keys.PublicKey
|
|
|
|
key, err = keys.NewPublicKeyFromBytes(hashOrKey)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("parameter given is neither a key nor a hash")
|
|
|
|
}
|
2020-04-30 13:00:33 +00:00
|
|
|
res, err = CheckKeyedWitness(ic, v, key)
|
2020-04-13 12:56:41 +00:00
|
|
|
} else {
|
2020-04-30 13:00:33 +00:00
|
|
|
res, err = CheckHashedWitness(ic, v, hash)
|
2020-04-13 12:56:41 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to check")
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(res)
|
|
|
|
return nil
|
|
|
|
}
|