Near Protocol API Integration Guide for Bitget Wallet

ยท

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.near

Available Methods Overview

MethodDescriptionReturn Type
getAccountIdRetrieves current user's account IDstring
getPublicKeyObtains public key of user accountstring
requestSignInInitiates NEAR Wallet sign-in flowPromise
isSignedInChecks user authentication statusboolean
signOutTerminates current sessionPromise
signAndSendTransactionExecutes blockchain transactionPromise
verifyOwnerValidates account ownershipPromise
requestSignTransactionsRequests batch transaction signingPromise

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:

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

  1. Error Handling: Always implement try-catch blocks for API calls
  2. State Management: Cache authentication status locally
  3. Permission Scoping: Request minimal required contract access
  4. 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.

๐Ÿ‘‰ Discover more Web3 integration possibilities