mirror of
https://gitlab.com/veilid/veilid.git
synced 2025-03-14 01:46:41 -04:00
added function to get record key
This commit is contained in:
parent
3255352097
commit
c92f17b312
@ -231,6 +231,19 @@ impl StorageManager {
|
||||
|
||||
/// Create a local record from scratch with a new owner key, open it, and return the opened descriptor
|
||||
#[instrument(level = "trace", target = "stor", skip_all)]
|
||||
pub async fn get_record_key(
|
||||
&self,
|
||||
kind: CryptoKind,
|
||||
schema: DHTSchema,
|
||||
owner_key: &PublicKey,
|
||||
) -> VeilidAPIResult<TypedKey> {
|
||||
let inner = self.lock().await?;
|
||||
schema.validate()?;
|
||||
|
||||
inner.get_record_key(kind, owner_key, schema).await
|
||||
}
|
||||
|
||||
/// Create a local record from scratch with a new owner key, open it, and return the opened descriptor
|
||||
pub async fn create_record(
|
||||
&self,
|
||||
kind: CryptoKind,
|
||||
|
@ -252,6 +252,9 @@ impl StorageManagerInner {
|
||||
// New values require a new owner key if not given
|
||||
let owner = owner.unwrap_or_else(|| vcrypto.generate_keypair());
|
||||
|
||||
// Calculate dht key
|
||||
let dht_key = Self::get_key(vcrypto.clone(), &owner.key, &schema_data);
|
||||
|
||||
// Make a signed value descriptor for this dht value
|
||||
let signed_value_descriptor = Arc::new(SignedValueDescriptor::make_signature(
|
||||
owner.key,
|
||||
@ -259,14 +262,12 @@ impl StorageManagerInner {
|
||||
vcrypto.clone(),
|
||||
owner.secret,
|
||||
)?);
|
||||
|
||||
// Add new local value record
|
||||
let cur_ts = Timestamp::now();
|
||||
let local_record_detail = LocalRecordDetail::new(safety_selection);
|
||||
let record =
|
||||
Record::<LocalRecordDetail>::new(cur_ts, signed_value_descriptor, local_record_detail)?;
|
||||
|
||||
let dht_key = Self::get_key(vcrypto.clone(), &record);
|
||||
local_record_store.new_record(dht_key, record).await?;
|
||||
|
||||
Ok((dht_key, owner))
|
||||
@ -703,18 +704,25 @@ impl StorageManagerInner {
|
||||
})
|
||||
}
|
||||
|
||||
/// # DHT Key = Hash(ownerKeyKind) of: [ ownerKeyValue, schema ]
|
||||
#[instrument(level = "trace", target = "stor", skip_all)]
|
||||
fn get_key<D>(vcrypto: CryptoSystemVersion, record: &Record<D>) -> TypedKey
|
||||
where
|
||||
D: fmt::Debug + Clone + Serialize,
|
||||
{
|
||||
let descriptor = record.descriptor();
|
||||
let compiled = descriptor.schema_data();
|
||||
let mut hash_data = Vec::<u8>::with_capacity(PUBLIC_KEY_LENGTH + 4 + compiled.len());
|
||||
pub async fn get_record_key(
|
||||
&self,
|
||||
kind: CryptoKind,
|
||||
owner_key: &PublicKey,
|
||||
schema: DHTSchema,,
|
||||
) -> VeilidAPIResult<TypedKey> {
|
||||
// Get cryptosystem
|
||||
let Some(vcrypto) = self.unlocked_inner.crypto.get(kind) else {
|
||||
apibail_generic!("unsupported cryptosystem");
|
||||
};
|
||||
|
||||
Ok(Self::get_key(vcrypto, owner_key, &schema.compile()))
|
||||
}
|
||||
|
||||
fn get_key(vcrypto: CryptoSystemVersion, owner_key: &PublicKey, schema_data: &[u8]) -> TypedKey {
|
||||
let mut hash_data = Vec::<u8>::with_capacity(PUBLIC_KEY_LENGTH + 4 + schema_data.len());
|
||||
hash_data.extend_from_slice(&vcrypto.kind().0);
|
||||
hash_data.extend_from_slice(&record.owner().bytes);
|
||||
hash_data.extend_from_slice(compiled);
|
||||
hash_data.extend_from_slice(&owner_key.bytes);
|
||||
hash_data.extend_from_slice(schema_data);
|
||||
let hash = vcrypto.generate_hash(&hash_data);
|
||||
TypedKey::new(vcrypto.kind(), hash)
|
||||
}
|
||||
|
@ -225,6 +225,26 @@ impl RoutingContext {
|
||||
///////////////////////////////////
|
||||
/// DHT Records
|
||||
|
||||
///
|
||||
#[instrument(target = "veilid_api", level = "debug", ret, err)]
|
||||
pub async fn get_dht_record_key(
|
||||
&self,
|
||||
schema: DHTSchema,
|
||||
owner_key: &PublicKey,
|
||||
kind: Option<CryptoKind>,
|
||||
) -> VeilidAPIResult<TypedKey> {
|
||||
event!(target: "veilid_api", Level::DEBUG,
|
||||
"RoutingContext::get_dht_record_key(self: {:?}, schema: {:?}, owner_key: {:?}, kind: {:?})", self, schema, owner_key, kind);
|
||||
schema.validate()?;
|
||||
|
||||
let kind = kind.unwrap_or(best_crypto_kind());
|
||||
Crypto::validate_crypto_kind(kind)?;
|
||||
let storage_manager = self.api.storage_manager()?;
|
||||
storage_manager
|
||||
.get_record_key(kind, schema, owner_key)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Creates a new DHT record
|
||||
///
|
||||
/// The record is considered 'open' after the create operation succeeds.
|
||||
|
Loading…
x
Reference in New Issue
Block a user