Explorar o código

Merge branch 'master' of github.com:darkrenaissance/darkfi

ghassmo %!s(int64=4) %!d(string=hai) anos
pai
achega
564708643e
Modificáronse 3 ficheiros con 102 adicións e 11 borrados
  1. 38 1
      script/research/bltprf/poly.sage
  2. 61 9
      src/bin/darkfid.rs
  3. 3 1
      todo.md

+ 38 - 1
script/research/bltprf/poly.sage

@@ -19,6 +19,7 @@ x = F(88)
 px = (F(110) + F(56) * X + F(89) * X^2 + F(6543) * X^3
 px = (F(110) + F(56) * X + F(89) * X^2 + F(6543) * X^3
       + F(2) * X^4 + F(110) * X^5 + F(44) * X^6 + F(78) * X^7)
       + F(2) * X^4 + F(110) * X^5 + F(44) * X^6 + F(78) * X^7)
 assert px.degree() <= n
 assert px.degree() <= n
+v = px(x)
 
 
 base_G = [E.random_element(), E.random_element(), E.random_element(),
 base_G = [E.random_element(), E.random_element(), E.random_element(),
           E.random_element(), E.random_element(), E.random_element(),
           E.random_element(), E.random_element(), E.random_element(),
@@ -28,7 +29,7 @@ base_U = E.random_element()
 
 
 # Make the initial commitment to px
 # Make the initial commitment to px
 blind = F.random_element()
 blind = F.random_element()
-C = int(blind) * base_H + sum(int(k) * G for k, G in zip(px, base_G))
+P = int(blind) * base_H + sum(int(k) * G for k, G in zip(px, base_G))
 
 
 # Dot product
 # Dot product
 def dot(x, y):
 def dot(x, y):
@@ -181,4 +182,40 @@ blind += r_randomness_1 * challenge_1
 # Finished looping
 # Finished looping
 assert len(a_1) == 1
 assert len(a_1) == 1
 a = a_1[0]
 a = a_1[0]
+assert len(G_1) == 1
+G = G_1[0]
+
+# Verify
+
+# This is a table of how often the challenges appear in G_1, G_2, ...
+# as well as a and b (applies equally)
+#
+#              12345678
+# challenge 3: 00001111
+# challenge 2: 00110011
+# challenge 1: 01010101
+#
+s_1 = F(1)
+s_2 = challenge_1
+s_3 = challenge_2
+s_4 = challenge_1 * challenge_2
+s_5 = challenge_3
+s_6 = challenge_1 * challenge_3
+s_7 = challenge_2 * challenge_3
+s_8 = challenge_1 * challenge_2 * challenge_3
+
+s = (s_1, s_2, s_3, s_4, s_5, s_6, s_7, s_8)
+
+# Verifier can recompute the final G value by doing this calc
+assert G == dot(s, base_G)
+assert a == dot([s_i^-1 for s_i in s], list(final_poly))
+assert b_1[0] == dot(s, [x^i for i in range(n)])
+b = b_1[0]
+
+msm = (P - int(v) * base_G[0] + int(iota) * s_poly_commitment
+    + int(challenge_1^-1) * l_1 + int(challenge_1) * r_1
+    + int(challenge_2^-1) * l_2 + int(challenge_2) * r_2
+    + int(challenge_3^-1) * l_3 + int(challenge_3) * r_3)
+rhs = int(a) * (G + int(b * z) * base_U) + int(blind) * base_H
+assert msm == rhs
 
 

+ 61 - 9
src/bin/darkfid.rs

@@ -128,20 +128,29 @@ impl Darkfid {
             return JsonResult::Err(jsonerr(InvalidParams, None, id));
             return JsonResult::Err(jsonerr(InvalidParams, None, id));
         };
         };
 
 
-        let symbol = symbol.as_str().unwrap().to_uppercase();
+        let symbol = symbol.as_str().unwrap();
 
 
+        let token_id = self.search_id(&symbol);
+        return JsonResult::Resp(jsonresp(json!(token_id), id));
+    }
+
+    // TODO: proper error handling here
+    fn search_id(self, symbol: &str) -> Value {
+        debug!(target: "DARKFID", "SEARCHING FOR {}", symbol);
         let file_contents =
         let file_contents =
             fs::read_to_string("token/solanatokenlist.json").expect("Can't find tokenlist file");
             fs::read_to_string("token/solanatokenlist.json").expect("Can't find tokenlist file");
-        let root: Value = serde_json::from_str(&file_contents).unwrap();
-        let tokens = root["tokens"].as_array().unwrap();
-
+        let root: Value =
+            serde_json::from_str(&file_contents).expect("Can't parse file into valid json");
+        let tokens = root["tokens"]
+            .as_array()
+            .expect("Can't find 'tokens' in file");
         for item in tokens {
         for item in tokens {
-            if item["symbol"] == symbol {
-                let address = &item["address"];
-                return JsonResult::Resp(jsonresp(json!(address), id));
+            if item["symbol"] == symbol.to_uppercase() {
+                let address = item["address"].clone();
+                return address;
             }
             }
         }
         }
-        return JsonResult::Err(jsonerr(InvalidParams, None, id));
+        unreachable!();
     }
     }
 
 
     // --> {"jsonrpc": "2.0", "method": "features", "params": [], "id": 42}
     // --> {"jsonrpc": "2.0", "method": "features", "params": [], "id": 42}
