# Create New Block

{% hint style="info" %}
**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 [Authentication](/authentication.md) section.
{% endhint %}

### Requirements

* Read the height and hash of the last block of the scope. [See example here](/api-reference/get-the-last-block.md).
* Authorized key-pair. [See Authentication](/authentication.md).

<div data-full-width="true"><figure><img src="/files/QkmNoOIIDMB8NN8HDR35" alt=""><figcaption></figcaption></figure></div>

## 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.

{% hint style="info" %}
The procedures explained below can be safely executed through the implementation of our [official libraries](/blockchain-libraries/javascript-createblock.md).
{% endhint %}

## 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.&#x20;

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

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

**Example:**

{% code overflow="wrap" %}

```javascript
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");
```

{% endcode %}

## Generate a new hash

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

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

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

**Example:**

{% code overflow="wrap" %}

```javascript
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");
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nxtfi.org/create-new-block.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
