@vainplex/shieldapi-cli 1.2.0 → 1.2.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/package.json +1 -1
- package/src/index.js +1 -1
- package/src/lib/api.js +5 -4
- package/src/lib/wallet.js +17 -10
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -14,7 +14,7 @@ export function run(argv) {
|
|
|
14
14
|
program
|
|
15
15
|
.name('shieldapi')
|
|
16
16
|
.description('🛡️ ShieldAPI CLI — Security intelligence from your terminal. Pay-per-request with USDC.')
|
|
17
|
-
.version('1.2.
|
|
17
|
+
.version('1.2.1')
|
|
18
18
|
.option('--wallet <key>', 'Private key for x402 payments (or set SHIELDAPI_WALLET_KEY)')
|
|
19
19
|
.option('--json', 'Output raw JSON instead of formatted output')
|
|
20
20
|
.option('--no-color', 'Disable colors')
|
package/src/lib/api.js
CHANGED
|
@@ -7,7 +7,7 @@ const BASE_URL = 'https://shield.vainplex.dev/api';
|
|
|
7
7
|
* @param {Record<string, string>} params - Query parameters
|
|
8
8
|
* @param {object} options
|
|
9
9
|
* @param {boolean} options.demo - Use demo mode
|
|
10
|
-
* @param {{
|
|
10
|
+
* @param {{ signer: object }|null} options.wallet - Wallet with x402 ClientEvmSigner
|
|
11
11
|
* @returns {Promise<object>} Parsed JSON response
|
|
12
12
|
*/
|
|
13
13
|
export async function apiRequest(endpoint, params = {}, { demo = false, wallet = null } = {}) {
|
|
@@ -28,8 +28,8 @@ export async function apiRequest(endpoint, params = {}, { demo = false, wallet =
|
|
|
28
28
|
return res.json();
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
// Paid endpoint — need wallet
|
|
32
|
-
if (!wallet?.
|
|
31
|
+
// Paid endpoint — need wallet with signer
|
|
32
|
+
if (!wallet?.signer) {
|
|
33
33
|
throw new Error(
|
|
34
34
|
'No wallet configured. Use --wallet <key> or set SHIELDAPI_WALLET_KEY environment variable.'
|
|
35
35
|
);
|
|
@@ -41,8 +41,9 @@ export async function apiRequest(endpoint, params = {}, { demo = false, wallet =
|
|
|
41
41
|
const { wrapFetchWithPayment } = await import('@x402/fetch');
|
|
42
42
|
|
|
43
43
|
// Create x402 client with EVM scheme registered
|
|
44
|
+
// wallet.signer is a ClientEvmSigner (from toClientEvmSigner) with .address + .signTypedData + .readContract
|
|
44
45
|
const client = new x402Client();
|
|
45
|
-
registerExactEvmScheme(client, { signer: wallet.
|
|
46
|
+
registerExactEvmScheme(client, { signer: wallet.signer });
|
|
46
47
|
|
|
47
48
|
// Wrap fetch with x402 payment handling
|
|
48
49
|
const paidFetch = wrapFetchWithPayment(fetch, client);
|
package/src/lib/wallet.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import { createWalletClient,
|
|
1
|
+
import { createWalletClient, createPublicClient, http } from 'viem';
|
|
2
2
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
3
3
|
import { base } from 'viem/chains';
|
|
4
|
+
import { toClientEvmSigner } from '@x402/evm';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
|
-
* Create a
|
|
7
|
-
*
|
|
7
|
+
* Create a wallet + x402-compatible signer from a private key.
|
|
8
|
+
*
|
|
9
|
+
* Uses toClientEvmSigner to compose an account (for signing) with
|
|
10
|
+
* a publicClient (for readContract), producing a ClientEvmSigner
|
|
11
|
+
* that has both `.address` and `.signTypedData` + `.readContract`.
|
|
8
12
|
*
|
|
9
13
|
* @param {string} privateKey - Hex private key (with or without 0x prefix)
|
|
10
|
-
* @returns {{
|
|
14
|
+
* @returns {{ signer: object, address: string }}
|
|
11
15
|
*/
|
|
12
16
|
export function createWallet(privateKey) {
|
|
13
17
|
if (!privateKey) return null;
|
|
@@ -16,23 +20,26 @@ export function createWallet(privateKey) {
|
|
|
16
20
|
|
|
17
21
|
try {
|
|
18
22
|
const account = privateKeyToAccount(key);
|
|
19
|
-
const
|
|
20
|
-
account,
|
|
23
|
+
const publicClient = createPublicClient({
|
|
21
24
|
chain: base,
|
|
22
25
|
transport: http(),
|
|
23
|
-
})
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// toClientEvmSigner composes account.signTypedData + publicClient.readContract
|
|
29
|
+
// and exposes .address correctly
|
|
30
|
+
const signer = toClientEvmSigner(account, publicClient);
|
|
24
31
|
|
|
25
|
-
return {
|
|
32
|
+
return { signer, address: account.address };
|
|
26
33
|
} catch (err) {
|
|
27
34
|
throw new Error(`Invalid private key: ${err.message}`);
|
|
28
35
|
}
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
/**
|
|
32
|
-
* Resolve wallet
|
|
39
|
+
* Resolve wallet/signer from CLI option or environment variable.
|
|
33
40
|
*
|
|
34
41
|
* @param {object} opts - Commander options
|
|
35
|
-
* @returns {{
|
|
42
|
+
* @returns {{ signer: object, address: string }|null}
|
|
36
43
|
*/
|
|
37
44
|
export function resolveWallet(opts) {
|
|
38
45
|
const key = opts?.wallet || process.env.SHIELDAPI_WALLET_KEY;
|