Skip to content
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: libs #166

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Add tests
sugyan committed May 14, 2024
commit 2d372e378acb243de2a7acf5f0c1a8209db40f60
82 changes: 81 additions & 1 deletion atrium-crypto/src/algorithm.rs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ impl Algorithm {
v.extend_from_slice(key);
multibase::encode(Base::Base58Btc, v)
}
pub fn decompress_pubkey(&self, key: &[u8]) -> Result<Vec<u8>> {
pub(crate) fn decompress_pubkey(&self, key: &[u8]) -> Result<Vec<u8>> {
self.pubkey_bytes(key, false)
}
pub fn verify_signature(&self, public_key: &[u8], msg: &[u8], signature: &[u8]) -> Result<()> {
@@ -59,3 +59,83 @@ impl Algorithm {
})
}
}

#[cfg(test)]
mod tests {
use super::Algorithm;
use crate::did::parse_did_key;
use crate::keypair::{Did, P256Keypair, Secp256k1Keypair};
use rand::rngs::ThreadRng;

#[test]
fn p256_compress_decompress() {
let did = P256Keypair::create(&mut ThreadRng::default()).did();
let (alg, key) = parse_did_key(&did).expect("parsing did key should succeed");
assert_eq!(alg, Algorithm::P256);
// compress a key to the correct length
let compressed = alg
.pubkey_bytes(&key, true)
.expect("compressing public key should succeed");
assert_eq!(compressed.len(), 33);
// decompress a key to the original
let decompressed = alg
.pubkey_bytes(&compressed, false)
.expect("decompressing public key should succeed");
assert_eq!(decompressed.len(), 65);
assert_eq!(key, decompressed);

// works consitesntly
let keys = (0..100)
.map(|_| {
let did = P256Keypair::create(&mut ThreadRng::default()).did();
let (_, key) = parse_did_key(&did).expect("parsing did key should succeed");
key
})
.collect::<Vec<_>>();
let compressed = keys
.iter()
.filter_map(|key| alg.pubkey_bytes(key, true).ok())
.collect::<Vec<_>>();
let decompressed = compressed
.iter()
.filter_map(|key| alg.pubkey_bytes(key, false).ok())
.collect::<Vec<_>>();
assert_eq!(keys, decompressed);
}

#[test]
fn secp256k1_compress_decompress() {
let did = Secp256k1Keypair::create(&mut ThreadRng::default()).did();
let (alg, key) = parse_did_key(&did).expect("parsing did key should succeed");
assert_eq!(alg, Algorithm::Secp256k1);
// compress a key to the correct length
let compressed = alg
.pubkey_bytes(&key, true)
.expect("compressing public key should succeed");
assert_eq!(compressed.len(), 33);
// decompress a key to the original
let decompressed = alg
.pubkey_bytes(&compressed, false)
.expect("decompressing public key should succeed");
assert_eq!(decompressed.len(), 65);
assert_eq!(key, decompressed);

// works consitesntly
let keys = (0..100)
.map(|_| {
let did = Secp256k1Keypair::create(&mut ThreadRng::default()).did();
let (_, key) = parse_did_key(&did).expect("parsing did key should succeed");
key
})
.collect::<Vec<_>>();
let compressed = keys
.iter()
.filter_map(|key| alg.pubkey_bytes(key, true).ok())
.collect::<Vec<_>>();
let decompressed = compressed
.iter()
.filter_map(|key| alg.pubkey_bytes(key, false).ok())
.collect::<Vec<_>>();
assert_eq!(keys, decompressed);
}
}
10 changes: 5 additions & 5 deletions atrium-crypto/src/did.rs
Original file line number Diff line number Diff line change
@@ -11,11 +11,6 @@ pub fn parse_multikey(multikey: &str) -> Result<(Algorithm, Vec<u8>)> {
Err(Error::UnsupportedMultikeyType)
}

pub fn format_did_key_str(alg: Algorithm, s: &str) -> Result<String> {
let (_, key) = multibase::decode(s)?;
format_did_key(alg, &key)
}

pub fn parse_did_key(did: &str) -> Result<(Algorithm, Vec<u8>)> {
if let Some(multikey) = did.strip_prefix(DID_KEY_PREFIX) {
parse_multikey(multikey)
@@ -24,6 +19,11 @@ pub fn parse_did_key(did: &str) -> Result<(Algorithm, Vec<u8>)> {
}
}

pub fn format_did_key_str(alg: Algorithm, s: &str) -> Result<String> {
let (_, key) = multibase::decode(s)?;
format_did_key(alg, &key)
}

pub fn format_did_key(alg: Algorithm, key: &[u8]) -> Result<String> {
Ok(prefix_did_key(&alg.format_multikey(key)?))
}