@unicitylabs/sphere-sdk 0.2.2 → 0.2.5

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
@@ -9,7 +9,7 @@ A modular TypeScript SDK for Unicity wallet operations supporting both Layer 1 (
9
9
  - **L3 Payments** - Token transfers with state transition proofs
10
10
  - **Payment Requests** - Request payments with async response tracking
11
11
  - **Nostr Transport** - P2P messaging with NIP-04 encryption
12
- - **IPFS Storage** - Decentralized token backup with Helia
12
+ - **IPFS Storage** - Decentralized token backup via HTTP API (browser + Node.js)
13
13
  - **Token Splitting** - Partial transfer amount calculations
14
14
  - **Multi-Address** - HD address derivation (BIP32/BIP44)
15
15
  - **TXF Serialization** - Token eXchange Format for storage and transfer
@@ -28,8 +28,8 @@ Choose your platform:
28
28
 
29
29
  | Platform | Guide | Required | Optional |
30
30
  |----------|-------|----------|----------|
31
- | **Browser** | [QUICKSTART-BROWSER.md](docs/QUICKSTART-BROWSER.md) | SDK only | `helia` (IPFS sync) |
32
- | **Node.js** | [QUICKSTART-NODEJS.md](docs/QUICKSTART-NODEJS.md) | SDK + `ws` | - |
31
+ | **Browser** | [QUICKSTART-BROWSER.md](docs/QUICKSTART-BROWSER.md) | SDK only | IPFS sync (built-in) |
32
+ | **Node.js** | [QUICKSTART-NODEJS.md](docs/QUICKSTART-NODEJS.md) | SDK + `ws` | IPFS sync (built-in) |
33
33
  | **CLI** | See below | SDK + `tsx` | - |
34
34
 
35
35
  ## CLI (Command Line Interface)
@@ -43,6 +43,9 @@ npm run cli -- --help
43
43
  # Initialize new wallet on testnet
44
44
  npm run cli -- init --network testnet
45
45
 
46
+ # Initialize with nametag (mints token on-chain)
47
+ npm run cli -- init --network testnet --nametag alice
48
+
46
49
  # Import existing wallet
47
50
  npm run cli -- init --mnemonic "your 24 words here"
48
51
 
@@ -52,35 +55,58 @@ npm run cli -- status
52
55
  # Check balance
53
56
  npm run cli -- balance
54
57
 
55
- # Show receive address
58
+ # Fetch pending transfers and finalize unconfirmed tokens
59
+ npm run cli -- balance --finalize
60
+
61
+ # Check for incoming transfers
56
62
  npm run cli -- receive
57
63
 
58
- # Send tokens
59
- npm run cli -- send @alice 1000000
64
+ # Check for incoming transfers and finalize unconfirmed tokens
65
+ npm run cli -- receive --finalize
66
+
67
+ # Send tokens (instant mode, default)
68
+ npm run cli -- send @alice 1 --coin UCT --instant
69
+
70
+ # Send tokens (conservative mode — collect all proofs first)
71
+ npm run cli -- send @alice 1 --coin UCT --conservative
72
+
73
+ # Request test tokens from faucet
74
+ npm run cli -- topup
60
75
 
61
76
  # Register nametag
62
77
  npm run cli -- nametag myname
63
78
 
64
79
  # Show transaction history
65
80
  npm run cli -- history 10
81
+
82
+ # Verify tokens against aggregator (detect spent tokens)
83
+ npm run cli -- verify-balance
66
84
  ```
67
85
 
68
86
  ### Available CLI Commands
69
87
 
70
88
  | Category | Command | Description |
71
89
  |----------|---------|-------------|
72
- | **Wallet** | `init [--network <net>] [--mnemonic "<words>"]` | Create or import wallet |
90
+ | **Wallet** | `init [--network <net>] [--mnemonic "<words>"] [--nametag <name>]` | Create or import wallet |
73
91
  | | `status` | Show wallet identity |
74
92
  | | `config` | Show/set configuration |
75
- | **Balance** | `balance` | Show L3 token balance |
76
- | | `tokens` | List all tokens |
93
+ | **Profiles** | `wallet list` | List all wallet profiles |
94
+ | | `wallet use <name>` | Switch to a wallet profile |
95
+ | | `wallet create <name> [--network <net>]` | Create a new wallet profile |
96
+ | | `wallet delete <name>` | Delete a wallet profile |
97
+ | | `wallet current` | Show current wallet profile |
98
+ | **Balance** | `balance [--finalize]` | Show L3 token balance (--finalize: fetch pending + resolve) |
99
+ | | `tokens` | List all tokens with details |
77
100
  | | `l1-balance` | Show L1 (ALPHA) balance |
78
- | **Transfers** | `send <recipient> <amount>` | Send tokens |
79
- | | `receive` | Show address for receiving |
101
+ | | `topup [coin] [amount]` | Request test tokens from faucet |
102
+ | | `verify-balance [--remove] [-v]` | Verify tokens against aggregator |
103
+ | **Transfers** | `send <to> <amount> [--coin SYM] [--instant\|--conservative]` | Send tokens |
104
+ | | `receive [--finalize]` | Check for incoming transfers |
80
105
  | | `history [limit]` | Show transaction history |
81
106
  | **Nametags** | `nametag <name>` | Register a nametag |
82
107
  | | `nametag-info <name>` | Lookup nametag info |
83
108
  | | `my-nametag` | Show current nametag |
109
+ | | `nametag-sync` | Re-publish nametag with chainPubkey |
84
110
  | **Utils** | `generate-key` | Generate random key |
85
111
  | | `to-human <amount>` | Convert to human readable |
86
112
  | | `parse-wallet <file>` | Parse wallet file |
@@ -152,7 +178,7 @@ const providers = createBrowserProviders({
152
178
  oracle: { url: 'https://custom-aggregator.example.com' }, // custom oracle
153
179
  });
154
180
 
155
- // Enable L1 with network defaults
181
+ // L1 is enabled by default — customize if needed
156
182
  const providers = createBrowserProviders({
157
183
  network: 'testnet',
158
184
  l1: { enableVesting: true }, // uses testnet electrum URL automatically
@@ -332,17 +358,20 @@ sphere.payments.onPaymentRequest((request) => {
332
358
  Access L1 payments through `sphere.payments.l1`:
333
359
 
334
360
  ```typescript
335
- // L1 configuration is optional (has defaults)
361
+ // L1 is enabled by default with lazy Fulcrum connection.
362
+ // Connection to Fulcrum is deferred until first L1 operation.
336
363
  const { sphere } = await Sphere.init({
337
364
  ...providers,
338
365
  autoGenerate: true,
339
- l1: {
340
- electrumUrl: 'wss://fulcrum.alpha.unicity.network:50004', // default
341
- defaultFeeRate: 10, // sat/byte, default
342
- enableVesting: true, // default
343
- },
366
+ // L1 config is optional — defaults are applied automatically:
367
+ // electrumUrl: network-specific (mainnet: fulcrum.alpha.unicity.network)
368
+ // defaultFeeRate: 10 sat/byte
369
+ // enableVesting: true
344
370
  });
345
371
 
372
+ // To explicitly disable L1:
373
+ // const { sphere } = await Sphere.init({ ...providers, l1: null });
374
+
346
375
  // Get L1 balance
347
376
  const balance = await sphere.payments.l1.getBalance();
348
377
  console.log('L1 Balance:', balance.total);
@@ -706,7 +735,7 @@ The SDK includes browser-ready provider implementations:
706
735
  | `LocalStorageProvider` | Browser localStorage with SSR fallback |
707
736
  | `NostrTransportProvider` | Nostr relay messaging with NIP-04 |
708
737
  | `UnicityAggregatorProvider` | Unicity aggregator for state proofs |
709
- | `IpfsStorageProvider` | Helia-based IPFS with HTTP fallback |
738
+ | `IpfsStorageProvider` | HTTP-based IPFS/IPNS storage (cross-platform) |
710
739
 
711
740
  ## Node.js Providers
712
741
 
@@ -847,14 +876,6 @@ const providers = createBrowserProviders({
847
876
  },
848
877
  });
