@newput-newlink/blockchain
v1.1.3
Published
The Blockchain module provides an easy-to-use and flexible integration for blockchain-related features, starting with Hedera Hashgraph support. It simplifies interaction with blockchain networks by managing configurations and providing pre-built services
Downloads
28
Keywords
Readme
Newlink Blockchain Module
The Blockchain module provides an easy-to-use and flexible integration for blockchain-related features, starting with Hedera Hashgraph support. It simplifies interaction with blockchain networks by managing configurations and providing pre-built services and controllers.
Features
Configurable Blockchain Options
Supports dynamic configuration of blockchain providers, networks, accounts, and keys.Hedera Hashgraph Support
Includes built-in services and controllers for working with Hedera.Dynamic Module Registration
Easily register and configure the Blockchain module based on your application's needs.Extensible
Additional blockchain providers can be supported in future implementations.
Installation
Use the module via newlink-cli:
npx @newput-newlink/cliInstallation Process
During installation, the CLI will guide you through two key configuration steps:
-> Architecture Selection: Choose between Monolithic or Microservice architecture based on your project requirements. -> Blockchain Setup: You'll be prompted to enable the blockchain service. Enter 'Y' to include this feature in your installation.
The CLI tool handles all dependency installation and initial setup automatically, creating a production-ready service configured to your specifications. What Happens Next? After completing these steps, the NewLink CLI will:
Install all required dependencies Generate necessary configuration files Set up your chosen architecture Configure the blockchain service (if selected) Initialize your project with the selected settings
Now your NewLink service is ready for development and deployment.
API Endpoints
Hedera Endpoints
POST /hedera/accounts- Create a new account.GET /hedera/accounts/{accountId}- Fetch details of a specific account, including its balance (if needed).POST /hedera/transfers- Execute a fund transfer operation.GET /hedera/accounts/{accountId}/transactions- List transactions for a specific account.GET /hedera/transactions/{transactionId}- Get details of a specific transaction.
npm install @newput-newlink/blockchainEnvironment Variables
The Blockchain module requires several environment variables for proper configuration. Below is a table describing each variable, whether it is required, its purpose, and its default value:
| Environment Variable | Required | Description | Default |
| ---------------------------- | -------- | ----------------------------------------------- | --------- |
| BLOCKCHAIN | Yes | Blockchain provider (e.g., hedera) | hedera |
| BLOCKCHAIN_NETWORK | Yes | Blockchain network (e.g., mainnet, testnet) | testnet |
| BLOCKCHAIN_ACCOUNT_ID | Yes | Account ID for blockchain transactions | None |
| BLOCKCHAIN_PRIVATE_KEY | Yes | Private key for blockchain account | None |
| BLOCKCHAIN_INITIAL_BALANCE | No | Initial balance for new accounts (in tinybars) | 1000 |
Example .env File
Create a .env file in the root directory of your project and populate it with the necessary environment variables:
BLOCKCHAIN=hedera
BLOCKCHAIN_NETWORK=testnet
BLOCKCHAIN_ACCOUNT_ID=your-account-id
BLOCKCHAIN_PRIVATE_KEY=your-private-key
BLOCKCHAIN_INITIAL_BALANCE=1000Usage
Importing the Module
To use the Blockchain module, import and register it in your application:
import { BlockchainModule } from '@newput-newlink/blockchain';
@Module({
imports: [
BlockchainModule.register({
blockchain: 'hedera',
network: 'testnet',
account_id: process.env.BLOCKCHAIN_ACCOUNT_ID,
private_key: process.env.BLOCKCHAIN_PRIVATE_KEY,
initial_balance: parseInt(process.env.BLOCKCHAIN_INITIAL_BALANCE || '1000', 10),
}),
],
})
export class AppModule {}Example: Creating a New Account (Hedera)
Use the HederaService to create a new account:
import { Injectable } from '@nestjs/common';
import { HederaService } from '@newput-newlink/blockchain';
@Injectable()
export class ExampleService {
constructor(private readonly hederaService: HederaService) {}
async createAccount() {
const newAccount = await this.hederaService.createAccount({
initialBalance: 1000,
});
console.log('New Account:', newAccount);
return newAccount;
}
}Example: Controller Integration
Use the HederaController to expose blockchain functionalities via APIs:
import { Controller, Post } from '@nestjs/common';
import { HederaService } from '@newput-newlink/blockchain';
@Controller('hedera')
export class ExampleController {
constructor(private readonly hederaService: HederaService) {}
@Post('create-account')
async createAccount() {
return await this.hederaService.createAccount({ initialBalance: 1000 });
}
}Testing
Run the unit tests to verify the module's functionality:
npm testSupported Blockchain Providers
| Blockchain Provider | Status | | -------------------- | --------------- | | Hedera Hashgraph | Fully Supported | | Ethereum | Coming Soon | | Solana | Coming Soon |
Contributing
We welcome contributions to extend the module's capabilities. Please follow these steps:
- Fork the repository.
- Create a new feature branch.
- Commit your changes.
- Submit a pull request with a detailed explanation.
License
This module is licensed under the MIT License.
