w3pk 0.4.1 โ 0.5.1
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 +282 -15
- package/dist/index.d.mts +357 -53
- package/dist/index.d.ts +357 -53
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -1
- package/dist/index.mjs.map +1 -1
- package/docs/QUICK_START.md +342 -0
- package/docs/ZK_INTEGRATION_GUIDE.md +725 -0
- package/package.json +32 -14
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# w3pk
|
|
2
2
|
|
|
3
|
-
WebAuthn SDK for passwordless authentication, encrypted Ethereum wallets,
|
|
3
|
+
WebAuthn SDK for passwordless authentication, encrypted Ethereum wallets, privacy-preserving stealth addresses, and zero-knowledge proofs.
|
|
4
4
|
|
|
5
5
|
Live demo: **https://d2u.w3hc.org/voting**
|
|
6
6
|
|
|
@@ -10,13 +10,24 @@ Live demo: **https://d2u.w3hc.org/voting**
|
|
|
10
10
|
npm install w3pk
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
### Optional Zero-Knowledge Proofs
|
|
14
|
+
|
|
15
|
+
ZK proofs require additional dependencies. Install only if you need ZK features:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# Install ZK dependencies (optional)
|
|
19
|
+
npm install snarkjs circomlibjs
|
|
20
|
+
```
|
|
21
|
+
|
|
13
22
|
## Features
|
|
14
23
|
|
|
15
24
|
- **๐ Passwordless Authentication**: WebAuthn/FIDO2 biometric authentication
|
|
16
25
|
- **๐ฐ Encrypted Wallet Management**: Client-side AES-GCM-256 encrypted wallets
|
|
17
|
-
- **๐ฑ HD Wallet Generation**: BIP39/BIP44 compliant wallet derivation
|
|
26
|
+
- **๐ฑ HD Wallet Generation**: BIP39/BIP44 compliant wallet derivation with multi-address support
|
|
18
27
|
- **๐ฅท Stealth Addresses**: Privacy-preserving stealth address generation with unlinkable transactions
|
|
28
|
+
- **๐ฌ Zero-Knowledge Proofs**: Privacy-preserving proofs (membership, threshold, range, ownership)
|
|
19
29
|
- **๐ก๏ธ Network Agnostic**: Works with any blockchain - you handle the transactions
|
|
30
|
+
- **โก React Optimized**: Streamlined for modern React applications
|
|
20
31
|
|
|
21
32
|
## Quick Start
|
|
22
33
|
|
|
@@ -56,7 +67,10 @@ createWeb3Passkey({
|
|
|
56
67
|
debug?: boolean, // Optional: Enable logs (default: false)
|
|
57
68
|
onError?: (error) => void, // Optional: Error handler
|
|
58
69
|
onAuthStateChanged?: (isAuth, user?) => void, // Optional: Auth callback
|
|
59
|
-
stealthAddresses?: {}
|
|
70
|
+
stealthAddresses?: {}, // Optional: Enable stealth address generation
|
|
71
|
+
zkProofs?: { // Optional: Enable zero-knowledge proofs
|
|
72
|
+
enabledProofs: ['membership', 'threshold', 'range', 'ownership']
|
|
73
|
+
}
|
|
60
74
|
})
|
|
61
75
|
```
|
|
62
76
|
|
|
@@ -118,6 +132,7 @@ w3pk.user // UserInfo | null - Current user data
|
|
|
118
132
|
w3pk.version // string - SDK version
|
|
119
133
|
w3pk.isBrowserEnvironment // boolean - Browser environment detection
|
|
120
134
|
w3pk.stealth // StealthAddressModule | null - Stealth address module
|
|
135
|
+
w3pk.zk // ZKProofModule | null - Zero-knowledge proof module
|
|
121
136
|
```
|
|
122
137
|
|
|
123
138
|
### Error Handling
|
|
@@ -304,6 +319,229 @@ console.log('Your stealth meta address:', keys.metaAddress)
|
|
|
304
319
|
console.log('Your viewing key (keep private):', keys.viewingKey)
|
|
305
320
|
```
|
|
306
321
|
|
|
322
|
+
### Zero-Knowledge Proofs API
|
|
323
|
+
|
|
324
|
+
**Prerequisites**: Install ZK dependencies first:
|
|
325
|
+
```bash
|
|
326
|
+
npm install snarkjs circomlibjs
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
When configured with `zkProofs` option, the SDK provides privacy-preserving zero-knowledge proof generation:
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
// Initialize SDK with ZK proofs enabled
|
|
333
|
+
const w3pk = createWeb3Passkey({
|
|
334
|
+
apiBaseUrl: 'https://webauthn.w3hc.org',
|
|
335
|
+
zkProofs: {
|
|
336
|
+
enabledProofs: ['membership', 'threshold', 'range', 'ownership', 'nft']
|
|
337
|
+
}
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
// Login first
|
|
341
|
+
await w3pk.login()
|
|
342
|
+
|
|
343
|
+
// 1. Membership Proof - Prove you're in a set without revealing identity
|
|
344
|
+
const members = ['user1', 'user2', 'user3']
|
|
345
|
+
const membershipProof = await w3pk.zk.proveMembership({
|
|
346
|
+
value: 'user2',
|
|
347
|
+
pathIndices: [1, 0],
|
|
348
|
+
pathElements: ['hash1', 'hash2'],
|
|
349
|
+
root: 'merkle_root'
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
// 2. Threshold Proof - Prove balance > threshold without revealing balance
|
|
353
|
+
const thresholdProof = await w3pk.zk.proveThreshold({
|
|
354
|
+
value: 5000n,
|
|
355
|
+
blinding: 123456n,
|
|
356
|
+
threshold: 1000n,
|
|
357
|
+
commitment: 'commitment_hash'
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
// 3. Range Proof - Prove age is 18-65 without revealing exact age
|
|
361
|
+
const rangeProof = await w3pk.zk.proveRange({
|
|
362
|
+
value: 25n,
|
|
363
|
+
blinding: 789012n,
|
|
364
|
+
min: 18n,
|
|
365
|
+
max: 65n,
|
|
366
|
+
commitment: 'age_commitment'
|
|
367
|
+
})
|
|
368
|
+
|
|
369
|
+
// 4. Ownership Proof - Prove you own an address without revealing private key
|
|
370
|
+
const ownershipProof = await w3pk.zk.proveOwnership({
|
|
371
|
+
privateKey: 'your_private_key',
|
|
372
|
+
nonce: 345678n,
|
|
373
|
+
address: '0x...',
|
|
374
|
+
challenge: 'challenge_string'
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
// 5. NFT Ownership Proof - Prove you own an NFT from a collection
|
|
378
|
+
import { generateNFTOwnershipProofInputs } from 'w3pk'
|
|
379
|
+
|
|
380
|
+
const holderAddresses = [
|
|
381
|
+
'0x742d35Cc6139FE1C2f1234567890123456789014',
|
|
382
|
+
'0x1234567890123456789012345678901234567890', // Your address
|
|
383
|
+
'0x9876543210987654321098765432109876543210'
|
|
384
|
+
]
|
|
385
|
+
const contractAddress = '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D' // Human Passport SBT contract
|
|
386
|
+
|
|
387
|
+
const { nftProofInput } = await generateNFTOwnershipProofInputs(
|
|
388
|
+
'0x1234567890123456789012345678901234567890', // Your address
|
|
389
|
+
contractAddress,
|
|
390
|
+
holderAddresses
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
const nftProof = await w3pk.zk.proveNFTOwnership(nftProofInput)
|
|
394
|
+
|
|
395
|
+
// Verify any proof
|
|
396
|
+
const isValid = await w3pk.zk.verify(membershipProof)
|
|
397
|
+
const nftIsValid = await w3pk.zk.verifyNFTOwnership(nftProof, contractAddress, nftProofInput.holdersRoot)
|
|
398
|
+
console.log('Proof valid:', isValid)
|
|
399
|
+
console.log('NFT proof valid:', nftIsValid)
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### ZK Proof Utilities
|
|
403
|
+
|
|
404
|
+
**Note**: These functions require `snarkjs` and `circomlibjs` to be installed.
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
// Create commitments for hiding values (requires circomlibjs)
|
|
408
|
+
const commitment = await w3pk.zk.createCommitment(value, blinding)
|
|
409
|
+
|
|
410
|
+
// Compute merkle roots for membership proofs (requires circomlibjs)
|
|
411
|
+
const root = await w3pk.zk.computeMerkleRoot(leaf, pathIndices, pathElements)
|
|
412
|
+
|
|
413
|
+
// Direct utility functions
|
|
414
|
+
import {
|
|
415
|
+
generateBlinding,
|
|
416
|
+
buildMerkleTree,
|
|
417
|
+
generateMerkleProof,
|
|
418
|
+
buildNFTHoldersMerkleTree,
|
|
419
|
+
generateNFTOwnershipProofInputs,
|
|
420
|
+
validateNFTOwnershipProofInputs
|
|
421
|
+
} from 'w3pk'
|
|
422
|
+
|
|
423
|
+
const blinding = generateBlinding()
|
|
424
|
+
const { root, tree } = await buildMerkleTree(['leaf1', 'leaf2', 'leaf3'])
|
|
425
|
+
const { pathIndices, pathElements } = generateMerkleProof(tree, 1)
|
|
426
|
+
|
|
427
|
+
// NFT-specific utilities
|
|
428
|
+
const holderAddresses = ['0x...', '0x...', '0x...']
|
|
429
|
+
const contractAddress = '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D'
|
|
430
|
+
const { root: nftRoot, tree: nftTree } = await buildNFTHoldersMerkleTree(holderAddresses, contractAddress)
|
|
431
|
+
const { nftProofInput } = await generateNFTOwnershipProofInputs('0x...', contractAddress, holderAddresses)
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
**Note:** ZK proofs require circuit compilation:
|
|
435
|
+
```bash
|
|
436
|
+
# Compile circuits (required for proof generation)
|
|
437
|
+
pnpm build:zk
|
|
438
|
+
|
|
439
|
+
# Run comprehensive ZK demo (optional)
|
|
440
|
+
tsx examples/zk-proof-demo.ts
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
## NFT Ownership Proof Example
|
|
444
|
+
|
|
445
|
+
Prove you own an NFT from a collection without revealing which specific NFT or your exact wallet address:
|
|
446
|
+
|
|
447
|
+
```typescript
|
|
448
|
+
import { createWeb3Passkey, generateNFTOwnershipProofInputs } from 'w3pk'
|
|
449
|
+
|
|
450
|
+
// Initialize SDK with NFT proofs enabled
|
|
451
|
+
const w3pk = createWeb3Passkey({
|
|
452
|
+
apiBaseUrl: 'https://webauthn.w3hc.org',
|
|
453
|
+
zkProofs: {
|
|
454
|
+
enabledProofs: ['nft']
|
|
455
|
+
}
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
// Login first
|
|
459
|
+
await w3pk.login()
|
|
460
|
+
|
|
461
|
+
// Example: Human Passport SBT contract
|
|
462
|
+
const HumanPassportSBTContract = '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D'
|
|
463
|
+
const SBTHolders = [
|
|
464
|
+
'0x742d35Cc6139FE1C2f1234567890123456789014',
|
|
465
|
+
'0x1234567890123456789012345678901234567890', // Your address
|
|
466
|
+
'0x9876543210987654321098765432109876543210',
|
|
467
|
+
'0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
|
|
468
|
+
]
|
|
469
|
+
|
|
470
|
+
// 1. Generate proof inputs for your address
|
|
471
|
+
const yourAddress = '0x1234567890123456789012345678901234567890'
|
|
472
|
+
const { nftProofInput, holderLeaves } = await generateNFTOwnershipProofInputs(
|
|
473
|
+
yourAddress,
|
|
474
|
+
HumanPassportSBTContract,
|
|
475
|
+
SBTHolders,
|
|
476
|
+
1n // Minimum balance requirement
|
|
477
|
+
)
|
|
478
|
+
|
|
479
|
+
// 2. Generate NFT ownership proof
|
|
480
|
+
const nftOwnershipProof = await w3pk.zk.proveNFTOwnership(nftProofInput)
|
|
481
|
+
|
|
482
|
+
console.log('NFT Ownership Proof Generated!')
|
|
483
|
+
console.log('Holder index:', nftProofInput.holderIndex) // Your position in holder list (private)
|
|
484
|
+
console.log('Proof type:', nftOwnershipProof.type) // "nft"
|
|
485
|
+
console.log('Public signals:', nftOwnershipProof.publicSignals) // Only merkle root visible
|
|
486
|
+
|
|
487
|
+
// 3. Verify the proof
|
|
488
|
+
const isValid = await w3pk.zk.verifyNFTOwnership(
|
|
489
|
+
nftOwnershipProof,
|
|
490
|
+
HumanPassportSBTContract,
|
|
491
|
+
nftProofInput.holdersRoot,
|
|
492
|
+
1n // Expected minimum balance
|
|
493
|
+
)
|
|
494
|
+
|
|
495
|
+
console.log('NFT proof is valid:', isValid)
|
|
496
|
+
|
|
497
|
+
// 4. Use case: Gated content access
|
|
498
|
+
if (isValid) {
|
|
499
|
+
console.log('โ
Access granted to Human Passport SBT holders-only content!')
|
|
500
|
+
console.log('โ
Your exact NFT and wallet address remain private')
|
|
501
|
+
} else {
|
|
502
|
+
console.log('โ Access denied - proof verification failed')
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// Example: SBT (Soulbound Token) Proof
|
|
506
|
+
const sbtContract = '0x1234567890123456789012345678901234567890'
|
|
507
|
+
const sbtHolders = [
|
|
508
|
+
'0xuser1...',
|
|
509
|
+
'0xuser2...',
|
|
510
|
+
yourAddress, // You have this SBT
|
|
511
|
+
'0xuser3...'
|
|
512
|
+
]
|
|
513
|
+
|
|
514
|
+
const { nftProofInput: sbtProofInput } = await generateNFTOwnershipProofInputs(
|
|
515
|
+
yourAddress,
|
|
516
|
+
sbtContract,
|
|
517
|
+
sbtHolders
|
|
518
|
+
)
|
|
519
|
+
|
|
520
|
+
const sbtProof = await w3pk.zk.proveNFTOwnership(sbtProofInput)
|
|
521
|
+
const sbtValid = await w3pk.zk.verifyNFTOwnership(sbtProof, sbtContract, sbtProofInput.holdersRoot)
|
|
522
|
+
|
|
523
|
+
if (sbtValid) {
|
|
524
|
+
console.log('โ
SBT ownership verified - access granted to exclusive community!')
|
|
525
|
+
}
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
### NFT Proof Privacy Features
|
|
529
|
+
|
|
530
|
+
- **๐ Private NFT ID**: Nobody knows which specific NFT you own from the collection
|
|
531
|
+
- **๐ Private Wallet**: Your exact wallet address is not revealed
|
|
532
|
+
- **๐ Private Balance**: Only proves you meet minimum balance requirement
|
|
533
|
+
- **โ
Public Verification**: Anyone can verify you own an NFT from the specified collection
|
|
534
|
+
- **โก Efficient Proofs**: Uses merkle trees for scalable verification (supports thousands of holders)
|
|
535
|
+
|
|
536
|
+
### Supported Use Cases
|
|
537
|
+
|
|
538
|
+
- **๐จ NFT-Gated Content**: Prove ownership for exclusive access without linking to specific wallet
|
|
539
|
+
- **๐
SBT Credentials**: Verify soulbound token ownership for reputation systems
|
|
540
|
+
- **๐ช Community Access**: Join exclusive groups based on NFT ownership
|
|
541
|
+
- **๐ณ๏ธ DAO Voting**: Anonymous voting rights based on NFT collection membership
|
|
542
|
+
- **๐ฎ Gaming**: Unlock features based on NFT ownership across multiple games
|
|
543
|
+
- **๐ Education**: Access courses/content based on credential NFTs/SBTs
|
|
544
|
+
|
|
307
545
|
## Complete Example
|
|
308
546
|
|
|
309
547
|
```typescript
|
|
@@ -367,15 +605,29 @@ w3pk.logout()
|
|
|
367
605
|
|
|
368
606
|
## Backend
|
|
369
607
|
|
|
370
|
-
|
|
608
|
+
### Using Hosted Backend (Recommended)
|
|
371
609
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
610
|
+
The easiest way to get started is to use the hosted WebAuthn backend:
|
|
611
|
+
|
|
612
|
+
```typescript
|
|
613
|
+
const w3pk = createWeb3Passkey({
|
|
614
|
+
apiBaseUrl: 'https://webauthn.w3hc.org'
|
|
615
|
+
})
|
|
377
616
|
```
|
|
378
617
|
|
|
618
|
+
This hosted service handles WebAuthn registration and authentication flows.
|
|
619
|
+
|
|
620
|
+
### Self-Hosted Backend
|
|
621
|
+
|
|
622
|
+
For production use, you may want to self-host your WebAuthn backend. The backend should implement:
|
|
623
|
+
|
|
624
|
+
- `POST /webauthn/register/begin` - Start WebAuthn registration
|
|
625
|
+
- `POST /webauthn/register/complete` - Complete WebAuthn registration
|
|
626
|
+
- `POST /webauthn/authenticate/usernameless/begin` - Start usernameless authentication
|
|
627
|
+
- `POST /webauthn/authenticate/usernameless/complete` - Complete usernameless authentication
|
|
628
|
+
|
|
629
|
+
See the [WebAuthn specification](https://w3c.github.io/webauthn/) for implementation details.
|
|
630
|
+
|
|
379
631
|
## Security
|
|
380
632
|
|
|
381
633
|
### Encryption & Storage
|
|
@@ -490,17 +742,24 @@ pnpm build
|
|
|
490
742
|
pnpm dev
|
|
491
743
|
|
|
492
744
|
# Run tests
|
|
493
|
-
pnpm test # Run basic + comprehensive test
|
|
745
|
+
pnpm test # Run basic + comprehensive + ZK test suites
|
|
494
746
|
pnpm test:basic # Run basic functionality tests only
|
|
495
|
-
pnpm test:comprehensive # Run full 23-test comprehensive suite
|
|
747
|
+
pnpm test:comprehensive # Run full 23-test comprehensive suite
|
|
748
|
+
pnpm test:zk # Run ZK proof module tests
|
|
749
|
+
pnpm test:nft # Run NFT ownership proof tests (6 tests)
|
|
750
|
+
|
|
751
|
+
# ZK circuit compilation (optional)
|
|
752
|
+
pnpm build:zk # Compile circom circuits for proof generation
|
|
496
753
|
|
|
497
754
|
# Publish to npm
|
|
498
755
|
pnpm prepublishOnly # Builds before publishing
|
|
499
756
|
```
|
|
500
757
|
|
|
758
|
+
Watch [Asciinema video](https://asciinema.org/a/s9EAGyxNpBH2UZilZvEUHcGSO) (running the tests)
|
|
759
|
+
|
|
501
760
|
### Test Coverage
|
|
502
761
|
|
|
503
|
-
The SDK includes a comprehensive test suite with **
|
|
762
|
+
The SDK includes a comprehensive test suite with **37 test cases** covering:
|
|
504
763
|
|
|
505
764
|
- โ
**Core SDK functionality** - Constructor, configuration, environment detection
|
|
506
765
|
- โ
**Wallet generation** - BIP39/BIP44 compliance, HD derivation, consistency
|
|
@@ -508,7 +767,10 @@ The SDK includes a comprehensive test suite with **23 test cases** covering:
|
|
|
508
767
|
- โ
**Storage operations** - IndexedDB CRUD, multiple wallets, cleanup
|
|
509
768
|
- โ
**Message signing** - Signature generation, address verification
|
|
510
769
|
- โ
**HD wallet derivation** - Multi-index support, validation, consistency
|
|
511
|
-
- โ
**
|
|
770
|
+
- โ
**ZK proof operations** - Commitment creation, merkle trees, proof setup
|
|
771
|
+
- โ
**NFT ownership proofs** - NFT merkle trees, proof generation, SBT support
|
|
772
|
+
- โ
**Circuit compilation** - Circom circuit status, dependency detection
|
|
773
|
+
- โ
**Error handling** - Graceful failure scenarios, mock mode fallbacks
|
|
512
774
|
- โ
**Integration testing** - End-to-end workflows
|
|
513
775
|
|
|
514
776
|
### Architecture
|
|
@@ -520,9 +782,14 @@ w3pk/
|
|
|
520
782
|
โ โโโ wallet/ # Wallet generation, signing, storage
|
|
521
783
|
โ โโโ auth/ # WebAuthn authentication flows
|
|
522
784
|
โ โโโ stealth/ # Privacy-preserving stealth addresses
|
|
785
|
+
โ โโโ zk/ # Zero-knowledge proof module
|
|
786
|
+
โ โ โโโ circuits/ # Circom circuit definitions (membership, threshold, range, ownership, nft)
|
|
787
|
+
โ โ โโโ templates/ # Compiled circuit artifacts (.r1cs, .sym, .wasm)
|
|
788
|
+
โ โ โโโ wasm/ # WebAssembly circuit files
|
|
523
789
|
โ โโโ utils/ # API client, validation utilities
|
|
524
790
|
โ โโโ types/ # TypeScript type definitions
|
|
525
|
-
โโโ test/ # Comprehensive test suite
|
|
791
|
+
โโโ test/ # Comprehensive test suite (37 test cases)
|
|
792
|
+
โโโ scripts/ # Circuit compilation and build scripts
|
|
526
793
|
โโโ dist/ # Built output (CJS + ESM + types)
|
|
527
794
|
```
|
|
528
795
|
|
|
@@ -544,6 +811,6 @@ Contact [Julien Bรฉranger](https://github.com/julienbrg):
|
|
|
544
811
|
|
|
545
812
|
## License
|
|
546
813
|
|
|
547
|
-
GPL-3.0
|
|
814
|
+
GPL-3.0
|
|
548
815
|
|
|
549
816
|
<img src="https://bafkreid5xwxz4bed67bxb2wjmwsec4uhlcjviwy7pkzwoyu5oesjd3sp64.ipfs.w3s.link" alt="built-with-ethereum-w3hc" width="100"/>
|