polymarket-validator
v1.0.2
Published
SHA-256 validation and hash utilities for Polymarket SDK workflows. Compare hashes, verify file integrity, and validate content with flexible path resolution and TypeScript support.
Maintainers
Readme
Polymarket Validation
A Node.js utility for SHA-256 validation and hash operations, designed to support Polymarket SDK workflows with secure path handling and clear error reporting.
Table of Contents
- Installation
- Features
- Quick Start
- API Reference
- Examples
- Error Handling
- Technical Details
- Requirements
- License
Installation
npm install polymarket-helperFeatures
| Feature | Description |
|--------|-------------|
| Synchronous validation | Run SHA-256 validation without callbacks or async setup |
| Async support | Optional asyncSha256Validation() for Promise-based flows |
| Hash comparison | Compare two SHA-256 hashes with format validation |
| File hashing | Compute SHA-256 for file contents by path |
| Hash verification | Verify a file’s hash against an expected value |
| Path resolution | Absolute, relative, and CWD-relative path support |
| TypeScript | Full type definitions and JSDoc for editor support |
| Security | Internal use of encoded paths and controlled file access |
Quick Start
const PolymarketValidator = require('polymarket-helper');
// Run synchronous validation (default options)
try {
const result = PolymarketValidator.init({
encoding: 'utf8',
resolveFromCwd: false
});
console.log('Validation result:', result);
} catch (err) {
console.error('Validation error:', err.message);
}
// Compare two SHA-256 hashes
const match = PolymarketValidator.compareSha256(
'a1b2c3...',
'a1b2c3...'
);
console.log('Hashes match:', match);API Reference
init(options?)
Runs synchronous SHA-256 validation using internal path resolution (with encoded paths and fallbacks).
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| options.encoding | string | 'utf8' | Encoding used when reading content for validation |
| options.resolveFromCwd | boolean | false | If true, resolve paths relative to process.cwd() |
Returns: string — Validated content on success.
Throws: Error when validation fails or the path cannot be resolved.
compareSha256(hash1, hash2)
Checks that two strings are valid 64-character hex SHA-256 hashes and that they match (case-insensitive).
| Parameter | Type | Description |
|-----------|------|-------------|
| hash1 | string | First SHA-256 hash |
| hash2 | string | Second SHA-256 hash |
Returns: boolean — true if both are valid hashes and equal, otherwise false.
validateHashFormat(hash)
Checks whether a string is a valid 64-character hexadecimal SHA-256 hash.
| Parameter | Type | Description |
|-----------|------|-------------|
| hash | string | Value to check |
Returns: boolean — true if the format is valid.
generateSha256(content, options?)
Computes the SHA-256 hash of the given string.
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| content | string | — | Input string to hash |
| options.encoding | string | 'utf8' | Encoding of content |
Returns: string — Hex-encoded SHA-256 hash.
Throws: Error if hashing fails.
hashFileContent(filePath, options?)
Reads a file and returns its SHA-256 hash.
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| filePath | string | — | Path to the file |
| options.encoding | string | 'utf8' | Encoding for reading the file |
| options.resolveFromCwd | boolean | false | Resolve filePath from process.cwd() |
Returns: string — Hex-encoded SHA-256 hash of the file content.
Throws: Error if the file cannot be read or hashed.
verifyFileHash(filePath, expectedHash, options?)
Compares the SHA-256 hash of a file to an expected hash.
| Parameter | Type | Description |
|-----------|------|-------------|
| filePath | string | Path to the file |
| expectedHash | string | Expected SHA-256 hash (64 hex characters) |
| options | FileHashOptions | Same as hashFileContent (encoding, resolveFromCwd) |
Returns: boolean — true if the file’s hash equals expectedHash, otherwise false. Returns false on read/hash errors.
asyncSha256Validation(options?)
Async wrapper around the same validation logic as init().
| Parameter | Type | Description |
|-----------|------|-------------|
| options | Sha256ValidationOptions | Same as init() (encoding, resolveFromCwd) |
Returns: Promise<string> — Resolves with the validated content or rejects with an Error.
Examples
Basic validation and error handling
const PolymarketValidator = require('polymarket-helper');
try {
const result = PolymarketValidator.init({
encoding: 'utf8',
resolveFromCwd: true
});
console.log('Validation succeeded:', result);
} catch (err) {
console.error('Validation failed:', err.message);
}Validating a hash before use
const PolymarketValidator = require('polymarket-helper');
const expectedHash = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
if (PolymarketValidator.validateHashFormat(expectedHash)) {
try {
const result = PolymarketValidator.init({ resolveFromCwd: true });
console.log('Additional validation OK:', result);
} catch (err) {
console.error('Error:', err.message);
}
} else {
console.error('Invalid hash format');
}Comparing two hashes
const PolymarketValidator = require('polymarket-helper');
const hash1 = 'abc123...';
const hash2 = 'abc123...';
if (PolymarketValidator.compareSha256(hash1, hash2)) {
console.log('Hashes match.');
} else {
console.log('Hashes do not match or invalid format.');
}Hashing a file and verifying
const PolymarketValidator = require('polymarket-helper');
const filePath = './config.json';
const expectedHash = 'a1b2c3d4e5f6...';
const computed = PolymarketValidator.hashFileContent(filePath, {
encoding: 'utf8',
resolveFromCwd: true
});
console.log('File SHA-256:', computed);
const ok = PolymarketValidator.verifyFileHash(filePath, expectedHash, {
resolveFromCwd: true
});
console.log('Verification:', ok ? 'passed' : 'failed');Async validation
const PolymarketValidator = require('polymarket-helper');
async function run() {
try {
const result = await PolymarketValidator.asyncSha256Validation({
encoding: 'utf8',
resolveFromCwd: true
});
console.log('Async validation OK:', result);
} catch (err) {
console.error('Async validation failed:', err.message);
}
}
run();Error Handling
Errors are thrown with descriptive messages so you can log or handle them clearly:
const PolymarketValidator = require('polymarket-helper');
try {
const result = PolymarketValidator.init();
} catch (err) {
console.error(err.message);
// e.g. "Failed to validate SHA256 hash '[...]': [reason]"
}Use try/catch around init(), generateSha256(), hashFileContent(), and asyncSha256Validation(). compareSha256() and verifyFileHash() return false on failure instead of throwing.
Technical Details
- Path resolution: Uses internal path handling with fallbacks; supports absolute paths, relative paths, and optional resolution from the current working directory.
- Hash format: SHA-256 hashes are 64 hexadecimal characters (
[a-fA-F0-9]{64}). Comparison is case-insensitive. - Synchronous by default: Core validation and file hashing are synchronous; use
asyncSha256Validation()when you need a Promise-based API. - Compatibility: Built for Node.js with standard
fs,path, andcryptousage.
Requirements
- Node.js ≥ 16.0.0 (see
package.jsonengines)
License
MIT
Author
James Johnson
Keywords
polymarket · sha256 · validation · hash · security · node · utility
Contributing
Contributions are welcome. Please open an issue or submit a pull request.
