Skip to content

Commit

Permalink
feat(eos): <- use a default pub key in place of older keys that are n…
Browse files Browse the repository at this point in the history
…ot currently parsable
  • Loading branch information
gskapka committed Feb 16, 2024
1 parent 2cc2bd8 commit e9c4bc0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
5 changes: 5 additions & 0 deletions common/eos/src/eos_constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use crate::eos_crypto::eos_public_key::EosPublicKey;
lazy_static! {
pub static ref EOS_DEFAULT_PUB_KEY: EosPublicKey = EosPublicKey::default();
}

pub const MEMO: &str = "";
pub const PRODUCER_REPS: u64 = 12;
pub const PUBLIC_KEY_SIZE: usize = 33;
Expand Down
37 changes: 33 additions & 4 deletions common/eos/src/eos_crypto/eos_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use common::{
errors::AppError,
types::{Byte, Bytes, Result},
};
use secp256k1::{self, key::PublicKey, Error::InvalidPublicKey, Secp256k1};
use secp256k1::{
self,
key::{PublicKey, SecretKey},
Error::InvalidPublicKey,
Secp256k1,
};

use crate::{
bitcoin_crate_alias::util::base58,
Expand All @@ -19,6 +24,18 @@ pub struct EosPublicKey {
pub public_key: PublicKey,
}

impl Default for EosPublicKey {
fn default() -> Self {
Self {
public_key: PublicKey::from_secret_key(
&Secp256k1::new(),
&SecretKey::from_str("0000000000000000000000000000000000000000000000000000000000000001").unwrap(),
),
compressed: true,
}
}
}

impl EosPublicKey {
pub fn write_into<W: io::Write>(&self, mut writer: W) {
writer.write_all(&self.public_key.serialize()).ok();
Expand Down Expand Up @@ -89,10 +106,14 @@ impl FromStr for EosPublicKey {
type Err = AppError;

fn from_str(s: &str) -> Result<EosPublicKey> {
if !s.starts_with("EOS") {
if s.starts_with("PUB_R1_") || s.starts_with("PUB_K1_") {
warn!("old eos public key found, using default key");
return Ok(Self::default());
} else if !s.starts_with("EOS") {
return Err(AppError::CryptoError(InvalidPublicKey));
}
let s_hex = base58::from(&s[3..])?;
};

let s_hex = base58::from(&s[3..])?; // NOTE `EOS` prefix length
let raw = &s_hex[..PUBLIC_KEY_SIZE];
let _checksum = &s_hex[PUBLIC_KEY_SIZE..];
let public_key = secp256k1::key::PublicKey::from_slice(raw)?;
Expand Down Expand Up @@ -249,4 +270,12 @@ mod test {
let result = EosPublicKey::from_bytes(&bytes).unwrap();
assert_eq!(key, result);
}

#[test]
fn should_use_default_key_if_older_eos_key_encountered() {
let key_str = "PUB_R1_6zzHRqKC78BHHTtpgsRAyhiA6ufP98wMkHregJQCNsR7VpReoP";
let result = EosPublicKey::from_str(key_str).unwrap();
let expected_result = *crate::eos_constants::EOS_DEFAULT_PUB_KEY;
assert_eq!(result, expected_result);
}
}

0 comments on commit e9c4bc0

Please sign in to comment.