Skip to content

Commit

Permalink
Fixed wrapper signing and added the writing module
Browse files Browse the repository at this point in the history
Author:    satan <[email protected]>
Date:      Wed Jan 3 16:26:52 2024 +0100
  • Loading branch information
batconjurer authored and grarco committed Jan 15, 2024
1 parent b951476 commit f77e339
Show file tree
Hide file tree
Showing 13 changed files with 826 additions and 84 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion light_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ namada_core = {path = "../core"}
namada_sdk = {path = "../sdk"}
prost.workspace = true
tendermint-config.workspace = true
tendermint-rpc = {worskpace = true, features = ["http-client"]}
tendermint-rpc = { workspace = true, features = ["http-client"] }
tokio = {workspace = true, features = ["rt"]}
serde_json = "1.0.108"
26 changes: 25 additions & 1 deletion light_sdk/src/reading/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use namada_core::ledger::storage::LastBlock;
use namada_core::types::address::Address;
use namada_core::types::storage::BlockResults;
use namada_core::types::token;
use namada_sdk::error::Error;
use namada_core::types::token::DenominatedAmount;
use namada_sdk::error::{EncodingError, Error};
use namada_sdk::io::StdIo;
use namada_sdk::queries::RPC;
use namada_sdk::rpc;
use tendermint_config::net::Address as TendermintAddress;
Expand Down Expand Up @@ -51,3 +53,25 @@ pub fn query_results(
let rt = Runtime::new().unwrap();
rt.block_on(rpc::query_results(&client))
}

/// Get a properly denominated amount of a token
pub fn denominate_amount(
tendermint_addr: &str,
amount: u64,
token: &str,
) -> Result<DenominatedAmount, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
let token = Address::decode(token)
.map_err(|e| Error::Encode(EncodingError::Decoding(e.to_string())))?;
let rt = Runtime::new().unwrap();
Ok(rt.block_on(rpc::denominate_amount(
&client,
&StdIo {},
&token,
token::Amount::from(amount),
)))
}
115 changes: 113 additions & 2 deletions light_sdk/src/transaction/account.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use namada_core::proto::Tx;
use namada_core::proto::{Signature, Tx, TxError};
use namada_core::types::address::Address;
use namada_core::types::hash::Hash;
use namada_core::types::key::common;
use namada_core::types::storage::Epoch;
use namada_core::types::token::DenominatedAmount;
use namada_core::types::transaction::GasLimit;

use super::GlobalArgs;
use super::{attach_fee, attach_fee_signature, GlobalArgs};
use crate::transaction;

const TX_INIT_ACCOUNT_WASM: &str = "tx_init_account.wasm";
Expand Down Expand Up @@ -51,10 +54,46 @@ impl InitAccount {
))
}

/// Attach the fee data to the tx
pub fn attach_fee(
self,
fee: DenominatedAmount,
token: Address,
fee_payer: common::PublicKey,
epoch: Epoch,
gas_limit: GasLimit,
) -> Self {
Self(attach_fee(self.0, fee, token, fee_payer, epoch, gas_limit))
}

/// Get the bytes of the fee data to sign
pub fn get_fee_sig_bytes(&self) -> Hash {
transaction::get_wrapper_sign_bytes(&self.0)
}

/// Attach a signature of the fee to the tx
pub fn attach_fee_signature(
self,
signer: common::PublicKey,
signature: common::Signature,
) -> Self {
Self(attach_fee_signature(self.0, signer, signature))
}

/// Generates the protobuf encoding of this transaction
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}

/// Gets the inner transaction without the domain wrapper
pub fn payload(self) -> Tx {
self.0
}

/// Validate this wrapper transaction
pub fn validate_tx(&self) -> Result<Option<&Signature>, TxError> {
self.0.validate_tx()
}
}

/// Transaction to reveal a public key to the ledger to validate signatures of
Expand Down Expand Up @@ -87,10 +126,46 @@ impl RevealPk {
))
}

/// Attach the fee data to the tx
pub fn attach_fee(
self,
fee: DenominatedAmount,
token: Address,
fee_payer: common::PublicKey,
epoch: Epoch,
gas_limit: GasLimit,
) -> Self {
Self(attach_fee(self.0, fee, token, fee_payer, epoch, gas_limit))
}

/// Get the bytes of the fee data to sign
pub fn get_fee_sig_bytes(&self) -> Hash {
transaction::get_wrapper_sign_bytes(&self.0)
}

/// Attach a signature of the fee to the tx
pub fn attach_fee_signature(
self,
signer: common::PublicKey,
signature: common::Signature,
) -> Self {
Self(attach_fee_signature(self.0, signer, signature))
}

/// Generates the protobuf encoding of this transaction
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}

/// Gets the inner transaction without the domain wrapper
pub fn payload(self) -> Tx {
self.0
}

/// Validate this wrapper transaction
pub fn validate_tx(&self) -> Result<Option<&Signature>, TxError> {
self.0.validate_tx()
}
}

/// Transaction to update the parameters of an established account
Expand Down Expand Up @@ -136,8 +211,44 @@ impl UpdateAccount {
))
}

/// Attach the fee data to the tx
pub fn attach_fee(
self,
fee: DenominatedAmount,
token: Address,
fee_payer: common::PublicKey,
epoch: Epoch,
gas_limit: GasLimit,
) -> Self {
Self(attach_fee(self.0, fee, token, fee_payer, epoch, gas_limit))
}

