Skip to content

Commit

Permalink
implemented acquireCredential method in the wasm credential client
Browse files Browse the repository at this point in the history
  • Loading branch information
jstuczyn committed Oct 10, 2023
1 parent 082fd35 commit a85db09
Show file tree
Hide file tree
Showing 29 changed files with 550 additions and 424 deletions.
13 changes: 6 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ url = "2.4"
zeroize = "1.6.0"

# wasm-related dependencies

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = "0.1.7"
gloo-utils = "0.1.7"
js-sys = "0.3.63"
serde-wasm-bindgen = "0.5.0"
Expand Down
6 changes: 5 additions & 1 deletion common/bandwidth-controller/src/acquire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ use std::str::FromStr;

pub mod state;

pub async fn deposit<C>(client: &C, amount: Coin) -> Result<State, BandwidthControllerError>
pub async fn deposit<C>(
client: &C,
amount: impl Into<Coin>,
) -> Result<State, BandwidthControllerError>
where
C: CoconutBandwidthSigningClient + Sync,
{
let mut rng = OsRng;
let signing_keypair = KeyPair::from(identity::KeyPair::new(&mut rng));
let encryption_keypair = KeyPair::from(encryption::KeyPair::new(&mut rng));
let params = Parameters::new(TOTAL_ATTRIBUTES).unwrap();
let amount = amount.into();
let voucher_value = amount.amount.to_string();

let tx_hash = client
Expand Down
16 changes: 8 additions & 8 deletions common/client-libs/validator-client/src/nyxd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum NyxdError {
#[error("{0} is not a valid tx hash")]
InvalidTxHash(String),

#[error("Tendermint RPC request failed - {0}")]
#[error("Tendermint RPC request failed: {0}")]
TendermintErrorRpc(#[from] TendermintRpcError),

#[error("tendermint library failure: {0}")]
Expand All @@ -56,22 +56,22 @@ pub enum NyxdError {
#[error("Failed when attempting to deserialize data ({0})")]
DeserializationError(String),

#[error("Failed when attempting to encode our protobuf data - {0}")]
#[error("Failed when attempting to encode our protobuf data: {0}")]
ProtobufEncodingError(#[from] prost::EncodeError),

#[error("Failed to decode our protobuf data - {0}")]
#[error("Failed to decode our protobuf data: {0}")]
ProtobufDecodingError(#[from] prost::DecodeError),

#[error("Account {0} does not exist on the chain")]
#[error("Account '{0}' does not exist on the chain")]
NonExistentAccountError(AccountId),

#[error("Failed on json serialization/deserialization - {0}")]
#[error("Failed on json serialization/deserialization: {0}")]
SerdeJsonError(#[from] serde_json::Error),

#[error("Account {0} is not a valid account address")]
#[error("Account '{0}' is not a valid account address")]
MalformedAccountAddress(String),

#[error("Account {0} has an invalid associated public key")]
#[error("Account '{0}' has an invalid associated public key")]
InvalidPublicKey(AccountId),

#[error("Queried contract (code_id: {0}) did not have any code information attached")]
Expand All @@ -86,7 +86,7 @@ pub enum NyxdError {
#[error("Block has an invalid height (either negative or larger than i64::MAX")]
InvalidHeight,

#[error("Failed to compress provided wasm code - {0}")]
#[error("Failed to compress provided wasm code: {0}")]
WasmCompressionError(io::Error),

#[error("Logs returned from the validator were malformed")]
Expand Down
3 changes: 2 additions & 1 deletion common/client-libs/validator-client/src/rpc/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ trait TendermintRpcErrorMap {

impl TendermintRpcErrorMap for reqwest::Error {
fn into_rpc_err(self) -> Error {
todo!()
// that's not the best error converion, but it's better than a panic
Error::client_internal(self.to_string())
}
}

Expand Down
7 changes: 3 additions & 4 deletions common/credential-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ edition = "2021"

[dependencies]
async-trait = { workspace = true }

log = { workspace = true }
thiserror = "1.0"
tokio = { version = "1.24.1", features = ["sync"]}
thiserror = { workspace = true }
tokio = { workspace = true, features = ["sync"]}

[target."cfg(not(target_arch = \"wasm32\"))".dependencies.sqlx]
version = "0.5"
Expand All @@ -23,4 +22,4 @@ features = [ "rt-multi-thread", "net", "signal", "fs" ]

[build-dependencies]
sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "sqlite", "macros", "migrate"] }
tokio = { version = "1.24.1", features = ["rt-multi-thread", "macros"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
2 changes: 1 addition & 1 deletion common/credential-storage/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct CoconutCredential {
#[allow(dead_code)]
pub id: i64,
Expand Down
29 changes: 25 additions & 4 deletions common/credentials/src/coconut/bandwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
// right now this has no double-spending protection, spender binding, etc
// it's the simplest possible case

use super::utils::prepare_credential_for_spending;
use crate::error::Error;
use cosmrs::tendermint::hash::Algorithm;
use cosmrs::tendermint::Hash;
use nym_coconut_interface::{
hash_to_scalar, prepare_blind_sign, Attribute, BlindSignRequest, Credential, Parameters,
PrivateAttribute, PublicAttribute, Signature, VerificationKey,
};
use nym_crypto::asymmetric::{encryption, identity};

use super::utils::prepare_credential_for_spending;
use crate::error::Error;
use std::fmt::{self, Debug, Formatter};

pub const PUBLIC_ATTRIBUTES: u32 = 2;
pub const PRIVATE_ATTRIBUTES: u32 = 2;
Expand Down Expand Up @@ -44,6 +44,27 @@ pub struct BandwidthVoucher {
blind_sign_request: BlindSignRequest,
}

impl Debug for BandwidthVoucher {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("BandwidthVoucher")
.field("serial_number", &self.serial_number)
.field("binding_number", &self.binding_number)
.field("voucher_value", &self.voucher_value)
.field("voucher_value_plain", &self.voucher_value_plain)
.field("voucher_info", &self.voucher_info)
.field("voucher_info_plain", &self.voucher_info_plain)
.field("tx_hash", &self.tx_hash)
.field("signing_key", &self.signing_key.to_base58_string())
.field("encryption_key", &self.encryption_key.to_base58_string())
.field(
"pedersen_commitments_openings",
&self.pedersen_commitments_openings,
)
.field("blind_sign_request", &self.blind_sign_request)
.finish()
}
}

impl BandwidthVoucher {
pub fn new(
params: &Parameters,
Expand Down Expand Up @@ -114,7 +135,7 @@ impl BandwidthVoucher {
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() < 32 * 5 + 4 * 8 {
return Err(Error::BandwidthVoucherDeserializationError(format!(
"Less then {} bytes needed",
"Less than {} bytes needed",
32 * 5 + 4 * 8
)));
}
Expand Down
8 changes: 8 additions & 0 deletions common/network-defaults/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ schemars = { version = "0.8", features = ["preserve_order"] }
serde = { workspace = true, features = ["derive"]}
thiserror = { workspace = true }
url = { workspace = true }

# 'wasm-serde-types' feature
tsify = { workspace = true, features = ["js"], optional = true }
wasm-bindgen = { workspace = true, optional = true }

[features]
default = []
wasm-serde-types = ["tsify", "wasm-bindgen"]
Loading

0 comments on commit a85db09

Please sign in to comment.