The Virto SDK provides the core functionalities that abstract the interaction with the blockchain and WebAuthn authentication.
Configuration
import SDK from '@virtonetwork/sdk';
const sdk = new SDK({
federate_server: 'http://localhost:3000/api',
provider_url: 'ws://localhost:12281',
config: {
wallet: 'polkadotjs'
}
});
Auth Methods
The sdk.auth object provides the following methods:
register(user)
Registers a new user with a WebAuthn passkey.
Parameters:
user: Object containing user profile and metadata
{
profile: {
id: string, // Required: Unique identifier for the user
name: string, // User's full name
}
}
Returns: Promise that resolves to the registration response with blockchain address.
Example:
const result = await sdk.auth.register({
profile: {
id: "alice@example.com",
name: "Alice Smith"
}
});
console.log(`User registered with address: ${result.address}`);
connect(userId)
Authenticates a user with their existing passkey and establishes a session.
Parameters:
userId: String - The unique identifier for the user (same as profile.id used during registration)
Returns: Promise that resolves to an object containing session information.
Example:
const session = await sdk.auth.connect("alice@example.com");
console.log(`Session established for user: ${session.userId}`);
console.log(`Blockchain address: ${session.address}`);
isRegistered(userId)
Checks if a user is already registered.
Parameters:
userId: String - The unique identifier for the user
Returns: Promise that resolves to a boolean indicating if the user is registered.
Example:
const exists = await sdk.auth.isRegistered("alice@example.com");
if (exists) {
console.log("User is already registered, proceed to connect");
} else {
console.log("User needs to register first");
}
sign(userId, command)
Signs a blockchain extrinsic using the user's active session.
Parameters:
userId: String - The unique identifier for the user
command: Object containing the extrinsic details
{
hex: string, // Required: Hexadecimal representation of the extrinsic
url: string, // Optional: URL endpoint for the extrinsic
body: any // Optional: Additional data for the request
}
Returns: Promise that resolves to an object containing the signed extrinsic.
Example:
const extrinsic = "0x1234..."; // Hex representation of the extrinsic
const signed = await sdk.auth.sign("alice@example.com", {
hex: extrinsic,
});
console.log("Original extrinsic:", signed.originalExtrinsic);
console.log("Signed extrinsic:", signed.signedExtrinsic);
Error Handling
All methods may throw errors that should be handled appropriately:
try {
const result = await sdk.auth.connect("alice@example.com");
// Process successful result
} catch (error) {
if (error.code === "E_CANT_GET_CREDENTIAL") {
console.error("Session expired or passkey not found");
// Prompt user to connect again
} else {
console.error("Authentication error:", error.message);
}
}