Emurgo / cardano-serialization-lib

This is a library, written in Rust, for serialization & deserialization of data structures used in Cardano's Haskell implementation of Alonzo along with useful utility functions.
Other
230 stars 124 forks source link

Trying to find a Rust Crate/Functionality Demonstration of COSESign1 and COSEKey #643

Closed JustLeif closed 10 months ago

JustLeif commented 10 months ago

Context

I am currently trying to build a form of custom authentication for a web application using Cardano CIP-30 Wallets. I found this article in the official documentation. https://developers.cardano.org/docs/integrate-cardano/user-wallet-authentication/

Issue

My back-end servers are written in Rust, I would prefer not to move over to NodeJS if at all possible.

The following JavaScript code is featured in the documentation using @emurgo/cardano-message-signing-nodejs and @emurgo/cardano-serialization-lib-nodejs

const decoded = COSESign1.from_bytes( Buffer.from(sigData.signature, "hex") );
const headermap = decoded.headers().protected().deserialized_headers();
const addressHex = Buffer.from( headermap.header( Label.new_text("address") ).to_bytes() )
    .toString("hex")
    .substring(4);
const address = Address.from_bytes( Buffer.from(addressHex, "hex") );

const key = COSEKey.from_bytes( Buffer.from(sigData.key, "hex") );
const pubKeyBytes = key.header( Label.new_int( Int.new_negative(BigNum.from_str("2")) ) ).as_bytes();
const publicKey = PublicKey.from_bytes(pubKeyBytes);

const payload = decoded.payload();
const signature = Ed25519Signature.from_bytes(decoded.signature());
const receivedData = decoded.signed_data().to_bytes();

const signerStakeAddrBech32 = RewardAddress.from_address(address).to_address().to_bech32();
const utf8Payload = Buffer.from(payload).toString("utf8");
const expectedPayload = `account: ${signerStakeAddrBech32}`; // reconstructed message

// verify:
const isVerified = publicKey.verify(receivedData, signature);
const payloadAsExpected = utf8Payload == expectedPayload;
const signerIsRegistered = registeredUsers.includes(signerStakeAddrBech32);

Is there anyway anyone knows to easily get these cryptography functions in Rust? My knowledge of Cardano specific cryptography is very limited so I rely very heavily on these helper functions.

Thanks in advance!

lisicky commented 10 months ago

HI @JustLeif ! Probably you are searching for https://github.com/Emurgo/message-signing

JustLeif commented 10 months ago

HI @JustLeif ! Probably you are searching for https://github.com/Emurgo/message-signing

Thanks for the fast reply I appreciate it 👍