Function validateScriptSyntax

Validates the set of given opcodes syntactically, but not semantically. Each opcode is checked if it's one of the known opcodes, and any push opcodes have payloads checked for size constraints.

string validateScriptSyntax (
  in const(ScriptType) type,
  in const(ubyte[]) opcodes,
  in const(ulong) StackMaxItemSize,
  out Script script
) pure nothrow @nogc @safe;

The semantics of the script are not checked here. This responsibility lies within the script execution engine.

Lock scripts may contain any of the supported opcodes, whereas unlocks scripts may only consist of stack push opcodes. This is for security reasons. If any opcodes were allowed, the unlock script could potentially cause premature successfull script evaluation without satisfying the constraints of the lock script.

Redeem scripts are treated the same as lock scripts.

Parameters

NameDescription
type the type of the script (lock / unlock / redeem)
opcodes the set of opcodes to validate
StackMaxItemSize maximum allowed payload size for a stack push operation
script will contain the validated Script if there were no errors

Returns

null if the set of opcodes are syntactically valid, otherwise the string explaining the reason why they're invalid

Example

import agora.crypto.Schnorr;
import agora.utils.Test;

Script result;
Pair kp = Pair.random();
auto sig = sign(kp, "Hello world");

// sanity checks
const key_hash = hashFull(kp.V);
Script lock_script = createLockP2PKH(key_hash);
assert(validateScriptSyntax(ScriptType.Lock, lock_script[], 512, result) is null);
Script unlock_script = createUnlockP2PKH(sig, SigHash.All, kp.V);
assert(validateScriptSyntax(ScriptType.Unlock, unlock_script[], 512, result)
    is null);