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.
You can skip straight to our example repo directly:
👉 Hedera Example
#NPM
01npm install --save @magic-ext/hedera
#Yarn
01yarn add @magic-ext/hedera
#Initializing Extension
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { HederaExtension } from '@magic-ext/hedera';
03
04const magic = new Magic("YOUR_API_KEY", {
05 extensions: [new HederaExtension({
06 network: 'testnet' // 'mainnet' or 'testnet'
07 })]
08});
#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
01import { Magic } from 'magic-sdk';
02import { HederaExtension } from '@magic-ext/hedera';
03
04const magic = new Magic("YOUR_API_KEY", {
05 extensions: [new HederaExtension({
06 network: 'testnet' // 'mainnet' or 'testnet'
07 })]
08});
09
10const { publicKeyDer } = await magic.hedera.getPublicKey()
11
12const magicSign = message => magic.hedera.sign(message);
13const magicWallet = new MagicWallet(publicAddress, new MagicProvider('testnet'), publicKeyDer, magicSign)
14
15let transaction = await new TransferTransaction()
16 .setNodeAccountIds([new AccountId(3)])
17 .addHbarTransfer(publicAddress, -1 * sendAmount)
18 .addHbarTransfer(destinationAddress, sendAmount)
19 .freezeWithSigner(magicWallet);
20
21
22
23transaction = await transaction.signWithSigner(magicWallet);
24const result = await transaction.executeWithSigner(magicWallet);
25const receipt = await result.getReceiptWithSigner(magicWallet);
26
27console.log(receipt.status.toString());