Skip to content

Commit

Permalink
feat: update for order change
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Aug 11, 2023
1 parent d70370a commit 6c88b3b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 85 deletions.
20 changes: 10 additions & 10 deletions packages/shared/lib/Cargo.lock

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

2 changes: 1 addition & 1 deletion packages/shared/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ gloo-utils = { version = "0.1.5", features = ["serde"] }
js-sys = "0.3.60"
masp_primitives = { git = "https://github.com/anoma/masp", rev = "252a6059565b125c1444e9e7d0b7c8da0fba8f8f" }
masp_proofs = { git = "https://github.com/anoma/masp", rev = "252a6059565b125c1444e9e7d0b7c8da0fba8f8f", default-features = false, features = ["local-prover"] }
namada = { git = "https://github.com/anoma/namada", branch = "fraccaman/multisignature-complete", default-features = false, features = ["abciplus", "namada-sdk"] }
namada = { git = "https://github.com/anoma/namada", branch = "murisi/alternative-multisig", default-features = false, features = ["abciplus", "namada-sdk"] }
# namada = { path = "/home/mj/Projects/heliax/namada/shared", default-features = false, features = ["abciplus", "namada-sdk"] }
prost = "0.9.0"
prost-types = "0.9.0"
Expand Down
126 changes: 53 additions & 73 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use borsh::{BorshDeserialize, BorshSerialize};
use namada::ledger::signing::SigningTxData;
use namada::ledger::tx::Error;
use namada::types::address::Address;
use namada::types::key::common::SecretKey;
use namada::types::tx::TxBuilder;
use namada::{
ledger::{
args,
Expand Down Expand Up @@ -39,14 +37,6 @@ pub enum TxType {
RevealPK = 5,
}

// Require that a public key is present
fn validate_pk(pk: Option<PublicKey>) -> Result<PublicKey, JsError> {
match pk {
Some(v) => Ok(v),
None => Err(JsError::new("No public key was provided!")),
}
}

/// Represents the Sdk public API.
#[wasm_bindgen]
pub struct Sdk {
Expand Down Expand Up @@ -120,40 +110,11 @@ impl Sdk {
wallet::add_spending_key(&mut self.wallet, xsk, password, alias)
}

async fn submit_reveal_pk(
&mut self,
args: &args::Tx,
pk: &PublicKey,
gas_payer: Option<SecretKey>,
) -> Result<(), JsError> {
// Build a transaction to reveal the signer of this transaction
let mut tx_builder = namada::ledger::tx::build_reveal_pk(
&self.client,
args,
//TODO: This is only needed for logging, I imagine it will be cleaned up in Namada
&args.gas_token,
&pk,
// In the case of web interface gas_payer is the same as the signer
&pk,
)
.await?;

// Add gas payer - hardware wallets should do it automatically
if let Some(gas_payer) = gas_payer {
tx_builder = tx_builder.add_gas_payer(gas_payer);
}

namada::ledger::tx::process_tx(&self.client, &mut self.wallet, &args, tx_builder.build())
.await?;

Ok(())
}

/// Sign and submit transactions
async fn sign_and_process_tx(
&mut self,
args: args::Tx,
tx_builder: TxBuilder,
mut tx: Tx,
signing_data: SigningTxData,
) -> Result<(), JsError> {
// We are revealing the signer of this transaction(if needed)
Expand All @@ -164,17 +125,27 @@ impl Sdk {
.into_iter()
.nth(0)
.expect("No public key provided");
let sk = self
.wallet
.find_key_by_pk(pk, args.clone().password)
.expect("No secret key found");

// Submit a reveal pk tx if necessary
self.submit_reveal_pk(&args, &pk, Some(sk)).await?;
let address = Address::from(pk);

if namada::ledger::tx::is_reveal_pk_needed(&self.client, &address, false).await? {
let mut tx = namada::ledger::tx::build_reveal_pk(
&self.client,
&args,
&address,
&pk,
&signing_data.gas_payer,
)
.await?;

signing::sign_tx(&mut self.wallet, &args, &mut tx, signing_data.clone());

namada::ledger::tx::process_tx(&self.client, &mut self.wallet, &args, tx).await?;
}

// Sign tx
let tx_builder = signing::sign_tx(&mut self.wallet, &args, tx_builder, signing_data)?;
let tx = tx_builder.build();
signing::sign_tx(&mut self.wallet, &args, &mut tx, signing_data.clone());

// Submit tx
namada::ledger::tx::process_tx(&self.client, &mut self.wallet, &args, tx).await?;

Expand Down Expand Up @@ -205,9 +176,11 @@ impl Sdk {
gas_payer: String,
) -> Result<JsValue, JsError> {
//TODO: verify if this works
let gas_payer = PublicKey::from_str(&gas_payer)?;
// We prefix 00 because PublicKey is an enum. TODO: fix when ledger is updated to handle
// payment addresses
let gas_payer = PublicKey::from_str(&format!("00{}", gas_payer))?;

let tx_builder = match tx_type {
let tx = match tx_type {
TxType::Bond => {
let args = tx::bond_tx_args(tx_msg, None)?;
let bond = namada::ledger::tx::build_bond(&self.client, args.clone(), &gas_payer)
Expand Down Expand Up @@ -242,32 +215,31 @@ impl Sdk {
}
TxType::Transfer => {
let args = tx::transfer_tx_args(tx_msg, None, None)?;
let (tx_builder, _) = namada::ledger::tx::build_transfer(
let (tx, _) = namada::ledger::tx::build_transfer(
&self.client,
&mut self.shielded_ctx,
args.clone(),
&gas_payer,
)
.await?;
tx_builder
tx
}
TxType::Unbond => {
let args = tx::unbond_tx_args(tx_msg, None)?;
let (tx_builder, _) = namada::ledger::tx::build_unbond(
let (tx, _) = namada::ledger::tx::build_unbond(
&self.client,
&mut self.wallet,
args.clone(),
&gas_payer,
)
.await?;
tx_builder
tx
}
TxType::Withdraw => {
let args = tx::withdraw_tx_args(tx_msg, None)?;
namada::ledger::tx::build_withdraw(&self.client, args.clone(), &gas_payer).await?
}
};
let tx = tx_builder.build();

to_js_result(tx.try_to_vec()?)
}
Expand Down Expand Up @@ -302,10 +274,6 @@ impl Sdk {
) -> Result<(), JsError> {
let transfer_tx = self.sign_tx(tx_bytes, raw_sig_bytes, wrapper_sig_bytes)?;
let args = tx::tx_args_from_slice(tx_msg)?;
let verification_key = args.verification_key.clone();
let pk = validate_pk(verification_key)?;

self.submit_reveal_pk(&args, &pk, None).await?;

namada::ledger::tx::process_tx(&self.client, &mut self.wallet, &args, transfer_tx)
.await
Expand All @@ -316,15 +284,15 @@ impl Sdk {

async fn signing_data(
&mut self,
address: Address,
owner: Option<Address>,
default_signer: Option<Address>,
tx_args: args::Tx,
) -> Result<SigningTxData, Error> {
let default_signer = signing::signer_from_address(Some(address.clone()));
signing::aux_signing_data(
&self.client,
&mut self.wallet,
&tx_args,
&address,
&owner,
default_signer,
)
.await
Expand All @@ -337,11 +305,16 @@ impl Sdk {
xsk: Option<String>,
) -> Result<(), JsError> {
let args = tx::transfer_tx_args(tx_msg, password, xsk)?;
let effective_address = args.source.effective_address();
let signing_data = self
.signing_data(args.source.effective_address(), args.tx.clone())
.signing_data(
Some(effective_address.clone()),
Some(effective_address.clone()),
args.tx.clone(),
)
.await?;

let (tx_builder, _) = namada::ledger::tx::build_transfer(
let (tx, _) = namada::ledger::tx::build_transfer(
&self.client,
&mut self.shielded_ctx,
args.clone(),
Expand All @@ -350,8 +323,7 @@ impl Sdk {
.await
.map_err(JsError::from)?;

self.sign_and_process_tx(args.tx, tx_builder, signing_data)
.await?;
self.sign_and_process_tx(args.tx, tx, signing_data).await?;

Ok(())
}
Expand All @@ -362,8 +334,10 @@ impl Sdk {
password: Option<String>,
) -> Result<(), JsError> {
let args = tx::ibc_transfer_tx_args(tx_msg, password)?;
let source = args.source.clone();

let signing_data = self
.signing_data(args.source.clone(), args.tx.clone())
.signing_data(Some(source.clone()), Some(source.clone()), args.tx.clone())
.await?;

let tx_builder = namada::ledger::tx::build_ibc_transfer(
Expand All @@ -385,8 +359,10 @@ impl Sdk {
password: Option<String>,
) -> Result<(), JsError> {
let args = tx::bond_tx_args(tx_msg, password)?;
let source = args.source.as_ref().expect("Source address is required");
let signing_data = self.signing_data(source.clone(), args.tx.clone()).await?;
let source = args.source.clone();
let signing_data = self
.signing_data(source.clone(), source.clone(), args.tx.clone())
.await?;

let tx_builder =
namada::ledger::tx::build_bond(&mut self.client, args.clone(), &signing_data.gas_payer)
Expand All @@ -405,8 +381,10 @@ impl Sdk {
password: Option<String>,
) -> Result<(), JsError> {
let args = tx::unbond_tx_args(tx_msg, password)?;
let source = args.source.as_ref().expect("Source address is required");
let signing_data = self.signing_data(source.clone(), args.tx.clone()).await?;
let source = args.source.clone();
let signing_data = self
.signing_data(source.clone(), source.clone(), args.tx.clone())
.await?;

let (tx_builder, _) = namada::ledger::tx::build_unbond(
&mut self.client,
Expand All @@ -428,8 +406,10 @@ impl Sdk {
password: Option<String>,
) -> Result<(), JsError> {
let args = tx::withdraw_tx_args(tx_msg, password)?;
let source = args.source.as_ref().expect("Source address is required");
let signing_data = self.signing_data(source.clone(), args.tx.clone()).await?;
let source = args.source.clone();
let signing_data = self
.signing_data(source.clone(), source.clone(), args.tx.clone())
.await?;

let tx_builder = namada::ledger::tx::build_withdraw(
&mut self.client,
Expand Down
1 change: 0 additions & 1 deletion packages/shared/lib/src/sdk/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ fn tx_msg_into_args(tx_msg: TxMsg, password: Option<String>) -> Result<args::Tx,
gas_token: token.clone(),
gas_limit: GasLimit::from(gas_limit),
gas_payer: None,
signatures: vec![],
output_folder: None,
expiration: None,
chain_id: Some(ChainId(String::from(chain_id))),
Expand Down

0 comments on commit 6c88b3b

Please sign in to comment.