Skip to content

Read ServiceVC-written data with a PurchaseViewerVC (Stage 6)

This is the capstone exercise for Stages 1–5. In one session, you run a scenario in which a Seller writes data with a ServiceVC and a Buyer reads it with a PurchaseViewerVC.

What you'll do: For one dataset, connect writes with a ServiceVC to reads with a PurchaseViewerVC

Prerequisites: you have done Hand a credential to the buyer's wallet (Bridge / Stage 5) and Authenticate machines (Service VC / Stage 4 prep)

Keep running: nothing needs to be running. Step E0 starts the Hardhat node, the publisher, the bridge and iot-market-ui. If iot-market-ui and Metro from Stage 5 are running, keep using them

What you need: PC + MetaMask + smartphone (iw3ip-wallet)

Time required: approx. 90 min

Where this page belongs

Capstone exercise for Stages 1–5

In one session, you run a scenario in which the Seller writes data continuously with a ServiceVC and the Buyer reads it with a PurchaseViewerVC. The backend is the one from Stages 1–5 plus a change that obtains the dataset_id dynamically from the Merchandise additionalInfo (this site calls it Stage 6 case B).

Choosing a dataset

This note is optional. You can skip it the first time. The example uses home/env/temperature, but the deploy script that you run in Step E0 also creates Merchandise contracts for home/event/possible_littering and home/event/flood_risk_high at the same time. Of the two Stage 0 event datasets, only possible_littering has ServiceVC / PurchaseViewerVC PDs, so use possible_littering (Merchandise #2) when you run Service ingest → Purchase → Viewer fetch with the same event format as Stage 0.

Goal

  • Confirm that writes with a ServiceVC and reads with a PurchaseViewerVC connect through one dataset. This exercise builds on the Stage 1 (ConsentVC) and Stage 3 (ViewerVC) flows; the steps on this page use two VC types, ServiceVC and PurchaseViewerVC (SellerVC, which handles the seller's identity, is covered in Stage 7)
  • Understand the division of roles between on-chain payment (MetaMask) and off-chain authorization (VC) through one continuous flow
  • Read the links in the audit log: follow the relationship ETH key → did:jwk → ServiceVC holder → PurchaseViewerVC holder

What this page covers

  • Data written with a ServiceVC can be read correctly with a PurchaseViewerVC for the same dataset (Stage 4 prep joined with Stage 5)
  • With Stage 6 case B, the Merchandise holds the dataset_id on-chain, and the bridge / iot-market-ui resolve the dataset without hardcoding
  • The links in the audit log that remain for one dataset

Overview

flowchart TB
  SELLER["Seller"]
  S1["present ServiceVC in the wallet<br>→ ServiceToken (1h)"]
  S2["POST /platform/ingest x N<br>→ accumulates in app.state.ingested"]
  BUYER["Buyer"]
  BL["bridge listener"]
  PUB["publisher"]
  W["iw3ip-wallet"]
  GET["GET /platform/data?merchandise=&lt;addr&gt;"]
  SELLER --> S1 --> S2
  BUYER -->|"Merchandise.purchase() with MetaMask<br>→ Purchase event"| BL
  BL -->|"resolves dataset_id from<br>Merchandise.additionalInfo<br>POST /marketplace/claim"| PUB
  PUB -->|"PurchaseViewerVC offer"| W
  W -->|"receive via OID4VCI → eth↔did binding<br>present via OID4VP → ViewerToken"| GET
  S2 -.->|"the N rows written by the Seller reach the Buyer"| GET

Required environment

  • You have gone through Stage 1 / Stage 3 / Stage 4 prep / Stage 5
  • The Stage 5 Hardhat node is restarted in Step E0 (publisher and iot-market-ui are started in E0, and the bridge and the publisher are recreated there)
  • $HOST_IP in commands and <HOST_IP> in URLs stand for the PC's LAN IP. In each terminal you use, first run export HOST_IP='<your PC LAN IP>' (see the Hands-on for how to find it). Replace <HOST_IP> in URLs you type into a browser or phone with the same IP
  • The steps assume a Mac and an iPhone, with the course repository cloned to ~/program/Blockchain_IoT_Marketplace and the wallet cloned to ~/program/iw3ip-wallet
  • In this hands-on, one iPhone wallet plays two roles (seller + buyer) for a single person (separating seller and buyer as in real operation is future work)

Step E0. Start the prerequisite environment (same as Stage 5)

If the Stage 5 Hardhat node is still running, first stop the Hardhat node in Terminal A with Ctrl+C. If the old node keeps port 8545, the new node cannot start, the deploy lands on the old chain, and the addresses do not match the table in Step E2.

Start the Hardhat node in Terminal A and leave it running (stop it with Ctrl+C).

cd ~/program/Blockchain_IoT_Marketplace/iot-market
git checkout main && git pull --ff-only
npx hardhat node --hostname 0.0.0.0

When Started HTTP and WebSocket JSON-RPC server appears, continue in Terminal B. First, deploy the latest version, in which Merchandise holds dataset_id in additionalInfo.

cd ~/program/Blockchain_IoT_Marketplace/iot-market
npx hardhat run scripts/deployMerchandiseWithIoTMarket.ts --network localhost

Note the IoTMarket address in the output (0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512) and the addresses of Merchandise #0 to #4.

Next, open the Hardhat console to register a PubKey for the buyer (Account #2).

npx hardhat console --network localhost

When the console opens, paste the following.

const [_, __, buyer] = await ethers.getSigners();
const pk = await ethers.getContractAt("PubKey", "0x5FbDB2315678afecb367f032d93F642f64180aa3", buyer);
await (await pk.registerKey("[handson]")).wait();
.exit   // leave the console and return to the shell

Then, in the same Terminal B, start the publisher and the bridge.

cd ~/program/Blockchain_IoT_Marketplace
docker compose -f infra/docker-compose.yml --profile ssi-wallet --profile mv-bridge up --build -d

Hardhat was restarted, so recreate the bridge and the publisher. The bridge watches from the block number at its startup. The publisher keeps the claims of the old chain in memory and, unless recreated, would return an already used code for a purchase with the same transaction ID. After recreating them, wait a few seconds for the bridge to start, then check the bridge log.

docker compose -f infra/docker-compose.yml --profile mv-bridge up -d --force-recreate publisher bridge
sleep 5
docker logs iw3ip-mv-bridge 2>&1 | tail -5

Expected output:

bridge: listening to 5 merchandise(s)
bridge: starting poll from block <current block number>
bridge: started rpc=http://host.docker.internal:8545 market=0xe7f1725... publisher=http://publisher:8080

Start iot-market-ui in another terminal (skip this if the Stage 5 Terminal D server is still running).

cd ~/program/Blockchain_IoT_Marketplace/iot-market-ui
npm run dev -- --host 0.0.0.0 --port 5173

Because Hardhat was restarted, if you use MetaMask, clear the old chain's nonce with Settings → Advanced → "Clear activity tab data" (or "Reset Account").

If you get stuck, see Troubleshooting in the Stage 5 hands-on.


Step E1. Seller phase — continuous writes with a ServiceVC

What to check

  • Presenting one ServiceVC is enough for multiple ingests to pass
  • The dataset written to (home/env/temperature) is what the buyer reads later

Steps

In a PC browser (Mac):

http://<HOST_IP>:8080/issuer/offer?type=ServiceVC&dataset_id=home/env/temperature&purpose=write_continuous

Show the QR code → scan it with the iPhone wallet → approve "IW3IP Service Credential" (shown as "IW3IP サービスクレデンシャル" in a Japanese-language wallet).

Then present it:

http://<HOST_IP>:8080/verifier/request?dataset_id=home/env/temperature&vc_kind=ServiceVC

QR code → select the ServiceVC in the wallet and present it.

Extract the token → five consecutive ingests

PUB=$(docker ps -qf name=publisher)
SERVICE=$(docker logs $PUB 2>&1 | grep "service_token_issued" | tail -1 | sed -E 's/.*token=([^ ]+).*/\1/')

for i in 1 2 3 4 5; do
  curl -s -X POST http://$HOST_IP:8080/platform/ingest \
    -H "Authorization: Bearer $SERVICE" \
    -H "Content-Type: application/json" \
    -d "{\"dataset_id\":\"home/env/temperature\",\"value\":$((30 + i))}" \
    | python3 -c "import json,sys;d=json.load(sys.stdin);print('iter',$i,d)"
done

Expected output

iter 1 {'status': 'received', 'count': N}
iter 2 {'status': 'received', 'count': N+1}
iter 3 {'status': 'received', 'count': N+2}
iter 4 {'status': 'received', 'count': N+3}
iter 5 {'status': 'received', 'count': N+4}

N is the number of rows the publisher had received before these writes, plus 1. Right after the publisher is recreated, N is 1.

Five rows with values 31–35 have now been accumulated in home/env/temperature in the seller role.


Step E2. Buyer phase — purchase a Merchandise

What to check

  • With Stage 6 case B, the bridge obtains the dataset_id written in the Merchandise additionalInfo automatically from on-chain
  • The five Merchandise contracts have different datasets (temperature / littering / flood). In this hands-on, choose a temperature Merchandise

Datasets of the Merchandise contracts

Merchandise Address dataset_id
#0 0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0 home/env/temperature
#1 0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9 home/env/temperature
#2 0x0165878A594ca255338adfa4d48449f69242Eb8F home/event/possible_littering
#3 0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6 home/env/temperature
#4 0x610178dA211FEF7D417bC0e6FeD39F05609AD788 home/event/flood_risk_high

(The contracts are deployed deterministically, so the values are the same every time.)

You want to read the temperature data written with the ServiceVC, so choose a temperature Merchandise that has not been purchased yet (for example, #3).

Steps (using the Hardhat console)

cd ~/program/Blockchain_IoT_Marketplace/iot-market
npx hardhat console --network localhost
const [_, __, buyer] = await ethers.getSigners();
const merch = await ethers.getContractAt(
  "Merchandise",
  "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6",   // Merchandise #3
  buyer
);
const tx = await merch.purchase({ value: await merch.getPrice() });
const r = await tx.wait();
console.log("tx hash:", r.hash, "status:", r.status);
.exit   // leave the console and return to the shell

→ tx hash: 0x..., status: 1

Expected: the bridge resolves the dataset_id from on-chain

docker logs iw3ip-mv-bridge 2>&1 | grep "Purchase event" | tail -1

→ The log contains dataset=home/env/temperature:

bridge: Purchase event from 0x2279B7A0... buyer=0x3C44... dataset=home/env/temperature tx=0x...

Confirm that the dataset is obtained from on-chain and is not a hardcoded value.

docker logs iw3ip-mv-bridge 2>&1 | grep "claim ok" | tail -1

Expected output:

bridge: claim ok jti=<claim_id> deeplink=openid-credential-offer://?credential_offer=...

In Step E3, you extract the part after deeplink= from this line and turn it into a QR code.


Step E3. Buyer phase — receive the PurchaseViewerVC

What to check

  • The receiving procedure is the same as in Stage 5, but the target dataset matches the one the seller wrote to
DEEPLINK=$(docker logs iw3ip-mv-bridge 2>&1 | grep "claim ok" | tail -1 | sed -E 's/.*deeplink=//')
ENCODED=$(python3 -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1]))" "$DEEPLINK")
open "https://api.qrserver.com/v1/create-qr-code/?size=400x400&data=$ENCODED"

open is a macOS command. On Linux use xdg-open, on Windows (WSL) use explorer.exe, or paste the printed URL into a browser.

Scan the QR code with the iPhone wallet and approve "IW3IP Purchase Viewer (Event-only)" (shown as "IW3IP 購入閲覧(Tier 1 / イベントのみ)" in a Japanese-language wallet). The command above sends the deeplink to an external QR generation service, so do not use it outside a local hands-on environment.

Confirm in the wallet that the VC claims contain dataset_id: home/env/temperature.

The code in the deeplink expires 30 minutes after the purchase claim. Receive the VC in the wallet within 30 minutes of the Step E2 purchase. If it has expired and the publisher's /issuer/token returns 400 invalid_grant, follow F in Troubleshooting in the Stage 5 hands-on: buy a temperature Merchandise you have not bought yet (#0 or #1) as in Step E2, and also set MERCHANDISE in Step E4 to that address.

audit:

curl -s "http://$HOST_IP:8080/audit/logs?limit=3" | python3 -m json.tool | grep eth_did_bound

→ eth_did_bound:claim=...:eth=0x3C44...:tx=0x...


Step E4. Buyer phase — obtain a ViewerToken and fetch the data

What to check

  • Presenting the PurchaseViewerVC yields a ViewerToken (Stage 5)
  • Fetching with merchandise=<addr> goes through the dataset_id resolved by the bridge, and you can read the values 31–35 that the seller wrote in Step E1
  • This is the point Stage 6 is meant to confirm

Steps

Request a presentation in a PC browser:

http://<HOST_IP>:8080/verifier/request?dataset_id=home/env/temperature&vc_kind=PurchaseViewerVC

QR code → in the wallet, select the PurchaseViewerVC (do not confuse it with the ServiceVC or the ViewerVC) and present it.

The ViewerToken expires after 60 seconds. Run the next commands right after presenting; if you get viewer_token_expired, present again. The first line is needed when you run this in a different terminal from Step E1.

PUB=$(docker ps -qf name=publisher)
TOKEN=$(docker logs $PUB 2>&1 | grep "viewer_token_issued vc_kind=PurchaseViewerVC" | tail -1 | sed -E 's/.*token=([^ ]+).*/\1/')
echo "TOKEN=$TOKEN"

MERCHANDISE=0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6
curl -s -H "Authorization: Bearer $TOKEN" \
  "http://$HOST_IP:8080/platform/data?merchandise=$MERCHANDISE" | python3 -m json.tool

Expected output

{
    "dataset_id": "home/env/temperature",
    "count": 5,
    "read_count": 1,
    "seller_did": "unknown",
    "allowed_views": [
        "event"
    ],
    "rows": [
        {
            "dataset_id": "home/env/temperature",
            "value": 31
        },
        {
            "dataset_id": "home/env/temperature",
            "value": 32
        },
        {
            "dataset_id": "home/env/temperature",
            "value": 33
        },
        {
            "dataset_id": "home/env/temperature",
            "value": 34
        },
        {
            "dataset_id": "home/env/temperature",
            "value": 35
        }
    ]
}

count is 5 only when nothing else has been written to this dataset. If you ingested into the same dataset before Step E1, the count is higher by that amount.

The Buyer has read the five rows that the Seller wrote in Step E1. This completes Stage 6.


Step E5. The audit log chain

What to check

  • For one dataset, multiple subjects and operations are recorded in order
curl -s "http://$HOST_IP:8080/audit/logs?limit=20" | python3 -m json.tool \
  | grep -E "raw_topic|reason|holder_did|subject_did" | head -40

Expected chain (newest first, excerpt):

Stage raw_topic reason Subject
1. Buyer reads platform/data viewer_token_used:<jti>:1:views=event did:jwk:... (buyer)
2. Buyer presents oid4vp/response ok did:jwk:... (buyer)
3. Buyer receives marketplace/issued eth_did_bound:claim=...:eth=0x3C44...:tx=... did:jwk:... (buyer)
4. Bridge hand-off marketplace/claim claim_received:<id>:tx=... eth:0x3C44...
5. Seller writes x5 platform/ingest service_token_used:<jti>:1–5 did:jwk:... (seller)
6. Seller presents oid4vp/response ok did:jwk:... (seller)

Note: In this hands-on the same wallet acts as both seller and buyer, so the holder_did is identical. In real operation they are separate devices and separate did:jwk values.

Points to observe

  • What links (4) and (5) is the dataset_id: both are home/env/temperature
  • (4) has an ETH subject → it is bound to a did:jwk in (3) → the read in (1) is done as that did:jwk, so three forms of identity are chained
  • The ServiceVC's service_token_used occurs five times in a row → it is read once with viewer_token_used:1, and the five rows arrive together

Completion matrix

Step Check item Example output
E0 Environment started bridge: listening to 5 merchandise(s)
E1 ServiceVC received "IW3IP Service Credential" in the wallet
E1 ServiceToken issued publisher log service_token_issued ttl=3600s
E1 Five consecutive ingests iter 1–5 all status: received
E2 Bridge resolves the dataset_id on-chain dataset=home/env/temperature
E2 Bridge claim issued claim ok jti=...
E3 PurchaseViewerVC received "IW3IP Purchase Viewer (Event-only)" in the wallet
E3 eth_did_bound audit marketplace/issued
E4 Five rows fetched by reverse lookup from the merchandise count: 5, rows: [..., {value: 31}, ..., {value: 35}]
E5 The audit chain can be read service_token_used and viewer_token_used are chained on the same dataset

Comparison with the v1 lane (recap)

Aspect v1 (emitUpload) v2 / Stage 6
Seller identity implicit (owner of the Merchandise) holder_did of the ServiceVC
Seller writes (mediator-owner puts it in simple-storage) ingest to the publisher (left in the audit log)
Buyer identity MetaMask eth address only eth + did:jwk (bound by eth_did_bound)
Data delivery encryptURI decryption publisher API (Bearer ViewerToken)
Audit coverage on-chain Upload event only 10 rows in the publisher audit log (seller presentation 1 + writes 5 + bridge claim 1 + VC issuance 1 + buyer presentation 1 + read 1)

Troubleshooting

A. dataset= in the bridge log does not match the item's dataset

Cause: The Merchandise is from an old deployment and has no dataset_id (a deployment from before Stage 6 case B). The bridge then uses the default home/env/temperature. For the temperature items (#0, #1, #3) the log looks the same, so to tell them apart buy #2 (home/event/possible_littering) or #4 (home/event/flood_risk_high) and check that dataset= is not home/env/temperature. You can also check additionalInfo on the /merchandise/<address> page.

Fix: Restart the Hardhat node (Ctrl+C → npx hardhat node --hostname 0.0.0.0) and then redeploy with the latest deployMerchandiseWithIoTMarket.ts. MetaMask needs its chainId cache reset (Troubleshooting A in Stage 5).

A2. SERVICE is empty at the start of Step E1

SERVICE=

Cause: The publisher was recreated and the past service_token_issued logs are gone, or you have not yet completed the issuance and presentation of Step E1. (Step E0 recreates the publisher, so tokens from earlier sessions are not in the logs)

Fix: Run the two steps "issue the ServiceVC → receive it in the wallet → present the ServiceVC", and then grep the token. Issuance and presentation use different URLs (/issuer/offer and /verifier/request), so run both.

B. count: 0 in Step E4

Cause: app.state.ingested has been cleared, for example because the ServiceToken ingest in Step E1 ran in a different publisher session from the presentation (the publisher was restarted).

Fix: Do not restart the publisher between Step E1 and Step E4. If you have restarted it, start over from Step E1.

C. Other issues

See also Troubleshooting in Stage 5.


Limits

  • Seller and buyer share one wallet: true seller-buyer separation needs two iPhones or a UI that supports switching accounts inside the wallet
  • SellerVC: the mechanism that requires an identity VC when a seller registers a Merchandise is not covered on this page (it is covered in Stage 7)
  • Observing continuity: load testing of the case where multiple buyers read within the 1-hour TTL of the ServiceVC (multi-tenant read) has not been done
  • EIP-712 signatures: protection against impersonation of eth_addr ↔ did:jwk is still at the minimal-implementation stage

Next steps

Next is Back the seller's identity (Seller VC / Stage 7). You back the seller's identity with a SellerVC.

  • Keep the Hardhat node (Terminal A) running. Stage 7 uses the contracts deployed in Step E0. If you restart the node, redo Step E0 first.
  • You can also leave the publisher, the bridge, iot-market-ui and Metro running. Step S0 of Stage 7 recreates the publisher.