-
Notifications
You must be signed in to change notification settings - Fork 4
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
Feat/substrate #73
Feat/substrate #73
Changes from all commits
c94e8b5
0e75dcd
283af8d
4fa212e
e08475f
ae79a0e
d8c7457
6c8c8fb
57e8ea5
022effa
8dd4d35
ea0a45e
ee6067f
dd42d93
a9ae35a
96908bf
57e54bc
c9040d6
2f1f25b
13c0342
f9ae548
e1e9332
2aff9d7
86e3669
943b2a5
da388f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pub mod hash; | |
pub mod keypair; | ||
pub mod mnemonic; | ||
pub mod secp256k1; | ||
pub mod sr25519; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use coins_bip39::{English, Mnemonic}; | ||
use kos_types::{error::Error, Bytes32}; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
use sp_core::{sr25519, Pair}; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
|
||
#[wasm_bindgen] | ||
pub struct Sr25519KeyPair { | ||
keypair: sr25519::Pair, | ||
} | ||
|
||
impl Serialize for Sr25519KeyPair { | ||
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
todo!() | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for Sr25519KeyPair { | ||
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
todo!() | ||
} | ||
} | ||
|
||
impl Clone for Sr25519KeyPair { | ||
fn clone(&self) -> Sr25519KeyPair { | ||
Sr25519KeyPair { | ||
keypair: self.keypair.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl Sr25519KeyPair { | ||
pub fn random<R>(rng: &mut R) -> Self | ||
where | ||
R: rand::Rng + ?Sized, | ||
{ | ||
let mut secret = Bytes32::zeroed(); | ||
rng.fill(secret.as_mut()); | ||
|
||
Sr25519KeyPair::new(secret.into()) | ||
} | ||
|
||
pub fn new(secret: [u8; 32]) -> Self { | ||
let keypair = sr25519::Pair::from_seed_slice(&secret).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential errors instead of using Using Apply this diff to handle the potential error: - let keypair = sr25519::Pair::from_seed_slice(&secret).unwrap();
+ let keypair = sr25519::Pair::from_seed_slice(&secret)?; Also, update the function signature to return a - pub fn new(secret: [u8; 32]) -> Self {
+ pub fn new(secret: [u8; 32]) -> Result<Self, Error> { And adjust the return statement: - Self { keypair }
+ Ok(Self { keypair })
|
||
Self { keypair } | ||
} | ||
|
||
pub fn new_from_mnemonic_phrase_with_path( | ||
phrase: &str, | ||
path: &str, | ||
password: Option<&str>, | ||
) -> Result<Self, Error> { | ||
let mnemonic = Mnemonic::<English>::new_from_phrase(phrase)?; | ||
Self::new_from_mnemonic(path, mnemonic, password) | ||
} | ||
|
||
pub fn new_from_mnemonic( | ||
path: &str, | ||
m: Mnemonic<English>, | ||
password: Option<&str>, | ||
) -> Result<Self, Error> { | ||
// Convert mnemonic to seed | ||
let seed = format!("{}{}", m.to_phrase(), path); | ||
|
||
// Derive keypair based on the provided path and seed | ||
let keypair = Pair::from_string(&seed, password).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential errors instead of using Using Apply this diff to handle the potential error: - let keypair = Pair::from_string(&seed, password).unwrap();
+ let keypair = Pair::from_string(&seed, password)?; Also, ensure that the function handles the
|
||
|
||
Ok(Self { keypair }) | ||
} | ||
} | ||
|
||
impl Sr25519KeyPair { | ||
pub fn public_key(&self) -> Vec<u8> { | ||
self.keypair.public().0.to_vec() | ||
} | ||
|
||
pub fn secret_key(&self) -> Vec<u8> { | ||
self.keypair.to_raw_vec() | ||
} | ||
} | ||
|
||
impl Sr25519KeyPair { | ||
pub fn sign_digest(&self, message: &[u8]) -> Vec<u8> { | ||
self.keypair.sign(message).0.to_vec() | ||
} | ||
|
||
pub fn verify_digest(&self, message: &[u8], signature: &[u8], public_key: &[u8]) -> bool { | ||
let public = sr25519::Public::from_raw(<[u8; 32]>::try_from(public_key).unwrap()); | ||
|
||
let signature = sr25519::Signature::from_raw(<[u8; 64]>::try_from(signature).unwrap()); | ||
Comment on lines
+94
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential errors instead of using Using Apply this diff to handle the potential errors: - let public = sr25519::Public::from_raw(<[u8; 32]>::try_from(public_key).unwrap());
+ let public = sr25519::Public::from_raw(<[u8; 32]>::try_from(public_key)?); - let signature = sr25519::Signature::from_raw(<[u8; 64]>::try_from(signature).unwrap());
+ let signature = sr25519::Signature::from_raw(<[u8; 64]>::try_from(signature)?); Adjust the function signature to return a - pub fn verify_digest(&self, message: &[u8], signature: &[u8], public_key: &[u8]) -> bool {
+ pub fn verify_digest(&self, message: &[u8], signature: &[u8], public_key: &[u8]) -> Result<bool, Error> { Update the return statement accordingly: - sr25519::Pair::verify(&signature, message, &public)
+ Ok(sr25519::Pair::verify(&signature, message, &public))
|
||
|
||
sr25519::Pair::verify(&signature, message, &public) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle potential
None
values to prevent panicsIn the methods
sign_digest
,verify_digest
,public_key
, andsecret_key
, using.as_ref().unwrap()
on thesr25519
field can lead to a panic if the field isNone
. Even though the current design assumes that the field will beSome
whenkey_type
isSr25519
, it's safer to handle theNone
case explicitly to prevent unexpected panics and improve code robustness.Apply this diff to handle potential
None
values:Similarly, update the
verify_digest
,public_key
, andsecret_key
methods to handleNone
values appropriately.Also applies to: 98-102, 127-127, 143-143
🛠️ Refactor suggestion
Consider refactoring to reduce code duplication
The
match
statements in the methodssign_digest
,verify_digest
,public_key
, andsecret_key
have repetitive patterns across different key types. Consider refactoring these methods to eliminate duplication, which will enhance maintainability and readability.For example, you could introduce a trait that defines common behaviors for key pairs and implement it for each key type. Then, store the key pair in an enum or a trait object to simplify method implementations.
Also applies to: 98-102, 127-127, 143-143