Skip to content

Commit

Permalink
wip on extending SSI length
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 22, 2024
1 parent d921b97 commit aedcb6e
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 107 deletions.
127 changes: 100 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ path = "src/main.rs"
required-features = ["cli"]

[dependencies]
amplify = "4.6.0"
strict_encoding = "2.7.0-beta.4"
ascii-armor = "0.7.1"
amplify = "4.7.0"
strict_encoding = "2.7.0"
strict_types = "2.7.0"
ascii-armor = "0.7.2"
baid64 = "0.2.2"
base64 = "0.22.1"
secp256k1 = { version = "0.29.0", features = ["rand", "global-context", "rand-std"] }
secp256k1 = { version = "0.29.1", features = ["rand", "global-context", "rand-std"] }
ec25519 = { version = "0.1.0", features = ["blind-keys"] }
rand = "0.8.5"
chrono = "0.4.38"
clap = { version = "4.5.4", features = ["derive"], optional = true }
clap = { version = "4.5.18", features = ["derive"], optional = true }
shellexpand = { version = "3.1.0", optional = true }
sha2 = "0.10.8"
fluent-uri = "0.1.4"
percent-encoding = "2.3.1"
# Cli-specific
rpassword = { version = "7.3.1", optional = true }
aes-gcm = { version = "0.10.3", optional = true }
crossbeam-channel = { version = "0.5.12", optional = true }
crossbeam-channel = { version = "0.5.13", optional = true }

[features]
default = ["cli"]
Expand Down
34 changes: 13 additions & 21 deletions src/bip340.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ use secp256k1::{Keypair, Message, SecretKey, XOnlyPublicKey, SECP256K1};
use crate::{Algo, Chain, InvalidPubkey, InvalidSig, SsiPub, SsiSig};

#[derive(Clone, Eq, PartialEq, From)]
pub struct Bip340Secret(pub(crate) SecretKey);
pub struct Bip340Secret {
pub chain: Chain,
pub algo: Algo,
pub(crate) key: SecretKey,
}

impl Ord for Bip340Secret {
fn cmp(&self, other: &Self) -> Ordering { self.0.secret_bytes().cmp(&other.0.secret_bytes()) }
Expand All @@ -42,33 +46,21 @@ impl Hash for Bip340Secret {
fn hash<H: Hasher>(&self, state: &mut H) { self.0.secret_bytes().hash(state) }
}

impl From<Bip340Secret> for [u8; 32] {
fn from(ssi: Bip340Secret) -> Self { ssi.0.secret_bytes() }
}

impl From<[u8; 32]> for Bip340Secret {
fn from(value: [u8; 32]) -> Self {
Self(SecretKey::from_slice(&value).expect("invalid secret key"))
}
}

impl Bip340Secret {
pub fn new(chain: Chain) -> Self {
use rand::thread_rng;
loop {
let sk = SecretKey::new(&mut thread_rng());
let (pk, _) = sk.x_only_public_key(SECP256K1);
let data = pk.serialize();
if data[30] == u8::from(Algo::Bip340) && data[31] == u8::from(chain) {
return Self(sk);
}
let key = SecretKey::new(&mut thread_rng());
Self {
chain,
algo: Algo::Bip340,
key,
}
}

pub fn to_public(&self) -> SsiPub {
let (pk, _) = self.0.x_only_public_key(SECP256K1);
pub fn to_public(&self, chain: Chain, algo: Algo) -> SsiPub {
let (pk, _) = self.key.x_only_public_key(SECP256K1);
let data = pk.serialize();
SsiPub::from(data)
SsiPub::with(chain, algo, data)
}

pub fn sign(&self, msg: [u8; 32]) -> SsiSig {
Expand Down
Loading

0 comments on commit aedcb6e

Please sign in to comment.