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

sr25519 signature verification #1757

Closed
wants to merge 10 commits into from

Conversation

kziemianek
Copy link
Contributor

Add ability to call contract's sr25519_verify function.

sr25519_verify is still unstable so it's required to run node with type UnsafeUnstableInterface = ConstBool<true>; in pallet-contracts config.

This is related to paritytech/substrate#13703.

crates/env/src/api.rs Outdated Show resolved Hide resolved
@kziemianek kziemianek requested a review from cmichi April 20, 2023 06:54
message: &[u8],
pub_key: &[u8; 32],
) -> Result<()> {
let context = signing_context(b"substrate");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For further use, i think there should be an option to verify signature from any context. Right now there is such limitation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create a SIGNING_CTX constant like in the substrate and add a comment with the link to the code on the substrate side(that substrate uses the same context)?

@goastler
Copy link
Contributor

goastler commented May 4, 2023

Hey, was just testing out your changes here which solve my sr25519 signing issue in the ink smart contracts - many thanks for your changes. Just a quick fyi that if the public key is invalid the error is not caught and causes a panic (in the smart contract call, not sure where the panic originates).

I found this from one of my unit tests (https://github.com/prosopo/protocol/blob/81c90b2edde0b71072b1f56b5b3af65a5589e9dc/contracts/lib.rs#L2292) which deliberately uses an incorrect public key. Produces the following error:


---- prosopo::tests::test_verify_sr25519_invalid_public_key stdout ----
data_hash: [70, 251, 116, 8, 212, 242, 133, 34, 143, 74, 245, 22, 234, 37, 133, 27]
data_hex: "46fb7408d4f285228f4af516ea25851b"
payload: <Bytes>0x46fb7408d4f285228f4af516ea25851b</Bytes>
payload_hex: 3c42797465733e307834366662373430386434663238353232386634616635313665613235383531623c2f42797465733e
signature: 0a7da2b631704cdcfe93c740e41217b9ac667a0c8755d8da1a8232db527f487c87e780d2edc1896aeb6b1bef0bc7c38d9df2135b633eab8bfb1777e82fad3a8f
caller AccountId([213, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125])
sig [10, 125, 162, 182, 49, 112, 76, 220, 254, 147, 199, 64, 228, 18, 23, 185, 172, 102, 122, 12, 135, 85, 216, 218, 26, 130, 50, 219, 82, 127, 72, 124, 135, 231, 128, 210, 237, 193, 137, 106, 235, 107, 27, 239, 11, 199, 195, 141, 157, 242, 19, 91, 99, 62, 171, 139, 251, 23, 119, 232, 47, 173, 58, 143]
payload [60, 66, 121, 116, 101, 115, 62, 48, 120, 52, 54, 102, 98, 55, 52, 48, 56, 100, 52, 102, 50, 56, 53, 50, 50, 56, 102, 52, 97, 102, 53, 49, 54, 101, 97, 50, 53, 56, 53, 49, 98, 60, 47, 66, 121, 116, 101, 115, 62]
thread 'prosopo::tests::test_verify_sr25519_invalid_public_key' panicked at 'called `Result::unwrap()` on an `Err` value: PointDecompressionError', /home/geopro/.cargo/git/checkouts/ink-b050680cd543eb5a/9ca19c4/crates/env/src/engine/off_chain/impls.rs:350:68

Happily, incorrect signatures / payload data result in verify returning false. The valid verification test also passes.

@goastler
Copy link
Contributor

any update on this? @cmichi @kziemianek :)

@kziemianek
Copy link
Contributor Author

kziemianek commented May 24, 2023

any update on this? @cmichi @kziemianek :)

I'm a bit busy recently. I doubt I'll have a time to look at it in the near future.

@forgetso
Copy link
Contributor

@cmichi Would it be possible to merge this given that the review suggestions have been completed? Obviously, after master is merged in. Thx

@SkymanOne
Copy link
Contributor

Can you please address the unhandled error resulting in the call panic which was addressed by @goastler ?

@SkymanOne
Copy link
Contributor

Hey @kziemianek did you have a change at the review comments?

@kziemianek
Copy link
Contributor Author

kziemianek commented Jul 6, 2023

Hey @kziemianek did you have a change at the review comments?

Yes, they all are resolved.

@goastler
Copy link
Contributor

goastler commented Jul 6, 2023

I have fixed the unhandled errors I was having with this PR, added tests, added docs and fixed the merge conflicts in my PR (#1840) that builds upon this one

message: &[u8],
pub_key: &[u8; 32],
) -> Result<()> {
let context = signing_context(b"substrate");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create a SIGNING_CTX constant like in the substrate and add a comment with the link to the code on the substrate side(that substrate uses the same context)?

@@ -63,7 +63,7 @@ impl TryFrom<ast::AttributeArgs> for E2EConfig {
return Err(format_err_spanned!(
arg,
"expected a string literal for `additional_contracts` ink! E2E test configuration argument",
))
));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you exclude this file from the PR, please?=)

@@ -263,7 +263,7 @@ impl ItemMod {
.into_combine(format_err!(
overlap.span(),
"first ink! message with overlapping wildcard selector here",
)))
)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you exclude this file from the PR, please?=)

@@ -384,7 +384,7 @@ impl InkItemTrait {
).into_combine(format_err_spanned!(
duplicate_selector,
"first ink! trait constructor or message with same selector found here",
)))
)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you exclude this file from the PR, please?=)

@@ -880,6 +880,16 @@ where
.map_err(|_| Error::EcdsaRecoveryFailed)
}

pub fn sr25519_verify(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment here as well, please?=)

@xgreenx
Copy link
Collaborator

xgreenx commented Jul 16, 2023

Closed in favor of #1840

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

Successfully merging this pull request may close these issues.

6 participants