1

I'm trying to create a basic HTTP server that authorizes users with Authorization Pubkey:Sig like what Crust explained here.

However, creating a Signature from &str with Signature::fromSlice() failing. How can I convert the &str to a Signature?

pub type Signature = subxt::sp_core::sr25519::Signature;
pub type PublicKey = subxt::sp_core::sr25519::Public;
let pub_key = value.get(0).unwrap(); // 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
let sig = value.get(1).unwrap(); // f2f7211fcc2204dc3fa50cf1aca98b7c49c82510efbc84fd808b46c459069017db7a47acf4040d1cc0363eb5e3416c197e15fd2439e70a69dcdfa2eb8b520280
let public_key = PublicKey::from_str(pub_key);
let signature = Signature::from_slice(sig.as_bytes());

if public_key.is_err() {
    return Err(actix_web::error::ErrorUnauthorized("Invalid Public Key"));
}

if signature.is_none() {
    return Err(actix_web::error::ErrorUnauthorized("Invalid Signature"));
}

1 Answer 1

2

Are you sure that sig you're getting from value.get(1).unwrap() is correct?

documentation on from_slice says the following:

A new instance from the given slice that should be 64 bytes long. NOTE: No checking goes on to ensure this is a real signature. Only use it if you are certain that the array actually is a signature. GIGO!

also, checking your code:

let sig = "f2f7211fcc2204dc3fa50cf1aca98b7c49c82510efbc84fd808b46c459069017db7a47acf4040d1cc0363eb5e3416c197e15fd2439e70a69dcdfa2eb8b520280";
println!("{}", sig.as_bytes().len());

prints out 128, which is 2x the expected.

if I cut it down to half:

let sig = "f2f7211fcc2204dc3fa50cf1aca98b7c49c82510efbc84fd808b46c459069017";
let signature = Signature::from_slice(sig.as_bytes());
println!("{:?}", signature);

I get:

Some(66326637323131666363323230346463336661353063663161636139386237633439633832353130656662633834666438303862343663343539303639303137)

Which doesn't necessarily mean that you should also cut down in half whatever is coming from value.get(1).unwrap(). But it does mean that for some reason, this value is 2x bigger than a correct signature would be.

1
  • I signed Alice's public key using polkadot.js.org/apps/#/signing. I',m sure that sig is what I send with the header. After signing, I send the combination as a header using Postman. I guess I need to use a client library that can create a 64-byes long signature.
    – Burak
    Jun 9, 2022 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.