Skip to content

Commit

Permalink
Use correct encoding for private and public key
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtrnka committed Feb 6, 2024
1 parent 9b8a0d1 commit d90bb98
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SRI Pool config
authority_public_key = "3VANfft6ei6jQq1At7d8nmiZzVhBFS4CiQujdgim1ign"
authority_public_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"
authority_secret_key = "7qbpUjScc865jyX2kiB4NVJANoC7GA7TAJupdzXWkc62"
cert_validity_sec = 3600
test_only_listen_adress_plain = "0.0.0.0:34250"
Expand Down
5 changes: 3 additions & 2 deletions roles/pool/config-examples/pool-config-local-tp-example.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SRI Pool config
authority_public_key = "3VANfft6ei6jQq1At7d8nmiZzVhBFS4CiQujdgim1ign"
authority_secret_key = "7qbpUjScc865jyX2kiB4NVJANoC7GA7TAJupdzXWkc62"
authority_public_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"
authority_secret_key = "mkDLTBBRxdBv998612qipDYoTK3YUrqLe8uWw7gu3iXbSrn2n"
#authority_secret_key = "7qbpUjScc865jyX2kiB4NVJANoC7GA7TAJupdzXWkc62"
cert_validity_sec = 3600
test_only_listen_adress_plain = "0.0.0.0:34250"
listen_address = "0.0.0.0:34254"
Expand Down
27 changes: 21 additions & 6 deletions utils/key-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl TryFrom<String> for Secp256k1SecretKey {
type Error = Error;

fn try_from(value: String) -> Result<Self, Self::Error> {
let decoded = decode(value).into_vec()?;
let decoded = decode(value).with_check(None).into_vec()?;
let secret = SecretKey::from_slice(&decoded)?;
Ok(Secp256k1SecretKey(secret))
}
Expand All @@ -46,7 +46,7 @@ impl TryFrom<String> for Secp256k1SecretKey {
impl From<Secp256k1SecretKey> for String {
fn from(secret: Secp256k1SecretKey) -> Self {
let bytes = secret.0.secret_bytes();
bs58::encode(bytes).into_string()
bs58::encode(bytes).with_check().into_string()
}
}

Expand All @@ -58,16 +58,31 @@ impl TryFrom<String> for Secp256k1PublicKey {
type Error = Error;

fn try_from(value: String) -> Result<Self, Self::Error> {
let decoded = decode(value).into_vec()?;
let public = XOnlyPublicKey::from_slice(&decoded).expect("Invalid public key");
let decoded = decode(value).with_check(None).into_vec()?;
if decoded.len() < 34 {
return Err(Error::Custom);
}
if decoded[..2] != [1, 0] {
return Err(Error::Custom);
}
let public = XOnlyPublicKey::from_slice(&decoded[2..]).expect("Invalid public key");
Ok(Secp256k1PublicKey(public))
}
}

impl From<Secp256k1PublicKey> for String {
fn from(public: Secp256k1PublicKey) -> Self {
let bytes = public.0.serialize();
bs58::encode(bytes).into_string()
public.to_string()
}
}

impl Display for Secp256k1PublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = [0_u8; 34];
output[0] = 1;
let bytes = self.0.serialize();
output[2..].copy_from_slice(&bytes);
f.write_str(&bs58::encode(&output).with_check().into_string())
}
}

Expand Down

0 comments on commit d90bb98

Please sign in to comment.