How to Integrate with the Near Blockchain with Magic
How to Integrate with the Near Blockchain with Magic
#Installation
Magic interacts with the Near blockchain via Magic's extension NPM package @magic-ext/near
. The Near extension also lets you interact with the blockchain using methods from near-api-js.
You can skip straight to our Near Guide directly:
👉 Near Guide
#NPM
npm install --save @magic-ext/near
#Yarn
yarn add @magic-ext/near
#Initializing Extension
#ES Modules/TypeScript
import { Magic } from "magic-sdk";
import { NearExtension } from "@magic-ext/near";
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new NearExtension({
rpcUrl: '',
}),
]});
#Call signTransaction Extension Method
To sign a standard Near blockchain transaction, you can call the magic.near.signTransaction
method to get the signature and raw transaction then send to blockchain by yourself.
#ES Modules/TypeScript
import { Magic } from "magic-sdk";
import { NearExtension } from "@magic-ext/near";
import { transactions, utils } from 'near-api-js'
const publicKeyString = await magic.near.getPublicKey();
const publicKey = utils.PublicKey.fromString(publicKeyString);
const actions = [
transactions.transfer(sendAmount)
];
const transaction = transactions.createTransaction(publicAddress, publicKey, destinationAddress, 0, actions, '9av2U6cova7LZPA9NPij6CTUrpBbgPG6');
const rawTransaction = transaction.encode();
const result = await magic.near.signTransaction({rawTransaction, networkID: 'testnet'});
const signedTransaction = transactions.SignedTransaction.decode(Buffer.from(result.encodedSignedTransaction));
console.log('signedTransaction', signedTransaction)