NxtFi
  • NxtFi Blockchain
  • Testing Network
  • Block Viewer
  • Playground
  • Authentication
  • Create New Block
  • Blockchain Libraries
    • NxtFi Tools
    • Javascript - CreateBlock
  • Smart Contracts
  • The Storage System
  • Upload Media Files
  • Blockchain API Interactions
  • Access Control Layer
    • How it Works
    • Syntax & Smart Contract requirements
    • Example
  • API - Listing Files
  • API Reference
    • Get node health
    • Get the last block
    • Get block information by hash
    • Get block information by timestamp
    • Get block information by height
    • Get files from storage by scope
    • Storage tracking interactions
    • Storage tracking timestamp block info
    • Submit new block
    • Presign media file upload
Powered by GitBook
On this page
  • Requirements
  • Build a new block
  • Sign a block
  • Generate a new hash

Create New Block

Here you will find the description on how new blocks structure should be configured

PreviousAuthenticationNextBlockchain Libraries

Last updated 1 year ago

IMPORTANT: To write new blocks on the blockchain, an administrator must first register a public key and thus obtain the necessary permissions, please see the section.

Requirements

  • Read the height and hash of the last block of the scope. .

  • Authorized key-pair. .

Build a new block

The following data is required to build a new block:

  • prevHash: Hash of the last block of the scope

  • height: Height of the last block of the scope + 1

  • version: API version, at the time of writing this documentation is 2

  • data: Block content: Any type. will be stringified as default.

  • timestamp: Tiemstamp in Unix format

  • scope:Name of the scope in which to write. This could be signer, child, or Smart Contract hash (soon to be SC alias).

  • by: Name of the authorized signer. Owner of the key-pair used to sign

  • signature: Block signature

  • hash: Hash of the new block

To obtain the last two fields, it is necessary to go through a process to sign and generate the hash of the new block, which is explained below.

Sign a block

The signature of a new block is generated using a cryptographic tool that generates a hash based on the RSA-PSS algorithm.

The data used to generate the signature is an object that contains the following information:

{ prevHash, height, version, data, timestamp, by, scope }

Example:

let blockToSign = {
    prevHash: "bd125e337f0cc7782b0e3465157b60535151362220f490650bed6b4be69bbb4b",
    height: 14,
    version: 2,
    data: "Hello World!",
    timestamp: 1669768443723,
    scope: "DATA"
};

const algorithmParameters = {
    name: "RSA-PSS",
    saltLength: 32,
};

let signature0 = await subtle.sign(
    algorithmParameters,
    await getPrivateKey(),
    Buffer.from(JSON.stringify(blockToSign, null, 2))
);
let signature = Buffer.from(signature0).toString("base64");

Generate a new hash

Once you have the signature of the block, you must generate the new hash using the SHA-256 cryptographic algorithm.

The data used to generate the hash is an object that contains the following information:

{prevHash, height, version, data, timestamp, by, scope, signature }

Example:

const blockToHash = { 
    prevHash: "bd125e337f0cc7782b0e3465157b60535151362220f490650bed6b4be69bbb4b", 
    height: 14, 
    version: 2, 
    data: "Hello World!", 
    timestamp: 1669768443723, 
    by: "DATA",
    scope: "DATA",
    signature: "WQJFA6B1cr8jZqGu08fh/LHk41nWif4Qun+2g1eIrrE8/l3WxSiSFicM86YLqCWwtFVQ+i5j8wsY8BfrxE4Cz/6Mc8FXOsUgYKdUzIKC5Og0+PgwlJOSLIaazNHS/4ypKExcIatB1dpQKU4Y5e+ozytQIM/i5E4tPbPTzP3avVYfnsAutINpGKcvhW6+6A8FUWH90OmeOpXE+c7OQb400z4BlCCQWE7rYnxSM4gUCJBuJ1T5ABm7/JfdGffNk+5xThe1NBVu7WEo85OhMMOFpOm48k9q53At1pAduJaTuO1IYU5qXfmQJa5w5tm7MLmOsC0mGJVY/PoRDQ9/K/xu/g==",
}
let hash256 = crypto.createHash("sha256");
const data = hash256.update(JSON.stringify(blockToHash));
let hash = data.digest("hex");

The procedures explained below can be safely executed through the implementation of our .

official libraries
Authentication
See example here
See Authentication