webylib 0.2.0

Webcash HD wallet library β€” bearer e-cash with BIP32-style key derivation, SQLite storage, AES-256-GCM encryption, and full C FFI for cross-platform SDKs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# Webcash System Architecture

## πŸ“‹ **CRITICAL CORRECTION NOTICE**

> **🚨 IMPORTANT**: This document has been corrected to accurately reflect the **centralized** nature of the Webcash system. Previous versions contained incorrect information about decentralization.

## 🎯 **System Overview**

Webcash is a **centralized electronic cash system** operated by Webcash LLC, a Wyoming Limited Liability Company. Unlike decentralized cryptocurrencies, Webcash operates more like a traditional banking system where:

- **Webcash LLC** runs the authoritative server
- **No account balances** are stored (only transaction validation)
- **Miners create new webcash** through proof-of-work
- **Server validates** all transactions and mining
- **Central bank-like control** over monetary supply

## πŸ›οΈ **Core Architecture Components**

### **1. Webcash LLC Server (Central Authority)**
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    WEBCASH LLC SERVER                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚ PostgreSQL Database                               β”‚    β”‚
β”‚  β”‚ β”œβ”€ MiningReports (PoW validation)               β”‚    β”‚
β”‚  β”‚ β”œβ”€ Replacements (transaction validation)        β”‚    β”‚
β”‚  β”‚ β”œβ”€ Burns (supply reduction)                     β”‚    β”‚
β”‚  β”‚ └─ UnspentOutputs (double-spend prevention)     β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                                                             β”‚
β”‚  API Endpoints:                                             β”‚
β”‚  β”œβ”€ /api/v1/mining_report (validate PoW, create webcash)   β”‚
β”‚  β”œβ”€ /api/v1/replace (validate transactions)                 β”‚
β”‚  β”œβ”€ /api/v1/health_check (check spent status)               β”‚
β”‚  β”œβ”€ /api/v1/target (get mining parameters)                  β”‚
β”‚  └─ /api/v1/burn (destroy webcash)                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

**Key Functions:**
- **Mining Validation**: Accepts proof-of-work, creates new webcash
- **Transaction Validation**: Validates webcash replacements (spending)
- **Double-Spend Prevention**: Tracks spent webcash hashes
- **Supply Control**: Enforces mining schedule and halving
- **No Balance Storage**: Only validates transactions, doesn't store balances

### **2. Wallet Clients (Python, C++, Rust)**
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    WALLET CLIENT                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚ SQLite Database                                   β”‚    β”‚
β”‚  β”‚ β”œβ”€ webcash (owned secrets)                       β”‚    β”‚
β”‚  β”‚ β”œβ”€ unconfirmed (pending transactions)            β”‚    β”‚
β”‚  β”‚ β”œβ”€ log (transaction history)                     β”‚    β”‚
β”‚  β”‚ └─ master_secret (HD wallet seed)                β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                                                             β”‚
β”‚  Operations:                                                β”‚
β”‚  β”œβ”€ setup (accept terms)                                    β”‚
β”‚  β”œβ”€ insert (receive webcash)                                β”‚
β”‚  β”œβ”€ pay (send webcash)                                      β”‚
β”‚  β”œβ”€ check (verify ownership)                                β”‚
β”‚  β”œβ”€ recover (restore from seed)                             β”‚
β”‚  └─ mine (create new webcash)                               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### **3. Mining Process**
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   MINER     │───▢│  PROOF-OF-WORK │───▢│ WEBCASH SERVER β”‚
β”‚             β”‚    β”‚                 β”‚    β”‚                 β”‚
β”‚ 1. Generate β”‚    β”‚ SHA256(preimage)β”‚    β”‚ 3. Validate PoW β”‚
β”‚    secrets  β”‚    β”‚ < target        β”‚    β”‚    βœ“ Accept     β”‚
β”‚             β”‚    β”‚                 β”‚    β”‚    βœ“ Create     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚    βœ“ Return     β”‚
                                          β”‚      webcash    β”‚
                                          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸ”„ **Transaction Flow**

