Signing NEAR transactions from a canister with threshold Ed25519

I built a small canister that owns a NEAR testnet account and signs transfers with sign_with_schnorr (Ed25519). On mainnet the key is split across the subnet’s nodes and never exists in one place.

How it works

The canister derives an Ed25519 key with schnorr_public_key. Its NEAR account is the implicit account hex(public_key): funding it creates it, with the canister’s key as its only full-access key.
The canister encodes the NEAR transaction itself (Borsh), signs sha256(tx), and verifies the signature before returning it.
Nonce and block hash come from the caller and are untrusted by design: a wrong value only makes NEAR reject the transaction, it can’t change what gets paid to whom.
The encoder is tested against vectors generated by @near-js/transactions, NEAR’s own library, rather than against itself. Before signing, a script also refuses to proceed if the canister’s bytes differ from the library’s.

First canister-signed transfer on NEAR testnet, signed on a local network (test_key_1), mainnet deployment next:
https://testnet.nearblocks.io/txns/HUvCWehTDRtJDVqsipXcgsb16kHXaUsbDYbxegNAL1qN

Built with icp-cli and ic-cdk 0.20. Two questions for people who have run this in production:

Beyond the documented fee: what throughput and latency should I expect from sign_with_schnorr with key_1 if a canister signs several times a minute? Are there per-canister rate limits?
Any pitfalls with Ed25519 signatures for chains that sign a hash of the payload rather than the payload itself?

Code will be public soon. Happy to hear feedback on the approach.

If you are on the fiduciary subnet, it costs more in terms of cycles, you can get a signatures in 1-2s. If your canister is on another subnet then about 4-6 seconds due to X-net comms between subnets. Dfinity did a great job recently in boosting the presign pool, if we remember correctly it can do 100 sigs pre signed, and it should refill fairly quickly. In some conditions where you have a big load we often rely on queue and batches txs but likely your users won’t use this much unless you have it on trading or servicing other users in SDK.

Congratulations on integrating near with an encoder it’s a nasty job especially verifying it, we had a verifier for Near that computes txs we were preparing to launch soon for the public, out of curiosity the canister is in rust or Motoko?

Thanks, that helps. The canister is in Rust, on ic-cdk 0.20.

A few signatures a minute is all I need, so 4-6 s on a regular subnet is fine and I shouldn’t need batching. The docs say the fee follows the key’s subnet rather than the caller’s, so I don’t see a reason to deploy on the fiduciary subnet just for signing. I’ll measure the latency once it’s on mainnet.

The encoder was manageable because it only covers Transfer. I test it against vectors generated by @near-js/transactions, and a script compares the bytes twice: before signing, and again on the signed transaction before it’s submitted. The second check matters because preview_transfer is a query, so a single replica answers it.

I’m curious about your verifier. Does it decode signed transactions and compare them to an expected payload, or does it do more than that?

As promised, the code is public now: https://github.com/n1c0la5-lab/unheld

I measured the latency on mainnet. The canister is ch3s7-hiaaa-aaaao-qqm3q-cai, on a regular application subnet with key_1. Signed requests took a median of 5.43 s (n=10, 5.01 to 7.85 s), refused ones 1.28 s, measured with icp canister call one after another and including the CLI start. That matches the 4-6 s you mentioned. It is not a load test. The balance fell by 26,218,941,691 cycles across one signature, which is the documented key_1 fee plus about 65 million for the surrounding calls.

The canister signed a 0.01 NEAR transfer on mainnet, and NEAR testnet finalized it: https://testnet.nearblocks.io/txns/HVSorG3F2y85HcBxC5Sun7LTWDUPmRYJE3Am9ooKfBhj

Since my first post it became a guard for an AI agent. I built it at NEAR HACKATHON and submitted it today. The agent runs on NEAR AI Cloud and holds no key. To pay, it asks the canister. Before signing, the canister checks a policy the agent cannot change: a per-payment limit, a daily limit and a receiver allowlist. NEP-413 messages get their own recipient allowlist. Every request, signed or refused, goes into the canister’s audit log. In a prompt-injection demo over 70 runs, the model was fooled in 4 of 5 attack types and the canister signed nothing the policy forbids. The agent and the demo ran on a local network, not on mainnet.

Demo video (2:17): https://x.com/Parzival_3000/status/2121607601943195691

./setup.sh builds the canister on a local network and replays the attack requests. It needs no key, no model and no cycles.

One question for people running signing canisters in production: the policy limits amounts, not the number of signatures. A compromised agent could keep asking for tiny transfers to an allowed receiver, and every signature costs the canister about 26 billion cycles until it freezes. Do you rate-limit callers inside the canister (signatures per hour), make the caller pay for each signature, or rely on the cycle balance and freezing threshold?