---
title: ledger_nonce
description: Generate a 32 byte nonce for use in an emitted transaction
---
import { Tabs, TabItem } from '@astrojs/starlight/components';

### Behaviour

<Tabs>
<TabItem label="C">
* Write a 32 byte random value to the write_ptr
</TabItem>

<TabItem label="JavaScript">
* Retrieves the nonce of the current ledger.
</TabItem>
</Tabs>



### Definition

C

<Tabs>
<TabItem label="C">
```c
int64_t ledger_nonce (
    uint32_t write_ptr,
    uint32_t write_len
);
```


</TabItem>

<TabItem label="JavaScript">
```javascript
function ledger_nonce(): ErrorCode | ByteArray
```
</TabItem>
</Tabs>

### Example

<Tabs>
<TabItem label="C">
```c
uint8_t n[32];
int64_t bytes_written = 
    ledger_nonce(n, 32);
```


</TabItem>

<TabItem label="JavaScript">
```javascript
const nonce = ledger_nonce()
```
</TabItem>
</Tabs>



### Parameters

<Tabs>
<TabItem label="C">
| Name       | Type      | Description                                                                              |
| ---------- | --------- | ---------------------------------------------------------------------------------------- |
| write_ptr | uint32_t | Pointer to a buffer of a suitable size to store the output. Should be at least 32 bytes. |
| write_len | uint32_t | Length of the output buffer.                                                             |


</TabItem>

<TabItem label="JavaScript">

</TabItem>
</Tabs>



### Return Code

<Tabs>
<TabItem label="C">


| Type                   | Description                                                                                                                           |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| ErrorCode \| ByteArray | Returns an error code if an error occurs, or an array representing the nonce of the current ledger. specified outside of hook memory. |
</TabItem>

<TabItem label="JavaScript">


| Type     | Description                                                                                                                                            |
| -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| int64_t | <p>The number of bytes written<br /><br />If negative, an error:<br /><code>OUT_OF_BOUNDS</code><br />- pointers/lengths specified outside of hook memory.</p> |
</TabItem>
</Tabs>

