ghassmo пре 4 година
родитељ
комит
6a1953d801
4 измењених фајлова са 26 додато и 18 уклоњено
  1. 1 0
      script/sql/coins.sql
  2. 5 2
      src/crypto/note.rs
  3. 1 0
      src/tx/builder.rs
  4. 19 16
      src/wallet/walletdb.rs

+ 1 - 0
script/sql/coins.sql

@@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS coins(
 	serial BLOB NOT NULL,
 	coin_blind BLOB NOT NULL,
 	valcom_blind BLOB NOT NULL,
+	token_blind BLOB NOT NULL,
 	value BLOB NOT NULL,
 	network BLOB NOT NULL,
 	drk_address BLOB NOT NULL,

+ 5 - 2
src/crypto/note.rs

@@ -5,14 +5,14 @@ use crate::{
     crypto::{
         diffie_hellman::{kdf_sapling, sapling_ka_agree},
         keypair::{PublicKey, SecretKey},
-        types::*,
+        types::{DrkCoinBlind, DrkSerial, DrkTokenId, DrkValueBlind},
     },
     util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable},
     Error, Result,
 };
 
 /// Plaintext size is serial + value + token_id + coin_blind + value_blind
-pub const NOTE_PLAINTEXT_SIZE: usize = 32 + 8 + 32 + 32 + 32;
+pub const NOTE_PLAINTEXT_SIZE: usize = 32 + 8 + 32 + 32 + 32 + 32;
 pub const AEAD_TAG_SIZE: usize = 16;
 pub const ENC_CIPHERTEXT_SIZE: usize = NOTE_PLAINTEXT_SIZE + AEAD_TAG_SIZE;
 
@@ -23,6 +23,7 @@ pub struct Note {
     pub token_id: DrkTokenId,
     pub coin_blind: DrkCoinBlind,
     pub value_blind: DrkValueBlind,
+    pub token_blind: DrkValueBlind,
 }
 
 impl Note {
@@ -84,6 +85,7 @@ mod tests {
             token_id: DrkTokenId::random(&mut OsRng),
             coin_blind: DrkCoinBlind::random(&mut OsRng),
             value_blind: DrkValueBlind::random(&mut OsRng),
+            token_blind: DrkValueBlind::random(&mut OsRng),
         };
 
         let keypair = Keypair::random(&mut OsRng);
@@ -92,5 +94,6 @@ mod tests {
         let note2 = encrypted_note.decrypt(&keypair.secret).unwrap();
         assert_eq!(note.value, note2.value);
         assert_eq!(note.token_id, note2.token_id);
+        assert_eq!(note.token_blind, note2.token_blind);
     }
 }

+ 1 - 0
src/tx/builder.rs

@@ -147,6 +147,7 @@ impl TransactionBuilder {
                 token_id: output.token_id,
                 coin_blind,
                 value_blind,
+                token_blind,
             };
 
             let encrypted_note = note.encrypt(&output.public)?;

+ 19 - 16
src/wallet/walletdb.rs

@@ -153,7 +153,7 @@ impl WalletDb {
         debug!("Returning default keypair");
         let mut conn = self.conn.acquire().await?;
 
-        let is_default: u32 = 1;
+        let is_default = 1;
 
         let row = sqlx::query("SELECT * FROM keys WHERE is_default = ?1;")
             .bind(is_default)
@@ -267,11 +267,10 @@ impl WalletDb {
             let serial = deserialize(row.get("serial"))?;
             let coin_blind = deserialize(row.get("coin_blind"))?;
             let value_blind = deserialize(row.get("valcom_blind"))?;
-            // TODO: FIXME:
-            let value_bytes: Vec<u8> = row.get("value");
-            let value = u64::from_le_bytes(value_bytes.try_into().unwrap());
+            let value = deserialize(row.get("value"))?;
             let token_id = deserialize(row.get("drk_address"))?;
-            let note = Note { serial, value, token_id, coin_blind, value_blind };
+            let token_blind = deserialize(row.get("token_blind"))?;
+            let note = Note { serial, value, token_id, coin_blind, value_blind, token_blind };
 
             let secret = deserialize(row.get("secret"))?;
             let nullifier = deserialize(row.get("nullifier"))?;
@@ -296,14 +295,16 @@ impl WalletDb {
         let serial = serialize(&own_coin.note.serial);
         let coin_blind = serialize(&own_coin.note.coin_blind);
         let value_blind = serialize(&own_coin.note.value_blind);
-        let value = own_coin.note.value.to_le_bytes();
+        let token_blind = serialize(&own_coin.note.token_blind);
+        let value = serialize(&own_coin.note.value);
         let drk_address = serialize(&own_coin.note.token_id);
         let secret = serialize(&own_coin.secret);
         let nullifier = serialize(&own_coin.nullifier);
         let leaf_position = serialize(&own_coin.leaf_position);
-        let is_spent: u32 = 0;
+        let is_spent: u8 = 0;
 
         let token_id_enc = bs58::encode(&own_coin.note.token_id.to_repr()).into_string();
+
         let (network, net_address) =
             if let Some((network, token_info)) = tokenlist.by_addr.get(&token_id_enc) {
                 (network, token_info.net_address.clone())
@@ -312,21 +313,24 @@ impl WalletDb {
                 (&NetworkName::DarkFi, "unknown".to_string())
             };
 
+        let network = serialize(network);
+
         let mut conn = self.conn.acquire().await?;
         sqlx::query(
             "INSERT OR REPLACE INTO coins
-            (coin, serial, coin_blind, valcom_blind, value,
+            (coin, serial, coin_blind, valcom_blind, token_blind, value,
              network, drk_address, net_address,
              secret, is_spent, nullifier, leaf_position)
             VALUES
-             (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12);",
+             (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13);",
         )
         .bind(coin)
         .bind(serial)
         .bind(coin_blind)
         .bind(value_blind)
-        .bind(value.to_vec())
-        .bind(serialize(network))
+        .bind(token_blind)
+        .bind(value)
+        .bind(network)
         .bind(drk_address) // token_id
         .bind(net_address)
         .bind(secret)
@@ -348,7 +352,7 @@ impl WalletDb {
 
     pub async fn confirm_spend_coin(&self, coin: &Coin) -> Result<()> {
         debug!("Confirm spend coin");
-        let is_spent: u32 = 1;
+        let is_spent = 1;
         let coin = serialize(coin);
 
         let mut conn = self.conn.acquire().await?;
@@ -376,9 +380,7 @@ impl WalletDb {
 
         let mut list = vec![];
         for row in rows {
-            // TODO: FIXME:
-            let value_bytes: Vec<u8> = row.get("value");
-            let value = u64::from_le_bytes(value_bytes.try_into().unwrap());
+            let value = deserialize(row.get("value"))?;
             let token_id = deserialize(row.get("drk_address"))?;
             let nullifier = deserialize(row.get("nullifier"))?;
             list.push(Balance { token_id, value, nullifier });
@@ -409,7 +411,7 @@ impl WalletDb {
     pub async fn token_id_exists(&self, token_id: DrkTokenId) -> Result<bool> {
         debug!("Checking if token ID exists");
 
-        let is_spent: u32 = 0;
+        let is_spent = 0;
         let id = serialize(&token_id);
 
         let mut conn = self.conn.acquire().await?;
@@ -453,6 +455,7 @@ mod tests {
             token_id: *t,
             coin_blind: DrkCoinBlind::random(&mut OsRng),
             value_blind: DrkValueBlind::random(&mut OsRng),
+            token_blind: DrkValueBlind::random(&mut OsRng),
         };
 
         let coin = Coin(pallas::Base::random(&mut OsRng));