62 lines
2.8 KiB
TypeScript
62 lines
2.8 KiB
TypeScript
/**
|
|
* Detects whether a string is a pure CashAssembly expression.
|
|
*
|
|
* CashAssembly expressions look like `$(<variable>)` or `$(<a> <b>)`. This pattern checks
|
|
* that the entire string is one such expression and nothing else. It will not match if
|
|
* there is other text surrounding the expression.
|
|
*
|
|
* For example:
|
|
* `$(<fee>)` matches (a full expression)
|
|
* `OP_DUP $(<fee>)` does not match (extra text before it)
|
|
* `$()` does not match (empty expression)
|
|
*/
|
|
export const CASHASSEMBLY_EXPRESSION_PATTERN = /^\$\([^)]+\)$/;
|
|
|
|
/**
|
|
* Finds all CashAssembly evaluations embedded in a larger string.
|
|
*
|
|
* An evaluation looks like `$(...)`, for example `$(<fee>)` or `$(<a> <b>)`. This pattern
|
|
* locates every occurrence in the input and returns them all (global flag `g`).
|
|
* Empty evaluations `$()` are intentionally excluded because they reference no variables.
|
|
*
|
|
* For example, scanning `"OP_DUP <$(<pubkeyHash>)> OP_HASH160 $(<fee>)"` would return
|
|
* `['$(<pubkeyHash>)', '$(<fee>)']`.
|
|
*/
|
|
export const CASHASSEMBLY_EVALUATION_PATTERN = /\$\([^)]+\)/g;
|
|
|
|
/**
|
|
* Extracts variable names from angle-bracket references inside a CashAssembly evaluation.
|
|
*
|
|
* Inside an evaluation like `$(<pubkeyHash> <fee>)`, variables are referenced as `<name>`.
|
|
* This pattern captures the name between the brackets. The global flag `g` allows iterating
|
|
* over every variable reference in a single evaluation string.
|
|
*
|
|
* For example, running this against `$(<pubkeyHash> <fee>)` would return the variable names
|
|
* `["pubkeyHash", "fee"]`.
|
|
*/
|
|
export const CASHASSEMBLY_VARIABLE_PATTERN = /<([^>]+)>/g;
|
|
|
|
/**
|
|
* Identifies CashAssembly literal tokens that appear inside angle-bracket push statements.
|
|
*
|
|
* Inside an evaluation, not everything between `<` and `>` is a variable name. Literals are
|
|
* also valid push contents: numeric literals (e.g. `<0>`, `<32>`), hex literals (`<0x02>`),
|
|
* binary literals (`<0b1010>`), and string literals (`<"minting">`, `<'hello'>`). This pattern
|
|
* matches any captured token that starts with a digit or a quote character.
|
|
*/
|
|
export const CASHASSEMBLY_LITERAL_TOKEN_PATTERN = /^[0-9"']/;
|
|
|
|
/**
|
|
* Matches a single dot variable method reference inside an angle-bracket identifier.
|
|
*
|
|
* Used to detect primitive method references such as `expiry.toIso8601`.
|
|
*
|
|
* For example:
|
|
* `expiry.toIso8601` matches (base `expiry`, method `toIso8601`)
|
|
* `requestedSatoshis` does not match (no method)
|
|
* `key.schnorr_signature.all_outputs` does not match (more than one dot)
|
|
* `key.public_key` matches the pattern shape but it is only resolved
|
|
* when `hint` maps to a primitive
|
|
*/
|
|
export const CASHASSEMBLY_VARIABLE_METHOD_REFERENCE_PATTERN = /^([^.]+)\.([^.]+)$/;
|