How to Integrate with the Algorand Blockchain with Magic

How to Integrate with the Algorand Blockchain with Magic

Installation

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

note

You can skip straight to our kitchen sink example directly:

šŸ‘‰ Algorand Example

NPM

npm install --save @magic-ext/algorand

Yarn

yarn add @magic-ext/algorand

Initializing Extension

ES Modules/TypeScript

import { Magic } from 'magic-sdk'; import { AlgorandExtension } from '@magic-ext/algorand'; const magic = new Magic('YOUR_API_KEY', { extensions: { algorand: new AlgorandExtension({ rpcUrl: 'algorand rpc url', }), }, });

Get User Info

Get Wallet

Using getWallet function to get Algorand public address for current user.

ES Modules/TypeScript

import { Magic } from 'magic-sdk'; import { AlgorandExtension } from '@magic-ext/algorand'; const magic = new Magic('YOUR_API_KEY', { extensions: { algorand: new AlgorandExtension({ rpcUrl: 'algorand rpc url', }), }, }); // Get user's Algorand public address const publicAddress = await magic.algorand.getWallet(); console.log('algorand public address', publicAddress);

Sign Transaction

Getting Test Algorand

Before you can send transaction on the Algorand blockchain, you'll need to acquire some test Algorand.

  1. Go to our Algorand Example application
  2. Login with your email address
  3. Copy your Algorand public address
  4. Go to the Algorand Faucet
  5. Paste your copied Algorand public address in the text input
  6. Now you can use your test Algorand in our example app

Call Extension Method

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

ES Modules/TypeScript

import { Magic } from 'magic-sdk'; import { AlgorandExtension } from '@magic-ext/algorand'; const magic = new Magic('YOUR_API_KEY', { extensions: { algorand: new AlgorandExtension({ rpcUrl: 'algorand rpc url', }), }, }); const txn = { to: '7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q', fee: 10, amount: 50, firstRound: 51, lastRound: 61, genesisID: 'devnet-v33.0', genesisHash: 'JgsgCaCTqIaLeVhyL6XlRu3n7Rfk2FxMeK+wRSaQ7dI=', closeRemainderTo: 'IDUTJEUIEVSMXTU4LGTJWZ2UE2E6TIODUKU6UW3FU3UKIQQ77RLUBBBFLA', note: new Uint8Array(Buffer.from('6gAVR0Nsv5Y=', 'base64')), }; const tx = await magic.algorand.signTransaction(txn); console.log('signed transaction', tx);

Sign Bid

Call Extension Method

Note that the Magic Algorand extension follows the method names and conventions by Algorand's Javascript SDK. To sign a standard Algorand blockchain Bid, you can call the magic.algorand.signBid() method.

ES Modules/TypeScript

import { Magic } from 'magic-sdk'; import { AlgorandExtension } from '@magic-ext/algorand'; const magic = new Magic('YOUR_API_KEY', { extensions: { algorand: new AlgorandExtension({ rpcUrl: 'algorand rpc url', }), }, }); const bid = { bidderKey: 'IB3NJALXLDX5JLYCD4TMTMLVCKDRZNS4JONHMIWD6XM7DSKYR7MWHI6I7U', auctionKey: '7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q', bidAmount: 1000, maxPrice: 10, bidID: 2, auctionID: 56, }; const tx = await magic.algorand.signBid(bid); console.log('signed bid', tx);

Sign Group Transaction

Call Extension Method

To sign a standard Algorand blockchain Group transaction with magic user, you can call the magic.algorand.signGroupTransaction() method.

import { Magic } from "magic-sdk"; import { AlgorandExtension } from "@magic-ext/algorand"; const algosdk = require('algosdk'); const magic = new Magic('YOUR_API_KEY', { extensions: { algorand: new AlgorandExtension({ rpcUrl: 'algorand rpc url' }) } }); let client = null; async function setupClient() { if( client == null){ const token = { "x-api-key": "x api key" }; const server = "algorand rpc url"; const port = ''; let algodClient = new algosdk.Algodv2(token, server, port); client = algodClient; } else { return client; } return client; } let algodClient = await setupClient(); let params = await algodClient.getTransactionParams().do(); const txns = [{ from: 'magic user public address', to: 'OFHW3Z3T2RML7J2S6KYGHPAMO6IQH76PE2HSCAIN5U5NBGXAIPBOY7DCHI', amount: 1000000, closeRemainderTo: undefined, note: undefined, suggestedParams: params, }, { from: 'magic user public address', to: 'XRKQBEV7FINQ66SYAFY33UYHOC4GRAICWI3V6V2TXLCQMPJBGGRHLG2E74', amount: 1000000, closeRemainderTo: undefined, note: undefined, suggestedParams: params, } ] const signedTX = await magic.algorand.signGroupTransaction(txns); console.log("signedTX", signedTX);
How to Integrate with the Algorand Blockchain with Magic