https://github.com/k-nuth/js-api
Bitcoin full node as a Javascript library
https://github.com/k-nuth/js-api
Last synced: 3 months ago
JSON representation
Bitcoin full node as a Javascript library
- Host: GitHub
- URL: https://github.com/k-nuth/js-api
- Owner: k-nuth
- Created: 2020-01-30T03:35:02.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-12-06T00:26:29.000Z (7 months ago)
- Last Synced: 2025-03-18T03:56:14.002Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 2.33 MB
- Stars: 9
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Javascript/Typescript API
> Bitcoin Cash full node as a Javascript/Typescript library
[](https://www.npmjs.com/package/@knuth/bch)
[](LICENSE.md)
[](https://github.com/feross/standard)
![Telegram][badge.telegram]
[Knuth Javascript/Typescript API](https://www.npmjs.com/package/@knuth/bch) is a high performance implementation of the Bitcoin Cash protocol focused on users requiring extra performance and flexibility. It is a Bitcoin Cash node you can use as a library.
## Getting started with Javascript
1. Create a new Javascript console project:
```
$ mkdir HelloKnuth
$ cd HelloKnuth
$ npm init
```2. Add a reference to our Javascript API package:
```
$ npm install @knuth/bch
```3. Create a new file called `index.js` and write some code:
```Javascript
const kth = require("@knuth/bch")let running_ = false;
async function main() {
process.on('SIGINT', shutdown);
const config = kth.settings.getDefault(kth.network.mainnet);
const node = new kth.node.Node(config, false);
await node.launch(kth.startModules.all);
console.log("Knuth node has been launched.");
running_ = true;const [_, height] = await node.chain.getLastHeight();
console.log(`Current height in local copy: ${height}`);if (await comeBackAfterTheBCHHardFork(node)) {
console.log("Bitcoin Cash has been created!");
}node.close();
console.log("Good bye!");
}async function comeBackAfterTheBCHHardFork(node) {
const hfHeight = 478559;
while (running_) {
const [_, height] = await node.chain.getLastHeight();
if (height >= hfHeight) return true;
await sleep(10000);
}
return false;
}function shutdown() {
console.log('Graceful shutdown ...');
running_ = false;
}function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}(async () => {
try {
await main();
} catch (e) {
console.log(e);
}
})();```
4. Enjoy Knuth node as a Javascript library:
```
$ node index.js
```## Getting started with Typescript
1. Create a new Typescript console project:
```
$ mkdir HelloKnuth
$ cd HelloKnuth
$ npm init
```2. Add a reference to our Typescript API package and TypeScript definitions for Node.js:
```
$ npm install @knuth/bch
$ npm install @types/node```
3. Create a new file called `index.ts` and write some code:
```Typescript
import * as kth from "@knuth/bch";let running_ = false;
async function main() {
process.on('SIGINT', shutdown);
const config = kth.settings.getDefault(kth.network.mainnet);
const node = new kth.node.Node(config, false);
await node.launch(kth.startModules.all);
console.log("Knuth node has been launched.");
running_ = true;const [_, height] = await node.chain.getLastHeight();
console.log(`Current height in local copy: ${height}`);if (await comeBackAfterTheBCHHardFork(node)) {
console.log("Bitcoin Cash has been created!");
}node.close();
console.log("Good bye!");
}async function comeBackAfterTheBCHHardFork(node : kth.node.Node) {
const hfHeight = 478559;
while (running_) {
const [_, height] = await node.chain.getLastHeight();
if (height >= hfHeight) return true;
await sleep(10000);
}
return false;
}function shutdown() {
console.log('Graceful shutdown ...');
running_ = false;
}function sleep(ms : number) {
return new Promise((r) => setTimeout(r, ms));
}(async () => {
try {
await main();
} catch (e) {
console.log(e);
}
})();```
4. Enjoy Knuth node as a Typescript library:
```
$ ts-node index.ts
```## Issues
Each of our modules has its own Github repository, but in case you want to create an issue, please do so in our [main repository](https://github.com/k-nuth/kth/issues).
[badge.Travis]: https://travis-ci.org/k-nuth/js-api.svg?branch=master
[badge.Appveyor]: https://img.shields.io/appveyor/ci/Knuth/js-api.svg?style=for-the-badge&label=build&logo=appveyor&logoColor=white
[badge.Cirrus]: https://api.cirrus-ci.com/github/k-nuth/js-api.svg?branch=master
[badge.version]: https://badge.fury.io/gh/k-nuth%2Fkth-js-api.svg
[badge.release]: https://img.shields.io/github/release/k-nuth/js-api.svg
[badge.c]: https://img.shields.io/badge/C-11-blue.svg?style=flat&logo=c
[badge.telegram]: https://img.shields.io/badge/telegram-badge-blue.svg?logo=telegram&style=for-the-badge
[badge.slack]: https://img.shields.io/badge/slack-badge-orange.svg?logo=slack&style=for-the-badge