@optimystic/quereus-plugin-crypto 0.2.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 +279 -0
- package/dist/index.d.ts +317 -0
- package/dist/index.js +474 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin.d.ts +42 -0
- package/dist/plugin.js +212 -0
- package/dist/plugin.js.map +1 -0
- package/package.json +71 -0
- package/src/crypto.ts +335 -0
- package/src/digest.ts +173 -0
- package/src/index.ts +33 -0
- package/src/plugin.ts +87 -0
- package/src/sign.ts +245 -0
- package/src/signature-valid.ts +279 -0
package/README.md
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# @optimystic/quereus-plugin-crypto
|
|
2
|
+
|
|
3
|
+
Quereus plugin providing cryptographic functions for SQL queries with base64url encoding by default.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Hash Functions**: SHA-256, SHA-512, BLAKE3 hashing with base64url output
|
|
8
|
+
- **Hash Modulo**: Fixed-size hash values (16-bit, 32-bit, etc.) for sharding and partitioning
|
|
9
|
+
- **Random Bytes**: Generate cryptographically secure random bytes (default: 256 bits)
|
|
10
|
+
- **Signature Functions**: secp256k1, P-256, Ed25519 signing with base64url encoding
|
|
11
|
+
- **Verification Functions**: Signature verification for all supported curves
|
|
12
|
+
- **Key Generation**: Generate and derive cryptographic keys
|
|
13
|
+
- **Multiple Encodings**: Support for base64url, base64, hex, utf8, and raw bytes
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @optimystic/quereus-plugin-crypto
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### As a Quereus Plugin
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { Database } from '@quereus/quereus';
|
|
27
|
+
import { loadPlugin } from '@quereus/quereus/util/plugin-loader.js';
|
|
28
|
+
|
|
29
|
+
const db = new Database();
|
|
30
|
+
await loadPlugin('npm:@optimystic/quereus-plugin-crypto', db);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Direct Import (for JavaScript/TypeScript code)
|
|
34
|
+
|
|
35
|
+
All cryptographic functions are also exported directly so you can use the same implementations in your JavaScript code:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import {
|
|
39
|
+
digest,
|
|
40
|
+
hashMod,
|
|
41
|
+
randomBytes,
|
|
42
|
+
sign,
|
|
43
|
+
verify,
|
|
44
|
+
generatePrivateKey,
|
|
45
|
+
getPublicKey
|
|
46
|
+
} from '@optimystic/quereus-plugin-crypto';
|
|
47
|
+
|
|
48
|
+
// Hash data (base64url by default)
|
|
49
|
+
const hash = digest('hello world', 'sha256', 'utf8', 'base64url');
|
|
50
|
+
|
|
51
|
+
// Get a 16-bit hash for sharding
|
|
52
|
+
const shard = hashMod('user@example.com', 16, 'sha256', 'utf8');
|
|
53
|
+
|
|
54
|
+
// Generate random bytes (256 bits by default)
|
|
55
|
+
const nonce = randomBytes(256, 'base64url');
|
|
56
|
+
const salt = randomBytes(128, 'hex');
|
|
57
|
+
|
|
58
|
+
// Generate keys
|
|
59
|
+
const privateKey = generatePrivateKey('secp256k1', 'base64url');
|
|
60
|
+
const publicKey = getPublicKey(privateKey, 'secp256k1', 'base64url', 'base64url');
|
|
61
|
+
|
|
62
|
+
// Sign and verify
|
|
63
|
+
const signature = sign(hash, privateKey, 'secp256k1', 'base64url', 'base64url', 'base64url');
|
|
64
|
+
const isValid = verify(hash, signature, publicKey, 'secp256k1', 'base64url', 'base64url', 'base64url');
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## SQL Functions
|
|
68
|
+
|
|
69
|
+
All SQL functions use **base64url encoding by default** for inputs and outputs. This is URL-safe and SQL-friendly.
|
|
70
|
+
|
|
71
|
+
### digest(data, algorithm?, inputEncoding?, outputEncoding?)
|
|
72
|
+
|
|
73
|
+
Hash data using SHA-256 (default), SHA-512, or BLAKE3.
|
|
74
|
+
|
|
75
|
+
```sql
|
|
76
|
+
-- Hash base64url data (default)
|
|
77
|
+
SELECT digest('aGVsbG8gd29ybGQ') as hash;
|
|
78
|
+
|
|
79
|
+
-- Hash UTF-8 text
|
|
80
|
+
SELECT digest('hello world', 'sha256', 'utf8', 'base64url') as hash;
|
|
81
|
+
|
|
82
|
+
-- Hash with SHA-512
|
|
83
|
+
SELECT digest('data', 'sha512', 'utf8', 'base64url') as sha512_hash;
|
|
84
|
+
|
|
85
|
+
-- Hash with BLAKE3, output as hex
|
|
86
|
+
SELECT digest('data', 'blake3', 'utf8', 'hex') as blake3_hex;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### hash_mod(data, bits, algorithm?, inputEncoding?)
|
|
90
|
+
|
|
91
|
+
Hash data and return modulo 2^bits for fixed-size hash values.
|
|
92
|
+
|
|
93
|
+
```sql
|
|
94
|
+
-- Get 16-bit hash (0-65535) for sharding
|
|
95
|
+
SELECT hash_mod('user@example.com', 16, 'sha256', 'utf8') as shard_id;
|
|
96
|
+
|
|
97
|
+
-- Get 32-bit hash
|
|
98
|
+
SELECT hash_mod('session_token', 32, 'sha256', 'utf8') as hash32;
|
|
99
|
+
|
|
100
|
+
-- Use with base64url input
|
|
101
|
+
SELECT hash_mod('aGVsbG8', 16) as hash16;
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### random_bytes(bits?, encoding?)
|
|
105
|
+
|
|
106
|
+
Generate cryptographically secure random bytes.
|
|
107
|
+
|
|
108
|
+
```sql
|
|
109
|
+
-- Generate 256 bits (32 bytes) of random data (default)
|
|
110
|
+
SELECT random_bytes() as nonce;
|
|
111
|
+
|
|
112
|
+
-- Generate 128 bits as hex
|
|
113
|
+
SELECT random_bytes(128, 'hex') as salt;
|
|
114
|
+
|
|
115
|
+
-- Generate 512 bits as base64url
|
|
116
|
+
SELECT random_bytes(512, 'base64url') as token;
|
|
117
|
+
|
|
118
|
+
-- Generate 64 bits for a random ID
|
|
119
|
+
SELECT random_bytes(64) as random_id;
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### sign(data, privateKey, curve?, inputEncoding?, keyEncoding?, outputEncoding?)
|
|
123
|
+
|
|
124
|
+
Sign data using secp256k1 (default), P-256, or Ed25519.
|
|
125
|
+
|
|
126
|
+
```sql
|
|
127
|
+
-- Sign with secp256k1 (Bitcoin/Ethereum)
|
|
128
|
+
SELECT sign('aGVsbG8', 'cHJpdmF0ZUtleQ') as signature;
|
|
129
|
+
|
|
130
|
+
-- Sign UTF-8 text
|
|
131
|
+
SELECT sign('hello', 'cHJpdmF0ZUtleQ', 'secp256k1', 'utf8') as signature;
|
|
132
|
+
|
|
133
|
+
-- Sign with P-256 (NIST)
|
|
134
|
+
SELECT sign('data', 'cHJpdmF0ZUtleQ', 'p256', 'utf8') as p256_sig;
|
|
135
|
+
|
|
136
|
+
-- Sign with Ed25519
|
|
137
|
+
SELECT sign('message', 'cHJpdmF0ZUtleQ', 'ed25519', 'utf8') as ed25519_sig;
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### verify(data, signature, publicKey, curve?, inputEncoding?, sigEncoding?, keyEncoding?)
|
|
141
|
+
|
|
142
|
+
Verify signatures. Returns 1 for valid, 0 for invalid.
|
|
143
|
+
|
|
144
|
+
```sql
|
|
145
|
+
-- Verify secp256k1 signature
|
|
146
|
+
SELECT verify('aGVsbG8', 'c2lnbmF0dXJl', 'cHVibGljS2V5') as is_valid;
|
|
147
|
+
|
|
148
|
+
-- Verify UTF-8 text signature
|
|
149
|
+
SELECT verify('hello', 'c2lnbmF0dXJl', 'cHVibGljS2V5', 'secp256k1', 'utf8') as is_valid;
|
|
150
|
+
|
|
151
|
+
-- Verify P-256 signature
|
|
152
|
+
SELECT verify('data', 'c2lnbmF0dXJl', 'cHVibGljS2V5', 'p256', 'utf8') as is_valid;
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Supported Algorithms
|
|
156
|
+
|
|
157
|
+
### Hash Algorithms
|
|
158
|
+
- `sha256` - SHA-256 (default) - 256-bit secure hash
|
|
159
|
+
- `sha512` - SHA-512 - 512-bit secure hash
|
|
160
|
+
- `blake3` - BLAKE3 - Fast, secure, parallel hash
|
|
161
|
+
|
|
162
|
+
### Elliptic Curves
|
|
163
|
+
- `secp256k1` - Bitcoin/Ethereum curve (default)
|
|
164
|
+
- `p256` - NIST P-256 curve (secp256r1)
|
|
165
|
+
- `ed25519` - Edwards curve for EdDSA
|
|
166
|
+
|
|
167
|
+
### Encoding Formats
|
|
168
|
+
- `base64url` - URL-safe base64 without padding (default)
|
|
169
|
+
- `base64` - Standard base64 encoding
|
|
170
|
+
- `hex` - Hexadecimal encoding
|
|
171
|
+
- `utf8` - UTF-8 text encoding
|
|
172
|
+
- `bytes` - Raw Uint8Array (JavaScript only)
|
|
173
|
+
|
|
174
|
+
## Why base64url?
|
|
175
|
+
|
|
176
|
+
Base64url is the default encoding because it's:
|
|
177
|
+
- **URL-safe**: No `+`, `/`, or `=` characters
|
|
178
|
+
- **SQL-friendly**: No special characters that need escaping
|
|
179
|
+
- **Compact**: More efficient than hex (33% smaller)
|
|
180
|
+
- **Standard**: Defined in RFC 4648
|
|
181
|
+
|
|
182
|
+
## Complete Example
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
import { Database } from '@quereus/quereus';
|
|
186
|
+
import { loadPlugin } from '@quereus/quereus/util/plugin-loader.js';
|
|
187
|
+
import {
|
|
188
|
+
digest,
|
|
189
|
+
randomBytes,
|
|
190
|
+
sign,
|
|
191
|
+
verify,
|
|
192
|
+
generatePrivateKey,
|
|
193
|
+
getPublicKey
|
|
194
|
+
} from '@optimystic/quereus-plugin-crypto';
|
|
195
|
+
|
|
196
|
+
// Load plugin
|
|
197
|
+
const db = new Database();
|
|
198
|
+
await loadPlugin('npm:@optimystic/quereus-plugin-crypto', db);
|
|
199
|
+
|
|
200
|
+
// Use in SQL
|
|
201
|
+
db.exec(`
|
|
202
|
+
CREATE TABLE users (
|
|
203
|
+
id INTEGER PRIMARY KEY,
|
|
204
|
+
email TEXT,
|
|
205
|
+
shard_id INTEGER AS (hash_mod(email, 16, 'sha256', 'utf8')),
|
|
206
|
+
nonce TEXT DEFAULT (random_bytes(256))
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
INSERT INTO users (id, email) VALUES (1, 'alice@example.com');
|
|
210
|
+
|
|
211
|
+
SELECT id, email, shard_id, nonce FROM users;
|
|
212
|
+
`);
|
|
213
|
+
|
|
214
|
+
// Use in JavaScript
|
|
215
|
+
const privateKey = generatePrivateKey('secp256k1', 'base64url');
|
|
216
|
+
const publicKey = getPublicKey(privateKey);
|
|
217
|
+
|
|
218
|
+
const message = 'Hello, World!';
|
|
219
|
+
const hash = digest(message, 'sha256', 'utf8', 'base64url');
|
|
220
|
+
const signature = sign(hash, privateKey);
|
|
221
|
+
const isValid = verify(hash, signature, publicKey);
|
|
222
|
+
|
|
223
|
+
console.log('Valid signature:', isValid); // true
|
|
224
|
+
|
|
225
|
+
// Generate random nonce
|
|
226
|
+
const nonce = randomBytes(256, 'base64url');
|
|
227
|
+
console.log('Random nonce:', nonce);
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## JavaScript API Reference
|
|
231
|
+
|
|
232
|
+
### digest(data, algorithm?, inputEncoding?, outputEncoding?)
|
|
233
|
+
Hash data using SHA-256, SHA-512, or BLAKE3.
|
|
234
|
+
- **Returns**: Hash as string (or Uint8Array if encoding is 'bytes')
|
|
235
|
+
|
|
236
|
+
### hashMod(data, bits, algorithm?, inputEncoding?)
|
|
237
|
+
Hash data and return modulo 2^bits for fixed-size hash values.
|
|
238
|
+
- **Returns**: Number between 0 and 2^bits - 1
|
|
239
|
+
|
|
240
|
+
### randomBytes(bits?, encoding?)
|
|
241
|
+
Generate cryptographically secure random bytes.
|
|
242
|
+
- **Default**: 256 bits, base64url encoding
|
|
243
|
+
- **Returns**: Random bytes as string (or Uint8Array if encoding is 'bytes')
|
|
244
|
+
|
|
245
|
+
### sign(data, privateKey, curve?, inputEncoding?, keyEncoding?, outputEncoding?)
|
|
246
|
+
Sign data using ECC (secp256k1, P-256, or Ed25519).
|
|
247
|
+
- **Returns**: Signature as string (or Uint8Array if encoding is 'bytes')
|
|
248
|
+
|
|
249
|
+
### verify(data, signature, publicKey, curve?, inputEncoding?, sigEncoding?, keyEncoding?)
|
|
250
|
+
Verify an ECC signature.
|
|
251
|
+
- **Returns**: Boolean (true if valid, false if invalid)
|
|
252
|
+
|
|
253
|
+
### generatePrivateKey(curve?, encoding?)
|
|
254
|
+
Generate a random private key for the specified curve.
|
|
255
|
+
- **Returns**: Private key as string (or Uint8Array if encoding is 'bytes')
|
|
256
|
+
|
|
257
|
+
### getPublicKey(privateKey, curve?, keyEncoding?, outputEncoding?)
|
|
258
|
+
Derive the public key from a private key.
|
|
259
|
+
- **Returns**: Public key as string (or Uint8Array if encoding is 'bytes')
|
|
260
|
+
|
|
261
|
+
## Security Notes
|
|
262
|
+
|
|
263
|
+
- All cryptographic operations use audited libraries (@noble/*)
|
|
264
|
+
- Private keys should be handled securely and never exposed
|
|
265
|
+
- Random number generation uses platform CSPRNG (crypto.getRandomValues)
|
|
266
|
+
- Signatures use deterministic k-generation (RFC 6979)
|
|
267
|
+
- All algorithms provide industry-standard security levels
|
|
268
|
+
|
|
269
|
+
## React Native Compatibility
|
|
270
|
+
|
|
271
|
+
These functions are fully compatible with React Native and don't require any native modules. They use:
|
|
272
|
+
- `@noble/curves` for elliptic curve operations
|
|
273
|
+
- `@noble/hashes` for hashing
|
|
274
|
+
- `crypto.getRandomValues()` for secure randomness (polyfill required for React Native)
|
|
275
|
+
|
|
276
|
+
## License
|
|
277
|
+
|
|
278
|
+
MIT
|
|
279
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cryptographic Functions for Quereus
|
|
3
|
+
*
|
|
4
|
+
* Idiomatic ES module exports with base64url as default encoding.
|
|
5
|
+
* All functions accept and return base64url strings by default for SQL compatibility.
|
|
6
|
+
*/
|
|
7
|
+
type HashAlgorithm$1 = 'sha256' | 'sha512' | 'blake3';
|
|
8
|
+
type CurveType$2 = 'secp256k1' | 'p256' | 'ed25519';
|
|
9
|
+
type Encoding = 'base64url' | 'base64' | 'hex' | 'utf8' | 'bytes';
|
|
10
|
+
/**
|
|
11
|
+
* Compute hash digest of input data
|
|
12
|
+
*
|
|
13
|
+
* @param data - Data to hash (base64url string or Uint8Array)
|
|
14
|
+
* @param algorithm - Hash algorithm (default: 'sha256')
|
|
15
|
+
* @param inputEncoding - Encoding of input string (default: 'base64url')
|
|
16
|
+
* @param outputEncoding - Encoding of output (default: 'base64url')
|
|
17
|
+
* @returns Hash digest in specified encoding
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Hash UTF-8 text, output as base64url
|
|
22
|
+
* const hash = digest('hello world', 'sha256', 'utf8');
|
|
23
|
+
*
|
|
24
|
+
* // Hash base64url data with SHA-512
|
|
25
|
+
* const hash2 = digest('SGVsbG8', 'sha512');
|
|
26
|
+
*
|
|
27
|
+
* // Get raw bytes
|
|
28
|
+
* const bytes = digest('data', 'blake3', 'utf8', 'bytes');
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare function digest(data: string | Uint8Array, algorithm?: HashAlgorithm$1, inputEncoding?: Encoding, outputEncoding?: Encoding): string | Uint8Array;
|
|
32
|
+
/**
|
|
33
|
+
* Hash data and return modulo of specified bit length
|
|
34
|
+
* Useful for generating fixed-size hash values (e.g., 16-bit, 32-bit)
|
|
35
|
+
*
|
|
36
|
+
* @param data - Data to hash
|
|
37
|
+
* @param bits - Number of bits for the result (e.g., 16 for 16-bit hash)
|
|
38
|
+
* @param algorithm - Hash algorithm (default: 'sha256')
|
|
39
|
+
* @param inputEncoding - Encoding of input string (default: 'base64url')
|
|
40
|
+
* @returns Integer hash value modulo 2^bits
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // Get 16-bit hash (0-65535)
|
|
45
|
+
* const hash16 = hashMod('hello', 16, 'sha256', 'utf8');
|
|
46
|
+
*
|
|
47
|
+
* // Get 32-bit hash
|
|
48
|
+
* const hash32 = hashMod('world', 32, 'sha256', 'utf8');
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function hashMod(data: string | Uint8Array, bits: number, algorithm?: HashAlgorithm$1, inputEncoding?: Encoding): number;
|
|
52
|
+
/**
|
|
53
|
+
* Sign data with a private key
|
|
54
|
+
*
|
|
55
|
+
* @param data - Data to sign (typically a hash)
|
|
56
|
+
* @param privateKey - Private key (base64url string or Uint8Array)
|
|
57
|
+
* @param curve - Elliptic curve (default: 'secp256k1')
|
|
58
|
+
* @param inputEncoding - Encoding of data input (default: 'base64url')
|
|
59
|
+
* @param keyEncoding - Encoding of private key (default: 'base64url')
|
|
60
|
+
* @param outputEncoding - Encoding of signature output (default: 'base64url')
|
|
61
|
+
* @returns Signature in specified encoding
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* // Sign a hash with secp256k1
|
|
66
|
+
* const sig = sign(hashData, privateKey);
|
|
67
|
+
*
|
|
68
|
+
* // Sign with Ed25519
|
|
69
|
+
* const sig2 = sign(hashData, privateKey, 'ed25519');
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
declare function sign(data: string | Uint8Array, privateKey: string | Uint8Array, curve?: CurveType$2, inputEncoding?: Encoding, keyEncoding?: Encoding, outputEncoding?: Encoding): string | Uint8Array;
|
|
73
|
+
/**
|
|
74
|
+
* Verify a signature
|
|
75
|
+
*
|
|
76
|
+
* @param data - Data that was signed
|
|
77
|
+
* @param signature - Signature to verify
|
|
78
|
+
* @param publicKey - Public key
|
|
79
|
+
* @param curve - Elliptic curve (default: 'secp256k1')
|
|
80
|
+
* @param inputEncoding - Encoding of data input (default: 'base64url')
|
|
81
|
+
* @param sigEncoding - Encoding of signature (default: 'base64url')
|
|
82
|
+
* @param keyEncoding - Encoding of public key (default: 'base64url')
|
|
83
|
+
* @returns true if signature is valid, false otherwise
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // Verify a signature
|
|
88
|
+
* const isValid = verify(hashData, signature, publicKey);
|
|
89
|
+
*
|
|
90
|
+
* // Verify with Ed25519
|
|
91
|
+
* const isValid2 = verify(hashData, signature, publicKey, 'ed25519');
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
declare function verify(data: string | Uint8Array, signature: string | Uint8Array, publicKey: string | Uint8Array, curve?: CurveType$2, inputEncoding?: Encoding, sigEncoding?: Encoding, keyEncoding?: Encoding): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Generate cryptographically secure random bytes
|
|
97
|
+
*
|
|
98
|
+
* @param bits - Number of bits to generate (default: 256)
|
|
99
|
+
* @param encoding - Output encoding (default: 'base64url')
|
|
100
|
+
* @returns Random bytes in the specified encoding
|
|
101
|
+
*/
|
|
102
|
+
declare function randomBytes(bits?: number, encoding?: Encoding): string | Uint8Array;
|
|
103
|
+
/**
|
|
104
|
+
* Generate a random private key
|
|
105
|
+
*/
|
|
106
|
+
declare function generatePrivateKey(curve?: CurveType$2, encoding?: Encoding): string | Uint8Array;
|
|
107
|
+
/**
|
|
108
|
+
* Get public key from private key
|
|
109
|
+
*/
|
|
110
|
+
declare function getPublicKey(privateKey: string | Uint8Array, curve?: CurveType$2, keyEncoding?: Encoding, outputEncoding?: Encoding): string | Uint8Array;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Digest Function for Quereus
|
|
114
|
+
*
|
|
115
|
+
* Computes the hash of all arguments combined.
|
|
116
|
+
* Uses SHA-256 from @noble/hashes for portable implementation.
|
|
117
|
+
* Compatible with React Native and all JS environments.
|
|
118
|
+
*/
|
|
119
|
+
/**
|
|
120
|
+
* Hash algorithm options
|
|
121
|
+
*/
|
|
122
|
+
type HashAlgorithm = 'sha256' | 'sha512' | 'blake3';
|
|
123
|
+
/**
|
|
124
|
+
* Input type for digest function - can be string, Uint8Array, or number
|
|
125
|
+
*/
|
|
126
|
+
type DigestInput$1 = string | Uint8Array | number | boolean | null | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* Options for the digest function
|
|
129
|
+
*/
|
|
130
|
+
interface DigestOptions {
|
|
131
|
+
/** Hash algorithm to use (default: sha256) */
|
|
132
|
+
algorithm?: HashAlgorithm;
|
|
133
|
+
/** Output format (default: uint8array) */
|
|
134
|
+
output?: 'uint8array' | 'hex';
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Computes the hash of all arguments
|
|
138
|
+
*
|
|
139
|
+
* @param {...DigestInput} args - Variable number of arguments to hash
|
|
140
|
+
* @returns {Uint8Array} The computed hash as a Uint8Array
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* // Hash a string
|
|
145
|
+
* const hash1 = Digest('hello world');
|
|
146
|
+
*
|
|
147
|
+
* // Hash multiple arguments
|
|
148
|
+
* const hash2 = Digest('user:', 123, 'session');
|
|
149
|
+
*
|
|
150
|
+
* // Hash with specific algorithm
|
|
151
|
+
* const hash3 = Digest.withOptions({ algorithm: 'sha512' })('data1', 'data2');
|
|
152
|
+
*
|
|
153
|
+
* // Get hex output
|
|
154
|
+
* const hexHash = Digest.withOptions({ output: 'hex' })('hello');
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
declare function Digest(...args: DigestInput$1[]): Uint8Array;
|
|
158
|
+
declare namespace Digest {
|
|
159
|
+
var withOptions: (options: DigestOptions) => (...args: DigestInput$1[]) => string | Uint8Array<ArrayBufferLike>;
|
|
160
|
+
var sha256: (...args: DigestInput$1[]) => Uint8Array;
|
|
161
|
+
var sha512: (...args: DigestInput$1[]) => Uint8Array;
|
|
162
|
+
var blake3: (...args: DigestInput$1[]) => Uint8Array;
|
|
163
|
+
var hex: (...args: DigestInput$1[]) => string;
|
|
164
|
+
var sha256Hex: (...args: DigestInput$1[]) => string;
|
|
165
|
+
var sha512Hex: (...args: DigestInput$1[]) => string;
|
|
166
|
+
var blake3Hex: (...args: DigestInput$1[]) => string;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Sign Function for Quereus
|
|
171
|
+
*
|
|
172
|
+
* Returns the signature for the given payload using an ECC private key.
|
|
173
|
+
* Uses secp256k1 from @noble/curves for portable implementation.
|
|
174
|
+
* Compatible with React Native and all JS environments.
|
|
175
|
+
*/
|
|
176
|
+
/**
|
|
177
|
+
* Supported elliptic curve types
|
|
178
|
+
*/
|
|
179
|
+
type CurveType$1 = 'secp256k1' | 'p256' | 'ed25519';
|
|
180
|
+
/**
|
|
181
|
+
* Private key input - can be Uint8Array, hex string, or bigint
|
|
182
|
+
*/
|
|
183
|
+
type PrivateKeyInput = Uint8Array | string | bigint;
|
|
184
|
+
/**
|
|
185
|
+
* Digest input - can be Uint8Array or hex string
|
|
186
|
+
*/
|
|
187
|
+
type DigestInput = Uint8Array | string;
|
|
188
|
+
/**
|
|
189
|
+
* Signature output format options
|
|
190
|
+
*/
|
|
191
|
+
type SignatureFormat = 'uint8array' | 'hex' | 'compact' | 'der';
|
|
192
|
+
/**
|
|
193
|
+
* Options for the Sign function
|
|
194
|
+
*/
|
|
195
|
+
interface SignOptions {
|
|
196
|
+
/** Elliptic curve to use (default: secp256k1) */
|
|
197
|
+
curve?: CurveType$1;
|
|
198
|
+
/** Output format for signature (default: uint8array) */
|
|
199
|
+
format?: SignatureFormat;
|
|
200
|
+
/** Additional entropy for signatures (hedged signatures) */
|
|
201
|
+
extraEntropy?: boolean | Uint8Array;
|
|
202
|
+
/** Use low-S canonical signatures (default: true) */
|
|
203
|
+
lowS?: boolean;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Sign a digest using the specified private key and curve
|
|
207
|
+
*
|
|
208
|
+
* @param {DigestInput} digest - The digest/hash to sign
|
|
209
|
+
* @param {PrivateKeyInput} privateKey - The private key to use for signing
|
|
210
|
+
* @param {SignOptions} [options] - Optional signing parameters
|
|
211
|
+
* @returns {Uint8Array | string} The signature in the requested format
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* // Basic usage with secp256k1
|
|
216
|
+
* const digest = new Uint8Array(32).fill(1); // Your hash here
|
|
217
|
+
* const privateKey = 'a'.repeat(64); // Your private key hex
|
|
218
|
+
* const signature = Sign(digest, privateKey);
|
|
219
|
+
*
|
|
220
|
+
* // With specific curve and format
|
|
221
|
+
* const sig = Sign(digest, privateKey, {
|
|
222
|
+
* curve: 'p256',
|
|
223
|
+
* format: 'hex'
|
|
224
|
+
* });
|
|
225
|
+
*
|
|
226
|
+
* // With hedged signatures for extra security
|
|
227
|
+
* const hedgedSig = Sign(digest, privateKey, {
|
|
228
|
+
* extraEntropy: true
|
|
229
|
+
* });
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
declare function Sign(digest: DigestInput, privateKey: PrivateKeyInput, options?: SignOptions): Uint8Array | string;
|
|
233
|
+
declare namespace Sign {
|
|
234
|
+
var secp256k1: (digest: DigestInput, privateKey: PrivateKeyInput, options?: Omit<SignOptions, "curve">) => string | Uint8Array<ArrayBufferLike>;
|
|
235
|
+
var p256: (digest: DigestInput, privateKey: PrivateKeyInput, options?: Omit<SignOptions, "curve">) => string | Uint8Array<ArrayBufferLike>;
|
|
236
|
+
var ed25519: (digest: DigestInput, privateKey: PrivateKeyInput, options?: Omit<SignOptions, "curve">) => string | Uint8Array<ArrayBufferLike>;
|
|
237
|
+
var generatePrivateKey: (curve?: CurveType$1) => Uint8Array;
|
|
238
|
+
var getPublicKey: (privateKey: PrivateKeyInput, curve?: CurveType$1) => Uint8Array;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* SignatureValid Function for Quereus
|
|
243
|
+
*
|
|
244
|
+
* Returns true if the ECC signature is valid for the given digest and public key.
|
|
245
|
+
* Uses @noble/curves for portable implementation.
|
|
246
|
+
* Compatible with React Native and all JS environments.
|
|
247
|
+
*/
|
|
248
|
+
/**
|
|
249
|
+
* Supported elliptic curve types
|
|
250
|
+
*/
|
|
251
|
+
type CurveType = 'secp256k1' | 'p256' | 'ed25519';
|
|
252
|
+
/**
|
|
253
|
+
* Input types that can be Uint8Array or hex string
|
|
254
|
+
*/
|
|
255
|
+
type BytesInput = Uint8Array | string;
|
|
256
|
+
/**
|
|
257
|
+
* Options for signature verification
|
|
258
|
+
*/
|
|
259
|
+
interface VerifyOptions {
|
|
260
|
+
/** Elliptic curve to use (default: secp256k1) */
|
|
261
|
+
curve?: CurveType;
|
|
262
|
+
/** Signature format (default: auto-detect) */
|
|
263
|
+
signatureFormat?: 'compact' | 'der' | 'raw';
|
|
264
|
+
/** Allow malleable signatures (default: false for ECDSA, true for EdDSA) */
|
|
265
|
+
allowMalleableSignatures?: boolean;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Verify if an ECC signature is valid
|
|
269
|
+
*
|
|
270
|
+
* @param {BytesInput} digest - The digest/hash that was signed
|
|
271
|
+
* @param {BytesInput} signature - The signature to verify
|
|
272
|
+
* @param {BytesInput} publicKey - The public key to verify against
|
|
273
|
+
* @param {VerifyOptions} [options] - Optional verification parameters
|
|
274
|
+
* @returns {boolean} True if the signature is valid, false otherwise
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```typescript
|
|
278
|
+
* // Basic usage with secp256k1
|
|
279
|
+
* const isValid = SignatureValid(digest, signature, publicKey);
|
|
280
|
+
*
|
|
281
|
+
* // With specific curve
|
|
282
|
+
* const isValid = SignatureValid(digest, signature, publicKey, {
|
|
283
|
+
* curve: 'p256'
|
|
284
|
+
* });
|
|
285
|
+
*
|
|
286
|
+
* // With specific signature format
|
|
287
|
+
* const isValid = SignatureValid(digest, signature, publicKey, {
|
|
288
|
+
* curve: 'secp256k1',
|
|
289
|
+
* signatureFormat: 'der'
|
|
290
|
+
* });
|
|
291
|
+
*
|
|
292
|
+
* // Allow malleable signatures
|
|
293
|
+
* const isValid = SignatureValid(digest, signature, publicKey, {
|
|
294
|
+
* allowMalleableSignatures: true
|
|
295
|
+
* });
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
declare function SignatureValid(digest: BytesInput, signature: BytesInput, publicKey: BytesInput, options?: VerifyOptions): boolean;
|
|
299
|
+
declare namespace SignatureValid {
|
|
300
|
+
var secp256k1: (digest: BytesInput, signature: BytesInput, publicKey: BytesInput, options?: Omit<VerifyOptions, "curve">) => boolean;
|
|
301
|
+
var p256: (digest: BytesInput, signature: BytesInput, publicKey: BytesInput, options?: Omit<VerifyOptions, "curve">) => boolean;
|
|
302
|
+
var ed25519: (digest: BytesInput, signature: BytesInput, publicKey: BytesInput, options?: Omit<VerifyOptions, "curve">) => boolean;
|
|
303
|
+
var batch: (verifications: Array<{
|
|
304
|
+
digest: BytesInput;
|
|
305
|
+
signature: BytesInput;
|
|
306
|
+
publicKey: BytesInput;
|
|
307
|
+
options?: VerifyOptions;
|
|
308
|
+
}>) => boolean[];
|
|
309
|
+
var detailed: (digest: BytesInput, signature: BytesInput, publicKey: BytesInput, options?: VerifyOptions) => {
|
|
310
|
+
valid: boolean;
|
|
311
|
+
curve: CurveType;
|
|
312
|
+
signatureFormat: string;
|
|
313
|
+
error?: string;
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export { type BytesInput, type CurveType$2 as CurveType, Digest, type DigestInput$1 as DigestInput, type DigestOptions, type Encoding, type HashAlgorithm$1 as HashAlgorithm, type PrivateKeyInput, Sign, type SignOptions, SignatureValid, type VerifyOptions, digest, generatePrivateKey, getPublicKey, hashMod, randomBytes, sign, verify };
|