-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf43a4b
commit 3764710
Showing
12 changed files
with
281 additions
and
235 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use core::{mem::MaybeUninit, ptr::addr_of_mut}; | ||
|
||
use arrayref::array_ref; | ||
use jubjub::{AffinePoint, Scalar}; | ||
use nom::{bytes::complete::take, number::complete::le_u64}; | ||
|
||
use crate::{ | ||
crypto::{decrypt, read_scalar}, | ||
ironfish::{errors::IronfishError, public_address::PublicAddress}, | ||
parser::AssetIdentifier, | ||
parser::FromBytes, | ||
}; | ||
|
||
use super::{memo::Memo, ParserError, ENCRYPTED_NOTE_SIZE, MAC_SIZE}; | ||
|
||
/// A note (think bank note) represents a value in the owner's "account". | ||
/// When spending, proof that the note exists in the tree needs to be provided, | ||
/// along with a nullifier key that is made public so the owner cannot attempt | ||
/// to spend that note again.447903 | ||
/// | ||
/// When receiving funds, a new note needs to be created for the new owner | ||
/// to hold those funds. | ||
pub struct Note { | ||
/// Asset identifier the note is associated with | ||
pub(crate) asset_id: AssetIdentifier, | ||
|
||
/// A public address for the owner of the note. | ||
pub(crate) owner: PublicAddress, | ||
|
||
/// Value this note represents. | ||
pub(crate) value: u64, | ||
|
||
/// A random value generated when the note is constructed. | ||
/// This helps create zero knowledge around the note, | ||
/// allowing the owner to prove they have the note without revealing | ||
/// anything else about it. | ||
pub(crate) randomness: jubjub::Fr, | ||
|
||
/// Arbitrary note the spender can supply when constructing a spend so the | ||
/// receiver has some record from whence it came. | ||
/// Note: While this is encrypted with the output, it is not encoded into | ||
/// the proof in any way. | ||
pub(crate) memo: Memo, | ||
|
||
/// A public address for the sender of the note. | ||
pub(crate) sender: PublicAddress, | ||
} | ||
|
||
impl Note { | ||
/// Create a note from its encrypted representation, given the spender's | ||
/// view key. | ||
/// | ||
/// The note is stored on the [`crate::outputs::OutputDescription`] in | ||
/// encrypted form. The spender encrypts it when they construct the output | ||
/// using a shared secret derived from the owner's public key. | ||
/// | ||
/// This function allows the owner to decrypt the note using the derived | ||
/// shared secret and their own view key. | ||
#[inline(never)] | ||
pub(crate) fn from_spender_encrypted( | ||
// public_address: SubgroupPoint, | ||
public_address: AffinePoint, | ||
shared_secret: &[u8; 32], | ||
encrypted_bytes: &[u8; ENCRYPTED_NOTE_SIZE + MAC_SIZE], | ||
) -> Result<Self, IronfishError> { | ||
let mut this = MaybeUninit::uninit(); | ||
Note::decrypt_note_parts(shared_secret, encrypted_bytes, &mut this) | ||
.map_err(|_| IronfishError::FailedXChaCha20Poly1305Decryption)?; | ||
|
||
let owner = PublicAddress(public_address); | ||
|
||
let out = this.as_mut_ptr(); | ||
unsafe { | ||
addr_of_mut!((*out).owner).write(owner); | ||
} | ||
|
||
Ok(unsafe { this.assume_init() }) | ||
} | ||
|
||
#[inline(never)] | ||
fn decrypt_note_parts( | ||
shared_secret: &[u8; 32], | ||
encrypted_bytes: &[u8; ENCRYPTED_NOTE_SIZE + MAC_SIZE], | ||
out: &mut MaybeUninit<Self>, | ||
) -> Result<(), ParserError> { | ||
let out = out.as_mut_ptr(); | ||
|
||
let plaintext_bytes: [u8; ENCRYPTED_NOTE_SIZE] = | ||
decrypt(shared_secret, encrypted_bytes).map_err(|_| ParserError::UnexpectedError)?; | ||
|
||
// Fr | ||
let (rem, randomness) = read_scalar(&plaintext_bytes[..])?; | ||
|
||
let (rem, value) = le_u64(rem)?; | ||
|
||
// Memo | ||
let memo = unsafe { &mut *addr_of_mut!((*out).memo).cast() }; | ||
let rem = Memo::from_bytes_into(rem, memo)?; | ||
|
||
// Asset Identifier | ||
let asset_id = unsafe { &mut *addr_of_mut!((*out).asset_id).cast() }; | ||
let rem = AssetIdentifier::from_bytes_into(rem, asset_id)?; | ||
let asset_id = unsafe { asset_id.assume_init() }; | ||
|
||
// PublicAddress | ||
let sender = unsafe { &mut *addr_of_mut!((*out).sender).cast() }; | ||
let rem = PublicAddress::from_bytes_into(rem, sender)?; | ||
|
||
unsafe { | ||
addr_of_mut!((*out).randomness).write(randomness); | ||
addr_of_mut!((*out).value).write(value); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/******************************************************************************* | ||
* (c) 2018 - 2024 Zondax AG | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
********************************************************************************/ | ||
#include "parser_utils.h" | ||
|
||
#include "coin.h" | ||
#include "crypto_helper.h" | ||
#include "zxformat.h" | ||
#include "zxmacros.h" | ||
|
||
parser_error_t _toStringBalance(int64_t *amount, uint8_t decimalPlaces, const char postfix[], const char prefix[], | ||
char *outValue, uint16_t outValueLen, uint8_t pageIdx, uint8_t *pageCount) { | ||
char bufferUI[200] = {0}; | ||
if (int64_to_str(bufferUI, sizeof(bufferUI), *amount) != NULL) { | ||
return parser_unexpected_value; | ||
} | ||
|
||
if (intstr_to_fpstr_inplace(bufferUI, sizeof(bufferUI), decimalPlaces) == 0) { | ||
return parser_unexpected_value; | ||
} | ||
|
||
if (z_str3join(bufferUI, sizeof(bufferUI), prefix, postfix) != zxerr_ok) { | ||
return parser_unexpected_buffer_end; | ||
} | ||
|
||
number_inplace_trimming(bufferUI, 1); | ||
|
||
pageString(outValue, outValueLen, bufferUI, pageIdx, pageCount); | ||
return parser_ok; | ||
} |
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,31 @@ | ||
/******************************************************************************* | ||
* (c) 2018 - 2024 Zondax AG | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
********************************************************************************/ | ||
#pragma once | ||
|
||
#include "parser_common.h" | ||
#include "parser_txdef.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
parser_error_t _toStringBalance(int64_t *amount, uint8_t decimalPlaces, const char postfix[], const char prefix[], | ||
char *outValue, uint16_t outValueLen, uint8_t pageIdx, uint8_t *pageCount); | ||
|
||
#ifdef __cplusplus | ||
} | ||
|
||
#endif |
Oops, something went wrong.