@@ -181,7 +190,13 @@ impl Darkfid {
 
 
         if token.as_str().is_none() {
         if token.as_str().is_none() {
             return JsonResult::Err(jsonerr(InvalidParams, None, id));
             return JsonResult::Err(jsonerr(InvalidParams, None, id));
-        };
+        }
+
+        let tkn_str = token.as_str().unwrap();
+
+        // check if the token input is an ID
+        // if not, find the associated ID
+        let _token_id = self.clone().parse_token(tkn_str);
 
 
         // TODO: Optional sanity checking here, but cashier *must* do so too.
         // TODO: Optional sanity checking here, but cashier *must* do so too.
 
 
@@ -215,6 +230,23 @@ impl Darkfid {
         }
         }
     }
     }
 
 
+    fn parse_token(self, token: &str) -> Value {
+        let vec: Vec<char> = token.chars().collect();
+        let mut counter = 0;
+        for c in vec {
+            if c.is_alphabetic() {
+                counter += 1;
+            }
+        }
+        if counter == token.len() {
+            let token_id = self.search_id(token);
+            return token_id;
+        } else {
+            let token_id: Value = serde_json::from_str(token).unwrap();
+            return token_id;
+        }
+    }
+
     // --> {"method": "withdraw", "params": [network, token, publickey, amount]}
     // --> {"method": "withdraw", "params": [network, token, publickey, amount]}
     // The publickey sent here is the address where the caller wants to receive
     // The publickey sent here is the address where the caller wants to receive
     // the tokens they plan to withdraw.
     // the tokens they plan to withdraw.
@@ -340,3 +372,23 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
         });
         });
     }
     }
 }
 }
+
+mod tests {
+
+    #[test]
+    fn test_token_parsing() {
+        let token = "usdc";
+
+        let vec: Vec<char> = token.chars().collect();
+        let mut counter = 0;
+        for c in vec {
+            if c.is_alphabetic() {
+                counter += 1;
+                println!("Found letter: {}", c)
+            }
+        }
+        if counter == token.len() {
+            println!("Every character is a letter");
+        }
+    }
+}

+ 3 - 1
todo.md

@@ -16,6 +16,7 @@
 - [ ] drk2: check user input is valid tokenID and not symbol
 - [ ] drk2: check user input is valid tokenID and not symbol
 - [ ] drk2: retrieve cashier features and error if don't support the network
 - [ ] drk2: retrieve cashier features and error if don't support the network
 - [ ] standardize error handling in drk2, cashierd, darkfid
 - [ ] standardize error handling in drk2, cashierd, darkfid
+- [ ] load token/solanatokenlist.json into global variable in darkfid
 
 
 ## deposit
 ## deposit
 
 
@@ -24,6 +25,7 @@
 ## bridge
 ## bridge
 
 
 - [ ] implement listen function
 - [ ] implement listen function
+- [ ] solana: Check if given token mint is valid and initialized on deposito/withdraw
 
 
 ## withdraw
 ## withdraw
 
 
@@ -64,7 +66,7 @@ Open research questions.
 - [ ] first MPC services
 - [ ] first MPC services
 - [ ] DAO
 - [ ] DAO
 - [ ] auctions
 - [ ] auctions
-- [ ] staking. Look up how TORN was distributed anonymously.
+- [x] staking. Look up how TORN was distributed anonymously.
 - [ ] swaps
 - [ ] swaps
 - [ ] token issuance
 - [ ] token issuance
 - [ ] NFTs
 - [ ] NFTs