1.0.34 • Published 1 year ago

@hsuite/nestjs-hedera v1.0.34

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

HSuite - Hedera for NestJS

An hedera-js-sdk wrapper for NestJS.

Installation

If you use npm, you shall run:

npm install @hsuite/nestjs-hedera

instead, if you use yarn:

yarn add @hsuite/nestjs-hedera

Import into your AppModule

First you need to import the HederaModule into your app.module.ts. You can use the forRoot method in order to pass the needed variables to create the Hedera Client and to connect to a Mirror Node, like this:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { HederaModule } from '@hsuite/nestjs-hedera/lib';

@Module({
  imports: [
    HederaModule.forRoot({
      operators: [
          {
            accountId: 'YOUR_OPERATOR_ACCOUNT_ID', 
            privateKey: 'YOUR_OPERATOR_PRIVATE_KEY'
          }
        ], 
        mirrorNode: {
          url: 'https://mainnet-public.mirrornode.hedera.com'
        }, 
        network: 'mainnet'
      }),
  ],
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {}

You can also use NestJS ConfigService, to protect your keys by calling the forRootAsync method, like this:

HederaModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
  useFactory: async (configService: ConfigService) => ({
    operators: configService.get<Array<Operator>>(`operators`),
    mirrorNode: configService.get<MirrorNode>(`mirrorNode`),
    network: configService.get<string>('network')
  }),
}),

Usage

Once imported, you can then import the service you need, and use it. For example:

import { Injectable } from '@nestjs/common';
import { KeysService } from '@hsuite/nestjs-hedera/lib/hedera/keys/keys.service';
import { HcsService } from '@hsuite/nestjs-hedera/lib/hedera/hcs/hcs.service';
import { HfsService } from '@hsuite/nestjs-hedera/lib/hedera/hfs/hfs.service';
import { AccountsService } from '@hsuite/nestjs-hedera/lib/hedera/accounts/accounts.service';
import { TransactionsRestService } from '@hsuite/nestjs-hedera/lib/hedera/transactions/transactions-rest.service';
import { TopicId, PrivateKey } from '@hashgraph/sdk';

@Injectable()
export class AppService {
  constructor(
    private keysService: KeysService,
    private hcsService: HcsService,
    private hfsService: HfsService,
    private transactionsService: TransactionsRestService,
    private accountsService: AccountsService
  ) {
    // creating an hedera account...
    this.accountsService.createAccount(1, 1).then(account => {
      console.log("account generated", account.key.toString());
      // generating a new private key...
      this.keysService.generateKey().then(key => {
        // updating the account with the new generated private key...
        this.accountsService.updateAccount(
          account.accountId, 
          PrivateKey.fromString(account.key.toString()),
          key).then(response => {
            console.log(response);
          }).catch(error => {
            console.error(error);
          });
      }).catch(error => {
        console.error(error);
      });
    }).catch(error => {
      console.error(error);
    })

    // fetching latest transactions from mirror node for a given accountId...
    this.transactionsService.getLatestTransactions('YOUR_ACCOUNT_ID_HERE').then(response => {
      console.log(response);
    }).catch(error => {
      console.error(error);
    });

    // subscribing to a HCS Topic...
    this.hcsService.getMessages(
      TopicId.fromString('YOUR_HCS_TOPIC_ID_HERE'),
      async(message) => {
        let hcsMessage = JSON.parse(Buffer.from(message.contents).toString());
        console.log(hcsMessage);
      }
      ).then(() => {
        console.log("subscribed");
      }).catch(error => {
        console.error(error);
      })

    // creating an HFS file...
    this.hfsService.create(
      PrivateKey.fromString('YOUR_PRIVATE_KEY_HERE'),
      'YOUR_CONTENT_HERE'
    ).then((response) => {
      console.log(response);
    }).catch(error => {
      console.error(error);
    })
  }
}
1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.34

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.30

1 year ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.9

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.23

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago