Solflare
Search…
Connecting your Solana dApp with Solflare
If you have Sollet or some other wallet provider implemented, you can use the same code, just replace https://www.sollet.io with the https://solflare.com/provider.

Using library sol-wallet-adapter

Install

1
npm install --save @project-serum/sol-wallet-adapter
Copied!

Usage

Sign a transaction

1
import { Connection, SystemProgram, Transaction, clusterApiUrl } from '@solana/web3.js';
2
3
let connection = new Connection(clusterApiUrl('devnet'));
4
let providerUrl = 'https://solflare.com/provider';
5
let wallet = new Wallet(providerUrl);
6
wallet.on('connect', publicKey => console.log('Connected to ' + publicKey.toBase58()));
7
wallet.on('disconnect', () => console.log('Disconnected'));
8
await wallet.connect();
9
10
let transaction = new Transaction().add(
11
SystemProgram.transfer({
12
fromPubkey: wallet.publicKey,
13
toPubkey: wallet.publicKey,
14
lamports: 100,
15
})
16
);
17
let { blockhash } = await connection.getRecentBlockhash();
18
transaction.recentBlockhash = blockhash;
19
transaction.feePayer = wallet.publicKey;
20
let signed = await wallet.signTransaction(transaction);
21
let txid = await connection.sendRawTransaction(signed.serialize());
22
await connection.confirmTransaction(txid);
Copied!

Sign a message

1
const providerUrl = 'https://solflare.com/provider';
2
const wallet = new Wallet(providerUrl);
3
wallet.on('connect', publicKey => console.log('Connected to ' + publicKey.toBase58()));
4
wallet.on('disconnect', () => console.log('Disconnected'));
5
await wallet.connect();
6
7
const message = "Please sign this message for proof of address ownership.";
8
const data = new TextEncoder().encode(message);
9
let { signature } = await wallet.sign(data, 'utf8');
Copied!

Solflare Extension

If you have Phantom implemented, you can use the same code, just replace window.solana with window.solflare.

Detecting the Provider

1
const isSolflareInstalled = window.solflare && window.solflare.isSolflare;
2
3
const getProvider = () => {
4
if ('solflare' in window) {
5
const provider = window.solflare;
6
if (provider.isSolFlare) {
7
return provider;
8
}
9
}
10
window.open('https://solflare.com', '_blank');
11
};
Copied!

Connecting

The window.solflare object exposes a connect() function that returns a Promise. It will resolve to true if the user accepted the connection request and false if not.
1
window.solflare.connect();
Copied!
You can also listen for the connect event.
1
window.solflare.on("connect", () => console.log("connected!"));
Copied!
Once the dApp is connected to the SolFlare extension, the isConnected field becomes true and the publicKeyfield resolves to the user's public key (PublicKey object from @solana/web3.js)
1
window.solflare.publicKey.toString();
2
// 26qv4GCcx98RihuK3c4T6ozB3J7L6VwCuFVc7Ta2A3Uo
3
window.solflare.isConnected;
4
// true
Copied!

Signing a transaction

The signTransaction(transaction) function expects a Transaction object from the @solana/web3.js library and returns the signed Transaction object.
1
const network = '<NETWORK_URL>';
2
const connection = new Connection(network);
3
const transaction = new Transaction();
4
const signedTransaction = await window.solflare.signTransaction(transaction);
5
const signature = await connection.sendRawTransaction(signedTransaction.serialize());
Copied!

Signing a message

The signMessage function expects an Uint8Array and a data display type parameter (either utf8 for readable text or hex for binary data) and returns an object with keys signature (signature of the input message) and publicKey.
1
const message = 'To avoid digital dognappers, sign below to authenticate with CryptoCorgis';
2
const encodedMessage = new TextEncoder().encode(message);
3
const signedMessage = await window.solflare.signMessage(encodedMessage, 'utf8');
4
// { signature, publicKey }
Copied!