nano-pow
TypeScript icon, indicating that this package has built-in type declarations

3.1.2 • Public • Published

nano-pow

Proof-of-work generation and validation with WebGPU/WebGL for Nano cryptocurrency.

NanoPow uses WebGPU to generate proof-of-work nonces meeting the requirements of the Nano cryptocurrency. WebGPU is cutting edge technology, so for browsers which do not yet support it, a WebGL 2.0 implementation is used as a fallback.

All calculations take place client-side, so nonces can be generated offline and cached for the next transaction block. For more information about the proof-of-work equation defined by Nano, see https://docs.nano.org/integration-guides/work-generation/#work-calculation-details

Installation

npm i nano-pow

Usage

Import

The easiest way to use NanoPow is to import it directly. Based on the features available in the environment, NanoPow will try to use its most performant API.

The following two import statements are equivalent, and both are provided to accomodate project style preferences:

import { NanoPow } from 'nano-pow'
// OR
import NanoPow from 'nano-pow'

A specific API can be explicitly imported:

import { NanoPowGpu, NanoPowGl } from 'nano-pow'

Use it directly on a webpage with a script module:

<script type="module">
	(async () => {
		const { NanoPow } = await import('https://cdn.jsdelivr.net/npm/nano-pow@latest')
		const work = await NanoPow.search(some_hash)
		console.log(work)
	})()
</script>

Search

// `hash` is a 64-char hex string
const hash = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
const work = await NanoPow.search(hash)
// Result is a 16-char hex string

Validate

// `work` is a 16-char hex string
const work = 'fedcba0987654321'
// `hash` is a 64-char hex string
const hash = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
const isValid = await NanoPow.validate(work, hash)
// Result is a boolean

Options

const options = {
	// default 0xFFFFFFF8 for send/change blocks
	threshold: number,
	// default 8, valid range 1-32
	effort: number,
	// default false
	debug: true
}
const work = await NanoPow.search(hash, options)

Command Line

NanoPow can be installed globally and executed from the command line. This is useful for systems without a graphical interface.

$ npm -g i nano-pow
$ nano-pow --help  # view command documentation

Notes

The work field in a Nano transaction block contains an 8-byte nonce that satisfies the following equation:

𝘣𝘭𝘢𝘬𝘦2𝘣(𝘯𝘰𝘯𝘤𝘦 || 𝘣𝘭𝘰𝘤𝘬𝘩𝘢𝘴𝘩) ≥ 𝘵𝘩𝘳𝘦𝘴𝘩𝘰𝘭𝘥

  • 𝘣𝘭𝘢𝘬𝘦2𝘣() is the cryptographic hash function BLAKE2b.
  • 𝘯𝘰𝘯𝘤𝘦, an 8-byte value, is generated for the transaction.
  • || is concatenation.
  • 𝘣𝘭𝘰𝘤𝘬𝘩𝘢𝘴𝘩, a 32-byte value, is either the public key of brand new accounts without transactions or the hash of the most recent block in the account chain for all other accounts.
  • 𝘵𝘩𝘳𝘦𝘴𝘩𝘰𝘭𝘥 is 0xFFFFFFF800000000 for send/change blocks and 0xFFFFFE0000000000 for receive/open/epoch blocks.

The threshold is implemented in code as only the first 32 bits due to WGSL only supporting u32 integer types, but the result is the same. For example, if checking whether a two-digit number xx > 55, and it is known that the first digit is 6, it is also automatically known that xx is greater than 55. The default threshold used is the send/change difficulty. Any other threshold can be specified in practice.

The BLAKE2b implementation has been optimized to the extreme for this package due to the very narrow use case to which it is applied. The compute shader is consequently immense, but the goal is to squeeze every last bit of speed and performance out of it.

Tests

test.html in the source repository contains some basic tests to compare the speed of this tool. Feel free to check out how your system fares.

Building

  1. Clone source
  2. Enter the directory
  3. Install dev dependencies
  4. Compile, minify, and bundle
git clone https://zoso.dev/nano-pow.git
cd nano-pow
npm i
npm run build

Reporting Bugs

Email: bug-nano-pow@zoso.dev

Acknowledgements

numtel/nano-webgl-pow for his WebGL implementation

Licenses

GPLv3 (or later) & MIT

Donations

If you find this package helpful, please consider tipping the developer.

nano_1zosoqs47yt47bnfg7sdf46kj7asn58b7uzm9ek95jw7ccatq37898u1zoso

Package Sidebar

Install

npm i nano-pow

Homepage

zoso.dev

Weekly Downloads

132

Version

3.1.2

License

(GPL-3.0-or-later AND MIT)

Unpacked Size

119 kB

Total Files

10

Last publish

Collaborators

  • zosoled