Open the marketplace and buy on a phone (v1)¶
Open the v1 marketplace (purchase with MetaMask [1]; data is handed over through simple-storage on :3000) in a phone browser and go as far as buying an item. The v2 VC flow on a phone is covered on a separate page, Marketplace as a phone app (Stage A).
What you'll do: Open the marketplace screen in a phone browser and walk through a purchase
Prerequisites: section 1.3 of Hands-on (listing and buying on the PC) is done, and the v1 stack is running (Quickstart (run the stack))
Keep running: the Quickstart stack used in 1.3 (terminals 1–7, including IPFS and PostgreSQL). The publisher is not used
What you need: PC + smartphone on the same network
Time required: approx. 20 min
Where this page belongs
- This is section 1.4 of Part 1 (Hands-on). This page is optional. It is not part of the Part 1 "Done when…" checks.
- Do the listing and buying on the PC first (section 1.3 of Hands-on). This page opens the same screens on a phone.
- If you stopped IPFS after §1.3, start it again with step 5 of Quickstart (run the stack).
- Previous page: section 1.3 of Hands-on
- Next page: section 2.1 of Part 2 (Share detections, not raw frames or Share disaster events)
Goal¶
Confirm that you can open the marketplace from a smartphone and buy an item.
What this page covers¶
- what is required to reach the marketplace UI from a smartphone
- the roles of the routes that iot-market-ui exposes (
/,/merchandise/[address],/purchased/[txHash],/seller) - which screens to check when opening and when buying
/mobile is not implemented
The legacy exercise program (examples/hands_on/mobile_viewer/)
assumes a /mobile route, but the current iot-market-ui does
not have one. Use the actual entry points listed below.
Common pitfalls¶
- if the PC side is not exposed on
0.0.0.0, the smartphone cannot reach it - even on what looks like the same Wi-Fi, a split network prevents the connection
- purchase actions do not progress well unless done through the MetaMask Mobile browser
Required environment¶
iot-market-uican be started on the PC- PC and smartphone are connected to the same LAN / Wi-Fi
- MetaMask Mobile or an equivalent wallet browser is available on the phone
Exercise Programs¶
This section is optional. The exercise programs assume a /mobile route, but the current iot-market-ui has no such route. You can skip this section. The steps from "Shortest path" onward are enough to complete 1.4.
This exercise builds the smartphone access URL from the PC's LAN IP.
The legacy problem program assumes /mobile; in the current code
read it as the homepage / instead.
Shortest path¶
For a first pass, these five steps are enough.
- prepare
iot-market-ui/.env.localand set the endpoint for your setup (127.0.0.1for PC-only, the LAN IP for a phone) - start
iot-market-uion0.0.0.0:5173 - check the PC LAN IP
- open
http://<HOST_IP>:5173/from the smartphone (homepage) - confirm that the search UI is visible
Branches after that:
- Specific item: open
http://<HOST_IP>:5173/merchandise/<address>directly (use theProduct deployed: 0x…address shown in themediator-ownerterminal (created by running the USB webcam sample in mock mode as in §1.3 of the Hands-on; the 5 items deployed in step 2 of the Quickstart deliver no data when bought)) - Purchase path: open the same URL in MetaMask Mobile
- VC-mediated path (v2): go to Marketplace as a phone app (Stage A)
- If the connection fails early: check Troubleshooting first
Prepare the connection baseline¶
The following diagram shows how the smartphone connects to the PC. The smartphone must reach two ports (5173 and 8545) on the PC's LAN IP.
flowchart LR
subgraph PHONE["Smartphone"]
BR["MetaMask in-app browser"]
MM["MetaMask wallet<br>(Account #35;2)"]
end
subgraph PC["PC (LAN IP)"]
UI["iot-market-ui<br>listens on 0.0.0.0:5173"]
HH["Hardhat node<br>listens on 0.0.0.0:8545"]
end
BR -->|"opens #lt;HOST_IP#gt;:5173"| UI
BR -->|"reads merchandise info<br>(VITE_RPC_URL in .env.local)"| HH
MM -->|"purchase transaction<br>(RPC URL added to MetaMask)"| HH
If .env.local or the MetaMask network still uses 127.0.0.1, the smartphone points to itself and never reaches the PC.
0. Configure the frontend endpoint (.env.local)¶
iot-market-ui reads the blockchain (RPC) and publisher endpoints from iot-market-ui/.env.local. If it points at the wrong host, the merchandise page returns HTTP 500 (if the file is missing, 127.0.0.1 is used for RPC and localhost for the publisher, so the phone cannot connect). Copy the template first and match it to your setup.
cd ~/program/Blockchain_IoT_Marketplace/iot-market-ui
cp .env.example .env.local
Edit .env.local and set the host for your scenario.
-
PC-only check: keep
127.0.0.1.VITE_RPC_URL=http://127.0.0.1:8545 VITE_PUBLISHER_URL=http://127.0.0.1:8080 -
From a phone: replace it with the PC's LAN IP. Find it with
ipconfig getifaddr en0on macOS (the same command as in step 2).VITE_RPC_URL=http://<HOST_IP>:8545 VITE_PUBLISHER_URL=http://<HOST_IP>:8080
Expose the Hardhat node on the LAN for phone access
The default npx hardhat node only listens on 127.0.0.1, so a phone or
MetaMask Mobile cannot reach it. For phone access, restart the node with
npx hardhat node --hostname 0.0.0.0 and allow 8545 through the PC firewall.
Restarting the node resets the chain and the deployed contracts are gone.
After restarting, redo step 2 (deploy) of the Quickstart (run the stack)
and restart mediator-owner and mediator-buyer as well. Earlier item addresses no longer work.
Run the USB webcam (or HUSKYLENS2) mock again and use the newly shown Product deployed address.
Restart the frontend (the Vite dev server [2] started by npm run dev) after editing .env.local.
1. Start the PC side on the LAN¶
First stop the npm run dev running in Terminal 3 of the Quickstart (run the stack) with Ctrl+C (it uses the same port 5173; otherwise the new server starts on another port). Then run this in the terminal you moved into iot-market-ui in step 0.
npm run dev -- --host 0.0.0.0 --port 5173
2. Check the PC LAN IP¶
Run this in another terminal; the step 1 terminal is still running npm run dev.
ipconfig getifaddr en0
ipconfig getifaddr en0 is a macOS command. On Windows use ipconfig; on Linux use ip addr. Replace <HOST_IP> in the following steps with the IP address you found here (the Hands-on also lists how to check it on each OS).
Open the marketplace from the smartphone¶
3. Open from the smartphone¶
http://<HOST_IP>:5173/
Example (homepage):
http://192.168.1.20:5173/
To open an individual item directly (use the Product deployed: 0x… address shown in the mediator-owner terminal (created by running the USB webcam sample in mock mode as in §1.3 of the Hands-on; the 5 items deployed in step 2 of the Quickstart deliver no data when bought; an item already bought in §1.3 cannot be bought again)):
http://<HOST_IP>:5173/merchandise/<merchandise_address>
4. Notes on purchasing¶
- To purchase from the smartphone, use the MetaMask Mobile in-app browser
- Add the local chain network to MetaMask Mobile beforehand:
- RPC URL:
http://<HOST_IP>:8545(e.g.http://192.168.1.20:8545) - Chain ID:
31337/ Currency symbol:ETH
- RPC URL:
- Also import the Account #2 Private Key into MetaMask Mobile and buy with that account (the same key as in Quickstart (run the stack)). Only Account #1 (
mediator-owner) and Account #2 (mediator-buyer) have a registered public key. The buyer-side mediator runs as Account #2. Buying with Account #1 delivers the data todownloadsofmediator-owner, and any other account reverts - If MetaMask rejects the network, confirm the node was started with
--hostname 0.0.0.0and that both.env.localand the MetaMask RPC use the PC's LAN IP, not 127.0.0.1
5. What to check¶
The following diagram shows the flow from a purchase until the data reaches mediator-buyer (v1 lane).
sequenceDiagram
autonumber
participant S as Smartphone<br>(MetaMask in-app browser)
participant M as Merchandise contract<br>(Hardhat :8545)
participant O as mediator-owner<br>(Account #35;1)
participant ST as simple-storage<br>(:3000)
participant B as mediator-buyer<br>(Account #35;2)
Note over M,B: At startup, the Account #35;2 public key<br>was registered in the PubKey contract
S->>M: purchase() (transaction signed by Account #35;2)
M-->>O: Purchase event (buyer's public key)
M-->>B: Purchase event (You bought a product of ...)
O->>ST: uploads the event file
ST-->>O: storage location (URI)
O->>M: emitUpload (URI encrypted with the public key)
M-->>B: Upload event (encrypted URI)
B->>B: decrypts the URI with its private key
B->>ST: downloads the file (File downloaded successfully)
B->>M: verify (file hash)
M-->>B: result (Verification result: true)
The data itself is never written to the chain; only its storage location, encrypted with the buyer's public key, is. Only mediator-buyer, which holds the matching private key, can decrypt it, so step 4 buys with Account #2. At the end, the hash of the received file is checked against the merchandise contract.
- The iot-market-ui search UI or a merchandise page opens on the smartphone
- The purchase action runs on an individual Merchandise page
- After purchase, the page moves to
/purchased/[txHash]. This v2 screen calls the publisher's/marketplace/claimto show a QR code for receiving a VC. On v1 the publisher is not running, so it shows "エラー: ..." (error), but the purchase itself has completed - Confirm the purchase in the PC's
mediator-buyerlog:You bought a product of ...followed byFile downloaded successfullyandVerification result: truemeans the data arrived from simple-storage (:3000)
Success example¶
- The iot-market-ui homepage opens from the smartphone
- You can proceed from a merchandise page to the purchase flow
-
You can reach the purchase confirmation screen through MetaMask Mobile
-
The
mediator-buyerlog shows the purchase and download lines
If you cannot connect or the purchase does not proceed, check the troubleshooting items below.
Troubleshooting¶
- Symptom: the smartphone cannot connect
- Check: both devices are on the same network
- Check: PC firewall settings allow access
- Check: the PC LAN IP did not change
- Symptom: the merchandise page returns HTTP 500
- Check:
VITE_RPC_URLiniot-market-ui/.env.localpoints at the running Hardhat node (8545) - Check: the endpoint is not someone else's IP or a wrong port such as
8546 - See the "Merchandise page returns 500" item in Troubleshooting
- Symptom: the purchase action does not proceed / cannot add the network to MetaMask
- Check: MetaMask Mobile in-app browser is being used
- Check: the MetaMask RPC and the node's exposure match (
--hostname 0.0.0.0+ LAN IP) - See the "Cannot add the network to MetaMask" item in Troubleshooting
- Symptom: the purchase transaction fails (revert) /
mediator-buyernever printsYou bought a product of - Check: the account selected in MetaMask Mobile is Account #2 (accounts other than #1 and #2 have no registered public key and cannot buy; data bought with Account #1 goes to
mediator-owner, notmediator-buyer) - Check: whether you already bought that item with the same account (an item bought in §1.3 cannot be bought again; use a
Product deployedaddress you have not bought yet) - Fix: import the same Account #2 Private Key as in Quickstart (run the stack) and buy again with that account
- See the "Purchase does not succeed" item in Troubleshooting
Next steps¶
Next is section 2.1 of Part 2. Pick one of Share detections, not raw frames and Share disaster events (Hands-on).
- 2.1 uses the publisher and does not use the Quickstart stack.
- First stop the Quickstart IPFS (
cd ~/program/Blockchain_IoT_Marketplace/ipfs && docker compose down). IPFS and the publisher both use port 8080. - Also stop the Quickstart frontend (terminal 3, port 5173) with Ctrl+C. Step 6 of Hand a credential to the buyer's wallet (Bridge / Stage 5) in 2.4 starts iot-market-ui again on port 5173.
- You may also stop the other Quickstart processes.
If you end Part 1 here, check the Part 1 "Done when…" list in Hands-on.
References¶
[1] MetaMask, "MetaMask," https://metamask.com/
[2] Vite, "Vite," https://vite.dev/