Use ROLA

Rola as a Service

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.

See https://docs.radixdlt.com/docs/rola-radix-off-ledger-auth for more information.

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:

  1. Create a challenge and pass this to your RadixDappToolkit

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);
  });
};
radixDappToolkit.walletApi.setRequestData(
  DataRequestBuilder.persona().withProof(),
  DataRequestBuilder.accounts().exactly(1).withProof()
);
    
radixDappToolkit.walletApi.provideChallengeGenerator(getChallenge);
  1. Verify the signed challenge (use proof from your WalletAPI)

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;
};
radixDappToolkit.walletApi.dataRequestControl(async (walletData) => {
  const valid = await verifyRola(walletData.proofs[0]);
  if (valid.verification != 'successful') {
     // raise error
     throw new Error("Authentication failed!");
  }
});

Check this video for more help:

Last updated