Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compact form EIP-2098 signature recovery #551

Open
iqdecay opened this issue Aug 17, 2023 · 0 comments
Open

Compact form EIP-2098 signature recovery #551

iqdecay opened this issue Aug 17, 2023 · 0 comments
Assignees

Comments

@iqdecay
Copy link
Contributor

iqdecay commented Aug 17, 2023

From @Braqzen

Motivation

In Ethers-rs (and I assume it's also in Ethers-js but I have not gotten there yet), when a message is signed the return type is a Signature which when converted into a vector consists of 65 bytes that represent the r, s and v values.

The issue is that Sway uses 64 bytes.
There is a function that can be implemented to transform the representation here (to_compact()).

This is not the final representation because it's not a single vector of bytes but we can have that function and perhaps another which takes these two values and outputs a vector.

Possible implementations

This is an implementation I have written in Rust inspired from Ethers-rs. I have not tested this to prove that it works but I think it might. It may be taken as an example from Rust and ported to Ts (and also added into the Rust SDK).

fn compact(signature: &Signature) -> [u8; 64] {
    let shifted_parity = U256::from(signature.v - 27) << 255;

    let r = signature.r;
    let yParityAndS = shifted_parity | signature.s;

    let mut sig = [0u8; 64];
    let mut r_bytes = [0u8; 32];
    let mut s_bytes = [0u8; 32];
    r.to_big_endian(&mut r_bytes);
    yParityAndS.to_big_endian(&mut s_bytes);
    sig[..32].copy_from_slice(&r_bytes);
    sig[32..64].copy_from_slice(&s_bytes);
    return sig;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant