refactor(hashing): introduce Hashable derive macro and migrate server types
This commit is contained in:
14
server/Cargo.lock
generated
14
server/Cargo.lock
generated
@@ -696,12 +696,25 @@ dependencies = [
|
|||||||
name = "arbiter-crypto"
|
name = "arbiter-crypto"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"alloy",
|
||||||
"base64",
|
"base64",
|
||||||
|
"chrono",
|
||||||
|
"hmac",
|
||||||
"memsafe",
|
"memsafe",
|
||||||
"ml-dsa",
|
"ml-dsa",
|
||||||
"rand 0.10.0",
|
"rand 0.10.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "arbiter-macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"arbiter-crypto",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.117",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "arbiter-proto"
|
name = "arbiter-proto"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@@ -736,6 +749,7 @@ dependencies = [
|
|||||||
"alloy",
|
"alloy",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"arbiter-crypto",
|
"arbiter-crypto",
|
||||||
|
"arbiter-macros",
|
||||||
"arbiter-proto",
|
"arbiter-proto",
|
||||||
"arbiter-tokens-registry",
|
"arbiter-tokens-registry",
|
||||||
"argon2",
|
"argon2",
|
||||||
|
|||||||
@@ -47,3 +47,4 @@ miette = { version = "7.6.0", features = ["fancy", "serde"] }
|
|||||||
mutants = "0.0.4"
|
mutants = "0.0.4"
|
||||||
ml-dsa = { version = "0.1.0-rc.8", features = ["zeroize"] }
|
ml-dsa = { version = "0.1.0-rc.8", features = ["zeroize"] }
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
|
hmac = "0.12.1"
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ ml-dsa = {workspace = true, optional = true }
|
|||||||
rand = {workspace = true, optional = true}
|
rand = {workspace = true, optional = true}
|
||||||
base64 = {workspace = true, optional = true }
|
base64 = {workspace = true, optional = true }
|
||||||
memsafe = {version = "0.4.0", optional = true}
|
memsafe = {version = "0.4.0", optional = true}
|
||||||
|
hmac.workspace = true
|
||||||
|
alloy.workspace = true
|
||||||
|
chrono.workspace = true
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use base64::{Engine as _, prelude::BASE64_STANDARD};
|
use base64::{Engine as _, prelude::BASE64_STANDARD};
|
||||||
|
use hmac::digest::Digest;
|
||||||
use ml_dsa::{
|
use ml_dsa::{
|
||||||
EncodedVerifyingKey, Error, KeyGen, MlDsa87, Seed, Signature as MlDsaSignature,
|
EncodedVerifyingKey, Error, KeyGen, MlDsa87, Seed, Signature as MlDsaSignature,
|
||||||
SigningKey as MlDsaSigningKey, VerifyingKey as MlDsaVerifyingKey, signature::Keypair as _,
|
SigningKey as MlDsaSigningKey, VerifyingKey as MlDsaVerifyingKey, signature::Keypair as _,
|
||||||
@@ -17,6 +18,12 @@ pub type KeyParams = MlDsa87;
|
|||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct PublicKey(Box<MlDsaVerifyingKey<KeyParams>>);
|
pub struct PublicKey(Box<MlDsaVerifyingKey<KeyParams>>);
|
||||||
|
|
||||||
|
impl crate::hashing::Hashable for PublicKey {
|
||||||
|
fn hash<H: Digest>(&self, hasher: &mut H) {
|
||||||
|
hasher.update(self.to_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Signature(Box<MlDsaSignature<KeyParams>>);
|
pub struct Signature(Box<MlDsaSignature<KeyParams>>);
|
||||||
|
|
||||||
@@ -25,7 +32,7 @@ pub struct SigningKey(Box<MlDsaSigningKey<KeyParams>>);
|
|||||||
|
|
||||||
impl PublicKey {
|
impl PublicKey {
|
||||||
pub fn to_bytes(&self) -> Vec<u8> {
|
pub fn to_bytes(&self) -> Vec<u8> {
|
||||||
self.0.encode().to_vec()
|
self.0.encode().0.to_vec()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify(&self, nonce: i32, context: &[u8], signature: &Signature) -> bool {
|
pub fn verify(&self, nonce: i32, context: &[u8], signature: &Signature) -> bool {
|
||||||
@@ -39,7 +46,7 @@ impl PublicKey {
|
|||||||
|
|
||||||
impl Signature {
|
impl Signature {
|
||||||
pub fn to_bytes(&self) -> Vec<u8> {
|
pub fn to_bytes(&self) -> Vec<u8> {
|
||||||
self.0.encode().to_vec()
|
self.0.encode().0.to_vec()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,25 @@
|
|||||||
use hmac::digest::Digest;
|
pub use hmac::digest::Digest;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
/// Deterministically hash a value by feeding its fields into the hasher in a consistent order.
|
/// Deterministically hash a value by feeding its fields into the hasher in a consistent order.
|
||||||
|
#[diagnostic::on_unimplemented(
|
||||||
|
note = "for local types consider adding `#[derive(arbiter_macros::Hashable)]` to your `{Self}` type",
|
||||||
|
note = "for types from other crates check whether the crate offers a `Hashable` implementation"
|
||||||
|
)]
|
||||||
pub trait Hashable {
|
pub trait Hashable {
|
||||||
fn hash<H: Digest>(&self, hasher: &mut H);
|
fn hash<H: Digest>(&self, hasher: &mut H);
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_numeric {
|
macro_rules! impl_numeric {
|
||||||
($($t:ty),*) => {
|
($($t:ty),*) => {
|
||||||
$(
|
$(
|
||||||
impl Hashable for $t {
|
impl Hashable for $t {
|
||||||
fn hash<H: Digest>(&self, hasher: &mut H) {
|
fn hash<H: Digest>(&self, hasher: &mut H) {
|
||||||
hasher.update(&self.to_be_bytes());
|
hasher.update(&self.to_be_bytes());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
)*
|
||||||
)*
|
};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_numeric!(u8, u16, u32, u64, i8, i16, i32, i64);
|
impl_numeric!(u8, u16, u32, u64, i8, i16, i32, i64);
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
#[cfg(feature = "authn")]
|
#[cfg(feature = "authn")]
|
||||||
pub mod authn;
|
pub mod authn;
|
||||||
|
pub mod hashing;
|
||||||
#[cfg(feature = "safecell")]
|
#[cfg(feature = "safecell")]
|
||||||
pub mod safecell;
|
pub mod safecell;
|
||||||
|
|||||||
18
server/crates/arbiter-macros/Cargo.toml
Normal file
18
server/crates/arbiter-macros/Cargo.toml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
[package]
|
||||||
|
name = "arbiter-macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
proc-macro2 = "1.0"
|
||||||
|
quote = "1.0"
|
||||||
|
syn = { version = "2.0", features = ["derive", "fold", "full", "visit-mut"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
arbiter-crypto = { path = "../arbiter-crypto" }
|
||||||
|
|
||||||
|
[lints]
|
||||||
|
workspace = true
|
||||||
133
server/crates/arbiter-macros/src/hashable.rs
Normal file
133
server/crates/arbiter-macros/src/hashable.rs
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
use proc_macro2::{Span, TokenStream, TokenTree};
|
||||||
|
use quote::quote;
|
||||||
|
use syn::parse_quote;
|
||||||
|
use syn::spanned::Spanned;
|
||||||
|
use syn::{DataStruct, DeriveInput, Fields, Generics, Index};
|
||||||
|
|
||||||
|
use crate::utils::{HASHABLE_TRAIT_PATH, HMAC_DIGEST_PATH};
|
||||||
|
|
||||||
|
pub(crate) fn derive(input: &DeriveInput) -> TokenStream {
|
||||||
|
match &input.data {
|
||||||
|
syn::Data::Struct(struct_data) => hashable_struct(input, struct_data),
|
||||||
|
syn::Data::Enum(_) => {
|
||||||
|
syn::Error::new_spanned(input, "Hashable can currently be derived only for structs")
|
||||||
|
.to_compile_error()
|
||||||
|
}
|
||||||
|
syn::Data::Union(_) => {
|
||||||
|
syn::Error::new_spanned(input, "Hashable cannot be derived for unions")
|
||||||
|
.to_compile_error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hashable_struct(input: &DeriveInput, struct_data: &syn::DataStruct) -> TokenStream {
|
||||||
|
let ident = &input.ident;
|
||||||
|
let hashable_trait = HASHABLE_TRAIT_PATH.to_path();
|
||||||
|
let hmac_digest = HMAC_DIGEST_PATH.to_path();
|
||||||
|
let generics = add_hashable_bounds(input.generics.clone(), &hashable_trait);
|
||||||
|
let field_accesses = collect_field_accesses(struct_data);
|
||||||
|
let hash_calls = build_hash_calls(&field_accesses, &hashable_trait);
|
||||||
|
|
||||||
|
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #impl_generics #hashable_trait for #ident #ty_generics #where_clause {
|
||||||
|
fn hash<H: #hmac_digest>(&self, hasher: &mut H) {
|
||||||
|
#(#hash_calls)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_hashable_bounds(mut generics: Generics, hashable_trait: &syn::Path) -> Generics {
|
||||||
|
for type_param in generics.type_params_mut() {
|
||||||
|
type_param.bounds.push(parse_quote!(#hashable_trait));
|
||||||
|
}
|
||||||
|
|
||||||
|
generics
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FieldAccess {
|
||||||
|
access: TokenStream,
|
||||||
|
span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn collect_field_accesses(struct_data: &DataStruct) -> Vec<FieldAccess> {
|
||||||
|
match &struct_data.fields {
|
||||||
|
Fields::Named(fields) => {
|
||||||
|
// Keep deterministic alphabetical order for named fields.
|
||||||
|
// Do not remove this sort, because it keeps hash output stable regardless of source order.
|
||||||
|
let mut named_fields = fields
|
||||||
|
.named
|
||||||
|
.iter()
|
||||||
|
.map(|field| {
|
||||||
|
let name = field
|
||||||
|
.ident
|
||||||
|
.as_ref()
|
||||||
|
.expect("Fields::Named(fields) must have names")
|
||||||
|
.clone();
|
||||||
|
(name.to_string(), name)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
named_fields.sort_by(|a, b| a.0.cmp(&b.0));
|
||||||
|
|
||||||
|
named_fields
|
||||||
|
.into_iter()
|
||||||
|
.map(|(_, name)| FieldAccess {
|
||||||
|
access: quote! { #name },
|
||||||
|
span: name.span(),
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
Fields::Unnamed(fields) => fields
|
||||||
|
.unnamed
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, field)| FieldAccess {
|
||||||
|
access: {
|
||||||
|
let index = Index::from(i);
|
||||||
|
quote! { #index }
|
||||||
|
},
|
||||||
|
span: field.ty.span(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
Fields::Unit => Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_hash_calls(
|
||||||
|
field_accesses: &[FieldAccess],
|
||||||
|
hashable_trait: &syn::Path,
|
||||||
|
) -> Vec<TokenStream> {
|
||||||
|
field_accesses
|
||||||
|
.iter()
|
||||||
|
.map(|field| {
|
||||||
|
let access = &field.access;
|
||||||
|
let call = quote! {
|
||||||
|
#hashable_trait::hash(&self.#access, hasher);
|
||||||
|
};
|
||||||
|
|
||||||
|
respan(call, field.span)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Recursively set span on all tokens, including interpolated ones.
|
||||||
|
fn respan(tokens: TokenStream, span: Span) -> TokenStream {
|
||||||
|
tokens
|
||||||
|
.into_iter()
|
||||||
|
.map(|tt| match tt {
|
||||||
|
TokenTree::Group(g) => {
|
||||||
|
let mut new = proc_macro2::Group::new(g.delimiter(), respan(g.stream(), span));
|
||||||
|
new.set_span(span);
|
||||||
|
TokenTree::Group(new)
|
||||||
|
}
|
||||||
|
mut other => {
|
||||||
|
other.set_span(span);
|
||||||
|
other
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
10
server/crates/arbiter-macros/src/lib.rs
Normal file
10
server/crates/arbiter-macros/src/lib.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
use syn::{DeriveInput, parse_macro_input};
|
||||||
|
|
||||||
|
mod hashable;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
#[proc_macro_derive(Hashable)]
|
||||||
|
pub fn derive_hashable(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
|
hashable::derive(&input).into()
|
||||||
|
}
|
||||||
19
server/crates/arbiter-macros/src/utils.rs
Normal file
19
server/crates/arbiter-macros/src/utils.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
pub struct ToPath(pub &'static str);
|
||||||
|
|
||||||
|
impl ToPath {
|
||||||
|
pub fn to_path(&self) -> syn::Path {
|
||||||
|
syn::parse_str(self.0).expect("Invalid path")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! ensure_path {
|
||||||
|
($path:path) => {{
|
||||||
|
#[cfg(test)]
|
||||||
|
#[expect(unused_imports)]
|
||||||
|
use $path as _;
|
||||||
|
ToPath(stringify!($path))
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const HASHABLE_TRAIT_PATH: ToPath = ensure_path!(::arbiter_crypto::hashing::Hashable);
|
||||||
|
pub const HMAC_DIGEST_PATH: ToPath = ensure_path!(::arbiter_crypto::hashing::Digest);
|
||||||
@@ -18,6 +18,7 @@ diesel-async = { version = "0.8.0", features = [
|
|||||||
] }
|
] }
|
||||||
arbiter-proto.path = "../arbiter-proto"
|
arbiter-proto.path = "../arbiter-proto"
|
||||||
arbiter-crypto.path = "../arbiter-crypto"
|
arbiter-crypto.path = "../arbiter-crypto"
|
||||||
|
arbiter-macros.path = "../arbiter-macros"
|
||||||
tracing.workspace = true
|
tracing.workspace = true
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
tonic.workspace = true
|
tonic.workspace = true
|
||||||
@@ -44,7 +45,7 @@ restructed = "0.2.2"
|
|||||||
strum = { version = "0.28.0", features = ["derive"] }
|
strum = { version = "0.28.0", features = ["derive"] }
|
||||||
pem = "3.0.6"
|
pem = "3.0.6"
|
||||||
sha2.workspace = true
|
sha2.workspace = true
|
||||||
hmac = "0.12"
|
hmac.workspace = true
|
||||||
spki.workspace = true
|
spki.workspace = true
|
||||||
alloy.workspace = true
|
alloy.workspace = true
|
||||||
prost-types.workspace = true
|
prost-types.workspace = true
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use tracing::{error, info};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
actors::{GlobalActors, client::session::ClientSession},
|
actors::{GlobalActors, client::session::ClientSession},
|
||||||
crypto::integrity::{Integrable, hashing::Hashable},
|
crypto::integrity::Integrable,
|
||||||
db,
|
db,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -15,6 +15,7 @@ pub struct ClientProfile {
|
|||||||
pub metadata: ClientMetadata,
|
pub metadata: ClientMetadata,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(arbiter_macros::Hashable)]
|
||||||
pub struct ClientCredentials {
|
pub struct ClientCredentials {
|
||||||
pub pubkey: authn::PublicKey,
|
pub pubkey: authn::PublicKey,
|
||||||
pub nonce: i32,
|
pub nonce: i32,
|
||||||
@@ -24,13 +25,6 @@ impl Integrable for ClientCredentials {
|
|||||||
const KIND: &'static str = "client_credentials";
|
const KIND: &'static str = "client_credentials";
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hashable for ClientCredentials {
|
|
||||||
fn hash<H: sha2::Digest>(&self, hasher: &mut H) {
|
|
||||||
hasher.update(self.pubkey.to_bytes());
|
|
||||||
self.nonce.hash(hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ClientConnection {
|
pub struct ClientConnection {
|
||||||
pub(crate) db: db::DatabasePool,
|
pub(crate) db: db::DatabasePool,
|
||||||
pub(crate) actors: GlobalActors,
|
pub(crate) actors: GlobalActors,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use arbiter_crypto::authn;
|
use arbiter_crypto::authn;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, arbiter_macros::Hashable)]
|
||||||
pub struct UserAgentCredentials {
|
pub struct UserAgentCredentials {
|
||||||
pub pubkey: authn::PublicKey,
|
pub pubkey: authn::PublicKey,
|
||||||
pub nonce: i32,
|
pub nonce: i32,
|
||||||
@@ -38,18 +38,3 @@ pub mod session;
|
|||||||
|
|
||||||
pub use auth::authenticate;
|
pub use auth::authenticate;
|
||||||
pub use session::UserAgentSession;
|
pub use session::UserAgentSession;
|
||||||
|
|
||||||
use crate::crypto::integrity::hashing::Hashable;
|
|
||||||
|
|
||||||
impl Hashable for authn::PublicKey {
|
|
||||||
fn hash<H: sha2::Digest>(&self, hasher: &mut H) {
|
|
||||||
hasher.update(self.to_bytes());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hashable for UserAgentCredentials {
|
|
||||||
fn hash<H: sha2::Digest>(&self, hasher: &mut H) {
|
|
||||||
self.pubkey.hash(hasher);
|
|
||||||
self.nonce.hash(hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{actors::keyholder, crypto::integrity::hashing::Hashable};
|
use crate::actors::keyholder;
|
||||||
use arbiter_crypto::safecell::SafeCellHandle as _;
|
use arbiter_crypto::hashing::Hashable;
|
||||||
use hmac::{Hmac, Mac as _};
|
use hmac::Hmac;
|
||||||
use sha2::Sha256;
|
use sha2::Sha256;
|
||||||
|
|
||||||
use diesel::{ExpressionMethods as _, QueryDsl, dsl::insert_into, sqlite::Sqlite};
|
use diesel::{ExpressionMethods as _, QueryDsl, dsl::insert_into, sqlite::Sqlite};
|
||||||
@@ -8,8 +8,6 @@ use diesel_async::{AsyncConnection, RunQueryDsl};
|
|||||||
use kameo::{actor::ActorRef, error::SendError};
|
use kameo::{actor::ActorRef, error::SendError};
|
||||||
use sha2::Digest as _;
|
use sha2::Digest as _;
|
||||||
|
|
||||||
pub mod hashing;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
actors::keyholder::{KeyHolder, SignIntegrity, VerifyIntegrity},
|
actors::keyholder::{KeyHolder, SignIntegrity, VerifyIntegrity},
|
||||||
db::{
|
db::{
|
||||||
@@ -203,10 +201,6 @@ mod tests {
|
|||||||
use diesel::{ExpressionMethods as _, QueryDsl};
|
use diesel::{ExpressionMethods as _, QueryDsl};
|
||||||
use diesel_async::RunQueryDsl;
|
use diesel_async::RunQueryDsl;
|
||||||
use kameo::{actor::ActorRef, prelude::Spawn};
|
use kameo::{actor::ActorRef, prelude::Spawn};
|
||||||
|
|
||||||
use sha2::Digest;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
actors::keyholder::{Bootstrap, KeyHolder},
|
actors::keyholder::{Bootstrap, KeyHolder},
|
||||||
@@ -215,20 +209,11 @@ mod tests {
|
|||||||
use arbiter_crypto::safecell::{SafeCell, SafeCellHandle as _};
|
use arbiter_crypto::safecell::{SafeCell, SafeCellHandle as _};
|
||||||
|
|
||||||
use super::{Error, Integrable, sign_entity, verify_entity};
|
use super::{Error, Integrable, sign_entity, verify_entity};
|
||||||
use super::hashing::Hashable;
|
#[derive(Clone, arbiter_macros::Hashable)]
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct DummyEntity {
|
struct DummyEntity {
|
||||||
payload_version: i32,
|
payload_version: i32,
|
||||||
payload: Vec<u8>,
|
payload: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hashable for DummyEntity {
|
|
||||||
fn hash<H: Digest>(&self, hasher: &mut H) {
|
|
||||||
self.payload_version.hash(hasher);
|
|
||||||
self.payload.hash(hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Integrable for DummyEntity {
|
impl Integrable for DummyEntity {
|
||||||
const KIND: &'static str = "dummy_entity";
|
const KIND: &'static str = "dummy_entity";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,19 +127,19 @@ pub enum SpecificMeaning {
|
|||||||
TokenTransfer(token_transfers::Meaning),
|
TokenTransfer(token_transfers::Meaning),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, arbiter_macros::Hashable)]
|
||||||
pub struct TransactionRateLimit {
|
pub struct TransactionRateLimit {
|
||||||
pub count: u32,
|
pub count: u32,
|
||||||
pub window: Duration,
|
pub window: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, arbiter_macros::Hashable)]
|
||||||
pub struct VolumeRateLimit {
|
pub struct VolumeRateLimit {
|
||||||
pub max_volume: U256,
|
pub max_volume: U256,
|
||||||
pub window: Duration,
|
pub window: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, arbiter_macros::Hashable)]
|
||||||
pub struct SharedGrantSettings {
|
pub struct SharedGrantSettings {
|
||||||
pub wallet_access_id: i32,
|
pub wallet_access_id: i32,
|
||||||
pub chain: ChainId,
|
pub chain: ChainId,
|
||||||
@@ -200,7 +200,7 @@ pub enum SpecificGrant {
|
|||||||
TokenTransfer(token_transfers::Settings),
|
TokenTransfer(token_transfers::Settings),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, arbiter_macros::Hashable)]
|
||||||
pub struct CombinedSettings<PolicyGrant> {
|
pub struct CombinedSettings<PolicyGrant> {
|
||||||
pub shared: SharedGrantSettings,
|
pub shared: SharedGrantSettings,
|
||||||
pub specific: PolicyGrant,
|
pub specific: PolicyGrant,
|
||||||
@@ -219,38 +219,3 @@ impl<P: Integrable> Integrable for CombinedSettings<P> {
|
|||||||
const KIND: &'static str = P::KIND;
|
const KIND: &'static str = P::KIND;
|
||||||
const VERSION: i32 = P::VERSION;
|
const VERSION: i32 = P::VERSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::crypto::integrity::hashing::Hashable;
|
|
||||||
|
|
||||||
impl Hashable for TransactionRateLimit {
|
|
||||||
fn hash<H: sha2::Digest>(&self, hasher: &mut H) {
|
|
||||||
self.count.hash(hasher);
|
|
||||||
self.window.hash(hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hashable for VolumeRateLimit {
|
|
||||||
fn hash<H: sha2::Digest>(&self, hasher: &mut H) {
|
|
||||||
self.max_volume.hash(hasher);
|
|
||||||
self.window.hash(hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hashable for SharedGrantSettings {
|
|
||||||
fn hash<H: sha2::Digest>(&self, hasher: &mut H) {
|
|
||||||
self.wallet_access_id.hash(hasher);
|
|
||||||
self.chain.hash(hasher);
|
|
||||||
self.valid_from.hash(hasher);
|
|
||||||
self.valid_until.hash(hasher);
|
|
||||||
self.max_gas_fee_per_gas.hash(hasher);
|
|
||||||
self.max_priority_fee_per_gas.hash(hasher);
|
|
||||||
self.rate_limit.hash(hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<P: Hashable> Hashable for CombinedSettings<P> {
|
|
||||||
fn hash<H: sha2::Digest>(&self, hasher: &mut H) {
|
|
||||||
self.shared.hash(hasher);
|
|
||||||
self.specific.hash(hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ impl From<Meaning> for SpecificMeaning {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// A grant for ether transfers, which can be scoped to specific target addresses and volume limits
|
// A grant for ether transfers, which can be scoped to specific target addresses and volume limits
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, arbiter_macros::Hashable)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub target: Vec<Address>,
|
pub target: Vec<Address>,
|
||||||
pub limit: VolumeRateLimit,
|
pub limit: VolumeRateLimit,
|
||||||
@@ -61,15 +61,6 @@ impl Integrable for Settings {
|
|||||||
const KIND: &'static str = "EtherTransfer";
|
const KIND: &'static str = "EtherTransfer";
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::crypto::integrity::hashing::Hashable;
|
|
||||||
|
|
||||||
impl Hashable for Settings {
|
|
||||||
fn hash<H: sha2::Digest>(&self, hasher: &mut H) {
|
|
||||||
self.target.hash(hasher);
|
|
||||||
self.limit.hash(hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Settings> for SpecificGrant {
|
impl From<Settings> for SpecificGrant {
|
||||||
fn from(val: Settings) -> SpecificGrant {
|
fn from(val: Settings) -> SpecificGrant {
|
||||||
SpecificGrant::EtherTransfer(val)
|
SpecificGrant::EtherTransfer(val)
|
||||||
|
|||||||
@@ -340,7 +340,7 @@ proptest::proptest! {
|
|||||||
) {
|
) {
|
||||||
use rand::{SeedableRng, seq::SliceRandom};
|
use rand::{SeedableRng, seq::SliceRandom};
|
||||||
use sha2::Digest;
|
use sha2::Digest;
|
||||||
use crate::crypto::integrity::hashing::Hashable;
|
use arbiter_crypto::hashing::Hashable;
|
||||||
|
|
||||||
let addrs: Vec<Address> = raw_addrs.iter().map(|b| Address::from(*b)).collect();
|
let addrs: Vec<Address> = raw_addrs.iter().map(|b| Address::from(*b)).collect();
|
||||||
let mut shuffled = addrs.clone();
|
let mut shuffled = addrs.clone();
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ impl From<Meaning> for SpecificMeaning {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// A grant for token transfers, which can be scoped to specific target addresses and volume limits
|
// A grant for token transfers, which can be scoped to specific target addresses and volume limits
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, arbiter_macros::Hashable)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub token_contract: Address,
|
pub token_contract: Address,
|
||||||
pub target: Option<Address>,
|
pub target: Option<Address>,
|
||||||
@@ -72,16 +72,6 @@ impl Integrable for Settings {
|
|||||||
const KIND: &'static str = "TokenTransfer";
|
const KIND: &'static str = "TokenTransfer";
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::crypto::integrity::hashing::Hashable;
|
|
||||||
|
|
||||||
impl Hashable for Settings {
|
|
||||||
fn hash<H: sha2::Digest>(&self, hasher: &mut H) {
|
|
||||||
self.token_contract.hash(hasher);
|
|
||||||
self.target.hash(hasher);
|
|
||||||
self.volume_limits.hash(hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Settings> for SpecificGrant {
|
impl From<Settings> for SpecificGrant {
|
||||||
fn from(val: Settings) -> SpecificGrant {
|
fn from(val: Settings) -> SpecificGrant {
|
||||||
SpecificGrant::TokenTransfer(val)
|
SpecificGrant::TokenTransfer(val)
|
||||||
|
|||||||
@@ -419,7 +419,7 @@ proptest::proptest! {
|
|||||||
) {
|
) {
|
||||||
use rand::{SeedableRng, seq::SliceRandom};
|
use rand::{SeedableRng, seq::SliceRandom};
|
||||||
use sha2::Digest;
|
use sha2::Digest;
|
||||||
use crate::crypto::integrity::hashing::Hashable;
|
use arbiter_crypto::hashing::Hashable;
|
||||||
|
|
||||||
let limits: Vec<VolumeRateLimit> = raw_limits
|
let limits: Vec<VolumeRateLimit> = raw_limits
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
Reference in New Issue
Block a user