> For the complete documentation index, see [llms.txt](https://docs.seismic.systems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.seismic.systems/clients/python/precompiles/secp256k1-sign.md).

# secp256k1\_sign

On-chain secp256k1 ECDSA signing

Sign a message with the secp256k1 signing precompile at `0x69`.

## Overview

`secp256k1_sign()` and `async_secp256k1_sign()`:

* hash the input `message` with an EIP-191 personal-sign prefix
* sign the hash using the provided private key
* return signature bytes (`HexBytes`)

## Signature

```python
def secp256k1_sign(
    w3: Web3,
    *,
    sk: PrivateKey,
    message: str,
) -> HexBytes

async def async_secp256k1_sign(
    w3: AsyncWeb3,
    *,
    sk: PrivateKey,
    message: str,
) -> HexBytes
```

## Parameters

| Parameter | Type                                                                                           | Required | Description                   |
| --------- | ---------------------------------------------------------------------------------------------- | -------- | ----------------------------- |
| `w3`      | `Web3` / `AsyncWeb3`                                                                           | Yes      | Connected Seismic client      |
| `sk`      | [`PrivateKey`](https://docs.seismic.systems/clients/python/api-reference/types/private-key.md) | Yes      | 32-byte secp256k1 private key |
| `message` | `str`                                                                                          | Yes      | Message text to sign          |

## Returns

| Type       | Description                              |
| ---------- | ---------------------------------------- |
| `HexBytes` | Signature bytes (r/s plus recovery byte) |

## Examples

### Basic Usage

```python
import os
from seismic_web3 import PrivateKey, create_public_client
from seismic_web3 import precompiles as sp

w3 = create_public_client("https://testnet-1.seismictest.net/rpc")

sk = PrivateKey(os.urandom(32))
sig = sp.secp256k1_sign(w3, sk=sk, message="hello")
print(sig.hex())
```

### Verify Off-Chain

```python
from eth_account import Account
from eth_account.messages import encode_defunct

message = "hello"
sig = sp.secp256k1_sign(w3, sk=sk, message=message)

recovered = Account.recover_message(encode_defunct(text=message), signature=sig)
expected = Account.from_key(bytes(sk)).address
assert recovered == expected
```

### Async Usage

```python
import os
from seismic_web3 import create_async_public_client
from seismic_web3 import PrivateKey
from seismic_web3 import precompiles as sp

async def main():
    w3 = create_async_public_client("https://testnet-1.seismictest.net/rpc")
    sk = PrivateKey(os.urandom(32))
    sig = await sp.async_secp256k1_sign(w3, sk=sk, message="async hello")
    print(sig.hex())
```

### Read Signature Components

```python
r = int.from_bytes(sig[0:32], "big")
s = int.from_bytes(sig[32:64], "big")
v = sig[64] if len(sig) > 64 else None
```

## Hashing Behavior

The SDK hashes with:

```python
from eth_hash.auto import keccak

prefix = f"\x19Ethereum Signed Message:\n{len(message)}".encode()
message_hash = keccak(prefix + message.encode())
```

## Gas Cost

Fixed cost: `3000`.

## Notes

* `message` is UTF-8 encoded before hashing/signing.
* Keep private keys out of logs and telemetry.
* The output format is suitable for typical Ethereum signature verification flows.

## See Also

* [secp256k1 Sign — Precompiles Reference](https://docs.seismic.systems/reference/precompiles/secp256k1-sign.md)
* [PrivateKey](https://docs.seismic.systems/clients/python/api-reference/types/private-key.md)
* [EIP-191](https://eips.ethereum.org/EIPS/eip-191)
* [ecdh](https://docs.seismic.systems/clients/python/precompiles/ecdh.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.seismic.systems/clients/python/precompiles/secp256k1-sign.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
