reverse all IDs serialization to have a trailing A instead of all IDs starting with A

pull/19/head
Niko PLP 6 months ago
parent e3a3b556fa
commit 446e5cafa8
  1. 1
      ng-net/src/app_protocol.rs
  2. 9
      ng-repo/src/block.rs
  3. 17
      ng-repo/src/types.rs
  4. 15
      ng-repo/src/utils.rs
  5. 20
      ng-sdk-js/app-node/index.js
  6. 3
      ngone/src/main.rs

@ -15,6 +15,7 @@ use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use ng_repo::errors::NgError; use ng_repo::errors::NgError;
#[allow(unused_imports)]
use ng_repo::log::*; use ng_repo::log::*;
use ng_repo::types::*; use ng_repo::types::*;
use ng_repo::utils::{decode_digest, decode_key, decode_sym_key}; use ng_repo::utils::{decode_digest, decode_key, decode_sym_key};

@ -79,10 +79,11 @@ impl BlockV0 {
} }
} }
impl From<Digest> for String { impl From<&Digest> for String {
fn from(id: BlockId) -> Self { fn from(id: &Digest) -> Self {
base64_url::encode(&serde_bare::to_vec(&id).unwrap()) let mut ser = serde_bare::to_vec(id).unwrap();
//hex::encode(to_vec(&id).unwrap()) ser.reverse();
base64_url::encode(&ser)
} }
} }

@ -57,8 +57,7 @@ impl Digest {
impl fmt::Display for Digest { impl fmt::Display for Digest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ser = serde_bare::to_vec(&self).unwrap(); write!(f, "{}", std::string::String::from(self))
write!(f, "{}", base64_url::encode(&ser))
} }
} }
@ -114,7 +113,8 @@ impl SymKey {
impl fmt::Display for SymKey { impl fmt::Display for SymKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ser = serde_bare::to_vec(&self).unwrap(); let mut ser = serde_bare::to_vec(&self).unwrap();
ser.reverse();
write!(f, "{}", base64_url::encode(&ser)) write!(f, "{}", base64_url::encode(&ser))
} }
} }
@ -196,7 +196,8 @@ impl PubKey {
impl fmt::Display for PubKey { impl fmt::Display for PubKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ser = serde_bare::to_vec(&self).unwrap(); let mut ser = serde_bare::to_vec(&self).unwrap();
ser.reverse();
write!(f, "{}", base64_url::encode(&ser)) write!(f, "{}", base64_url::encode(&ser))
} }
} }
@ -278,7 +279,8 @@ impl TryFrom<&str> for PrivKey {
impl fmt::Display for PrivKey { impl fmt::Display for PrivKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ser = serde_bare::to_vec(&self).unwrap(); let mut ser = serde_bare::to_vec(&self).unwrap();
ser.reverse();
write!(f, "{}", base64_url::encode(&ser)) write!(f, "{}", base64_url::encode(&ser))
} }
} }
@ -543,8 +545,9 @@ pub enum OverlayId {
impl fmt::Display for OverlayId { impl fmt::Display for OverlayId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let overlay_ser = serde_bare::to_vec(&self).unwrap(); let mut ser = serde_bare::to_vec(&self).unwrap();
write!(f, "{}", base64_url::encode(&overlay_ser)) ser.reverse();
write!(f, "{}", base64_url::encode(&ser))
} }
} }

@ -56,27 +56,32 @@ pub fn from_ed_privkey_to_dh_privkey(private: &PrivKey) -> PrivKey {
/// don't forget to zeroize the string later on /// don't forget to zeroize the string later on
pub fn decode_key(key_string: &str) -> Result<PubKey, NgError> { pub fn decode_key(key_string: &str) -> Result<PubKey, NgError> {
let vec = base64_url::decode(key_string).map_err(|_| NgError::InvalidKey)?; let mut vec = base64_url::decode(key_string).map_err(|_| NgError::InvalidKey)?;
vec.reverse();
Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?) Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?)
} }
pub fn decode_priv_key(key_string: &str) -> Result<PrivKey, NgError> { pub fn decode_priv_key(key_string: &str) -> Result<PrivKey, NgError> {
let vec = base64_url::decode(key_string).map_err(|_| NgError::InvalidKey)?; let mut vec = base64_url::decode(key_string).map_err(|_| NgError::InvalidKey)?;
vec.reverse();
Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?) Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?)
} }
pub fn decode_sym_key(key_string: &str) -> Result<SymKey, NgError> { pub fn decode_sym_key(key_string: &str) -> Result<SymKey, NgError> {
let vec = base64_url::decode(key_string).map_err(|_| NgError::InvalidKey)?; let mut vec = base64_url::decode(key_string).map_err(|_| NgError::InvalidKey)?;
vec.reverse();
Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?) Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?)
} }
pub fn decode_digest(key_string: &str) -> Result<crate::types::Digest, NgError> { pub fn decode_digest(key_string: &str) -> Result<crate::types::Digest, NgError> {
let vec = base64_url::decode(key_string).map_err(|_| NgError::InvalidKey)?; let mut vec = base64_url::decode(key_string).map_err(|_| NgError::InvalidKey)?;
vec.reverse();
Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?) Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?)
} }
pub fn decode_overlayid(id_string: &str) -> Result<OverlayId, NgError> { pub fn decode_overlayid(id_string: &str) -> Result<OverlayId, NgError> {
let vec = base64_url::decode(id_string).map_err(|_| NgError::InvalidKey)?; let mut vec = base64_url::decode(id_string).map_err(|_| NgError::InvalidKey)?;
vec.reverse();
Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?) Ok(serde_bare::from_slice(&vec).map_err(|_| NgError::InvalidKey)?)
} }

