# 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](https://docs.nxtfi.org/authentication) section.
{% endhint %}

### Requirements

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

<div data-full-width="true"><figure><img src="https://2652816227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fn2y6HOlnzwlDdvSh1vTk%2Fuploads%2FLX3My7tbYklR42PuoCrF%2FNXTFI%20V2%20(1).jpg?alt=media&#x26;token=5f58f8ef-79a1-419b-aa4d-584ffe104316" 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](https://docs.nxtfi.org/blockchain-libraries/javascript-createblock).
{% 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 %}
