x402storage 1.0.1 → 1.1.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/dist/config.d.ts +20 -7
- package/dist/config.js +115 -13
- package/dist/index.js +32 -5
- package/dist/upload.d.ts +1 -1
- package/dist/upload.js +1 -1
- package/package.json +1 -1
package/dist/config.d.ts
CHANGED
|
@@ -1,13 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Wallet configuration for x402storage CLI
|
|
3
|
+
*
|
|
4
|
+
* Priority order:
|
|
5
|
+
* 1. X402_PRIVATE_KEY environment variable (if set)
|
|
6
|
+
* 2. Stored wallet in ~/.x402storage/wallet.json
|
|
7
|
+
* 3. Auto-generate new wallet (first use)
|
|
3
8
|
*/
|
|
4
9
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
10
|
+
* Gets the private key, with priority:
|
|
11
|
+
* 1. X402_PRIVATE_KEY env var
|
|
12
|
+
* 2. Stored wallet
|
|
13
|
+
* 3. Auto-generate new wallet
|
|
14
|
+
*
|
|
15
|
+
* Returns the private key and shows helpful messages for new wallets.
|
|
7
16
|
*/
|
|
8
|
-
export declare function
|
|
17
|
+
export declare function getPrivateKey(): `0x${string}`;
|
|
9
18
|
/**
|
|
10
|
-
*
|
|
11
|
-
* Should only be called after validateEnvironment().
|
|
19
|
+
* Get the current wallet address (for display purposes)
|
|
12
20
|
*/
|
|
13
|
-
export declare function
|
|
21
|
+
export declare function getWalletAddress(): `0x${string}` | null;
|
|
22
|
+
/**
|
|
23
|
+
* Validate environment - now just a no-op since we auto-create wallets
|
|
24
|
+
* Kept for backwards compatibility
|
|
25
|
+
*/
|
|
26
|
+
export declare function validateEnvironment(): void;
|
package/dist/config.js
CHANGED
|
@@ -1,23 +1,125 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Wallet configuration for x402storage CLI
|
|
3
|
+
*
|
|
4
|
+
* Priority order:
|
|
5
|
+
* 1. X402_PRIVATE_KEY environment variable (if set)
|
|
6
|
+
* 2. Stored wallet in ~/.x402storage/wallet.json
|
|
7
|
+
* 3. Auto-generate new wallet (first use)
|
|
3
8
|
*/
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { homedir } from 'node:os';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
|
|
13
|
+
const CONFIG_DIR = join(homedir(), '.x402storage');
|
|
14
|
+
const WALLET_FILE = join(CONFIG_DIR, 'wallet.json');
|
|
4
15
|
/**
|
|
5
|
-
*
|
|
6
|
-
* Exits with code 1 and helpful error message if X402_PRIVATE_KEY is missing.
|
|
16
|
+
* Ensure the config directory exists
|
|
7
17
|
*/
|
|
8
|
-
|
|
9
|
-
if (!
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
function ensureConfigDir() {
|
|
19
|
+
if (!existsSync(CONFIG_DIR)) {
|
|
20
|
+
mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generate a new wallet
|
|
25
|
+
*/
|
|
26
|
+
function generateWallet() {
|
|
27
|
+
const privateKey = generatePrivateKey();
|
|
28
|
+
const account = privateKeyToAccount(privateKey);
|
|
29
|
+
return {
|
|
30
|
+
privateKey,
|
|
31
|
+
address: account.address,
|
|
32
|
+
createdAt: Date.now(),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Save wallet to config file
|
|
37
|
+
*/
|
|
38
|
+
function saveWallet(wallet) {
|
|
39
|
+
ensureConfigDir();
|
|
40
|
+
writeFileSync(WALLET_FILE, JSON.stringify(wallet, null, 2), { mode: 0o600 });
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Load wallet from config file
|
|
44
|
+
*/
|
|
45
|
+
function loadWallet() {
|
|
46
|
+
try {
|
|
47
|
+
if (!existsSync(WALLET_FILE)) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const data = readFileSync(WALLET_FILE, 'utf-8');
|
|
51
|
+
const wallet = JSON.parse(data);
|
|
52
|
+
// Validate wallet shape
|
|
53
|
+
if (wallet &&
|
|
54
|
+
typeof wallet === 'object' &&
|
|
55
|
+
typeof wallet.privateKey === 'string' &&
|
|
56
|
+
typeof wallet.address === 'string' &&
|
|
57
|
+
wallet.privateKey.startsWith('0x') &&
|
|
58
|
+
wallet.address.startsWith('0x')) {
|
|
59
|
+
return wallet;
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get or create wallet
|
|
69
|
+
* Returns the wallet and whether it was newly created
|
|
70
|
+
*/
|
|
71
|
+
function getOrCreateWallet() {
|
|
72
|
+
const existing = loadWallet();
|
|
73
|
+
if (existing) {
|
|
74
|
+
return { wallet: existing, isNew: false };
|
|
15
75
|
}
|
|
76
|
+
const newWallet = generateWallet();
|
|
77
|
+
saveWallet(newWallet);
|
|
78
|
+
return { wallet: newWallet, isNew: true };
|
|
16
79
|
}
|
|
17
80
|
/**
|
|
18
|
-
* Gets the private key
|
|
19
|
-
*
|
|
81
|
+
* Gets the private key, with priority:
|
|
82
|
+
* 1. X402_PRIVATE_KEY env var
|
|
83
|
+
* 2. Stored wallet
|
|
84
|
+
* 3. Auto-generate new wallet
|
|
85
|
+
*
|
|
86
|
+
* Returns the private key and shows helpful messages for new wallets.
|
|
20
87
|
*/
|
|
21
88
|
export function getPrivateKey() {
|
|
22
|
-
|
|
89
|
+
// Check env var first (for power users / CI)
|
|
90
|
+
if (process.env.X402_PRIVATE_KEY) {
|
|
91
|
+
return process.env.X402_PRIVATE_KEY;
|
|
92
|
+
}
|
|
93
|
+
// Get or create wallet
|
|
94
|
+
const { wallet, isNew } = getOrCreateWallet();
|
|
95
|
+
if (isNew) {
|
|
96
|
+
console.log('');
|
|
97
|
+
console.log('Created new wallet for x402.storage');
|
|
98
|
+
console.log(`Address: ${wallet.address}`);
|
|
99
|
+
console.log('');
|
|
100
|
+
console.log('Fund this wallet with USDC on Base to upload files.');
|
|
101
|
+
console.log('Visit https://x402.storage to add funds.');
|
|
102
|
+
console.log('');
|
|
103
|
+
console.log(`Wallet saved to: ${WALLET_FILE}`);
|
|
104
|
+
console.log('');
|
|
105
|
+
}
|
|
106
|
+
return wallet.privateKey;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Get the current wallet address (for display purposes)
|
|
110
|
+
*/
|
|
111
|
+
export function getWalletAddress() {
|
|
112
|
+
if (process.env.X402_PRIVATE_KEY) {
|
|
113
|
+
const account = privateKeyToAccount(process.env.X402_PRIVATE_KEY);
|
|
114
|
+
return account.address;
|
|
115
|
+
}
|
|
116
|
+
const wallet = loadWallet();
|
|
117
|
+
return wallet?.address ?? null;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Validate environment - now just a no-op since we auto-create wallets
|
|
121
|
+
* Kept for backwards compatibility
|
|
122
|
+
*/
|
|
123
|
+
export function validateEnvironment() {
|
|
124
|
+
// No-op - we auto-create wallets now
|
|
23
125
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command } from 'commander';
|
|
3
3
|
import { resolve } from 'node:path';
|
|
4
|
-
import {
|
|
4
|
+
import { getPrivateKey, getWalletAddress } from './config.js';
|
|
5
5
|
import { uploadFile } from './upload.js';
|
|
6
6
|
import { FileNotFoundError, UploadError } from './errors.js';
|
|
7
|
-
// Validate environment at startup - fail fast with clear error
|
|
8
|
-
validateEnvironment();
|
|
9
7
|
const program = new Command();
|
|
10
8
|
program
|
|
11
9
|
.name('x402storage')
|
|
12
10
|
.description('Upload files to x402.storage with permanent IPFS storage')
|
|
13
|
-
.version('1.0.0')
|
|
11
|
+
.version('1.0.0');
|
|
12
|
+
// Default command: upload a file
|
|
13
|
+
program
|
|
14
14
|
.argument('<file>', 'file path to upload')
|
|
15
15
|
.action(async (filePath) => {
|
|
16
16
|
try {
|
|
@@ -19,7 +19,7 @@ program
|
|
|
19
19
|
const privateKey = getPrivateKey();
|
|
20
20
|
const result = await uploadFile(resolvedPath, privateKey);
|
|
21
21
|
console.log('Success! File stored permanently.');
|
|
22
|
-
console.log(`URL: ${result.
|
|
22
|
+
console.log(`URL: ${result.url}`);
|
|
23
23
|
console.log(`CID: ${result.cid}`);
|
|
24
24
|
process.exit(0);
|
|
25
25
|
}
|
|
@@ -32,6 +32,10 @@ program
|
|
|
32
32
|
if (error.type === 'insufficient_balance') {
|
|
33
33
|
console.error('Error: Insufficient balance.\n');
|
|
34
34
|
console.error('Fund your wallet at https://x402.storage or transfer USDC on Base.');
|
|
35
|
+
const address = getWalletAddress();
|
|
36
|
+
if (address) {
|
|
37
|
+
console.error(`Your wallet address: ${address}`);
|
|
38
|
+
}
|
|
35
39
|
process.exit(1);
|
|
36
40
|
}
|
|
37
41
|
if (error.type === 'network_error') {
|
|
@@ -48,4 +52,27 @@ program
|
|
|
48
52
|
process.exit(1);
|
|
49
53
|
}
|
|
50
54
|
});
|
|
55
|
+
// Wallet command: show wallet info
|
|
56
|
+
program
|
|
57
|
+
.command('wallet')
|
|
58
|
+
.description('Show your wallet address')
|
|
59
|
+
.action(() => {
|
|
60
|
+
// Check if wallet exists first
|
|
61
|
+
let address = getWalletAddress();
|
|
62
|
+
// If no wallet exists, create one (getPrivateKey will show creation message)
|
|
63
|
+
if (!address) {
|
|
64
|
+
getPrivateKey();
|
|
65
|
+
address = getWalletAddress();
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// Wallet exists, just show info
|
|
69
|
+
console.log('');
|
|
70
|
+
console.log('x402.storage wallet');
|
|
71
|
+
console.log(`Address: ${address}`);
|
|
72
|
+
console.log('');
|
|
73
|
+
console.log('Fund this wallet with USDC on Base to upload files.');
|
|
74
|
+
console.log('Visit https://x402.storage to add funds.');
|
|
75
|
+
console.log('');
|
|
76
|
+
}
|
|
77
|
+
});
|
|
51
78
|
program.parse();
|
package/dist/upload.d.ts
CHANGED
package/dist/upload.js
CHANGED