### **1. Mining (Creating New Webcash)**
```mermaid
sequenceDiagram
    participant M as Miner
    participant S as Webcash Server
    participant W as Wallet

    M->>M: Generate mining secrets
    M->>M: Create preimage JSON
    M->>M: Find SHA256(preimage) < target
    M->>S: Submit mining_report
    S->>S: Validate proof-of-work
    S->>S: Check preimage format
    S->>S: Verify mining amount
    S->>S: Create new webcash
    S->>M: Return success + new webcash
    M->>W: Store in wallet
```

### **2. Payment (Replacing Webcash)**
```mermaid
sequenceDiagram
    participant A as Sender Wallet
    participant S as Webcash Server
    participant B as Receiver Wallet

    A->>A: Select input webcash
    A->>A: Generate new secrets for change/output
    A->>S: Submit replace request
    S->>S: Validate inputs exist & unspent
    S->>S: Check input amounts = output amounts
    S->>S: Mark inputs as spent
    S->>S: Record outputs as unspent
    S->>A: Return success
    A->>B: Send output webcash string
    B->>B: Store received webcash
```

### **3. Verification (Health Check)**
```mermaid
sequenceDiagram
    participant W as Wallet
    participant S as Webcash Server

    W->>S: Query webcash status
    S->>S: Check UnspentOutputs table
    S->>S: Check SpentHashes table
    S->>W: Return status + amount
```

## 🏦 **Central Bank Economics**

### **Monetary Policy Control**
- **Supply Schedule**: Fixed mining rewards that halve every epoch
- **Total Supply**: Controlled by Webcash LLC mining parameters
- **Inflation Control**: Mining rewards decrease over time
- **No Voting**: Webcash LLC sets policy unilaterally

### **Current Economics (2025)**
```rust
// Mining parameters (controlled by Webcash LLC)
const INITIAL_MINING_AMOUNT: u64 = 200_000_000_000_000; // 200M webcash
const INITIAL_SUBSIDY_AMOUNT: u64 = 10_000_000_000_000;  // 10M webcash
const REPORTS_PER_EPOCH: u64 = 525_000;                  // ~6 months
const TARGET_INTERVAL: Duration = Duration::seconds(10); // 10 second blocks

// Epoch calculation
fn get_epoch(num_reports: u64) -> u64 {
    num_reports / REPORTS_PER_EPOCH
}

// Mining reward for current epoch
fn get_mining_amount(epoch: u64) -> u64 {
    INITIAL_MINING_AMOUNT >> epoch.min(63)
}
```

### **Trust Model**
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    TRUST MODEL                              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Users trust Webcash LLC to:                                β”‚
β”‚ βœ“ Operate server honestly                                  β”‚
β”‚ βœ“ Validate transactions correctly                          β”‚
β”‚ βœ“ Not double-spend or counterfeit                          β”‚
β”‚ βœ“ Maintain service availability                            β”‚
β”‚ βœ“ Comply with US financial regulations                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Unlike decentralized systems:                              β”‚
β”‚ βœ— No censorship resistance                                 β”‚
β”‚ βœ— No algorithmic monetary policy                          β”‚
β”‚ βœ— Single point of failure                                  β”‚
β”‚ βœ— Subject to legal jurisdiction                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸ” **Security Model**

### **Server Security (Webcash LLC Responsibility)**
- **Transaction Validation**: Prevent double-spending
- **Mining Validation**: Verify proof-of-work
- **Data Integrity**: Maintain accurate spent/unspent records
- **Service Availability**: Keep server operational
- **Legal Compliance**: Follow US financial regulations

### **Client Security (Wallet Responsibility)**
- **Secret Storage**: Protect private keys and secrets
- **Transaction Validation**: Verify server responses
- **Input Sanitization**: Validate all user inputs
- **Memory Safety**: Prevent information leakage

