Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/k-nuth/js-api

Bitcoin full node as a Javascript library
https://github.com/k-nuth/js-api

Last synced: 2 days ago
JSON representation

Bitcoin full node as a Javascript library

Awesome Lists containing this project

README

        

# Javascript/Typescript API

> Bitcoin Cash full node as a Javascript/Typescript library

[![NPM Version](https://img.shields.io/npm/v/@knuth/bch?logo=npm&style=for-the-badge)](https://www.npmjs.com/package/@knuth/bch)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=for-the-badge&logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAHYcAAB2HAY%2Fl8WUAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMTCtCgrAAAADB0lEQVR4XtWagXETMRREUwIlUAIlUAodQAl0AJ1AB9BB6AA6gA6MduKbkX%2BevKecNk525jHO3l%2Fp686xlJC70%2Bl0C942vjV%2Bn9FreVQbBc0wWujfRpW8Z78JaIb53hhJ1ygTA80w9PQ36duBMjHQHPCuoQZfutSjeqU1PAJN4E3j2pN7aVKv6pnWcgGawNfGa5N6prVcgGZBn8yvVXZXQbOgPXokXaPMNZwoc41D%2FaHZ8b7hpBrKjnCizIjD%2FaHZ8aPR6%2BeZXqqh7Agnyow43B%2BaZz40qnQ36a6rlsYgnChDLOkPzTN1z%2B9PafU0N3OAcaIMsaQ%2FNBufG1X9JyrtDMr0Y4xwokxlWX%2BPjAYdemhPrWeDvYcPJ8r0LO3v4oszNfivQQuTp2u9qJGKE2V6lvZ38UVj9q3t3oqEE2U2lvfXF4t6qPjTqDUV1fRyhw8nymws768vfOr2NtqOqFY4UUZE%2BusL6VDRX7%2FGzOHDiTIi0t9WMPsUKzNPx4kysf62gmuHir3sPXw4USbWny485ZOc2PsJ7VTro%2F3pwp5DxV7qHq2xa41TrY%2F2J7PfJkaHir3UwwdtU061PtqfTP0CUaYm2v3LxCtoDI2lMWk8p1of7Y8K0jhRJgaaYZwoE0P%2FpFUndZqtP6T4BE2zC5qtP6T4BE2zC5qtPyRN8OvhZUQae3ZBtT7anyb49PA6Ivp5wKnWR%2FvbJkncZXr6wokysf62CXRCWjmJxhqd2JwoE%2BuvTqS37JGJlB39GLzhRJmN5f31gz8XTpSJgWYYJ8rEQDOME2VioBnGiTIx0AzjRJkYaIZxokwMNMM4USYGmmGcKBMDzTBOlImBZhgnysRAM4wTZWKgGcaJMjHQDONEmRhohnGiTAw0wzhRJgaaYZwoEwPNME6UiYFmGCfKxEAzjBNlYqAZxokyMdAMoL%2FO%2BNi4bzjpT1e%2BNFb8V7gFzUXMLHqk%2BM1A8wArFj1S5GagOUly0SMtuxloTnJrUU%2B7QXOSW4t62g2ak9xa1NNu0Jzk1qKednK6%2Bw9roIB8keT%2F3QAAAABJRU5ErkJggg%3D%3D)](LICENSE.md)
[![js-standard-style](https://img.shields.io/badge/javascript-standard%20code%20style-green.svg?style=for-the-badge)](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