Algorand

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 install --save @magic-ext/algorand

Initializing Extension

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.

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.

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.

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)