Keypairs and Signatures
Native Signatures
The Signature
class in o1js refers to a Schnorr signature over the Pallas curve. It works with the PrivateKey
and PublicKey
classes to
sign and verify messages of Field arrays.
const privateKey = PrivateKey.random();
const publicKey = privateKey.toPublicKey();
const message = CircuitString.fromString("Hello World!").values.map((char) =>
char.toField()
);
const signature = Signature.create(privateKey, message);
signature.verify(publicKey, message);
ECDSA Signatures (Ethereum)
ECDSA signatures are supported as well. In general, elliptic curve cryptography is supported over foreign curves, but you can follow the snippet below for verifying signatures specifically for an Ethereum configuration.
class Secp256k1 extends createForeignCurve(Crypto.CurveParams.Secp256k1) {}
class Ecdsa extends createEcdsa(Secp256k1) {}
class Bytes32 extends Bytes(32) {}
const messageEth = "Secrets hidden, truth in ZKPs ;)";
// compressed public key generated by ethers.js
const publicKeyEth = Secp256k1.fromEthers(
"0x020957928494c38660d254dc03ba78f091a4aea0270afb447f193c4daf6648f02b"
);
// ECDSA signature generated by ethers.js
const signatureEth = Ecdsa.fromHex(
"0x6fada464c3bc2ae127f8c907c0c4bccbd05ba83a584156edb808b7400346b4c9558598d9c7869f5fd75d81128711f6621e4cb5ba2f52a2a51c46c859f49a833a1b"
);
const msgBytes = Bytes32.fromString(messageEth);
signatureEth.verifyEthers(msgBytes, publicKeyEth);
Read more at the language reference: Signature, PrivateKey, PublicKey, createEcdsa.