Signature Verification and Off-Chain Verification with Starknet.js
Starknet.js is the JavaScript library for interacting with Starknet, and signature handling is one of its most practical areas. This guide explains how to verify signatures off-chain, how that differs from on-chain verification, and how to structure your code so that signatures and co-signers can be checked reliably before a transaction is ever submitted.
Off-chain verification is useful whenever you want to confirm that a message or typed data was signed by the expected account without paying gas or waiting for a block. Starknet.js gives you the primitives to do this in JavaScript, whether you are running in a browser, a Node.js backend, or a test environment.
Why verify signatures off-chain?
On Starknet, account contracts validate signatures when a transaction is executed. That on-chain check is authoritative, but it happens only after a transaction is sent. Off-chain verification lets you answer the same question earlier: did this account authorize this payload?
- Better UX: show a confirmation state before asking a user to approve a transaction.
- Safer backends: reject unauthorized requests at your API boundary instead of relying on a later on-chain failure.
- Co-signer flows: check that every required signer has signed a message before assembling a multisig-style transaction.
- Testing: assert signature behavior in unit tests without deploying to a network.
Off-chain verification is a complement to on-chain validation, not a replacement. Treat it as a pre-check and let the account contract remain the source of truth.
How Starknet signatures work
Starknet accounts are contracts, and each account implementation defines how it validates signatures. The common pattern is that a signer produces a signature over a message hash, and the account contract checks that signature against the public key or keys it holds.
Two details matter for off-chain verification:
- The message hash must match exactly. The same payload, domain, and encoding used at signing time must be used at verification time.
- The account implementation matters. Different account contracts may use different curves, key layouts, or validation rules. Verify against the same account class that will ultimately validate on-chain.
Starknet.js exposes the signing and verification utilities you need for these steps, along with typed data support for structured messages.
Verifying a signature with Starknet.js
A typical off-chain verification flow has four steps:
- Define the message. Use a plain message hash or typed data, depending on what your application signs.
- Collect the signature. The signer returns a signature array, which you pass to your verifier.
- Resolve the expected signer. Know which public key or account address should have produced the signature.
- Verify. Use the Starknet.js verification utilities for the relevant curve and compare the result to your expectation.
Keep the verification code close to the code that constructs the message. If the two drift apart, verification will fail for valid signatures or, worse, succeed for the wrong payload. Centralize message construction in one function and reuse it for both signing and verification.
Verifying account signatures and co-signers
When more than one party must approve an action, off-chain verification becomes a coordination tool. Each co-signer signs the same message, and your application verifies each signature against the expected signer before combining them.
- Track signer identity explicitly. Map each signature to the account or public key that produced it.
- Verify all signatures, not just the first. A partial set should be treated as incomplete, not valid.
- Decide on ordering. If your account contract expects signatures in a specific order, enforce that order during verification.
- Handle duplicates. Reject repeated signatures from the same signer unless your account logic intentionally allows them.
For account-level checks, you can also call the account contract's validation entry point through Starknet.js. This runs the same logic the network would run, giving you a stronger signal than a standalone key check. Use it when you need to confirm that a specific account contract accepts a signature, not just that a key pair matches.
Practical guidance and pitfalls
- Bind signatures to context. Include a domain, chain ID, or application identifier in the signed payload so a signature cannot be replayed elsewhere.
- Include a nonce or timestamp. Prevent reuse of old signatures in your off-chain flows.
- Normalize inputs. Hex strings, byte arrays, and big numbers can be represented differently. Normalize before hashing.
- Do not trust client-side verification alone. If a backend makes decisions based on a signature, verify it server-side as well.
- Test with real account contracts. Unit tests with raw keys are useful, but integration tests against the account class you deploy catch implementation-specific behavior.
- Log verification failures carefully. Avoid logging full signatures or sensitive payloads in production.
Frequently asked questions
Is off-chain verification enough to authorize a transaction?
No. Off-chain verification is a pre-check. The account contract validates the signature when the transaction executes, and that on-chain validation is what actually authorizes the action.
Can I verify signatures for any Starknet account?
You can verify against the account's public key material, but account contracts can implement custom validation. For exact behavior, validate through the account contract itself using Starknet.js.
What should I do if verification fails for a signature I believe is valid?
Check the message hash, the domain and chain ID, the encoding of the signature, and the account implementation. A mismatch in any of these will cause a valid signature to fail verification.
Does Starknet.js support typed data signing and verification?
Yes. Starknet.js includes typed data utilities so you can sign structured messages and verify them consistently off-chain.
How should I handle multiple co-signers?
Verify each signature against its expected signer, enforce any ordering your account contract requires, and treat incomplete sets as invalid until all required signatures are present.