Skip to content

Commit

Permalink
bump ed25519-dalek, bump version number
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed Feb 29, 2024
1 parent c193f17 commit 48dd3ed
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 239 deletions.
218 changes: 86 additions & 132 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "plain_bitnames_app"
version = "0.5.4"
version = "0.5.5"
edition = "2021"
authors = [ "Ash Manning <[email protected]>" ]

Expand Down
8 changes: 4 additions & 4 deletions app/gui/coins/tx_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use eframe::egui::{self, Response};
use hex::FromHex;

use plain_bitnames::{
authorization::PublicKey,
authorization::VerifyingKey,
bip300301::bitcoin,
types::{BitNameData, EncryptionPubKey, Hash, Transaction, Txid},
};
Expand All @@ -32,7 +32,7 @@ pub struct TrySetBitNameData {
/// optional pubkey used for encryption
pub encryption_pubkey: TrySetOption<EncryptionPubKey>,
/// optional pubkey used for signing messages
pub signing_pubkey: TrySetOption<PublicKey>,
pub signing_pubkey: TrySetOption<VerifyingKey>,
/// optional pubkey used for signing messages
pub paymail_fee: TrySetOption<u64>,
}
Expand Down Expand Up @@ -259,12 +259,12 @@ impl TxCreator {
});
let signing_pubkey_resp = ui.horizontal(|ui| {
let default_pubkey =
PublicKey::from_bytes(&<[u8; 32] as Default>::default())
VerifyingKey::from_bytes(&<[u8; 32] as Default>::default())
.unwrap();
let try_from_str = |s: String| {
<[u8; 32]>::from_hex(s).map_err(either::Left).and_then(
|bytes| {
PublicKey::from_bytes(&bytes).map_err(either::Right)
VerifyingKey::from_bytes(&bytes).map_err(either::Right)
},
)
};
Expand Down
6 changes: 3 additions & 3 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "plain_bitnames"
version = "0.5.4"
version = "0.5.5"
edition = "2021"
authors = [ "Ash Manning <[email protected]>" ]

Expand All @@ -14,8 +14,8 @@ borsh = { version = "1.3.1", features = ["derive"] }
bs58 = { version = "0.5.0", features = ["check"] }
byteorder = "1.4.3"
bytes = "1.4.0"
ed25519-dalek = { version = "1.0.1", features = ["batch", "serde"] }
ed25519-dalek-bip32 = "0.2.0"
ed25519-dalek = { version = "2.1.1", features = ["batch", "serde"] }
ed25519-dalek-bip32 = "0.3.0"
educe = { version = "0.4.23", features = ["Hash"] }
heed = { git = "https://github.com/meilisearch/heed", tag = "v0.12.4", version = "0.12.4" }
hex = { version = "0.4.3", features = ["serde"] }
Expand Down
94 changes: 50 additions & 44 deletions lib/authorization.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
pub use ed25519_dalek::{
Keypair, PublicKey, Signature, SignatureError, Signer, Verifier,
};
use rayon::prelude::*;
use rayon::iter::{IntoParallelRefIterator as _, ParallelIterator as _};
use serde::{Deserialize, Serialize};

use crate::types::{
Address, AuthorizedTransaction, Body, GetAddress, Transaction, Verify,
};

pub use ed25519_dalek::{
Signature, SignatureError, Signer, SigningKey, Verifier, VerifyingKey,
};

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("ed25519_dalek error")]
DalekError(#[from] SignatureError),
#[error("bincode error")]
BincodeError(#[from] bincode::Error),
#[error(
"wrong key for address: address = {address},
hash(verifying_key) = {hash_verifying_key}"
)]
WrongKeyForAddress {
address: Address,
hash_verifying_key: Address,
},
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Authorization {
pub public_key: PublicKey,
pub verifying_key: VerifyingKey,
pub signature: Signature,
}

impl GetAddress for Authorization {
fn get_address(&self) -> Address {
get_address(&self.public_key)
get_address(&self.verifying_key)
}
}

Expand All @@ -35,9 +52,9 @@ impl Verify for Authorization {
}
}

