How to Integrate with the Hedera Blockchain with Magic

How to Integrate with the Hedera Blockchain with Magic

#Installation

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

note

You can skip straight to our example repo directly:

👉 Hedera Example

#NPM

npm install --save @magic-ext/hedera

#Yarn

yarn add @magic-ext/hedera

#Initializing Extension

#ES Modules/TypeScript

import { Magic } from 'magic-sdk';
import { HederaExtension } from '@magic-ext/hedera';

const magic = new Magic("YOUR_API_KEY", {
    extensions: [new HederaExtension({
        network: 'testnet' // 'mainnet' or 'testnet'
    })]
});

#Send Transaction

#Call Extension Method

Note that the Magic Hedera extension follows the method names and conventions by hedera-sdk-js. To send a standard Hedera blockchain transaction, you can call the inject the MagicWallet to hedera-sdk-js. More details please reference to example-hedera github repo.

#ES Modules/TypeScript

import { Magic } from 'magic-sdk';
import { HederaExtension } from '@magic-ext/hedera';

const magic = new Magic("YOUR_API_KEY", {
    extensions: [new HederaExtension({
        network: 'testnet' // 'mainnet' or 'testnet'
    })]
});

const { publicKeyDer } = await magic.hedera.getPublicKey()

const magicSign = message => magic.hedera.sign(message);
const magicWallet = new MagicWallet(publicAddress, new MagicProvider('testnet'),                publicKeyDer, magicSign)

let transaction = await new TransferTransaction()
       .setNodeAccountIds([new AccountId(3)])
       .addHbarTransfer(publicAddress, -1 * sendAmount)
       .addHbarTransfer(destinationAddress, sendAmount)
       .freezeWithSigner(magicWallet);



transaction = await transaction.signWithSigner(magicWallet);
const result = await transaction.executeWithSigner(magicWallet);
const receipt = await result.getReceiptWithSigner(magicWallet);

console.log(receipt.status.toString());

Did you find what you were looking for?

How to Integrate with the Hedera Blockchain with Magic

Did you find what you were looking for?