-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added merkle root summary to tendermint-bsync
- Loading branch information
1 parent
28ce140
commit 2e2c531
Showing
5 changed files
with
57 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 13 additions & 7 deletions
20
integrations/tendermint/utils/merkle.ts → ...ions/tendermint-bsync/src/utils/merkle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
import * as crypto from '@cosmjs/crypto'; | ||
import { dataItemToSha256, generateMerkleRoot } from "@kyvejs/sdk"; | ||
import { dataItemToSha256, generateMerkleRoot } from '@kyvejs/sdk'; | ||
|
||
// Creates an Array of hashes from an array of data items (bundle). | ||
// The hash of a data item consists of the Merkle root from the block and the block | ||
// results (only two leafs) and the key of the data item. This allows the | ||
// Trustless API to serve block and block results independently. | ||
export function createHashesFromTendermintBundle(bundle: any[]): Uint8Array[] { | ||
return bundle.map(dataItem => { | ||
const blockHashes: Uint8Array[] = [dataItemToSha256(dataItem.value?.block), dataItemToSha256(dataItem.value?.block_results)]; | ||
return bundle.map((dataItem) => { | ||
const blockHashes: Uint8Array[] = [ | ||
dataItemToSha256(dataItem.value?.block), | ||
dataItemToSha256(dataItem.value?.block_results), | ||
]; | ||
|
||
const merkleRoot: Uint8Array = generateMerkleRoot(blockHashes); | ||
|
||
return tendermintDataItemToSha256(dataItem.key, merkleRoot) | ||
}) | ||
return tendermintDataItemToSha256(dataItem.key, merkleRoot); | ||
}); | ||
} | ||
|
||
function tendermintDataItemToSha256(key: string, merkleRoot: Uint8Array): Uint8Array { | ||
function tendermintDataItemToSha256( | ||
key: string, | ||
merkleRoot: Uint8Array | ||
): Uint8Array { | ||
const keyBytes = crypto.sha256(Buffer.from(key, 'utf-8')); | ||
|
||
const combined = Buffer.concat([keyBytes, merkleRoot]); | ||
|
||
return crypto.sha256(combined); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as crypto from '@cosmjs/crypto'; | ||
import { dataItemToSha256, generateMerkleRoot } from '@kyvejs/sdk'; | ||
|
||
// Creates an Array of hashes from an array of data items (bundle). | ||
// The hash of a data item consists of the Merkle root from the block and the block | ||
// results (only two leafs) and the key of the data item. This allows the | ||
// Trustless API to serve block and block results independently. | ||
export function createHashesFromTendermintBundle(bundle: any[]): Uint8Array[] { | ||
return bundle.map((dataItem) => { | ||
const blockHashes: Uint8Array[] = [ | ||
dataItemToSha256(dataItem.value?.block), | ||
dataItemToSha256(dataItem.value?.block_results), | ||
]; | ||
|
||
const merkleRoot: Uint8Array = generateMerkleRoot(blockHashes); | ||
|
||
return tendermintDataItemToSha256(dataItem.key, merkleRoot); | ||
}); | ||
} | ||
|
||
function tendermintDataItemToSha256( | ||
key: string, | ||
merkleRoot: Uint8Array | ||
): Uint8Array { | ||
const keyBytes = crypto.sha256(Buffer.from(key, 'utf-8')); | ||
|
||
const combined = Buffer.concat([keyBytes, merkleRoot]); | ||
|
||
return crypto.sha256(combined); | ||
} |