From 01b65b893aa9387377c0c3faa9d7e03470961296 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Sat, 10 Jun 2023 11:57:49 -0400 Subject: [PATCH] bitcoin rpc --- .env.example | 5 +++ .gitignore | 2 ++ .vscode/settings.json | 1 + package-lock.json | 69 ++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/app.module.ts | 10 ++++-- src/bitcoin-rpc.service.ts | 33 ++++++++++++++++++ 7 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 .env.example create mode 100644 src/bitcoin-rpc.service.ts diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..d591125 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +BITCOIN_RPC_URL=http://192.168.1.100 +BITCOIN_RPC_USER= +BITCOIN_RPC_PASSWORD= +BITCOIN_RPC_PORT=8332 +BITCOIN_RPC_TIMEOUT=10000 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 22f55ad..55e2101 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,8 @@ lerna-debug.log* *.launch .settings/ *.sublime-workspace +.env +settings.json # IDE - VSCode .vscode/* diff --git a/.vscode/settings.json b/.vscode/settings.json index f2b56d5..25b6851 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,7 @@ "cSpell.words": [ "coinb", "Fastify", + "getblocktemplate", "merkle", "nbits", "ntime", diff --git a/package-lock.json b/package-lock.json index 6c01c30..a81f90a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "UNLICENSED", "dependencies": { "@nestjs/common": "^9.0.0", + "@nestjs/config": "^2.3.2", "@nestjs/core": "^9.0.0", "@nestjs/platform-fastify": "^9.4.2", "class-transformer": "^0.5.1", @@ -1553,6 +1554,30 @@ } } }, + "node_modules/@nestjs/config": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.3.2.tgz", + "integrity": "sha512-VtGV8PBpxzMzz68kdxTWqPm9v7SYCSZXQ0tC72AMNnjdmU+CVjUSLpEpdnm0XcWHxE1nV6wSI3HZxsATIV4ZxA==", + "dependencies": { + "dotenv": "16.0.3", + "dotenv-expand": "10.0.0", + "lodash": "4.17.21", + "uuid": "9.0.0" + }, + "peerDependencies": { + "@nestjs/common": "^7.0.0 || ^8.0.0 || ^9.0.0", + "reflect-metadata": "^0.1.13", + "rxjs": "^6.0.0 || ^7.2.0" + } + }, + "node_modules/@nestjs/config/node_modules/uuid": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", + "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/@nestjs/core": { "version": "9.4.2", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.4.2.tgz", @@ -3449,6 +3474,22 @@ "node": ">=6.0.0" } }, + "node_modules/dotenv": { + "version": "16.0.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", + "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==", + "engines": { + "node": ">=12" + } + }, + "node_modules/dotenv-expand": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-10.0.0.tgz", + "integrity": "sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==", + "engines": { + "node": ">=12" + } + }, "node_modules/ecc-jsbn": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz", @@ -9531,6 +9572,24 @@ "uid": "2.0.2" } }, + "@nestjs/config": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.3.2.tgz", + "integrity": "sha512-VtGV8PBpxzMzz68kdxTWqPm9v7SYCSZXQ0tC72AMNnjdmU+CVjUSLpEpdnm0XcWHxE1nV6wSI3HZxsATIV4ZxA==", + "requires": { + "dotenv": "16.0.3", + "dotenv-expand": "10.0.0", + "lodash": "4.17.21", + "uuid": "9.0.0" + }, + "dependencies": { + "uuid": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", + "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==" + } + } + }, "@nestjs/core": { "version": "9.4.2", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.4.2.tgz", @@ -10968,6 +11027,16 @@ "esutils": "^2.0.2" } }, + "dotenv": { + "version": "16.0.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", + "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==" + }, + "dotenv-expand": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-10.0.0.tgz", + "integrity": "sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==" + }, "ecc-jsbn": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz", diff --git a/package.json b/package.json index 16067b8..c5186d3 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "dependencies": { "@nestjs/common": "^9.0.0", + "@nestjs/config": "^2.3.2", "@nestjs/core": "^9.0.0", "@nestjs/platform-fastify": "^9.4.2", "class-transformer": "^0.5.1", diff --git a/src/app.module.ts b/src/app.module.ts index d79a4dd..cb060d4 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,17 +1,23 @@ import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; import { AppController } from './app.controller'; import { AppService } from './app.service'; +import { BitcoinRpcService } from './bitcoin-rpc.service'; import { BitcoinStratumProvider } from './bitcoin-stratum.provider'; @Module({ imports: [ - + ConfigModule ], controllers: [AppController], - providers: [AppService, BitcoinStratumProvider], + providers: [ + AppService, + BitcoinStratumProvider, + BitcoinRpcService + ], }) export class AppModule { constructor(private readonly bitcoinStratumProvider: BitcoinStratumProvider) { diff --git a/src/bitcoin-rpc.service.ts b/src/bitcoin-rpc.service.ts new file mode 100644 index 0000000..310b5cc --- /dev/null +++ b/src/bitcoin-rpc.service.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { RPCClient } from 'rpc-bitcoin'; + + +@Injectable() +export class BitcoinRpcService { + + private client: RPCClient; + + constructor(configService: ConfigService) { + const url = configService.get('BITCOIN_RPC_URL'); + const user = configService.get('BITCOIN_RPC_USER'); + const pass = configService.get('BITCOIN_RPC_PASSWORD'); + const port = parseInt(configService.get('BITCOIN_RPC_PORT')); + const timeout = parseInt(configService.get('BITCOIN_RPC_TIMEOUT')); + this.client = new RPCClient({ url, port, timeout, user, pass }); + console.log('Bitcoin RPC connected'); + } + + + public async getBlockTemplate() { + + const result = await this.client.getblocktemplate({ + template_request: { + rules: ['segwit'], + mode: 'template', + capabilities: ['serverlist', 'proposal'] + } + }); + } +} +