Near
Near
#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
01npm install --save @magic-ext/near
#Initialization
01import { Magic } from "magic-sdk";
02import { NearExtension } from "@magic-ext/near";
03
04const magic = new Magic('YOUR_API_KEY', {
05 extensions: [
06 new NearExtension({
07 rpcUrl: '',
08 }),
09 ]});
#Common Methods
#Sign Transaction
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.
01import { Magic } from "magic-sdk";
02import { NearExtension } from "@magic-ext/near";
03import { transactions, utils } from 'near-api-js'
04
05
06const publicKeyString = await magic.near.getPublicKey();
07
08const publicKey = utils.PublicKey.fromString(publicKeyString);
09
10const actions = [
11 transactions.transfer(sendAmount)
12];
13const transaction = transactions.createTransaction(publicAddress, publicKey, destinationAddress, 0, actions, '9av2U6cova7LZPA9NPij6CTUrpBbgPG6');
14
15const rawTransaction = transaction.encode();
16
17const result = await magic.near.signTransaction({rawTransaction, networkID: 'testnet'});
18
19const signedTransaction = transactions.SignedTransaction.decode(Buffer.from(result.encodedSignedTransaction));
20
21console.log('signedTransaction', signedTransaction)