-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add encrypted_value to the UnblindedOutput (#4142)
Description --- The PR adds extra encrypted field to `UnblindedOutput` structs. What was done: - Added `EncryptedValue` struct - Implemented `ConsensusEncode`, `ConsensusDecode` and `ByteArray` for it - Added the `encrypted_value` field of that type to the `UnblindedOutput` struct - Added an extra parameter to metadata signature calculation methods - Added an extra field to the `GRPC` protocol (`UnblindedOutput` type) - Added an extra column and migrations for the `outputs` table - The types `OutputSql` and `NewOutputSql` updated to store and restore the `EncryptedValue` Important notes: 1. The value of `encrypted_value` passed as a parameter to the metadata signature evaluation but has not taken into account yet, because it will break the verification process of the `TransactionOutput` and we can complete that when the same `encrypted_value` field will be added to the `TransactionOutput` struct in the further PRs. 2. Real encryption process is not implemented yet. Instead of that I've added the method `todo_encrypt_from` that I used to mark the places where we have to put an encryption service and call it. Motivation and Context --- Add an encrypted amount to the `TransactionOutput`. How Has This Been Tested? --- CI
- Loading branch information
1 parent
5cc9a40
commit f79d383
Showing
26 changed files
with
269 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
base_layer/core/src/transactions/transaction_components/encrypted_value.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2022 The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE | ||
// | ||
// Portions of this file were originally copyrighted (c) 2018 The Grin Developers, issued under the Apache License, | ||
// Version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0. | ||
|
||
use std::io::{self, Read, Write}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use tari_utilities::{ByteArray, ByteArrayError}; | ||
|
||
use crate::consensus::{ConsensusDecoding, ConsensusEncoding}; | ||
|
||
const SIZE: usize = 24; | ||
|
||
#[derive(Debug, Clone, Deserialize, Serialize)] | ||
pub struct EncryptedValue { | ||
/// value: u64 + tag: [u8; 16] | ||
pub data: [u8; SIZE], | ||
} | ||
|
||
impl Default for EncryptedValue { | ||
fn default() -> Self { | ||
Self { data: [0; SIZE] } | ||
} | ||
} | ||
|
||
impl ByteArray for EncryptedValue { | ||
fn from_bytes(bytes: &[u8]) -> Result<Self, ByteArrayError> { | ||
if bytes.len() == SIZE { | ||
let mut this = Self::default(); | ||
this.data.copy_from_slice(bytes); | ||
Ok(this) | ||
} else { | ||
Err(ByteArrayError::IncorrectLength) | ||
} | ||
} | ||
|
||
fn as_bytes(&self) -> &[u8] { | ||
&self.data | ||
} | ||
} | ||
|
||
impl EncryptedValue { | ||
/// TODO: Replace this method with a real call of encryption service | ||
/// that will produce an encrypted value from the given `amount`. | ||
pub fn todo_encrypt_from(amount: impl Into<u64>) -> Self { | ||
let mut data: [u8; SIZE] = [0; SIZE]; | ||
let value = amount.into().to_le_bytes(); | ||
data[0..8].copy_from_slice(&value); | ||
Self { data } | ||
} | ||
} | ||
|
||
impl ConsensusEncoding for EncryptedValue { | ||
fn consensus_encode<W: Write>(&self, writer: &mut W) -> Result<(), io::Error> { | ||
self.data.consensus_encode(writer)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl ConsensusDecoding for EncryptedValue { | ||
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> { | ||
let data = <[u8; 24]>::consensus_decode(reader)?; | ||
Ok(Self { data }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.