Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
@truestamp/tree
Advanced tools
This library provides a TypeScript/JavaScript API for creating Merkle trees, and validating Merkle inclusion proofs.
This library provides a TypeScript/JavaScript API for creating Merkle trees, and validating Merkle inclusion proofs.
Data provided to create the tree is required to already be in binary form (Uint8Array
) and all
internal operations are performed on binary data.
The library provides several encodings for Merkle inclusion proofs, any one of which is acceptable for validation.
This library doesn't hash original data, you have to provide hashed (or arbitrary binary data of fixed size). All SHA2 and SHA3 common hash functions, implemented in pure TypeScript, are exported for your convenience and are the same hash functions used internally.
The library performs extensive compile time (TypeScript), and run time, validation of the incoming and outgoing data to ensure correctness and safety at the possible expense of raw performance. That being said, it is still extremely fast, able to round-trip process a 1,000 item tree in about 20 milliseconds.
In order to help prevent the possibility of a second pre-image attack (see 1 2 3) the data leaf nodes provided to construct the tree are prefixed with a 0x00
byte. All inner nodes of the tree are prefixed with a 0x01
byte. These prefixes are also applied during the validation step.
An additional step that can be taken to avoid second pre-image attack vulnerability is to pre-hash your leaves using a different hash function (see 3) than the function provided to construct the tree so that H(x) != H'(x)
. For example, use sha3-256
to pre-hash the leaves, and use sha2-256
to construct the tree.
This implementation is potentially vulnerable to a forgery attack for an unbalanced Merkle Tree (see 4), wherein, in an unbalanced Merkle Tree, the last leaf node can be duplicated to create an artificially balanced tree. This can result in the same root hash. To avoid this vulnerability, do not construct an unbalanced Merkle Trees (where the length of the data
array provided is not a power of 2). This library provides an optional requireBalanced
configuration flag that will throw an Error
if the data.length
is not a power of 2.
npm install @truestamp/tree
Node.js:
const { Tree } = require('@truestamp/tree')
// import { Tree } from '@truestamp/tree';
// A sample data array with 8 elements of 32 bytes each.
// The data elements should be the same length as the output of the hash.
const data = [
new Uint8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
]),
new Uint8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
]),
new Uint8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
]),
new Uint8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
]),
new Uint8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
]),
new Uint8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
]),
new Uint8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
]),
new Uint8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
]),
]
// Construct the Merkle tree, the hash function name and options are optional.
const t = new Tree(data, 'sha256', { requireBalanced: true, debug: false })
// Get the root of the tree for later use
const r = t.root()
// For example purposes, pick (and store a verifiable copy) of
// one data element that will be verified.
// In the real world likely store a pristine copy of the data
// for later re-hashing and verification.
const d = data[4]
// Extract the Merkle inclusion proof for that data element to store
// alongside your pristine copy of the data.
// In the real world you'd likely do the same for every data element
// to store and later verify).
const p = t.proofObject(d)
// At any time in the future, verify your proof by providing
// the stored root, the proof, and the data. The last argument, the hash
// function name, is optional in this case since we're using the
// proof object encoding.
console.log('verified?', Tree.verify(r, p, d)) // returns true or false
A more detailed version of this example can be found in [examples/example.cjs].
The TypeScript API documentation for this project is generated and published upon each new release.
Using the example code on a MacBook Pro and the following size sample data sets of random elements and the Node.js crypto
SHA-256 function:
1,000
data elements13.03ms
0.154ms
3.42ms
1.15ms
1,000,000
data elements3.490s
0.204ms
23.139ms
1.533ms
Please see our Github organization's profile at github.com/truestamp for quick access to links related to these and other important topics.
Copyright © 2019-2023 Truestamp Inc. All Rights Reserved.
FAQs
This library provides a TypeScript/JavaScript API for creating Merkle trees, and validating Merkle inclusion proofs.
The npm package @truestamp/tree receives a total of 5 weekly downloads. As such, @truestamp/tree popularity was classified as not popular.
We found that @truestamp/tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.