pub fn get_address(public_key: &PublicKey) -> Address {
pub fn get_address(verifying_key: &VerifyingKey) -> Address {
let mut hasher = blake3::Hasher::new();
let mut reader = hasher.update(&public_key.to_bytes()).finalize_xof();
let mut reader = hasher.update(&verifying_key.to_bytes()).finalize_xof();
let mut output: [u8; 20] = [0; 20];
reader.fill(&mut output);
Address(output)
Expand All @@ -46,7 +63,7 @@ pub fn get_address(public_key: &PublicKey) -> Address {
struct Package<'a> {
messages: Vec<&'a [u8]>,
signatures: Vec<Signature>,
public_keys: Vec<PublicKey>,
verifying_keys: Vec<VerifyingKey>,
}

pub fn verify_authorized_transaction(
Expand All @@ -56,18 +73,18 @@ pub fn verify_authorized_transaction(
let messages: Vec<_> = std::iter::repeat(serialized_transaction.as_slice())
.take(transaction.authorizations.len())
.collect();
let (public_keys, signatures): (Vec<PublicKey>, Vec<Signature>) =
let (verifying_keys, signatures): (Vec<VerifyingKey>, Vec<Signature>) =
transaction
.authorizations
.iter()
.map(
|Authorization {
public_key,
verifying_key,
signature,
}| (public_key, signature),
}| (verifying_key, signature),
)
.unzip();
ed25519_dalek::verify_batch(&messages, &signatures, &public_keys)?;
ed25519_dalek::verify_batch(&messages, &signatures, &verifying_keys)?;
Ok(())
}

Expand Down Expand Up @@ -99,14 +116,14 @@ pub fn verify_authorizations(body: &Body) -> Result<(), Error> {
let mut package = Package {
messages: Vec::with_capacity(package_size),
signatures: Vec::with_capacity(package_size),
public_keys: Vec::with_capacity(package_size),
verifying_keys: Vec::with_capacity(package_size),
};
for (authorization, message) in
&pairs[i * package_size..(i + 1) * package_size]
{
package.messages.push(*message);
package.signatures.push(authorization.signature);
package.public_keys.push(authorization.public_key);
package.verifying_keys.push(authorization.verifying_key);
}
packages.push(package);
}
Expand All @@ -116,8 +133,8 @@ pub fn verify_authorizations(body: &Body) -> Result<(), Error> {
.signatures
.push(authorization.signature);
packages[num_threads - 1]
.public_keys
.push(authorization.public_key);
.verifying_keys
.push(authorization.verifying_key);
}
assert_eq!(
packages.iter().map(|p| p.signatures.len()).sum::<usize>(),
Expand All @@ -129,41 +146,45 @@ pub fn verify_authorizations(body: &Body) -> Result<(), Error> {
|Package {
messages,
signatures,
public_keys,
verifying_keys,
}| {
ed25519_dalek::verify_batch(messages, signatures, public_keys)
ed25519_dalek::verify_batch(
messages,
signatures,
verifying_keys,
)
},
)
.collect::<Result<(), SignatureError>>()?;
Ok(())
}

pub fn sign(
keypair: &Keypair,
keypair: &SigningKey,
transaction: &Transaction,
) -> Result<Signature, Error> {
let message = bincode::serialize(&transaction)?;
Ok(keypair.sign(&message))
}

pub fn authorize(
addresses_keypairs: &[(Address, &Keypair)],
addresses_signing_keys: &[(Address, &SigningKey)],
transaction: Transaction,
) -> Result<AuthorizedTransaction, Error> {
let mut authorizations: Vec<Authorization> =
Vec::with_capacity(addresses_keypairs.len());
Vec::with_capacity(addresses_signing_keys.len());
let message = bincode::serialize(&transaction)?;
for (address, keypair) in addresses_keypairs {
let hash_public_key = get_address(&keypair.public);
if *address != hash_public_key {
return Err(Error::WrongKeypairForAddress {
for (address, signing_key) in addresses_signing_keys {
let hash_verifying_key = get_address(&signing_key.verifying_key());
if *address != hash_verifying_key {
return Err(Error::WrongKeyForAddress {
address: *address,
hash_public_key,
hash_verifying_key,
});
}
let authorization = Authorization {
public_key: keypair.public,
signature: keypair.sign(&message),
verifying_key: signing_key.verifying_key(),
signature: signing_key.sign(&message),
};
authorizations.push(authorization);
}
Expand All @@ -172,18 +193,3 @@ pub fn authorize(
transaction,
})
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(
"wrong keypair for address: address = {address}, hash(public_key) = {hash_public_key}"
)]
WrongKeypairForAddress {
address: Address,
hash_public_key: Address,
},
#[error("ed25519_dalek error")]
DalekError(#[from] SignatureError),
#[error("bincode error")]
BincodeError(#[from] bincode::Error),
}
24 changes: 17 additions & 7 deletions lib/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use std::{
net::{Ipv4Addr, Ipv6Addr},
};

use heed::types::*;
use heed::{Database, RoTxn, RwTxn};
use hashes::MerkleRoot;
use heed::{
types::{OwnedType, SerdeBincode},
Database, RoTxn, RwTxn,
};
use nonempty::{nonempty, NonEmpty};
use serde::{Deserialize, Serialize};

Expand All @@ -14,10 +17,17 @@ use bip300301::{
TwoWayPegData, WithdrawalBundleStatus,
};

use crate::types::{self, *};
use crate::{
authorization::{Authorization, PublicKey},
types::hashes::BitName,
authorization::{Authorization, VerifyingKey},
types::{
self, constants,
hashes::{self, BitName},
Address, AggregatedWithdrawal, Authorized, AuthorizedTransaction,
BatchIcannRegistrationData, BitNameDataUpdates, Body, EncryptionPubKey,
FilledOutput, FilledOutputContent, FilledTransaction, GetAddress as _,
GetValue as _, Hash, InPoint, OutPoint, OutputContent, SpentOutput,
Transaction, TxData, Txid, Update, Verify as _, WithdrawalBundle,
},
};

/** Data of type `T` paired with
Expand Down Expand Up @@ -51,7 +61,7 @@ pub struct BitNameData {
/// optional pubkey used for encryption
encryption_pubkey: RollBack<Option<EncryptionPubKey>>,
/// optional pubkey used for signing messages
signing_pubkey: RollBack<Option<PublicKey>>,
signing_pubkey: RollBack<Option<VerifyingKey>>,
/// optional minimum paymail fee, in sats
paymail_fee: RollBack<Option<u64>>,
}
Expand Down Expand Up @@ -686,7 +696,7 @@ impl State {
&tx.transaction.outputs,
&batch_icann_data.plain_names,
));
constants::BATCH_ICANN_PUBKEY
constants::BATCH_ICANN_VERIFYING_KEY
.verify_strict(&msg_hash, &batch_icann_data.signature)?;
}
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions lib/types/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use ed25519_dalek::PUBLIC_KEY_LENGTH;
use hex_literal::hex;
use lazy_static::lazy_static;

use crate::authorization::PublicKey;
use crate::authorization::VerifyingKey;

/// authorized pubkey that can make batch icann registration txs
const BATCH_ICANN_PUBKEY_BYTES: [u8; PUBLIC_KEY_LENGTH] =
const BATCH_ICANN_VERIFYING_KEY_BYTES: [u8; PUBLIC_KEY_LENGTH] =
// FIXME: choose a real key
hex!(
"0000000000000000000000000000000000000000000000000000000000000000"
);

lazy_static! {
pub static ref BATCH_ICANN_PUBKEY: PublicKey =
PublicKey::from_bytes(&BATCH_ICANN_PUBKEY_BYTES)
pub static ref BATCH_ICANN_VERIFYING_KEY: VerifyingKey =
VerifyingKey::from_bytes(&BATCH_ICANN_VERIFYING_KEY_BYTES)
.expect("invalid batch ICANN pubkey");
}
Loading

0 comments on commit 48dd3ed

Please sign in to comment.