/// Get the bytes of the fee data to sign
pub fn get_fee_sig_bytes(&self) -> Hash {
transaction::get_wrapper_sign_bytes(&self.0)
}

/// Attach a signature of the fee to the tx
pub fn attach_fee_signature(
self,
signer: common::PublicKey,
signature: common::Signature,
) -> Self {
Self(attach_fee_signature(self.0, signer, signature))
}

/// Generates the protobuf encoding of this transaction
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}

/// Gets the inner transaction without the domain wrapper
pub fn payload(self) -> Tx {
self.0
}

/// Validate this wrapper transaction
pub fn validate_tx(&self) -> Result<Option<&Signature>, TxError> {
self.0.validate_tx()
}
}
44 changes: 42 additions & 2 deletions light_sdk/src/transaction/bridge.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use namada_core::proto::Tx;
use namada_core::proto::{Signature, Tx, TxError};
use namada_core::types::address::Address;
pub use namada_core::types::eth_bridge_pool::{GasFee, TransferToEthereum};
use namada_core::types::hash::Hash;
use namada_core::types::key::common;
use namada_core::types::storage::Epoch;
use namada_core::types::token::DenominatedAmount;
use namada_core::types::transaction::GasLimit;

use super::GlobalArgs;
use super::{attach_fee, attach_fee_signature, GlobalArgs};
use crate::transaction;

const TX_BRIDGE_POOL_WASM: &str = "tx_bridge_pool.wasm";
Expand Down Expand Up @@ -47,8 +51,44 @@ impl BridgeTransfer {
))
}

/// Attach the fee data to the tx
pub fn attach_fee(
self,
fee: DenominatedAmount,
token: Address,
fee_payer: common::PublicKey,
epoch: Epoch,
gas_limit: GasLimit,
) -> Self {
Self(attach_fee(self.0, fee, token, fee_payer, epoch, gas_limit))
}

/// Get the bytes of the fee data to sign
pub fn get_fee_sig_bytes(&self) -> Hash {
transaction::get_wrapper_sign_bytes(&self.0)
}

/// Attach a signature of the fee to the tx
pub fn attach_fee_signature(
self,
signer: common::PublicKey,
signature: common::Signature,
) -> Self {
Self(attach_fee_signature(self.0, signer, signature))
}

/// Generates the protobuf encoding of this transaction
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}

/// Gets the inner transaction without the domain wrapper
pub fn payload(self) -> Tx {
self.0
}

/// Validate this wrapper transaction
pub fn validate_tx(&self) -> Result<Option<&Signature>, TxError> {
self.0.validate_tx()
}
}
73 changes: 71 additions & 2 deletions light_sdk/src/transaction/governance.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use namada_core::ledger::governance::storage::proposal::ProposalType;
use namada_core::ledger::governance::storage::vote::ProposalVote;
use namada_core::proto::Tx;
use namada_core::proto::{Signature, Tx, TxError};
use namada_core::types::address::Address;
use namada_core::types::hash::Hash;
use namada_core::types::key::common;
use namada_core::types::storage::Epoch;
use namada_core::types::token::DenominatedAmount;
use namada_core::types::transaction::GasLimit;

use super::GlobalArgs;
use super::{attach_fee, attach_fee_signature, GlobalArgs};
use crate::transaction;

const TX_INIT_PROPOSAL_WASM: &str = "tx_init_proposal.wasm";
Expand Down Expand Up @@ -62,10 +64,41 @@ impl InitProposal {
))
}

/// Attach the fee data to the tx
pub fn attach_fee(
self,
fee: DenominatedAmount,
token: Address,
fee_payer: common::PublicKey,
epoch: Epoch,
gas_limit: GasLimit,
) -> Self {
Self(attach_fee(self.0, fee, token, fee_payer, epoch, gas_limit))
}

/// Get the bytes of the fee data to sign
pub fn get_fee_sig_bytes(&self) -> Hash {
transaction::get_wrapper_sign_bytes(&self.0)
}

/// Attach a signature of the fee to the tx
pub fn attach_fee_signature(
self,
signer: common::PublicKey,
signature: common::Signature,
) -> Self {
Self(attach_fee_signature(self.0, signer, signature))
}

/// Generates the protobuf encoding of this transaction
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}

/// Gets the inner transaction without the domain wrapper
pub fn payload(self) -> Tx {
self.0
}
}

/// Transaction to vote on a governance proposal
Expand Down Expand Up @@ -111,8 +144,44 @@ impl VoteProposal {
))
}

/// Attach the fee data to the tx
pub fn attach_fee(
self,
fee: DenominatedAmount,
token: Address,
fee_payer: common::PublicKey,
epoch: Epoch,
gas_limit: GasLimit,
) -> Self {
Self(attach_fee(self.0, fee, token, fee_payer, epoch, gas_limit))
}

/// Get the bytes of the fee data to sign
pub fn get_fee_sig_bytes(&self) -> Hash {
transaction::get_wrapper_sign_bytes(&self.0)
}

/// Attach a signature of the fee to the tx
pub fn attach_fee_signature(
self,
signer: common::PublicKey,
signature: common::Signature,
) -> Self {
Self(attach_fee_signature(self.0, signer, signature))
}

/// Generates the protobuf encoding of this transaction
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}

/// Gets the inner transaction without the domain wrapper
pub fn payload(self) -> Tx {
self.0
}

/// Validate this wrapper transaction
pub fn validate_tx(&self) -> Result<Option<&Signature>, TxError> {
self.0.validate_tx()
}
}
Loading

0 comments on commit f77e339

Please sign in to comment.