Emurgo / message-signing

MIT License
27 stars 17 forks source link

I can't get Address from COSESign1 header (cip-30 like) #15

Closed zachyking closed 2 years ago

zachyking commented 2 years ago

I implemented sign function in rust based on Nami's implementation here

Then verify function. It seems to be working fine, beside I can't get address from the ProtectedHeader of COSESign1 - I'll get an error CBOR(TrailingData). After finding I mostly followed this example but couldn't get it work in rust.

using

use cardano_message_signing as ms;
use cardano_multiplatform_lib as cml;
...

In sign function:

let cbor_value_address = ms::cbor::CBORValue::new_bytes(
    cml::address::Address::from_bech32(address)
        .unwrap()
        .to_bytes(),
);
println!("SIGNING => cbor_value_address len {}", cbor_value_address.to_bytes().len().to_string());

protected_headers.set_header(
    &ms::Label::new_text("address".to_string()),
    &cbor_value_address,
);

In verify function:

let headers_to_verify = cose_sign1.headers();

let header_addr_val = headers_to_verify
    .protected()
    .deserialized_headers()
    .header(&ms::Label::new_text("address".to_string()))
    .unwrap()
    .to_bytes();

let cbor_val_address = ms::cbor::CBORValue::from_bytes(header_addr_val).unwrap();

println!("VERIFYING => cbor_val_address len {}", cbor_val_address.to_bytes().len().to_string());

let address = cml::address::Address::from_bytes(cbor_val_address.to_bytes())
    .unwrap() //this is line that panics
    .to_bech32(None)
    .unwrap();

I tried header_addr_val to cml::address::Address::from_bytes without going to ms::cbor::CBORValue first, but neither does work.

The len of bytes is the same, so is there some conversion needed for Rust when going from CBORValue?

zachyking commented 2 years ago

Changing to_bytes to as_bytes for CBORValue in the verify function fixes it.

This

let address = cml::address::Address::from_bytes(cbor_val_address.to_bytes())
    .unwrap() //this is line that panics
    .to_bech32(None)
    .unwrap();

To

let address = cml::address::Address::from_bytes(cbor_val_address.as_bytes().unwrap())
    .unwrap() //doesn't panic now - assuming correct COSESign1 headers
    .to_bech32(None)
    .unwrap();