MIRACL Trust Client JS Library is available as an NPM package.
npm install --save @miracl/client-js
You can include the library in your build toolchain either as an ECMAScript or a CommonJS module. The library provides both a Node-style callback interface and a promise interface.
ESM:
// Callback interface
import MIRACLTrust from "@miracl/client-js";
// Promise interface
import MIRACLTrust from "@miracl/client-js/promise";
CJS:
// Callback interface
const MIRACLTrust = require("@miracl/client-js");
// Promise interface
const MIRACLTrust = require("@miracl/client-js/promise");
You can also use the pre-built browser version located in the dist/
directory:
<!-- Callback interface -->
<script src="dist/client.min.js"></script>
<!-- Promise interface -->
<script src="dist/client.promise.min.js"></script>
To configure the library:
- Create an application in the MIRACL Trust platform. For information about how to do it, see the Getting Started guide.
- Create a new instance of MIRACLTrust and pass the configuration as an object:
const mcl = new MIRACLTrust({
projectId: "<YOUR_PROJECT_ID>", // required
seed: "hexEncodedRandomNumberGeneratorSeed", // required
userStorage: localStorage, // required
deviceName: "Name of Device",
cors: true,
});
To register a new User ID, you need to verify it. MIRACL Trust offers two options for that:
-
Built-in Email Verification
With this type of verification, the end user's email address serves as the User ID. Currently, MIRACL Trust provides two kinds of built-in email verification methods:
- Email Link (default)
- Email Code
Start the verification by calling of the
sendVerificationEmail
method:Promise:
try { const result = await mcl.sendVerificationEmail(userId); console.log(result); } catch (err) { // Handle any potential errors }
Callback:
mcl.sendVerificationEmail(userId, function (err, result) { if (err) { // Handle any potential errors } console.log(result); });
Then, a verification email is sent, and a response with backoff and email verification method is returned.
If the verification method you have chosen for your project is:
-
Email Code:
You must check the email verification method in the response.
-
If the end user is registering for the first time or resetting their PIN, an email with a verification code will be sent, and the email verification method in the response will be
code
. Then, ask the user to enter the code in the application. -
If the end user has already registered another device with the same User ID, a Verification URL will be sent, and the verification method in the response will be
link
. In this case, proceed as described for the Email Link verification method below.
-
-
Email Link: Your application must open when the end user follows the Verification URL in the email.
- To register the mobile device, get an activation token using the
getActivationToken
method and the received Verification URL:
Promise:
try {
const result = await mcl.getActivationToken(
"https://yourdomain.com/verification/confirmation?userId=alice@miracl.com&code=theVerificationCode",
);
console.log(result);
} catch (err) {
// Handle any potential errors
}
Callback:
mcl.getActivationToken(
"https://yourdomain.com/verification/confirmation?userId=alice@miracl.com&code=theVerificationCode",
function callback(err, result) {
if (err) {
// Handle any potential errors
}
console.log(result.actToken);
},
);
- Pass the User ID (email or any string you use for identification) and
activation token to the
register
method.
Promise:
try {
const result = await mcl.register(userId, actToken, function (passPin) {
// Here you need to prompt the user for their PIN
// and then call the passPin argument with the value
passPin(pin);
});
console.log(result);
} catch (err) {
// Handle any potential errors
}
Callback:
mcl.register(
userId,
actToken,
function (passPin) {
// Here you need to prompt the user for their PIN
// and then call the passPin argument with the value
passPin(pin);
},
function callback(err) {
if (err) {
// Handle any potential errors
}
},
);
If you call the register
method with the same User ID more than once, the User
ID will be overridden. Therefore, you can use it when you want to reset your
authentication PIN code.
MIRACL Trust SDK offers two options:
The authenticate
method generates a JWT authentication token
for а registered user.
Promise:
try {
const result = await mcl.authenticate(userId, pin);
console.log(result.jwt);
} catch (err) {
// Handle any potential errors
}
Callback:
mcl.authenticate(userId, pin, function callback(err, result) {
if (err) {
// Handle any potential errors
}
// The JWT in the result needs to be verified by your back end
// to ensure that the authentication was successful
console.log(result.jwt);
});
After the JWT authentication token is generated, it needs to be sent to the
application server for verification. Then, the application server verifies the
token signature using the MIRACL Trust
JWKS endpoint and the audience
claim,
which in this case is the application Project ID.
When using the library to build a hybrid mobile application, you can use it as an authenticator to authenticate a user on another application or device. There are three options:
-
Authenticate with AppLink
Use the
authenticateWithAppLink
method:try { await mcl.authenticateWithAppLink(userId, appLink, pin); } catch (err) { // Handle any potential errors }
-
Authenticate with QR code
Use the
authenticateWithQRCode
method:try { await mcl.authenticateWithQRCode(userId, qrCode, pin); } catch (err) { // Handle any potential errors }
-
Authenticate with a push notification
Use the
authenticateWithNotificationPayload
method:try { await mcl.authenticateWithNotificationPayload(pushNotificationPayload, pin); } catch (err) { // Handle any potential errors }
For more information about authenticating users on separate applications and devices, see Cross-Device Authentication.
DVS stands for Designated Verifier Signature, which is a protocol for cryptographic signing of documents. For more information, see Designated Verifier Signature. In the context of this SDK, we refer to it as ‘Signing’.
To sign a document, use the sign
method as follows:
Promise:
try {
const signature = await mcl.sign(
userId,
pin,
documentHash,
documentTimestamp,
);
console.log(signature);
} catch (err) {
// Handle any potential errors
}
Callback:
mcl.sign(
userId,
pin,
documentHash,
documentTimestamp,
function callback(err, signature) {
if (err) {
// Handle any potential errors
return;
}
console.log(signature);
},
);
The signature needs to be verified. This is done when the signature is sent to
the application server, which then makes an HTTP call to the
POST /dvs/verify
endpoint. If the MIRACL Trust platform returns status code 200
, the
certificate
entry in the response body indicates that signing is successful.
QuickCode is a way to register another device without going through the verification process.
To generate a QuickCode, call the generateQuickCode
method:
Promise:
try {
const result = await mcl.generateQuickCode(userId, pin);
console.log(result.otp);
} catch (err) {
// Handle any potential errors
}
Callback:
mcl.generateQuickCode(userId, pin, function callback(err, result) {
if (err) {
// Handle any potential errors
}
console.log(result.otp);
});
When instantiated, the library automatically initialises a user management object, which can be accessed via the MIRACLTrust.users property and includes the following methods:
To retrieve all registered User IDs on the current device, use the list
method:
const list = mcl.users.list();
To check if a User ID is already registered on the device, use the exists
method:
const exists = mcl.users.exists("alice@miracl.com");
To delete the registration on the current device for a specified User ID, use
the remove
method:
mcl.users.remove("alice@miracl.com");
Note that this only affects the device on which it's executed. Any other registered devices will still be able to authenticate.