Signing
Cardano data signatures are commonly used for login authentication, address ownership proof, and off-chain authorization. UKey Wallet returns a COSE format signature and public key via CIP-8.
Signature data (CIP-8)
The following example converts the message into a hexadecimal payload and then requests the user's signature:
const api = await window.cardano.ukey.enable();
const addresses = await api.getUsedAddresses();
const address = addresses[0];
// Note: message to sign
const payload = Buffer.from("Cardano demo message").toString("hex");
const signature = await api.signData(address, payload);
console.log({
signature: signature.signature, // COSE_Sign1 signature
key: signature.key, // COSE_Key 格式的公钥
});
Verification
import { COSESign1, COSEKey } from "@emurgo/cardano-message-signing-browser";
const coseSign1 = COSESign1.from_bytes(Buffer.from(signature.signature, "hex"));
const coseKey = COSEKey.from_bytes(Buffer.from(signature.key, "hex"));
// Retrieve signed payload
const signedPayload = coseSign1.payload();
// Validate signature
const publicKey = coseKey.header(0); // Retrieve the Ed25519 key
// place your validation logic here
Handle Errors
try {
const api = await window.cardano.ukey.enable();
} catch (error) {
if (error.code === -1) {
console.log("The user declined the connection");
} else if (error.code === -2) {
console.log("No account was returned");
} else {
console.error("Operation error:", error.message);
}
}
Top Errors
| Code | Details |
|---|---|
| -1 | User rejected the request |
| -2 | Account could not be found |
| -3 | Network selection is invalid |