1.0.48 • Published 2 years ago

hsuite-nestjs-hedera v1.0.48

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years 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.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.40

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

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.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago