Documentation Index
Fetch the complete documentation index at: https://docs.magic.link/llms.txt
Use this file to discover all available pages before exploring further.
Installation
To get started, install the following dependencies for your project:
npm install @magic-ext/solana @solana/web3.js magic-sdk
Initialization
The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key.
ES Modules/TypeScript
import { Magic } from 'magic-sdk';
import { SolanaExtension } from '@magic-ext/solana';
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new SolanaExtension({
rpcUrl: 'SOLANA_RPC_NODE_URL',
}),
],
});
CDN
<script src="https://auth.magic.link/sdk"></script>
<script type="text/javascript" src="https://auth.magic.link/sdk/extension/solana"></script>
<script>
const magic = new window.Magic("YOUR_API_KEY", {
extensions: [
new SolanaExtension({
rpcUrl: 'SOLANA_RPC_NODE_URL'
})
]
});
</script>
Common Methods
Note that the Magic Solana extension follows the method names and conventions by Solana’s JavaScript SDK.
Sign and Send Transaction
ES Modules/TypeScript
To sign a standard Solana blockchain transaction, call the magic.solana.signTransaction method. Note that you must sign with the Magic SDK method but send the transaction using the @solana/web3.js method connection.sendRawTransaction.
import * as web3 from "@solana/web3.js";
// Ensure you have Magic initialized with the Solana extension
// Ensure that user is already authenticated
const connection = new web3.Connection(clusterApiUrl("devnet"))
const metadata = await magic.user.getMetadata();
const userPublicKey = new web3.PublicKey(metadata.publicAddress);
const recipientPubkey = new web3.Keypair.generate().publicKey;
const blockhash = await connection?.getLatestBlockhash();
if (!blockhash) return;
const transaction = new web3.Transaction({
...blockhash,
feePayer: userPublicKey,
}).add(
web3.SystemProgram.transfer({
fromPubkey: userPublicKey,
toPubkey: recipientPubkey,
lamports: web3.LAMPORTS_PER_SOL * 0.01,
})
);
const signedTransaction = await magic?.solana.signTransaction(
transaction,
{
requireAllSignatures: false,
verifySignatures: true,
}
);
const signature = await connection?.sendRawTransaction(
Buffer.from(signedTransaction?.rawTransaction as string, "base64")
);
console.log(signature);
Sign Message
ES Modules/TypeScript
// Ensure you have Magic initialized with the Solana extension
// Ensure that user is already authenticated
const signedMessage = await magic?.solana.signMessage("Hello World")
console.log(signedMessage)
Resources