Last active
July 26, 2022 11:02
-
-
Save SkymanOne/c63ce13e58b23b527f1b4b2a651512d2 to your computer and use it in GitHub Desktop.
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 parity_scale_codec::{ Encode, Decode}; | |
use sp_core::{H256, H512, Pair}; | |
type Salt = u8; | |
const SALT: Salt = 5u8; | |
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone)] | |
pub struct BasicExtrinsic { | |
operation: Operation, | |
salt: Salt, | |
signature: H512, | |
signer: H256 | |
} | |
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone)] | |
pub enum Operation { | |
Add(u32), | |
Subtract(u32), | |
Multiply(u32), | |
Divide(u32), | |
} | |
fn main() { | |
let operation = Operation::Add(11u32); | |
let pair: sp_core::sr25519::Pair = Pair::generate_with_phrase(Some("key")).0; | |
let payload = (&operation, SALT).encode(); | |
println!("{:02x?}", payload); | |
let payload: [u8; 6] = payload.try_into().unwrap(); | |
println!("{:02x?}", payload); | |
let signature = pair.sign(&payload); | |
let tx = BasicExtrinsic { operation, salt: SALT, signature: sp_core::H512(signature.0), signer: sp_core::H256(pair.public().0) }; | |
let print_string = format!("{:02x?}", tx.encode()); | |
let print_string = print_string.replace(", ", ""); | |
let print_string = print_string.replace('[', ""); | |
let print_string = print_string.replace(']', ""); | |
println!("{}", print_string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment