Skip to content

Commit

Permalink
Add arithmetic types, plaintext, and conversion to JS objects
Browse files Browse the repository at this point in the history
  • Loading branch information
iamalwaysuncomfortable committed Nov 26, 2024
1 parent 733b3c7 commit ce326d1
Show file tree
Hide file tree
Showing 31 changed files with 1,376 additions and 90 deletions.
9 changes: 8 additions & 1 deletion wasm/src/account/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use crate::account::{PrivateKey, Signature, ViewKey};

use crate::types::native::AddressNative;
use crate::{account::compute_key::ComputeKey, types::native::AddressNative};
use core::{convert::TryFrom, fmt, ops::Deref, str::FromStr};
use wasm_bindgen::prelude::*;

Expand All @@ -43,6 +43,13 @@ impl Address {
Self(AddressNative::try_from(**view_key).unwrap())
}

/// Derive an Aleo address from a compute key.
///
/// @param {ComputeKey} compute_key The compute key to derive the address from
pub fn from_compute_key(compute_key: &ComputeKey) -> Self {
compute_key.address()
}

/// Create an aleo address object from a string representation of an address
///
/// @param {string} address String representation of an addressm
Expand Down
97 changes: 97 additions & 0 deletions wasm/src/account/compute_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Aleo SDK library.

// The Aleo SDK library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Aleo SDK library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.

use super::PrivateKey;
use crate::{
Address,
types::{Group, Scalar, native::ComputeKeyNative},
};

use core::{convert::TryFrom, ops::Deref};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComputeKey(ComputeKeyNative);

#[wasm_bindgen]
impl ComputeKey {
/// Create a new compute key from a private key.
///
/// @param {PrivateKey} private_key Private key
/// @returns {ComputeKey} Compute key
pub fn from_private_key(private_key: &PrivateKey) -> Self {
Self(ComputeKeyNative::try_from(**private_key).unwrap())
}

/// Get the address from the compute key.
///
/// @returns {Address}
pub fn address(&self) -> Address {
Address::from(self.0.to_address())
}

/// Get the sk_prf of the compute key.
pub fn sk_prf(&self) -> Scalar {
Scalar::from(self.0.sk_prf())
}

/// Get the pr_tag of the compute key.
///
/// @returns {Group} pr_tag
pub fn pk_sig(&self) -> Group {
Group::from(self.0.pk_sig())
}

/// Get the pr_sig of the compute key.
///
/// @returns {Group} pr_sig
pub fn pr_sig(&self) -> Group {
Group::from(self.0.pr_sig())
}
}

