Low-level Client-Side SDK
The Virto Client SDK provides functionalities that allow web applications to interact with the blockchain using WebAuthn authentication directly from the browser. This SDK is designed to run in browser environments and leverages native WebAuthn APIs.
Configuration
import SDK from '@virtonetwork/sdk';
const sdk = new SDK({
federate_server: 'https://your-federate-server.com/api',
provider_url: 'wss://your-blockchain-provider:12281',
config: {
wallet: 'polkadotjs' // Wallet type to use (polkadotjs or virto)
}
});Authentication Methods
The sdk.auth object provides the following methods:
register(user)
Registers a new user using a WebAuthn passkey. This method is only available in browser environments as it uses WebAuthn APIs.
Parameters:
user: Object containing the user profile and metadata
Returns: Promise that resolves with the registration response, including the blockchain address.
Example:
prepareRegistration(user)
Prepares registration data on the client side using WebAuthn. This method is used internally by register(), but can be useful for custom authentication flows.
Parameters:
user: Object containing the user profile and metadata (same format asregister)
Returns: Promise that resolves with the prepared registration data.
Example:
connect(userId)
Authenticates a user with their existing passkey and establishes a session. This method is only available in browser environments.
Parameters:
userId: String - The unique identifier for the user (the same asprofile.idused during registration)
Returns: Promise that resolves to an object containing session information.
Example:
prepareConnection(userId)
Prepares connection data on the client side using WebAuthn. This method is used internally by connect(), but can be useful for custom authentication flows.
Parameters:
userId: String - The unique identifier for the user
Returns: Promise that resolves with the prepared connection data.
Example:
isRegistered(userId)
Checks if a user is already registered with the federate server.
Parameters:
userId: String - The unique identifier for the user
Returns: Promise that resolves to a boolean indicating if the user is registered.
Example:
sign(userId, command)
Signs a blockchain extrinsic using the user's active session.
Parameters:
userId: String - The unique identifier for the usercommand: Object containing the extrinsic details
Returns: Promise that resolves to an object containing the signed extrinsic.
Example:
Authentication Flow
The typical flow for implementing authentication with the Virto SDK is:
Verify registration: Use
isRegistered()to check if the user is already registeredRegistration (if necessary): If not registered, use
register()to create a new passkeyConnection: Use
connect()to authenticate the user with their existing passkeySign transactions: Once connected, use
sign()to sign blockchain transactions
Error Handling
All methods may throw errors that should be handled appropriately:
Common error codes include:
E_CANT_CREATE_CREDENTIAL: Error creating WebAuthn credentialE_CANT_GET_CREDENTIAL: Error retrieving credential or wallet not foundE_ENVIRONMENT: Method called in an unsupported environment (e.g., outside the browser)