Skip to content

Commit

Permalink
Remove subxt-signer from server and entropy-protocol (#526)
Browse files Browse the repository at this point in the history
* Rm subxt-signer from protocol

* Rm subxt-signer from server

* Rm unused deps

* Rm comment

* Taplo

* Clippy

* Comments

* Add schnorrkel with wasm-bindgen feature as dependency of entropy-protocol

* Update changelog

* Rm uneeded entry from changelog

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

* Change log levels when logging public signing key

---------

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
  • Loading branch information
ameba23 and HCastano authored Nov 27, 2023
1 parent 79c4d4f commit 2a79249
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 178 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ At the moment this project **does not** adhere to
### Changed
- Change SocketAddr type for String ([#496](https://github.com/entropyxyz/entropy-core/pull/496/))
- Add `#[tracing::instrument]` macro to routes ([#515](https://github.com/entropyxyz/entropy-core/pull/515/))
- Remove subxt-signer from server and entropy-protocol ([#526](https://github.com/entropyxyz/entropy-core/pull/526/))

### Fixed
- Return package version instead of rustc version ([#523](https://github.com/entropyxyz/entropy-core/pull/523/))
Expand Down
15 changes: 2 additions & 13 deletions Cargo.lock

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

41 changes: 22 additions & 19 deletions crypto/protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ repository ='https://github.com/entropyxyz/entropy-core'
edition ='2021'

[dependencies]
async-trait ="0.1.73"
entropy-shared ={ path="../shared", default-features=false }
synedrion ={ git="ssh://git@github.com/entropyxyz/synedrion.git", branch="fix-32bit" }
serde ={ version="1.0", features=["derive"], default-features=false }
serde-big-array="0.5.1"
subxt ={ package="subxt", git="https://github.com/paritytech/subxt.git", tag="v0.32.1", default-features=false }
subxt-signer ={ version="0.31.0", features=["sr25519"], default-features=false }
tokio ={ version="1.16", features=["sync", "rt", "macros"] }
x25519-dalek ={ version="2.0.0", features=["static_secrets"] }
futures ="0.3"
hex ="*"
blake2 ="0.10.4"
thiserror ="1.0.48"
snow ="0.9.2"
getrandom ={ version="0.2", features=["js"] }
rand_core ={ version="0.6.4", features=["getrandom"] }
tracing ="0.1.37"
bincode ="1.3.3"
async-trait="0.1.73"
entropy-shared={ path="../shared", default-features=false }
synedrion={ git="ssh://git@github.com/entropyxyz/synedrion.git", branch="fix-32bit" }
serde={ version="1.0", features=["derive"], default-features=false }
subxt={ package="subxt", git="https://github.com/paritytech/subxt.git", tag="v0.32.1", default-features=false }
sp-core={ version="21.0.0", default-features=false, features=["full_crypto", "serde"] }
tokio={ version="1.16", features=["sync", "rt", "macros"] }
x25519-dalek={ version="2.0.0", features=["static_secrets"] }
futures="0.3"
hex="*"
blake2="0.10.4"
thiserror="1.0.48"
snow="0.9.2"
getrandom={ version="0.2", features=["js"] }
rand_core={ version="0.6.4", features=["getrandom"] }
tracing="0.1.37"
bincode="1.3.3"
schnorrkel={ version="0.9.1", default-features=false, features=[
"std",
"wasm-bindgen",
], optional=true }

# Used only with the `server` feature to implement the WsConnection trait
axum ={ version="0.6.18", features=["ws"], optional=true }
Expand All @@ -53,7 +56,6 @@ server=[
]
wasm=[
"entropy-shared/wasm",
"subxt-signer/web",
"dep:gloo-net",
"dep:wasm-bindgen-futures",
"dep:wasm-bindgen",
Expand All @@ -62,6 +64,7 @@ wasm=[
"dep:base64",
"subxt/web",
"dep:serde_json",
"dep:schnorrkel",
]

[lib]
Expand Down
47 changes: 17 additions & 30 deletions crypto/protocol/src/execute_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
use std::collections::HashMap;

use rand_core::{CryptoRngCore, OsRng};
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;
use sp_core::{sr25519, Pair};
use subxt::utils::AccountId32;
use subxt_signer::sr25519;
use synedrion::{
sessions::{
make_interactive_signing_session, make_key_refresh_session, make_keygen_and_aux_session,
Expand All @@ -30,40 +28,29 @@ pub type ChannelOut = Broadcaster;
/// Thin wrapper broadcasting channel out and messages from other nodes in
pub struct Channels(pub ChannelOut, pub ChannelIn);

struct SignerWrapper(sr25519::Keypair);
struct SignerWrapper(sr25519::Pair);

struct VerifierWrapper(sr25519::PublicKey);
#[derive(Clone)]
struct VerifierWrapper(sr25519::Public);

impl Clone for VerifierWrapper {
fn clone(&self) -> Self {
VerifierWrapper(sr25519::PublicKey(self.0 .0))
}
}

/// This is a raw signature from [sr25519::Signature]
// we cannot use Signature directly because it doesn't implement Serialize
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct SignatureWrapper(#[serde(with = "BigArray")] [u8; 64]);

impl RandomizedPrehashSigner<SignatureWrapper> for SignerWrapper {
impl RandomizedPrehashSigner<sr25519::Signature> for SignerWrapper {
fn sign_prehash_with_rng(
&self,
_rng: &mut impl CryptoRngCore,
prehash: &[u8],
) -> Result<SignatureWrapper, signature::Error> {
) -> Result<sr25519::Signature, signature::Error> {
// TODO: doesn't seem like there's a way to randomize signing?
Ok(SignatureWrapper(self.0.sign(prehash).0))
Ok(self.0.sign(prehash))
}
}

impl PrehashVerifier<SignatureWrapper> for VerifierWrapper {
impl PrehashVerifier<sr25519::Signature> for VerifierWrapper {
fn verify_prehash(
&self,
prehash: &[u8],
signature: &SignatureWrapper,
signature: &sr25519::Signature,
) -> Result<(), signature::Error> {
let sig = sr25519::Signature(signature.0);
if sr25519::verify(&sig, prehash, &self.0) {
if sr25519::Pair::verify(signature, prehash, &self.0) {
Ok(())
} else {
Err(signature::Error::new())
Expand All @@ -81,7 +68,7 @@ pub async fn execute_signing_protocol(
mut chans: Channels,
key_share: &KeyShare<KeyParams>,
prehashed_message: &PrehashedMessage,
threshold_signer: &sr25519::Keypair,
threshold_signer: &sr25519::Pair,
threshold_accounts: Vec<AccountId32>,
) -> Result<RecoverableSignature, ProtocolExecutionErr> {
tracing::debug!("Executing signing protocol");
Expand All @@ -108,7 +95,7 @@ pub async fn execute_signing_protocol(
// We should have `Public` objects at this point, not `AccountId32`.
let verifiers = threshold_accounts
.into_iter()
.map(|acc| VerifierWrapper(sr25519::PublicKey(acc.0)))
.map(|acc| VerifierWrapper(sr25519::Public(acc.0)))
.collect::<Vec<_>>();

// TODO (#375): this should come from whoever initiates the signing process,
Expand Down Expand Up @@ -179,7 +166,7 @@ pub async fn execute_signing_protocol(
)]
pub async fn execute_dkg(
mut chans: Channels,
threshold_signer: &sr25519::Keypair,
threshold_signer: &sr25519::Pair,
threshold_accounts: Vec<AccountId32>,
my_idx: &u8,
) -> Result<KeyShare<KeyParams>, ProtocolExecutionErr> {
Expand All @@ -203,7 +190,7 @@ pub async fn execute_dkg(
// We should have `Public` objects at this point, not `AccountId32`.
let verifiers = threshold_accounts
.into_iter()
.map(|acc| VerifierWrapper(sr25519::PublicKey(acc.0)))
.map(|acc| VerifierWrapper(sr25519::Public(acc.0)))
.collect::<Vec<_>>();

// TODO (#375): this should come from whoever initiates the signing process,
Expand Down Expand Up @@ -273,13 +260,13 @@ pub async fn execute_dkg(
)]
pub async fn execute_proactive_refresh(
mut chans: Channels,
threshold_signer: &sr25519::Keypair,
threshold_signer: &sr25519::Pair,
threshold_accounts: Vec<AccountId32>,
my_idx: &u8,
old_key: KeyShare<KeyParams>,
) -> Result<KeyShare<KeyParams>, ProtocolExecutionErr> {
tracing::debug!("Executing proactive refresh");
tracing::trace!("Signing with {:?}", &threshold_signer);
tracing::debug!("Signing with {:?}", &threshold_signer.public());
tracing::trace!("Previous key {:?}", &old_key);

let party_ids: Vec<PartyId> =
Expand All @@ -299,7 +286,7 @@ pub async fn execute_proactive_refresh(
// We should have `Public` objects at this point, not `AccountId32`.
let verifiers = threshold_accounts
.into_iter()
.map(|acc| VerifierWrapper(sr25519::PublicKey(acc.0)))
.map(|acc| VerifierWrapper(sr25519::Public(acc.0)))
.collect::<Vec<_>>();

// TODO (#375): this should come from whoever initiates the signing process,
Expand Down
11 changes: 5 additions & 6 deletions crypto/protocol/src/protocol_message.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::str;

use serde::{Deserialize, Serialize};
use sp_core::sr25519;
use synedrion::sessions::SignedMessage;

use crate::{
execute_protocol::SignatureWrapper, protocol_transport::errors::ProtocolMessageErr, PartyId,
};
use crate::{protocol_transport::errors::ProtocolMessageErr, PartyId};

/// A Message send during the signing or DKG protocol.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -16,7 +15,7 @@ pub struct ProtocolMessage {
/// If `None`, it's a broadcast message sent to all parties
pub to: Option<PartyId>,
/// The signed protocol message
pub payload: SignedMessage<SignatureWrapper>,
pub payload: SignedMessage<sr25519::Signature>,
}

impl TryFrom<&[u8]> for ProtocolMessage {
Expand All @@ -29,14 +28,14 @@ impl TryFrom<&[u8]> for ProtocolMessage {
}

impl ProtocolMessage {
pub(crate) fn new_bcast(from: &PartyId, payload: SignedMessage<SignatureWrapper>) -> Self {
pub(crate) fn new_bcast(from: &PartyId, payload: SignedMessage<sr25519::Signature>) -> Self {
Self { from: from.clone(), to: None, payload }
}

pub(crate) fn new_p2p(
from: &PartyId,
to: &PartyId,
payload: SignedMessage<SignatureWrapper>,
payload: SignedMessage<sr25519::Signature>,
) -> Self {
Self { from: from.clone(), to: Some(to.clone()), payload }
}
Expand Down
26 changes: 8 additions & 18 deletions crypto/protocol/src/protocol_transport/subscribe_message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;
use sp_core::{sr25519, Pair};
use subxt::utils::AccountId32;
use subxt_signer::sr25519;

/// A message sent by a party when initiating a websocket connection to participate
/// in the signing or DKG protcol
Expand All @@ -11,31 +10,22 @@ pub struct SubscribeMessage {
/// Protocol session identifier
pub session_id: String,
/// Public key of connecting party
pub public_key: [u8; 32],
pub public_key: sr25519::Public,
/// Signature to authenticate connecting party
#[serde(with = "BigArray")]
pub signature: [u8; 64],
pub signature: sr25519::Signature,
}

impl SubscribeMessage {
pub fn new(session_id: &str, sk: &sr25519::Keypair) -> Self {
let signature = sk.sign(session_id.as_bytes());
Self {
session_id: session_id.to_owned(),
public_key: sk.public_key().0,
signature: signature.0,
}
pub fn new(session_id: &str, pair: &sr25519::Pair) -> Self {
let signature = pair.sign(session_id.as_bytes());
Self { session_id: session_id.to_owned(), public_key: pair.public(), signature }
}

pub fn account_id(&self) -> AccountId32 {
self.public_key.into()
self.public_key.0.into()
}

pub fn verify(&self) -> bool {
sr25519::verify(
&sr25519::Signature(self.signature),
self.session_id.as_bytes(),
&sr25519::PublicKey(self.public_key),
)
sr25519::Pair::verify(&self.signature, self.session_id.as_bytes(), &self.public_key)
}
}
12 changes: 6 additions & 6 deletions crypto/protocol/src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
pub mod wasm;
use entropy_shared::SIGNING_PARTY_SIZE;
use futures::{future, Future};
use sp_core::{sr25519, Pair};
use subxt::utils::AccountId32;
use subxt_signer::sr25519;
use synedrion::KeyShare;
#[cfg(feature = "server")]
use tokio::spawn;
Expand All @@ -27,7 +27,7 @@ pub async fn user_participates_in_signing_protocol(
key_share: &KeyShare<KeyParams>,
sig_uid: &str,
validators_info: Vec<ValidatorInfo>,
user_signing_keypair: &sr25519::Keypair,
user_signing_keypair: &sr25519::Pair,
sig_hash: [u8; 32],
x25519_private_key: &x25519_dalek::StaticSecret,
) -> Result<RecoverableSignature, UserRunningProtocolErr> {
Expand Down Expand Up @@ -60,11 +60,11 @@ pub async fn user_participates_in_signing_protocol(
/// in the DKG protocol.
pub async fn user_participates_in_dkg_protocol(
validators_info: Vec<ValidatorInfo>,
user_signing_keypair: &sr25519::Keypair,
user_signing_keypair: &sr25519::Pair,
x25519_private_key: &x25519_dalek::StaticSecret,
) -> Result<KeyShare<KeyParams>, UserRunningProtocolErr> {
// Make WS connections to the given set of TSS servers
let sig_req_account: AccountId32 = user_signing_keypair.public_key().0.into();
let sig_req_account: AccountId32 = user_signing_keypair.public().0.into();
let session_id = sig_req_account.to_string();
let (channels, tss_accounts) = user_connects_to_validators(
open_ws_connection,
Expand All @@ -91,7 +91,7 @@ async fn user_connects_to_validators<F, Fut, W>(
open_ws_connection: F,
session_id: &str,
validators_info: Vec<ValidatorInfo>,
user_signing_keypair: &sr25519::Keypair,
user_signing_keypair: &sr25519::Pair,
x25519_private_key: &x25519_dalek::StaticSecret,
) -> Result<(Channels, Vec<AccountId32>), UserRunningProtocolErr>
where
Expand Down Expand Up @@ -164,7 +164,7 @@ where
let mut tss_accounts: Vec<AccountId32> =
validators_info.iter().map(|v| v.tss_account.clone()).collect();
// Add ourself to the list of partys as we will participate
tss_accounts.push(user_signing_keypair.public_key().0.into());
tss_accounts.push(user_signing_keypair.public().0.into());

Ok((channels, tss_accounts))
}
Loading

0 comments on commit 2a79249

Please sign in to comment.