Refactor, Added documentation, moved from avj-cli to avj, update script and added tests

This commit is contained in:
Kuldeep
2026-04-19 09:33:48 +00:00
parent cfd4c6a43b
commit 013583e7f8
26 changed files with 4216 additions and 9970 deletions

View File

@@ -1,6 +1,13 @@
# @xo-cash/utils
XO utilities and parser
Utility functions for XO
- Template parsing, serialization, validation and schema generation
- Script identifier generation
- Template identifier generation
- Extended JSON
> ⚠️ This project is in **early development phase**. Use it only if you understand what you are doing and accept the risks. **Do not use funds you are not willing to lose.** Minor version bumps may introduce **breaking changes**
## Installation
@@ -11,9 +18,48 @@ npm install @xo-cash/utils
## Usage
```typescript
import { scriptToScriptHash } from '@xo-cash/utils';
import { parseTemplate, scriptToScriptHash, generateTemplateIdentifier } from '@xo-cash/utils';
```
## Scripts
| Command | Description |
| ---------------------------------- | ------------------------------------------------------ |
| `npm run build` | Compile TypeScript to `dist/` |
| `npm run generate-schema` | Generate `xo-template.schema.json` from the Zod schema |
| `npm run test` | Run tests with coverage |
| `npm run style` | Lint with ESLint (read-only) |
| `npm run syntax` | Type-check without emitting (`tsc --noEmit`) |
| `npm run format` | Auto-fix formatting and linting |
| `npm run docs` | Generate TypeDoc API docs to `public/` |
| `npm run spellcheck` | Spell-check source and test files |
| `npm audit --audit-level=moderate` | Performs npm audit |
## Templates
### Parsing
`parseTemplate` accepts any string OR XOTemplate object and returns a validated `XOTemplate` object. It rejects unknown
properties, missing required fields, and incorrect value types. Native bigint is accepted in satoshi
fields; string encoding is also accepted for values sourced from JSON where bigint is unavailable.
### How validation works
Validation uses a Zod schema defined in `source/template/schemas.ts`. The schema mirrors the
`XOTemplate` TypeScript type and rejects unknown keys at every level. When validation fails, a
`TemplateInvalidError` is thrown with a message that lists every failing field and the reason it
failed, so all problems are visible in one pass.
### JSON Schema export
A JSON Schema representation of the template can be generated by running:
```bash
npm run generate-schema
```
This writes `source/template/xo-template.schema.json`, which can be consumed by other tools. However, it's recommended to use functions provided by this library for XO template development thanks to the extended JSON support.
## Links
- [Repository](https://gitlab.com/GeneralProtocols/xo/utils)
@@ -21,4 +67,4 @@ import { scriptToScriptHash } from '@xo-cash/utils';
## License
MIT
[MIT](LICENSE)