849
878
 
850
- // Enable multiple sync backends
851
- const providers = createBrowserProviders({
852
- network: 'mainnet',
853
- tokenSync: {
854
- ipfs: { enabled: true, useDht: true },
855
- cloud: { enabled: true, provider: 'aws', bucket: 'my-backup' }, // future
856
- },
857
- });
858
879
  ```
859
880
 
860
881
  ## Token Sync Backends
@@ -863,46 +884,34 @@ The SDK supports multiple token sync backends that can be enabled independently:
863
884
 
864
885
  | Backend | Status | Description |
865
886
  |---------|--------|-------------|
866
- | `ipfs` | ✅ Ready | Decentralized IPFS/IPNS with Helia browser DHT |
867
- | `mongodb` | Ready | MongoDB for centralized token storage |
887
+ | `ipfs` | ✅ Ready | HTTP-based IPFS/IPNS storage (browser + Node.js) |
888
+ | `mongodb` | 🚧 Planned | MongoDB for centralized token storage |
868
889
  | `file` | 🚧 Planned | Local file system (Node.js) |
869
890
  | `cloud` | 🚧 Planned | Cloud storage (AWS S3, GCP, Azure) |
870
891
 
871
892
  ```typescript
872
- // Enable IPFS sync with custom gateways
893
+ // Browser: enable IPFS sync
873
894
  const providers = createBrowserProviders({
874
895
  network: 'testnet',
875
896
  tokenSync: {
876
897
  ipfs: {
877
898
  enabled: true,
878
899
  additionalGateways: ['https://my-gateway.com'],
879
- useDht: true, // Enable browser DHT (Helia)
880
900
  },
881
901
  },
882
902
  });
