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

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 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