### **Cryptographic Security**
- **SHA256**: For proof-of-work and secret commitments
- **Secure Random**: For secret generation
- **Zero Knowledge**: Public webcash reveals no secret information
- **Deterministic Wallets**: Recoverable from master secret

## πŸ“Š **Data Structures**

### **SecretWebcash (Wallet Storage)**
```rust
struct SecretWebcash {
    secret: SecureString,  // 32-byte hex secret
    amount: Amount,        // 8-decimal precision amount
}
```

**Serialization Format:**
```
e{amount}:{type}:{secret}
Example: e1.50000000:secret:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890
```

### **PublicWebcash (Verification)**
```rust
struct PublicWebcash {
    hash: [u8; 32],  // SHA256(secret)
    amount: Amount,  // Same amount as secret
}
```

**Serialization Format:**
```
e{amount}:{type}:{hash}
Example: e1.50000000:public:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
```

### **Mining Report (Server Communication)**
```json
{
  "preimage": "base64-encoded JSON with webcash data",
  "work": "SHA256 hash of preimage",
  "legalese": {
    "terms": true
  }
}
```

## πŸš€ **API Endpoints**

### **Mining Report Submission**
```http
POST /api/v1/mining_report
Content-Type: application/json

{
  "preimage": "eyJ3ZWJjYXNoIjpb...]",
  "work": "0000000000000000...",
  "legalese": {"terms": true}
}
```

**Response:**
```json
{
  "status": "success",
  "difficulty_target": 28
}
```

### **Transaction Replacement**
```http
POST /api/v1/replace
Content-Type: application/json

{
  "webcashes": ["e1.00000000:secret:..."],
  "new_webcashes": ["e1.00000000:secret:..."],
  "legalese": {"terms": true}
}
```

**Response:**
```json
{"status": "success"}
```

### **Health Check**
```http
POST /api/v1/health_check
Content-Type: application/json

["e1.00000000:public:..."]
```

**Response:**
```json
{
  "status": "success",
  "results": {
    "e1.00000000:public:...": {
      "spent": false,
      "amount": "1.00000000"
    }
  }
}
```

## πŸ—„οΈ **Database Schema**

### **Server Database (PostgreSQL)**
```sql
-- Mining validation records
CREATE TABLE MiningReports (
    id BIGSERIAL PRIMARY KEY,
    received BIGINT NOT NULL,
    preimage TEXT UNIQUE NOT NULL,
    difficulty SMALLINT NOT NULL,
    next_difficulty SMALLINT NOT NULL,
    aggregate_work DOUBLE PRECISION NOT NULL
);

-- Transaction validation records
CREATE TABLE Replacements (
    id BIGSERIAL PRIMARY KEY,
    received BIGINT NOT NULL
);

-- Transaction inputs (many-to-one with Replacements)
CREATE TABLE ReplacementInputs (
    id BIGSERIAL PRIMARY KEY,
    replacement_id BIGINT NOT NULL,
    hash BYTEA NOT NULL,
    amount BIGINT NOT NULL,
    FOREIGN KEY (replacement_id) REFERENCES Replacements(id)
);

-- Transaction outputs (many-to-one with Replacements)
CREATE TABLE ReplacementOutputs (
    id BIGSERIAL PRIMARY KEY,
    replacement_id BIGINT NOT NULL,
    hash BYTEA NOT NULL,
    amount BIGINT NOT NULL,
    FOREIGN KEY (replacement_id) REFERENCES Replacements(id)
);

-- Burn records
CREATE TABLE Burns (...);

-- Double-spend prevention
CREATE TABLE UnspentOutputs (
    id BIGSERIAL PRIMARY KEY,
    hash BYTEA UNIQUE NOT NULL,
    amount BIGINT NOT NULL
);

-- Spent webcash tracking
CREATE TABLE SpentHashes (
    id BIGSERIAL PRIMARY KEY,
    hash BYTEA UNIQUE NOT NULL
);
```

