ScriptOp.fromAsm constructor
ScriptOp.fromAsm(
- String asm
Interpret a single script ASM string into a ScriptOp.
Implementation
factory ScriptOp.fromAsm(String asm) {
if (asm.isEmpty) throw InvalidScriptAsm();
// If it starts with OP_, then it is an opcode
if (asm.startsWith("OP_")) {
final code = scriptOpNameToCode[asm.substring(3)];
if (code == null) throw InvalidScriptAsm();
// Do not allow push data opcodes
if (code >= pushData1 && code <= pushData4) throw InvalidScriptAsm();
return ScriptOpCode(code);
}
// If "-1" or "81", then it is a 1NEGATE op code
if (asm == "-1" || asm == "81") return ScriptOpCode(op1Negate);
// If "80" then it is zero
if (asm == "80") return ScriptOpCode(0);
// If it is in the form of <n-bytes>, then it represents a push data matcher
// of n bytes
final nBytesStr = pushdataMatcherRegExp.firstMatch(asm)?.group(1);
if (nBytesStr != null) {
final n = int.tryParse(nBytesStr);
if (n == null || n == 0 || n > 0xffffffff) throw InvalidScriptAsm();
return ScriptPushDataMatcher(n);
}
// Otherwise assume hex. Provide opcode if available or else pushdata
late Uint8List bytes;
try {
bytes = hexToBytes(asm);
} on FormatException {
throw InvalidScriptAsm();
}
return bytes.length == 1 ? ScriptOp.fromNumber(bytes[0]) : ScriptPushData(bytes);
}