Introduction to NEAR Protocol Integration
When developing decentralized applications (DApps) that interact with the NEAR blockchain through Bitget Wallet, you can access the NEAR provider object via window.bitkeep.near. This guide provides comprehensive documentation for all available methods and properties.
NEAR Provider Object
To initialize the NEAR provider in your DApp:
const provider = window.bitkeep.nearAvailable Methods Overview
| Method | Description | Return Type |
|---|---|---|
getAccountId | Retrieves current user's account ID | string |
getPublicKey | Obtains public key of user account | string |
requestSignIn | Initiates NEAR Wallet sign-in flow | Promise |
isSignedIn | Checks user authentication status | boolean |
signOut | Terminates current session | Promise |
signAndSendTransaction | Executes blockchain transaction | Promise |
verifyOwner | Validates account ownership | Promise |
requestSignTransactions | Requests batch transaction signing | Promise |
Core Method Documentation
getAccountId()
Returns the NEAR account ID of the authenticated user.
Implementation Example:
const accountId = await provider.getAccountId();
console.log('User account:', accountId);Use cases:
- Personalizing user experience
- Displaying account-specific information
- Transaction logging
getPublicKey()
Retrieves the public cryptographic key associated with the user's account.
Implementation Example:
const publicKey = await provider.getPublicKey();
console.log('Public key:', publicKey);requestSignIn()
Initiates the NEAR Wallet authentication process with customizable parameters.
Parameters:
{
contractId: string, // Target smart contract
methodNames: string[], // Allowed method calls
createNew: boolean // Generate new access key
}Implementation Example:
await provider.requestSignIn({
contractId: 'example.near',
methodNames: ['transfer', 'view_balance'],
createNew: true
});๐ Learn more about NEAR authentication best practices
Transaction Management
signAndSendTransaction()
Submits signed transactions to the NEAR blockchain.
Transaction Structure:
{
receiverId: string, // Recipient account
actions: [], // Transaction operations
signerId: string // Sender account
}Security Methods
verifyOwner()
Validates account ownership through cryptographic verification.
Implementation Example:
const verification = await provider.verifyOwner(
'Verification message',
'user.near'
);
console.log('Ownership verified:', verification);Authentication Status
isSignedIn()
Monitors current authentication state.
Implementation Example:
setInterval(() => {
if (provider.isSignedIn()) {
console.log('User authenticated');
}
}, 5000);signOut()
Terminates the current authenticated session.
Implementation Example:
document.getElementById('logout').addEventListener('click', () => {
provider.signOut();
alert('Successfully disconnected');
});Advanced Transaction Handling
requestSignTransactions()
Processes multiple transactions simultaneously.
Implementation Example:
await provider.requestSignTransactions({
transactions: [
{ receiverId: 'contract1.near', actions: [...] },
{ receiverId: 'contract2.near', actions: [...] }
]
});๐ Explore multi-transaction use cases
Best Practices for NEAR Integration
- Error Handling: Always implement try-catch blocks for API calls
- State Management: Cache authentication status locally
- Permission Scoping: Request minimal required contract access
- User Experience: Provide clear authentication prompts
Frequently Asked Questions
How do I test NEAR integration during development?
Use NEAR testnet accounts and the testnet wallet during development. The API endpoints automatically switch between networks based on the account ID.
What's the difference between signIn and requestSignIn?
signIn is the legacy method while requestSignIn follows the current NEAR Wallet standards with enhanced security parameters.
How can I verify transaction success?
All transaction methods return promise resolutions containing transaction hashes which can be checked on NEAR Explorer.
Is there a rate limit for API calls?
The provider has built-in request throttling. For high-frequency applications, consider implementing client-side caching.
Can I use this with mobile applications?
Yes, the same provider object is available in Bitget Wallet's mobile app environment with identical functionality.
How do I handle wallet disconnections?
Implement event listeners for authentication state changes to update your UI accordingly.
Conclusion
This comprehensive guide covers all aspects of NEAR Protocol integration through Bitget Wallet. For optimal implementation, follow the provided examples and best practices while adapting to your specific DApp requirements.
Remember to regularly check for API updates as NEAR Protocol continues to evolve. For additional blockchain integration options, consider exploring our other supported networks.