Initialize

Using the SDK requires two preliminary steps (for every execution of your application):

  1. Initialize the SDK: Call the initFordefiSDK function. This function should be called exactly once (for every execution of your application) and before calling any other SDK functions.
async function initSdk() {
    try {
        await initFordefiSDK();
            console.log('SDK was initiated successfully!');
    } catch (e) {
        const error = e as FordefiSdkErrorResult;
        console.log(`Init Failed. code :${error.code}, message: ${error.message}`);
    }
}
  1. Authenticate the SDK to the Fordefi backend: Call the login function and pass in an end-user authentication token. This function should be called after calling initFordefiSDK and before calling any other function. When the authentication token expires (as indicated by errors returned from other functions), the login function must be called again with a fresh authentication token.
async function Login() {
  try {
    const res = await login('my-auth-token');
    console.log(`Successful login. userId ${res.userId}, deviceState ${res.deviceState}!`);
  } catch (e) {
    const error = e as FordefiSdkErrorResult;
    console.log(`Login error. code :${error.code}, message: ${error.message}`);
  }
}

The login function validates the authorization token and checks the state of the user's MPC shares. On the user's first login, this function also runs the MPC key generation protocol. The login function then returns a DeviceState:

  • BACKUP_REQUIRED: For a new end user, before any further operation can be conducted, it is required to complete the keys backup. Learn more.
  • RECOVERY_REQUIRED: This is returned when logging in on a new device, which does not yet have a copy of the user's MPC shares. In this case, you need to use the recover Key method to retrieve the previously generated MPC shares. Learn more.
  • NO_OPERATION_REQUIRED: Keys are in sync. You can proceed.
  • ERROR: See the list of possible errors.