@otskit/client 0.1.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -36,6 +36,7 @@
36
36
  - **Tree-shakeable** — Dual ESM/CJS build, zero runtime dependencies
37
37
  - **`AbortController` support** — Cancel any in-flight operation at any level
38
38
  - **Observable** — Drop-in `Logger` interface compatible with `console`, `pino`, `winston`, etc.
39
+ - **Built-in SHA-256 helpers** — `hashFile()` and `hashBuffer()` so you don't need to wire up `crypto` yourself
39
40
 
40
41
  ---
41
42
 
@@ -56,15 +57,13 @@ npm install @otskit/client
56
57
  ## Quick Start
57
58
 
58
59
  ```typescript
59
- import { OpenTimestampsClient } from '@otskit/client'
60
- import { createHash } from 'crypto'
61
- import { readFileSync, writeFileSync } from 'fs'
60
+ import { OpenTimestampsClient, hashFile } from '@otskit/client'
61
+ import { writeFileSync } from 'fs'
62
62
 
63
63
  const client = new OpenTimestampsClient()
64
64
 
65
65
  // 1. Hash the file you want to timestamp
66
- const fileBytes = readFileSync('contract.pdf')
67
- const hash = createHash('sha256').update(fileBytes).digest()
66
+ const hash = await hashFile('contract.pdf')
68
67
 
69
68
  // 2. Submit to calendars → get a pending .ots proof
70
69
  const pendingProof = await client.stamp(hash)
@@ -89,20 +88,33 @@ if (result.valid) {
89
88
 
90
89
  ## Usage
91
90
 
91
+ ### Hashing files and data
92
+
93
+ Use the built-in helpers to compute SHA-256 without importing `crypto` yourself:
94
+
95
+ ```typescript
96
+ import { hashFile, hashBuffer } from '@otskit/client'
97
+
98
+ // From a file path (streaming — safe for large files)
99
+ const hash = await hashFile('contract.pdf')
100
+
101
+ // From bytes already in memory
102
+ const hash = hashBuffer(Buffer.from('hello world'))
103
+ const hash = hashBuffer(new Uint8Array([...]))
104
+ ```
105
+
106
+ Both return a 32-byte `Buffer` ready to pass directly to `stamp()`.
107
+
92
108
  ### Stamping data
93
109
 
94
110
  `stamp()` accepts either a 32-byte `Buffer` or a 64-character hex string:
95
111
 
96
112
  ```typescript
97
- import { createHash } from 'crypto'
98
-
99
- // From a Buffer
100
- const hashBuffer = createHash('sha256').update(fileBytes).digest()
101
- const proof = await client.stamp(hashBuffer)
113
+ // From hashFile / hashBuffer
114
+ const proof = await client.stamp(await hashFile('contract.pdf'))
102
115
 
103
- // From a hex string
104
- const hashHex = createHash('sha256').update(fileBytes).digest('hex')
105
- const proof = await client.stamp(hashHex)
116
+ // Or a hex string
117
+ const proof = await client.stamp('a'.repeat(64))
106
118
  ```
107
119
 
108
120
  Internally, `stamp()` prepends a 16-byte cryptographic nonce to each submission, builds a Merkle tree over all concurrent submissions, and serializes the result as a standard `.ots` file.
@@ -396,6 +408,31 @@ All errors extend `OpenTimestampsClientError extends Error`.
396
408
 
397
409
  ---
398
410
 
411
+ ### Utility functions
412
+
413
+ #### `hashFile(path): Promise<Buffer>`
414
+
415
+ Returns the SHA-256 hash of a file as a 32-byte `Buffer`. Reads the file as a stream — safe for large files.
416
+
417
+ ```typescript
418
+ import { hashFile } from '@otskit/client'
419
+
420
+ const hash = await hashFile('contract.pdf')
421
+ const proof = await client.stamp(hash)
422
+ ```
423
+
424
+ #### `hashBuffer(data): Buffer`
425
+
426
+ Returns the SHA-256 hash of a `Buffer` or `Uint8Array` synchronously.
427
+
428
+ ```typescript
429
+ import { hashBuffer } from '@otskit/client'
430
+
431
+ const hash = hashBuffer(Buffer.from('my data'))
432
+ ```
433
+
434
+ ---
435
+
399
436
  ### Advanced exports
400
437
 
401
438
  These are available for custom integrations and advanced use cases.