Skip to content

Solana

js
const provider = window.bitkeep.solana
const provider = window.bitkeep.solana

connect

js
try {
  await provider.connect();
  const publicKey = await provider.getAccount();
  provider.publicKey.toString(); // Once the web application is connected to Bitkeep,
} catch {
  alert('connected error');
}
try {
  await provider.connect();
  const publicKey = await provider.getAccount();
  provider.publicKey.toString(); // Once the web application is connected to Bitkeep,
} catch {
  alert('connected error');
}

connected

js
provider.connected;
const publicKey = await provider.getAccount();
provider.publicKey.toString(); // Once the web application is connected to Bitkeep
provider.connected;
const publicKey = await provider.getAccount();
provider.publicKey.toString(); // Once the web application is connected to Bitkeep

signMessage

js
//string
provider.signMessage(
  '020006106e655af38ff7324bbf1d4e16b06084763269b9'
);

// uint8Array
const message = `You can use uint8array to verify`;
const encodedMessage = new TextEncoder().encode(message);
const signedMessage = await provider.signMessage(encodedMessage);
const nacl = require('tweetnacl');
const { PublicKey } = require('@solana/web3.js');
// nacl.sign.detached.verify(encodedMessage, signedMessage, publicKey)
nacl.sign.detached.verify(
  encodedMessage,
  signedMessage,
  new PublicKey(address).toBytes()
);
//string
provider.signMessage(
  '020006106e655af38ff7324bbf1d4e16b06084763269b9'
);

// uint8Array
const message = `You can use uint8array to verify`;
const encodedMessage = new TextEncoder().encode(message);
const signedMessage = await provider.signMessage(encodedMessage);
const nacl = require('tweetnacl');
const { PublicKey } = require('@solana/web3.js');
// nacl.sign.detached.verify(encodedMessage, signedMessage, publicKey)
nacl.sign.detached.verify(
  encodedMessage,
  signedMessage,
  new PublicKey(address).toBytes()
);

Event listeners

used eventemitter3

js
provider.on('connect', () => console.log('connected!'));
provider.on('connect', () => console.log('connected!'));

Versioned Transaction

On October 10, 2022, Solana introduced the concept of Versioned Transactions. Currently, the Solana runtime supports two types of transactions: legacy (see Legacy Transaction) and v0 (transactions that can include Address Lookup Tables (LUTS)).

The goal of v0 is to increase the maximum size of a transaction, and hence the number of accounts that can fit in a single atomic transaction. With LUTs, developers can now build transactions with a maximum of 256 accounts, as compared to the limit of 35 accounts in legacy transactions that do not utilize LUTs.

For a dive deep on Versioned Transactions, LUTs, and how the above changes affect the anatomy of a transaction, you can read this detailed guide.

1. Building a Versioned Transaction

Versioned transactions are built in a very similar fashion to legacy transactions. The only difference is that developers should use the VersionedTransaction class rather than the Transaction class.

The following example show how to build a simple transfer instruction. Once the transfer instruction is made, a MessageV0 formatted transaction message is constructed with the transfer instruction. Finally, a new VersionedTransaction is created, parsing in the v0 compatible message.

typescript
import { TransactionMessage, VersionedTransaction, SystemProgram } from '@solana/web3.js';

// create array of instructions
const instructions = [
  SystemProgram.transfer({
    fromPubkey: publicKey,
    toPubkey: publicKey,
    lamports: 10,
  }),
];

// create v0 compatible message
const messageV0 = new TransactionMessage({
  payerKey: publicKey,
  recentBlockhash: blockhash,
  instructions,
}).compileToV0Message();

// make a versioned transaction
const transactionV0 = new VersionedTransaction(messageV0);
import { TransactionMessage, VersionedTransaction, SystemProgram } from '@solana/web3.js';

// create array of instructions
const instructions = [
  SystemProgram.transfer({
    fromPubkey: publicKey,
    toPubkey: publicKey,
    lamports: 10,
  }),
];

// create v0 compatible message
const messageV0 = new TransactionMessage({
  payerKey: publicKey,
  recentBlockhash: blockhash,
  instructions,
}).compileToV0Message();

// make a versioned transaction
const transactionV0 = new VersionedTransaction(messageV0);

2. Signing a Versioned Transaction

Once a Versioned transaction is created, it can be signed via Bitget Wallet using the signTransaction method on the provider. The call will return a Promise for an object of the signed Transaction. This is the same way a legacy transaction is signed via the Bitget Wallet provider.

javascript

const provider = getProvider();
const network = "<NETWORK_URL>";
const connection = new Connection(network);

const signedTransaction = await provider.signTransaction(transactionV0);

const provider = getProvider();
const network = "<NETWORK_URL>";
const connection = new Connection(network);

const signedTransaction = await provider.signTransaction(transactionV0);

Legacy Transaction

1. Building a Legacy Transaction

javascript
  const pubKey = new PublicKey(publicKey.pubkey)
  const transaction = new Transaction().add(
    SystemProgram.transfer({
      fromPubkey: publicKey,
      toPubkey: publicKey,
      lamports: 100,
    })
  );
  transaction.feePayer = pubKey;

  const anyTransaction: any = transaction;
  anyTransaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;

  return transaction;
  const pubKey = new PublicKey(publicKey.pubkey)
  const transaction = new Transaction().add(
    SystemProgram.transfer({
      fromPubkey: publicKey,
      toPubkey: publicKey,
      lamports: 100,
    })
  );
  transaction.feePayer = pubKey;

  const anyTransaction: any = transaction;
  anyTransaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;

  return transaction;

2. Signing a Legacy Transaction

javascript
const provider = getProvider();
const network = "<NETWORK_URL>";
const connection = new Connection(network);

const signedTransaction = await provider.signTransaction(transaction);
const provider = getProvider();
const network = "<NETWORK_URL>";
const connection = new Connection(network);

const signedTransaction = await provider.signTransaction(transaction);