Last active
August 2, 2022 00:43
-
-
Save SkymanOne/b74096c4845e0af69b17fefb25eabf92 to your computer and use it in GitHub Desktop.
working signature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt::format; | |
use parity_scale_codec::{ Encode, Decode}; | |
use sp_core::{H256, H512, Pair}; | |
use sp_core::sr25519::{Signature}; | |
fn main() { | |
let print_string = generate("//Alice", Vote::Yes); | |
println!("Alice - Yes - {}", fm(print_string)); | |
let print_string = generate("//Bob", Vote::No); | |
println!("Bob - No - {}", fm(print_string)); | |
} | |
#[derive(Clone, Eq, PartialEq, Encode, Decode)] | |
pub enum Vote { | |
Yes, | |
No | |
} | |
fn generate(key: &str, vote: Vote) -> String { | |
let pair: sp_core::sr25519::Pair = Pair::from_string(key, None).unwrap(); | |
//println!("{}", fm(format!("{:02x?}", pair.public().0))); | |
let payload = (vote, 10u32).encode(); | |
let payload = payload.as_slice().to_owned(); | |
let signed = pair.sign(&payload); | |
//println!("{}", verify(signed.0)); | |
format!("{:02x?}", signed.0) | |
} | |
fn verify(load: [u8; 64]) -> bool { | |
let pair: sp_core::sr25519::Pair = Pair::from_string("//Bob", None).unwrap(); | |
let s = Signature::from_raw(load); | |
let payload = (Vote::Yes, 10u32).encode(); | |
sp_core::sr25519::Pair::verify(&s, payload, &pair.public()) | |
} | |
fn fm(s: String) -> String { | |
let print_string = s.replace(", ", ""); | |
let print_string = print_string.replace('[', ""); | |
print_string.replace(']', "") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment