How to Integrate with the Solana Blockchain with Magic

How to Integrate with the Solana Blockchain with Magic

#Installation

Magic interacts with the Solana blockchain via Magic's extension NPM package @magic-ext/solana. The Solana extension also lets you interact with the blockchain using methods from Solana's Javascript SDK.

note

You can skip straight to our kitchen sink example directly:

👉 Solana Example

#NPM

npm install --save @magic-ext/solana

#Yarn

yarn add @magic-ext/solana

#Initializing Extension

#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>

#Sign Transaction

Note that the Magic Solana extension follows the method names and conventions by Solana's Javascript SDK. To sign a standard Solana blockchain transaction, you can call the magic.solana.signTransaction method.

#ES Modules/TypeScript

import { Magic } from 'magic-sdk';
import { SolanaExtension } from '@magic-ext/solana';

import * as web3 from '@solana/web3.js';

const rpcUrl = 'SOLANA_RPC_NODE_URL'

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new SolanaExtension({
      rpcUrl
    }),
  ],
});

const metadata = await magic.user.getMetadata();
const recipientPubKey = new web3.PublicKey(destinationAddress);
const payer = new web3.PublicKey(metadata.publicAddress);
const connection = new web3.Connection(rpcUrl);

const hash = await connection.getRecentBlockhash();


let transactionMagic = new web3.Transaction({
  feePayer: payer,
  recentBlockhash: hash.blockhash
});

const transaction = web3.SystemProgram.transfer({
  fromPubkey: payer,
  toPubkey: recipientPubKey,
  lamports: sendAmount,
});

transactionMagic.add(...([transaction]));

const serializeConfig = {
  requireAllSignatures: false,
  verifySignatures: true
};

const signedTransaction = await magic.solana.signTransaction(transactionMagic, serializeConfig);

    console.log("Signed transaction", signedTransaction);

#Send Transaction

#ES Modules/TypeScript

import { Magic } from 'magic-sdk';
import { SolanaExtension } from '@magic-ext/solana';

import * as web3 from '@solana/web3.js';

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new SolanaExtension({
      rpcUrl: 'SOLANA_RPC_NODE_URL',
    }),
  ],
});

const connection = new web3.Connection(rpcUrl);
// `signedTransaction` comes from `magic.solana.signTransaction` above
const tx = web3.Transaction.from(signedTransaction.rawTransaction); 
const signature = await connection.sendRawTransaction(tx.serialize());

console.log(signature);

Did you find what you were looking for?

How to Integrate with the Solana Blockchain with Magic

Did you find what you were looking for?