### **Wallet Database (SQLite)**
```sql
-- Owned webcash secrets
CREATE TABLE webcash (
    id INTEGER PRIMARY KEY,
    secret TEXT NOT NULL,
    amount TEXT NOT NULL,
    created_at INTEGER NOT NULL
);

-- Pending transactions
CREATE TABLE unconfirmed (
    id INTEGER PRIMARY KEY,
    data TEXT NOT NULL,
    created_at INTEGER NOT NULL
);

-- Transaction log
CREATE TABLE log (
    id INTEGER PRIMARY KEY,
    type TEXT NOT NULL,
    memo TEXT,
    amount TEXT,
    timestamp INTEGER NOT NULL
);

-- HD wallet metadata
CREATE TABLE metadata (
    key TEXT PRIMARY KEY,
    value TEXT NOT NULL
);
```

## πŸ”„ **Operational Flow**

### **Normal Operation**
1. **User Setup**: Accept terms, generate master secret
2. **Receive Webcash**: Insert received secrets into wallet
3. **Send Webcash**: Replace owned secrets with new ones
4. **Verify Ownership**: Health check before spending
5. **Backup**: Secure master secret for recovery

### **Mining Operation**
1. **Get Target**: Query current mining parameters
2. **Generate Secrets**: Create mining webcash
3. **Find Proof**: SHA256(preimage) < target
4. **Submit Report**: Send to server for validation
5. **Receive Reward**: Get newly created webcash
6. **Secure Storage**: Replace mining secrets with secure ones

### **Recovery Process**
1. **Master Secret**: Restore from backup
2. **Regenerate Secrets**: Deterministic HD derivation
3. **Health Check**: Verify which secrets still own webcash
4. **Reconstruct Wallet**: Import valid webcash

## βš–οΈ **Legal & Compliance**

### **Regulatory Framework**
- **Wyoming LLC**: Webcash LLC incorporated in Wyoming
- **US Jurisdiction**: Subject to US financial regulations
- **KYC/AML**: May be required depending on use case
- **Consumer Protection**: Standard consumer protection laws apply

### **User Responsibilities**
- **Secure Storage**: Protect master secrets and backups
- **Legal Compliance**: Follow applicable laws and regulations
- **Terms Acceptance**: Agree to Webcash LLC terms of service
- **Due Diligence**: Understand centralized trust model

## 🎯 **Key Differences from Decentralized Systems**

| Aspect | Webcash (Centralized) | Bitcoin (Decentralized) |
|--------|----------------------|------------------------|
| **Governance** | Webcash LLC (central bank) | Algorithmic (consensus) |
| **Supply Control** | Webcash LLC discretion | Fixed algorithm |
| **Censorship Resistance** | None | High |
| **Trust Model** | Trust Webcash LLC | Trust mathematics |
| **Legal Compliance** | Required | Optional |
| **Transaction Speed** | Instant | 10-60 minutes |
| **Fees** | Zero | Variable (miner fees) |
| **Finality** | Instant | Probabilistic |

## πŸš€ **Development Roadmap**

### **Phase 1: Foundation** βœ…
- Core data types and cryptography
- Basic wallet operations
- Server communication framework

### **Phase 2: Full Compatibility** 🚧
- Complete Python/C++ API compatibility
- Full wallet functionality
- Mining integration

### **Phase 3: Enhanced Security** πŸ“‹
- Advanced cryptographic features
- Hardware wallet support
- Multi-signature capabilities

### **Phase 4: Performance & Scale** πŸ“‹
- Optimized algorithms
- Batch operations
- Memory-efficient data structures

### **Phase 5: Production Ready** πŸ“‹
- Comprehensive testing
- Security audit
- crates.io publication

---

**πŸ“– For implementation details, see [IMPLEMENTATION_STATUS.md](../IMPLEMENTATION_STATUS.md)**