impl Deref for ComputeKey {
type Target = ComputeKeyNative;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<ComputeKeyNative> for ComputeKey {
fn from(compute_key: ComputeKeyNative) -> Self {
Self(compute_key)
}
}

impl From<ComputeKey> for ComputeKeyNative {
fn from(compute_key: ComputeKey) -> Self {
compute_key.0
}
}

impl From<&ComputeKey> for ComputeKeyNative {
fn from(compute_key: &ComputeKey) -> Self {
compute_key.0.clone()
}
}

impl From<&ComputeKeyNative> for ComputeKey {
fn from(compute_key: &ComputeKeyNative) -> Self {
Self(compute_key.clone())
}
}
103 changes: 103 additions & 0 deletions wasm/src/account/graph_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Aleo SDK library.

// The Aleo SDK library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Aleo SDK library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.

use super::ViewKey;
use crate::types::{Field, native::GraphKeyNative};

use core::{convert::TryFrom, fmt, ops::Deref, str::FromStr};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GraphKey(GraphKeyNative);

#[wasm_bindgen]
impl GraphKey {
/// Create a new graph key from a view key.
///
/// @param {ViewKey} view_key View key
/// @returns {GraphKey} Graph key
pub fn from_view_key(view_key: &ViewKey) -> Self {
Self::from(GraphKeyNative::try_from(**view_key).unwrap())
}

/// Create a new graph key from a string representation of a graph key
///
/// @param {string} graph_key String representation of a graph key
/// @returns {GraphKey} Graph key
pub fn from_string(graph_key: &str) -> Self {
Self::from_str(graph_key).unwrap()
}

/// Get a string representation of a graph key
///
/// @returns {string} String representation of a graph key
#[allow(clippy::inherent_to_string_shadow_display)]
pub fn to_string(&self) -> String {
self.0.to_string()
}

/// Get the sk_tag of the graph key. Used to determine ownership of records.
pub fn sk_tag(&self) -> Field {
Field::from(self.0.sk_tag())
}
}

impl FromStr for GraphKey {
type Err = anyhow::Error;

fn from_str(graph_key: &str) -> Result<Self, Self::Err> {
Ok(Self(GraphKeyNative::from_str(graph_key)?))
}
}

impl Deref for GraphKey {
type Target = GraphKeyNative;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl fmt::Display for GraphKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<GraphKeyNative> for GraphKey {
fn from(value: GraphKeyNative) -> Self {
Self(value)
}
}

impl From<GraphKey> for GraphKeyNative {
fn from(value: GraphKey) -> Self {
value.0
}
}

impl From<&GraphKey> for GraphKeyNative {
fn from(value: &GraphKey) -> Self {
value.0.clone()
}
}

impl From<&GraphKeyNative> for GraphKey {
fn from(value: &GraphKeyNative) -> Self {
Self(value.clone())
}
}
3 changes: 3 additions & 0 deletions wasm/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ pub use private_key_ciphertext::*;
pub mod signature;
pub use signature::*;

mod compute_key;
mod graph_key;
pub mod view_key;

pub use view_key::*;
2 changes: 1 addition & 1 deletion wasm/src/account/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::account::{Address, Encryptor, PrivateKeyCiphertext, Signature, ViewKe

use crate::types::native::{CurrentNetwork, Environment, FromBytes, PrimeField, PrivateKeyNative, ToBytes};
use core::{convert::TryInto, fmt, ops::Deref, str::FromStr};
use rand::{rngs::StdRng, SeedableRng};
use rand::{SeedableRng, rngs::StdRng};
use wasm_bindgen::prelude::*;

/// Private key of an Aleo account
Expand Down
21 changes: 18 additions & 3 deletions wasm/src/account/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

use crate::account::{Address, PrivateKey};

use crate::types::native::SignatureNative;
use crate::types::{Scalar, native::SignatureNative};
use core::{fmt, ops::Deref, str::FromStr};
use rand::{rngs::StdRng, SeedableRng};
use rand::{SeedableRng, rngs::StdRng};
use wasm_bindgen::prelude::*;

/// Cryptographic signature of a message signed by an Aleo account
Expand All @@ -36,6 +36,21 @@ impl Signature {
Self(SignatureNative::sign_bytes(private_key, message, &mut StdRng::from_entropy()).unwrap())
}

/// Get an address from a signature.
pub fn to_address(&self) -> Address {
Address::from(self.0.to_address())
}

/// Get the challenge of a signature.
pub fn challenge(&self) -> Scalar {
Scalar::from(self.0.challenge())
}

/// Get the response of a signature.
pub fn response(&self) -> Scalar {
Scalar::from(self.0.response())
}

/// Verify a signature of a message with an address
///
/// @param {Address} address The address to verify the signature with
Expand Down Expand Up @@ -88,7 +103,7 @@ impl Deref for Signature {
mod tests {
use super::*;

use rand::{rngs::StdRng, Rng, SeedableRng};
use rand::{Rng, SeedableRng, rngs::StdRng};
use wasm_bindgen_test::*;

const ITERATIONS: u64 = 1_000;
Expand Down
22 changes: 22 additions & 0 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ extern "C" {
pub fn log(s: &str);
}

#[macro_export]
macro_rules! array {
($($value:expr),*$(,)?) => {{
let array = Array::new();

$(array.push(&JsValue::from($value));)*

array
}};
}

#[macro_export]
macro_rules! object {
($($key:literal: $value:expr,)*) => {{
let object = Object::new();

$(Reflect::set(&object, &JsValue::from_str($key), &JsValue::from($value)).unwrap();)*

object
}};
}

/// A trait providing convenient methods for accessing the amount of Aleo present in a record
pub trait Credits {
/// Get the amount of credits in the record if the record possesses Aleo credits
Expand Down
18 changes: 18 additions & 0 deletions wasm/src/programs/data/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Aleo SDK library.

// The Aleo SDK library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Aleo SDK library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.

mod plaintext;
pub use plaintext::Plaintext;
Loading

0 comments on commit ce326d1

Please sign in to comment.