883
903
 
884
- // Enable MongoDB sync
885
- const providers = createBrowserProviders({
886
- network: 'mainnet',
904
+ // Node.js: enable IPFS sync
905
+ const providers = createNodeProviders({
906
+ network: 'testnet',
907
+ dataDir: './wallet-data',
908
+ tokensDir: './tokens-data',
887
909
  tokenSync: {
888
- mongodb: {
910
+ ipfs: {
889
911
  enabled: true,
890
- uri: 'mongodb://localhost:27017',
891
- database: 'sphere_wallet',
892
- collection: 'tokens',
893
912
  },
894
913
  },
895
914
  });
896
-
897
- // Multiple backends for redundancy
898
- const providers = createBrowserProviders({
899
- tokenSync: {
900
- ipfs: { enabled: true },
901
- mongodb: { enabled: true, uri: 'mongodb://localhost:27017', database: 'wallet' },
902
- file: { enabled: true, directory: './tokens', format: 'txf' },
903
- cloud: { enabled: true, provider: 'aws', bucket: 'wallet-backup' },
904
- },
905
- });
906
915
  ```
907
916
 
908
917
  ## Custom Token Storage Provider
@@ -985,32 +994,32 @@ const { sphere } = await Sphere.init({
985
994
 
986
995
  ## Dynamic Provider Management (Runtime)
987
996
 
988
- After `Sphere.init()` is called, you can add/remove token storage providers dynamically through UI:
997
+ After `Sphere.init()` is called, you can add/remove token storage providers dynamically:
989
998
 
990
999
  ```typescript
991
- import { createMongoDbStorageProvider } from './my-mongodb-provider';
1000
+ import { createBrowserIpfsStorageProvider } from '@unicitylabs/sphere-sdk/impl/browser/ipfs';
1001
+ // For Node.js: import { createNodeIpfsStorageProvider } from '@unicitylabs/sphere-sdk/impl/nodejs/ipfs';
992
1002
 
993
- // Add a new provider at runtime (e.g., user enables MongoDB sync in settings)
994
- const mongoProvider = createMongoDbStorageProvider({
995
- uri: 'mongodb://localhost:27017',
996
- database: 'sphere_wallet',
1003
+ // Add a new provider at runtime (e.g., user enables IPFS sync in settings)
1004
+ const ipfsProvider = createBrowserIpfsStorageProvider({
1005
+ gateways: ['https://my-ipfs-node.com'],
997
1006
  });
998
1007
 
999
- await sphere.addTokenStorageProvider(mongoProvider);
1008
+ await sphere.addTokenStorageProvider(ipfsProvider);
1000
1009
 
1001
1010
  // Provider is now active and will be used in sync operations
1002
1011
 
1003
1012
  // Check if provider exists
1004
- if (sphere.hasTokenStorageProvider('mongodb-token-storage')) {
1005
- console.log('MongoDB sync is enabled');
1013
+ if (sphere.hasTokenStorageProvider('ipfs-token-storage')) {
1014
+ console.log('IPFS sync is enabled');
1006
1015
  }
1007
1016
 
1008
1017
  // Get all active providers
1009
1018
  const providers = sphere.getTokenStorageProviders();
1010
1019
  console.log('Active providers:', Array.from(providers.keys()));
1011
1020
 
1012
- // Remove a provider (e.g., user disables MongoDB sync)
1013
- await sphere.removeTokenStorageProvider('mongodb-token-storage');
1021
+ // Remove a provider (e.g., user disables IPFS sync)
1022
+ await sphere.removeTokenStorageProvider('ipfs-token-storage');
1014
1023
 
1015
1024
  // Listen for per-provider sync events
1016
1025
  sphere.on('sync:provider', (event) => {
@@ -1026,25 +1035,6 @@ sphere.on('sync:provider', (event) => {
1026
1035
  await sphere.payments.sync();
1027
1036
  ```
1028
1037
 
1029
- ### Multiple Providers Example
1030
-
1031
- ```typescript
1032
- // User configures multiple sync backends via UI
1033
- const ipfsProvider = createIpfsStorageProvider({ gateways: ['https://ipfs.io'] });
1034
- const mongoProvider = createMongoDbStorageProvider({ uri: 'mongodb://...' });
1035
- const s3Provider = createS3StorageProvider({ bucket: 'wallet-backup' });
1036
-
1037
- // Add all providers
1038
- await sphere.addTokenStorageProvider(ipfsProvider);
1039
- await sphere.addTokenStorageProvider(mongoProvider);
1040
- await sphere.addTokenStorageProvider(s3Provider);
1041
-
1042
- // Sync syncs with ALL active providers
1043
- // If one fails, others continue (fault-tolerant)
1044
- const result = await sphere.payments.sync();
1045
- console.log(`Synced: +${result.added} -${result.removed}`);
1046
- ```
1047
-
1048
1038
  ## Dynamic Relay Management
1049
1039
 
1050
1040
  Nostr relays can be added or removed at runtime through the transport provider:
@@ -1124,7 +1114,7 @@ function getRelayStatuses() {
1124
1114
 
1125
1115
  ## Nametags
1126
1116
 
1127
- Nametags provide human-readable addresses (e.g., `@alice`) for receiving payments.
1117
+ Nametags provide human-readable addresses (e.g., `@alice`) for receiving payments. Valid formats: lowercase alphanumeric with `_` or `-` (3–20 chars), or E.164 phone numbers (e.g., `+14155552671`). Input is normalized to lowercase automatically.
1128
1118
 
1129
1119
  > **Important:** Nametags are required to use the testnet faucet. Register a nametag before requesting test tokens.
1130
1120
 
@@ -1258,6 +1248,10 @@ const bobTag = sphere.getNametagForAddress(1); // 'bob'
1258
1248
 
1259
1249
  ---
1260
1250
 
1251
+ See [IPFS Storage Guide](docs/IPFS-STORAGE.md) for complete IPFS/IPNS documentation including configuration, caching, merge rules, and troubleshooting.
1252
+
1253
+ ---
1254
+
1261
1255
  ## Known Limitations / TODO
1262
1256
 
1263
1257
  ### Wallet Encryption