ROLA is method of authenticating something claimed by the user connected to your dApp with the Radix Wallet. It uses the capabilities of the Radix Network to make this possible in a way that is decentralized and flexible for the user.
RadixAPI offers ROLA as a Service, so that you do not need to run your own ROLA server. To use ROLA, follow the steps below:
Copy const getChallenge = async () => {
let challenge = '';
const params = {
'applicationName': 'Your dApp Name', // your dApp name as defined in the console
'dAppDefinitionAddress': 'account_xxx', // your dApp account address as defined in the console
'networkId': 1, // 1 = mainnet, 2 = testnet
'expectedOrigin': 'https://yourdomain.com', // the exact domain, use http://localhost:3000 for local development
'expires': 900 // 15 minutes in this case
};
try {
const requestOptionsRCV = {
method: "POST",
headers: {
'accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': "Bearer " + process.env.RADIXAPI_BEARER,
},
body: JSON.stringify(params),
};
const result = await fetch(
'https://api.radixapi.net/v1/challenge/create',
requestOptionsRCV
).then((res) => res.json());
challenge = result.challenge;
} catch (error) {
console.log("Error during challenge/create: ", error);
}
return new Promise((resolve, reject) => {
resolve(challenge);
});
};
Copy radixDappToolkit.walletApi.setRequestData(
DataRequestBuilder.persona().withProof(),
DataRequestBuilder.accounts().exactly(1).withProof()
);
radixDappToolkit.walletApi.provideChallengeGenerator(getChallenge);
Copy const verifyRola = async (personaProof) => {
let valid = false;
// all these params come from the rdt
const params = {
'type': personaProof.type,
'challenge': personaProof.challenge,
'proof': {
'publicKey': personaProof.proof.publicKey,
'signature': personaProof.proof.signature,
'curve': personaProof.proof.curve
},
'address': personaProof.address
};
const requestOptionsRCV = {
method: "POST",
headers: {
'accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': "Bearer " + process.env.RADIXAPI_BEARER,
},
body: JSON.stringify(params),
};
try {
const result = await fetch(
'https://api.radixapi.net/v1/challenge/verify',
requestOptionsRCV
).then((res) => res.json());
valid = result;
} catch (error) {
console.log("Error during challenge/verify: ", error);
}
return valid;
};
Copy radixDappToolkit.walletApi.dataRequestControl(async (walletData) => {
const valid = await verifyRola(walletData.proofs[0]);
if (valid.verification != 'successful') {
// raise error
throw new Error("Authentication failed!");
}
});