Transaction Confirmation
This section covers backend integration related to transaction confirmation.
How to Verify a Transaction Confirmation Result
- Verify the LoginID token.
- Compare the nonce values (if passed in as an option).
- Verify that the transaction hash (txHash) matches the original payload.
info
For verifying the LoginID token itself, click here.
To calculate the hash, follow this formula:
tx_hash = base64url(sha256(txPayload + nonce + lNonce));
Where the txPayload value is the txPayload sent in to LoginID.
Below is a NodeJS backend example demonstrating how to verify the transaction result.
- NodeJS
import { calculateTxHash } from "@/server/crypto";
import { checkAuthenticated } from "@/server/middleware/";
import jwt from "jsonwebtoken";
const LOGINID_BASE_URL = process.env.LOGINID_BASE_URL;
const LOGINID_API_KEY = process.env.LOGINID_API_KEY;
// Simulate a storage mechanism for persisting transaction data
const transactionStore = new Map();
app.post("/validateTx", checkAuthenticated, async (req, res) => {
try {
const { username } = req.username;
const { loginIdToken } = req.body;
// Verify the LoginID JWT
await verifyToken(loginIdToken);
// Decode and verify the JWT token
const decoded = jwt.decode(loginIdToken);
// Extract txHash, lNonce, and nonce from the JWT claims
const { txHash, lNonce, nonce } = decoded;
// Fetch the original nonce and txPayload from your transaction store
const { nonce: originalNonce, txPayload } = transactionStore.get(username);
// Compare nonces
if (nonce !== originalNonce) {
throw new Error("Nonce do not match");
}
// Calculated hashes should be the same. Look at bottom on
// how to calculate the hash
const calculatedHash = calculateTxHash(txPayload, nonce, lNonce);
if (calculatedHash !== txHash) {
throw new Error("Transaction payload do not match");
}
// Other business logic can go here
return res.status(204).end();
} catch (error) {
res.send(error);
}
});