core: fix contract-based verification script hash

When using contract-based verification it's important to load contract's hash
along with the script, otherwise it won't be valid.

Simplify things along the way.
This commit is contained in:
Roman Khimov 2020-11-26 22:45:51 +03:00
parent 0c7e727859
commit 49f6b33eae

View file

@ -1601,18 +1601,15 @@ var (
// initVerificationVM initializes VM for witness check.
func (bc *Blockchain) initVerificationVM(ic *interop.Context, hash util.Uint160, witness *transaction.Witness) error {
var offset int
var isNative bool
var initMD *manifest.Method
verification := witness.VerificationScript
flags := smartcontract.NoneFlag
if len(verification) != 0 {
v := ic.VM
if len(witness.VerificationScript) != 0 {
if witness.ScriptHash() != hash {
return ErrWitnessHashMismatch
}
if bc.contracts.ByHash(hash) != nil {
return ErrNativeContractWitness
}
v.LoadScriptWithFlags(witness.VerificationScript, smartcontract.NoneFlag)
} else {
cs, err := ic.DAO.GetContractState(hash)
if err != nil {
@ -1622,26 +1619,21 @@ func (bc *Blockchain) initVerificationVM(ic *interop.Context, hash util.Uint160,
if md == nil {
return ErrInvalidVerificationContract
}
verification = cs.Script
offset = md.Offset
initMD = cs.Manifest.ABI.GetMethod(manifest.MethodInit)
isNative = cs.ID < 0
flags = smartcontract.AllowStates
}
initMD := cs.Manifest.ABI.GetMethod(manifest.MethodInit)
v.LoadScriptWithHash(cs.Script, hash, smartcontract.AllowStates)
v.Jump(v.Context(), md.Offset)
v := ic.VM
v.LoadScriptWithFlags(verification, flags)
v.Jump(v.Context(), offset)
if isNative {
w := io.NewBufBinWriter()
emit.Opcodes(w.BinWriter, opcode.DEPTH, opcode.PACK)
emit.String(w.BinWriter, manifest.MethodVerify)
if w.Err != nil {
return w.Err
if cs.ID < 0 {
w := io.NewBufBinWriter()
emit.Opcodes(w.BinWriter, opcode.DEPTH, opcode.PACK)
emit.String(w.BinWriter, manifest.MethodVerify)
if w.Err != nil {
return w.Err
}
v.LoadScript(w.Bytes())
} else if initMD != nil {
v.Call(v.Context(), initMD.Offset)
}
v.LoadScript(w.Bytes())
} else if initMD != nil {
v.Call(v.Context(), initMD.Offset)
}
v.LoadScript(witness.InvocationScript)
return nil