@ -13,9 +13,9 @@ const ng = require("nextgraph");
global.WebSocket = WebSocket; global.WebSocket = WebSocket;
let config = { let config = {
server_peer_id: "ALyGZgFaDDALXLppJZLS2TrMScG0TQIS68RzRcPv99aN", server_peer_id: "FtdzuDYGewfXWdoPuXIPb0wnd0SAg1WoA2B14S7jW3MA",
admin_user_key:"ABI7mSYq1jzulcatewG6ssaFuCjYVVxF_esEmV33oBW4", admin_user_key: "pye0YFzk1ix1amKEwd6AeqaUAN_PNpH5zGLomh0M1PAA",
client_peer_key:"APbhJBuWUUmwZbuYwVm88eJ0b_ZulpSMOlA-9Zwi-S0Q" client_peer_key: "GRP0QnlzaB8o2vdiBaNoOYDNOFX-uehLZMxeCaG3JA0A"
}; };
ng.init_headless(config).then( async() => { ng.init_headless(config).then( async() => {
@ -24,9 +24,9 @@ ng.init_headless(config).then( async() => {
//let user_id = await ng.admin_create_user(config); //let user_id = await ng.admin_create_user(config);
//console.log("user created: ",user_id); //console.log("user created: ",user_id);
let other_user_id = "ACO9_6356BOZdCAwUr9mzTod8Wv-BM7p48sQzGNa2PwI"; let user_id = "tJVG293o6xirl3Ys5rzxMgdnPE_1d3IPAdrlR5qGRAIA";
let session = await ng.session_headless_start(other_user_id); let session = await ng.session_headless_start(user_id);
session_id = session.session_id; session_id = session.session_id;
console.log(session); console.log(session);
@ -39,12 +39,12 @@ ng.init_headless(config).then( async() => {
console.log(q); console.log(q);
} }
await ng.sparql_update(session.session_id, "DELETE DATA { <did:ng:t:AJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:i> <did:ng:j> }"); // await ng.sparql_update(session.session_id, "DELETE DATA { <did:ng:t:AJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:i> <did:ng:j> }");
//await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:t:AJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:i> <did:ng:j> }"); // await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:t:AJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:i> <did:ng:j> }");
//await ng.sparql_update(session.session_id, "INSERT { ?s <did:ng:i> <did:ng:k> } WHERE { ?s <did:ng:i> <did:ng:j> } "); // await ng.sparql_update(session.session_id, "INSERT { ?s <did:ng:i> <did:ng:k> } WHERE { ?s <did:ng:i> <did:ng:j> } ");
//await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:z> <did:ng:j> <did:ng:t:BJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE>. <did:ng:t:BJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:m> <did:ng:n> }"); // await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:z> <did:ng:j> <did:ng:t:BJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE>. <did:ng:t:BJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:m> <did:ng:n> }");
sparql_result = await ng.sparql_query(session.session_id, "SELECT ?a WHERE { ?a <did:ng:j> _:abc. _:abc <did:ng:m> <did:ng:n> }"); sparql_result = await ng.sparql_query(session.session_id, "SELECT ?a WHERE { ?a <did:ng:j> _:abc. _:abc <did:ng:m> <did:ng:n> }");
console.log(sparql_result); console.log(sparql_result);
@ -89,7 +89,7 @@ ng.init_headless(config).then( async() => {
// if you set this to true, the dataset is closed and removed from memory on the server. // if you set this to true, the dataset is closed and removed from memory on the server.
// next time you will open a session for this user, the dataset will be loaded again. // next time you will open a session for this user, the dataset will be loaded again.
let res = await ng.session_headless_stop(session.session_id, false); let res = await ng.session_headless_stop(session.session_id, false);
console.log(res); //console.log(res);
} catch (e) { } catch (e) {
console.error(e); console.error(e);

@ -91,8 +91,7 @@ impl Server {
fn get_wallet(&self, encoded_id: String) -> Result<Response, NgHttpError> { fn get_wallet(&self, encoded_id: String) -> Result<Response, NgHttpError> {
log_debug!("DOWNLOAD wallet {}", encoded_id); log_debug!("DOWNLOAD wallet {}", encoded_id);
let id = base64_url::decode(&encoded_id).map_err(|_e| NgHttpError::InvalidParams)?; let wallet_id: PubKey = decode_key(&encoded_id).map_err(|_e| NgHttpError::InvalidParams)?;
let wallet_id: PubKey = from_slice(&id).map_err(|_e| NgHttpError::InvalidParams)?;
let wallet_record = let wallet_record =
WalletRecord::open(&wallet_id, &self.store).map_err(|_e| NgHttpError::NotFound)?; WalletRecord::open(&wallet_id, &self.store).map_err(|_e| NgHttpError::NotFound)?;
let wallet = wallet_record.wallet().map_err(|_e| NgHttpError::NotFound)?; let wallet = wallet_record.wallet().map_err(|_e| NgHttpError::NotFound)?;

Loading…
Cancel
Save