Skip to content

Commit

Permalink
Use typed responses
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Jul 18, 2024
1 parent 9509ab9 commit 80e1df8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
10 changes: 10 additions & 0 deletions docs-test-gen/templates/core.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ struct Bls12VerifyMsg {
dst: Binary,
}

#[cw_serde]
struct RecoveryResponse {
public_key: HexBinary,
}

#[cw_serde]
struct VerifyResponse {
is_valid: bool,
}

#[cw_serde]
struct ExecuteMsg {}

Expand Down
12 changes: 8 additions & 4 deletions src/pages/core/standard-library/cryptography/bls12-381.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ pub fn query(
// Verify the signature. On chain!
let msg_hash = deps.api.bls12_381_hash_to_g2(HashFunction::Sha256, &msg.msg, &msg.dst)?;
let is_valid = deps.api.bls12_381_pairing_equality(&BLS12_381_G1_GENERATOR, &msg.signature, &msg.pubkey, &msg_hash)?;
let response = format!("{{ \"is_valid\": {is_valid} }}");
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```

Expand All @@ -50,9 +52,11 @@ pub fn query(
// Verify the signature. On chain!
let msg_hash = deps.api.bls12_381_hash_to_g1(HashFunction::Sha256, &msg.msg, &msg.dst)?;
let is_valid = deps.api.bls12_381_pairing_equality(&msg.signature, &BLS12_381_G2_GENERATOR, &msg.pubkey, &msg_hash)?;
let response = format!("{{ \"is_valid\": {is_valid} }}");
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```

Expand Down
6 changes: 4 additions & 2 deletions src/pages/core/standard-library/cryptography/ed25519.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ pub fn query(

// Verify the signature. On chain!
let is_valid = deps.api.ed25519_verify(&message, &signature, &public_key)?;
let response = format!("{{ \"is_valid\": {is_valid} }}");
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```

Expand Down
13 changes: 8 additions & 5 deletions src/pages/core/standard-library/cryptography/k256.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ pub fn query(

// Verify the signature. On chain!
let is_valid = deps.api.secp256k1_verify(&message_hash, &signature, &public_key)?;
let response = format!("{{ \"is_valid\": {is_valid} }}");
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```

Expand All @@ -50,9 +52,10 @@ pub fn query(

// Recover the public key. On chain!
let public_key = deps.api.secp256k1_recover_pubkey(&message_hash, &signature, recovery_id)?;
let public_key = HexBinary::from(public_key).to_hex();
let response = format!("{{ \"public_key\": \"{public_key}\" }}");
let response = to_json_binary(&RecoveryResponse {
public_key: public_key.into(),
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```
13 changes: 8 additions & 5 deletions src/pages/core/standard-library/cryptography/p256.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ pub fn query(

// Verify the signature. On chain!
let is_valid = deps.api.secp256r1_verify(&message_hash, &signature, &public_key)?;
let response = format!("{{ \"is_valid\": {is_valid} }}");
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```

Expand All @@ -48,9 +50,10 @@ pub fn query(

// Recover the public key. On chain!
let public_key = deps.api.secp256r1_recover_pubkey(&message_hash, &signature, recovery_id)?;
let public_key = HexBinary::from(public_key).to_hex();
let response = format!("{{ \"public_key\": \"{public_key}\" }}");
let response = to_json_binary(&RecoveryResponse {
public_key: public_key.into(),
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```

0 comments on commit 80e1df8

Please sign in to comment.