Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "tsc",
"format": "prettier --write \"src/**/*.ts\"",
"start": "export NODE_ENV=dev-local; export MONGODB_HOST=localhost; ts-node -r tsconfig-paths/register src/main.ts",
"start": "export NODE_ENV=dev-local; export MONGODB_HOST=localhost; export FAUCET_SECRET=s3cr3t; ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "nodemon",
"start:debug": "nodemon --config nodemon-debug.json",
"start:provided-env": "node dist/main.js",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ContactsModule } from './contacts/contacts.module'
import { BlockchainModule } from './blockchain/blockchain.module'
import { TerminusModule } from '@nestjs/terminus'
import { TerminusOptionsService } from './health/terminus-options.service'
import { FaucetModule } from './faucet/faucet.module'

@Module({
imports: [
Expand All @@ -14,6 +15,7 @@ import { TerminusOptionsService } from './health/terminus-options.service'
MessagingModule,
ContactsModule,
BlockchainModule,
FaucetModule,
TerminusModule.forRootAsync({
useClass: TerminusOptionsService,
}),
Expand Down
32 changes: 32 additions & 0 deletions src/faucet/faucet.auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
Injectable,
CanActivate,
ExecutionContext,
Inject,
} from '@nestjs/common'
import { Observable } from 'rxjs'
import { ConfigService } from '../config/config.service'

@Injectable()
export class FaucetAuthGuard implements CanActivate {
constructor(
@Inject('ConfigService') private readonly configService: ConfigService
) {}

public canActivate(
context: ExecutionContext
): boolean | Promise<boolean> | Observable<boolean> {
const faucetSecret = this.configService.get('FAUCET_SECRET')
if (!faucetSecret) {
console.log(
'No faucet secret defined. Is environment variable $FAUCET_SECRET defined?'
)
return false
}

const request = context.switchToHttp().getRequest()
const authorizationHeaderValue = request.headers.authorization

return faucetSecret === authorizationHeaderValue
}
}
43 changes: 43 additions & 0 deletions src/faucet/faucet.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Blockchain, Identity } from '@kiltprotocol/prototype-sdk'
import {
Controller,
Get,
Inject,
UseGuards,
Param,
Query,
} from '@nestjs/common'
import { BlockchainService } from '../blockchain/interfaces/blockchain.interfaces'
import { FaucetAuthGuard } from './faucet.auth.guard'

@Controller('faucet')
export class FaucetController {
constructor(
@Inject('BlockchainService')
private readonly blockchainService: BlockchainService
) {}

/**
* Transfer a certain amount of Kilt to an account.
*
* Call with curl:
*
* curl -X GET -H "Authorization: {FAUCET_SECRET}" \
* "http://{services-url}/faucet/transfer?receiverAddress={adress}&amount={amount}"
*
* @param receiverAddress address of the receiver account
* @param amount the amount of Kilt to tranfer
*/
@Get('transfer')
@UseGuards(FaucetAuthGuard)
public async transfer(
@Query('receiverAddress') receiverAddress: string,
@Query('amount') amount: number
): Promise<void> {
const blockchain: Blockchain = await this.blockchainService.connect()
const alice = Identity.buildFromURI('//Alice')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endowed identity needs to be injected via environment variable - and it's not alice any more


console.log(`Transferring ${amount} tokens to ${receiverAddress}`)
await blockchain.makeTransfer(alice, receiverAddress, amount)
}
}
9 changes: 9 additions & 0 deletions src/faucet/faucet.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common'
import { FaucetController } from './faucet.controller'

@Module({
imports: [],
controllers: [FaucetController],
providers: [],
})
export class FaucetModule {}