如何从CLI将钱包导入Composer REST服务器并实现自动化导入?
Got it, I’ve helped a few folks automate this exact workflow for demos and streamlined network setups. Instead of manually using the POST /wallet/import endpoint through the UI, we can replicate that form upload behavior directly in a shell script with curl. Here’s a step-by-step solution:
Core Concept
When you upload a .card file via the REST UI, it sends a multipart/form-data POST request with two key parts:
- The .card file itself (attached under the
cardfield) - A
nameparameter to label the wallet entry
We can mirror this behavior in a script to loop through all your .card files and import them automatically.
Shell Script Implementation
Here’s a reusable script you can integrate into your network setup workflow. It assumes all your .card files live in a single directory (adjust paths as needed):
#!/bin/bash # Configuration - update these values to match your environment REST_SERVER_URL="http://localhost:3000" # Base URL of your Composer REST Server CARD_DIRECTORY="./network-cards" # Path to folder containing .card files # Validate the card directory exists if [ ! -d "$CARD_DIRECTORY" ]; then echo "Error: Card directory '$CARD_DIRECTORY' doesn't exist. Check your path." exit 1 fi # Loop through every .card file in the directory for card_file in "$CARD_DIRECTORY"/*.card; do # Skip if no .card files are found in the directory [ -e "$card_file" ] || continue # Use the filename (without .card extension) as the wallet entry name wallet_name=$(basename "$card_file" .card) echo "Starting import for $card_file as wallet entry '$wallet_name'..." # Send the POST request to import the card curl -X POST \ -F "card=@$card_file" \ -F "name=$wallet_name" \ "$REST_SERVER_URL/wallet/import" # Add spacing between import logs for readability echo -e "\n---" done echo "All card imports completed successfully!"
Key Customizations & Tips
- Authentication: If your REST server uses basic auth, add
--user your-username:your-passwordto thecurlcommand. For token-based auth, include-H "Authorization: Bearer YOUR_AUTH_TOKEN". - Custom Wallet Names: If you don’t want to use the filename as the wallet name, you can hardcode names or create a CSV mapping file (linking filenames to custom labels) and read from it in the loop.
- Error Handling: To make the script more robust, add
-w "%{http_code}"to thecurlcommand to capture HTTP response codes, then log failures if the code isn’t 200. - CORS Considerations: If running the script against a remote REST server, ensure the server has CORS enabled for your script’s origin (or run the script directly on the server hosting the REST API to avoid CORS issues).
How to Use
- Save the script as
import-wallet-cards.sh - Make it executable:
chmod +x import-wallet-cards.sh - Place all your .card files in the specified
CARD_DIRECTORY - Run the script:
./import-wallet-cards.sh
This will handle all imports automatically, eliminating the need for manual UI interactions—perfect for speeding up demos or automated network provisioning.
内容的提问来源于stack exchange,提问作者Simon Mullaney

