Provider Classes
The Provider classes in the @layerg-ua-sdk/aa-sdk
package serve as the foundation for interacting with Universal Accounts in the LayerG Account Abstraction system. They handle the connection to the blockchain and provide methods for account management and transaction submission.
AuthProvider
AuthProvider manages authentication between your application and the Universal Account system.
Methods
initialize
initialize(config: AuthProviderConfig): Promise<void>
Initializes the authentication provider with the given configuration.
Parameters:
config: AuthProviderConfig
- Configuration object containing:bundlerUrl: string
- URL of the bundler serviceapiKey: string
- API key for authenticationdomain?: string
- Optional domain for signature verification
Returns:
Promise<void>
- Resolves when initialization is complete
Example:
import { AuthProvider } from '@layerg-ua-sdk/aa-sdk';
const authProvider = new AuthProvider();
await authProvider.initialize({
bundlerUrl: 'https://bundler.layerg.network',
apiKey: 'your-api-key',
domain: 'yourdapp.com'
});
createSignature
createSignature(domain: string): Promise<SignatureHeaders>
Creates authentication signature headers for API requests.
Parameters:
domain: string
- Domain for which the signature is being created
Returns:
Promise<SignatureHeaders>
- Object containing:signature: string
- The cryptographic signaturetimestamp: number
- Timestamp when signature was createdpublicKey: string
- Public key associated with the signature
Example:
const headers = await authProvider.createSignature('yourdapp.com');
// Use these headers in fetch requests to the bundler
LayerProvider
LayerProvider is the main interface for interacting with Universal Accounts on the blockchain.
Methods
initialize
initialize(config: LayerProviderConfig): Promise<void>
Initializes the Layer provider with the given configuration.
Parameters:
config: LayerProviderConfig
- Configuration object containing:authProvider: AuthProvider
- Initialized auth providerchainId: number
- Blockchain chain IDaccountAddress?: string
- Optional account address to use
Returns:
Promise<void>
- Resolves when initialization is complete
Example:
import { AuthProvider, LayerProvider } from '@layerg-ua-sdk/aa-sdk';
const authProvider = new AuthProvider();
await authProvider.initialize({
bundlerUrl: 'https://bundler.layerg.network',
apiKey: 'your-api-key'
});
const layerProvider = new LayerProvider();
await layerProvider.initialize({
authProvider,
chainId: 2484 // U2U Chain ID
});
getAccount
getAccount(): Promise<AccountInfo>
Retrieves information about the connected Universal Account.
Returns:
Promise<AccountInfo>
- Account information object containing:address: string
- The account's addressbalance: string
- Account balance in weinonce: number
- Current account nonce
Example:
const accountInfo = await layerProvider.getAccount();
console.log(`Account Address: ${accountInfo.address}`);
console.log(`Balance: ${ethers.utils.formatEther(accountInfo.balance)} ETH`);
sendUserOperation
sendUserOperation(request: UserOperationRequest, options?: SendOptions): Promise<UserOperationResponse>
Sends a user operation to the bundler.
Parameters:
request: UserOperationRequest
- The user operation requestoptions?: SendOptions
- Optional sending options:sponsor?: boolean
- Whether to sponsor gas fees (default: false)maxPriorityFeePerGas?: string
- Max priority fee per gasmaxFeePerGas?: string
- Max fee per gas
Returns:
Promise<UserOperationResponse>
- Response containing:userOpHash: string
- Hash of the user operationwait(): Promise<TransactionReceipt>
- Method to wait for transaction receipt
Example:
const txRequest = buildContractCallRequest({
sender: accountInfo.address,
contractAddress: '0x123...',
abi: CONTRACT_ABI,
method: 'transfer',
params: ['0xRecipient', '1000000000000000000']
});
const response = await layerProvider.sendUserOperation(txRequest, {
sponsor: true
});
// Wait for the transaction to be mined
const receipt = await response.wait();
console.log(`Transaction confirmed in block ${receipt.blockNumber}`);
GAccountProvider
GAccountProvider extends LayerProvider with additional functionality specific to G-type Universal Accounts.
Methods
createAccount
createAccount(owner: string): Promise<string>
Creates a new G-type Universal Account.
Parameters:
owner: string
- Address of the account owner
Returns:
Promise<string>
- Address of the newly created account
Example:
import { AuthProvider, GAccountProvider } from '@layerg-ua-sdk/aa-sdk';
const authProvider = new AuthProvider();
await authProvider.initialize({
bundlerUrl: 'https://bundler.layerg.network',
apiKey: 'your-api-key'
});
const gAccountProvider = new GAccountProvider();
await gAccountProvider.initialize({
authProvider,
chainId: 2484
});
const newAccountAddress = await gAccountProvider.createAccount('0xOwnerAddress');
console.log(`New G-Account created at: ${newAccountAddress}`);
getPermissions
getPermissions(accountAddress: string): Promise<AccountPermissions[]>
Gets the permissions set for a G-type account.
Parameters:
accountAddress: string
- Address of the account
Returns:
Promise<AccountPermissions[]>
- Array of permission objects
Example:
const permissions = await gAccountProvider.getPermissions(accountAddress);
console.log('Account Permissions:', permissions);
Error Handling
All provider methods throw standardized errors from the @layerg-ua-sdk/aa-sdk
package. Always wrap API calls in try/catch blocks:
try {
const accountInfo = await layerProvider.getAccount();
// Process account info
} catch (error) {
if (error.code === 'ACCOUNT_NOT_FOUND') {
// Handle specific error
} else {
console.error('Provider error:', error.message);
}
}
Next Steps
- See Account APIs for more information on account management
- Learn about Contract Calls for interacting with smart contracts