Skip to content

Commit

Permalink
fix: remove key_pair since not all signers will expose the private key
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jul 29, 2024
1 parent bec2474 commit f099cbe
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 61 deletions.
13 changes: 5 additions & 8 deletions cmd/soroban-cli/src/commands/contract/deploy/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,10 @@ impl NetworkRunnable for Cmd {
client
.verify_network_passphrase(Some(&network.network_passphrase))
.await?;
let key = config.key_pair()?;

// Get the account sequence number
let public_strkey =
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string();
let public_strkey = config.public_key().await?;
// TODO: use symbols for the method names (both here and in serve)
let account_details = client.get_account(&public_strkey).await?;
let account_details = client.get_account(&public_strkey.to_string()).await?;
let sequence: i64 = account_details.seq_num.into();
let network_passphrase = &network.network_passphrase;
let contract_id = contract_id_hash_from_asset(&asset, network_passphrase)?;
Expand All @@ -113,7 +110,7 @@ impl NetworkRunnable for Cmd {
sequence + 1,
self.fee.fee,
network_passphrase,
&key,
&public_strkey,
)?;
if self.fee.build_only {
return Ok(TxnResult::Txn(tx));
Expand Down Expand Up @@ -141,7 +138,7 @@ fn build_wrap_token_tx(
sequence: i64,
fee: u32,
_network_passphrase: &str,
key: &ed25519_dalek::SigningKey,
key: &stellar_strkey::ed25519::PublicKey,
) -> Result<Transaction, Error> {
let contract = ScAddress::Contract(contract_id.clone());
let mut read_write = vec![
Expand Down Expand Up @@ -180,7 +177,7 @@ fn build_wrap_token_tx(
};

Ok(Transaction {
source_account: MuxedAccount::Ed25519(Uint256(key.verifying_key().to_bytes())),
source_account: MuxedAccount::Ed25519(Uint256(key.0)),
fee,
seq_num: SequenceNumber(sequence),
cond: Preconditions::None,
Expand Down
27 changes: 13 additions & 14 deletions cmd/soroban-cli/src/commands/contract/deploy/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,17 @@ impl NetworkRunnable for Cmd {
client
.verify_network_passphrase(Some(&network.network_passphrase))
.await?;
let key = config.key_pair()?;

// Get the account sequence number
let public_strkey =
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string();

let account_details = client.get_account(&public_strkey).await?;
let public_strkey = config.public_key().await?;
let account_details = client.get_account(&public_strkey.to_string()).await?;
let sequence: i64 = account_details.seq_num.into();
let (txn, contract_id) = build_create_contract_tx(
wasm_hash,
sequence + 1,
self.fee.fee,
&network.network_passphrase,
salt,
&key,
&public_strkey,
)?;
if self.fee.build_only {
return Ok(TxnResult::Txn(txn));
Expand Down Expand Up @@ -239,11 +235,9 @@ fn build_create_contract_tx(
fee: u32,
network_passphrase: &str,
salt: [u8; 32],
key: &ed25519_dalek::SigningKey,
key: &stellar_strkey::ed25519::PublicKey,
) -> Result<(Transaction, Hash), Error> {
let source_account = AccountId(PublicKey::PublicKeyTypeEd25519(
key.verifying_key().to_bytes().into(),
));
let source_account = AccountId(PublicKey::PublicKeyTypeEd25519(key.0.into()));

let contract_id_preimage = ContractIdPreimage::Address(ContractIdPreimageFromAddress {
address: ScAddress::Account(source_account),
Expand All @@ -262,7 +256,7 @@ fn build_create_contract_tx(
}),
};
let tx = Transaction {
source_account: MuxedAccount::Ed25519(Uint256(key.verifying_key().to_bytes())),
source_account: MuxedAccount::Ed25519(Uint256(key.0)),
fee,
seq_num: SequenceNumber(sequence),
cond: Preconditions::None,
Expand All @@ -276,6 +270,7 @@ fn build_create_contract_tx(

#[cfg(test)]
mod tests {

use super::*;

#[test]
Expand All @@ -290,8 +285,12 @@ mod tests {
1,
"Public Global Stellar Network ; September 2015",
[0u8; 32],
&utils::parse_secret_key("SBFGFF27Y64ZUGFAIG5AMJGQODZZKV2YQKAVUUN4HNE24XZXD2OEUVUP")
.unwrap(),
&stellar_strkey::ed25519::PublicKey(
utils::parse_secret_key("SBFGFF27Y64ZUGFAIG5AMJGQODZZKV2YQKAVUUN4HNE24XZXD2OEUVUP")
.unwrap()
.verifying_key()
.to_bytes(),
),
);

assert!(result.is_ok());
Expand Down
10 changes: 3 additions & 7 deletions cmd/soroban-cli/src/commands/contract/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,13 @@ impl NetworkRunnable for Cmd {
let keys = self.key.parse_keys(contract)?;
let network = &config.get_network()?;
let client = Client::new(&network.rpc_url)?;
let key = config.key_pair()?;
let public_key = config.public_key().await?;
let extend_to = self.ledgers_to_extend();

// Get the account sequence number
let public_strkey =
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string();
let account_details = client.get_account(&public_strkey).await?;
let account_details = client.get_account(&public_key.to_string()).await?;
let sequence: i64 = account_details.seq_num.into();

let tx = Transaction {
source_account: MuxedAccount::Ed25519(Uint256(key.verifying_key().to_bytes())),
source_account: MuxedAccount::Ed25519(Uint256(public_key.0)),
fee: self.fee.fee,
seq_num: SequenceNumber(sequence + 1),
cond: Preconditions::None,
Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-cli/src/commands/contract/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub enum Error {
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
pub async fn run(&self) -> Result<(), Error> {
match &self {
Cmd::Asset(asset) => asset.run()?,
Cmd::Wasm(wasm) => wasm.run()?,
Cmd::Wasm(wasm) => wasm.run().await?,
}
Ok(())
}
Expand Down
12 changes: 7 additions & 5 deletions cmd/soroban-cli/src/commands/contract/id/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ pub enum Error {
CannotParseSalt(String),
}
impl Cmd {
pub fn run(&self) -> Result<(), Error> {
pub async fn run(&self) -> Result<(), Error> {
let salt: [u8; 32] = soroban_spec_tools::utils::padded_hex_from_str(&self.salt, 32)
.map_err(|_| Error::CannotParseSalt(self.salt.clone()))?
.try_into()
.map_err(|_| Error::CannotParseSalt(self.salt.clone()))?;
let contract_id_preimage =
contract_preimage(&self.config.key_pair()?.verifying_key(), salt);
let contract_id_preimage = contract_preimage(&self.config.public_key().await?, salt);
let contract_id = get_contract_id(
contract_id_preimage.clone(),
&self.config.get_network()?.network_passphrase,
Expand All @@ -46,8 +45,11 @@ impl Cmd {
}
}

pub fn contract_preimage(key: &ed25519_dalek::VerifyingKey, salt: [u8; 32]) -> ContractIdPreimage {
let source_account = AccountId(PublicKey::PublicKeyTypeEd25519(key.to_bytes().into()));
pub fn contract_preimage(
key: &stellar_strkey::ed25519::PublicKey,
salt: [u8; 32],
) -> ContractIdPreimage {
let source_account = AccountId(PublicKey::PublicKeyTypeEd25519(key.0.into()));
ContractIdPreimage::Address(ContractIdPreimageFromAddress {
address: ScAddress::Account(source_account),
salt: Uint256(salt),
Expand Down
26 changes: 12 additions & 14 deletions cmd/soroban-cli/src/commands/contract/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,12 @@ impl NetworkRunnable for Cmd {
tracing::warn!("the deployed smart contract {path} was built with Soroban Rust SDK v{rs_sdk_ver}, a release candidate version not intended for use with the Stellar Public Network", path = self.wasm.wasm.display());
}
}
let key = config.key_pair()?;

let public_strkey = config.public_key().await?;
// Get the account sequence number
let public_strkey =
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string();
let account_details = client.get_account(&public_strkey).await?;
let account_details = client.get_account(&public_strkey.to_string()).await?;
let sequence: i64 = account_details.seq_num.into();

let (tx_without_preflight, hash) =
build_install_contract_code_tx(&contract, sequence + 1, self.fee.fee, &key)?;
build_install_contract_code_tx(&contract, sequence + 1, self.fee.fee, &public_strkey)?;

if self.fee.build_only {
return Ok(TxnResult::Txn(tx_without_preflight));
Expand Down Expand Up @@ -231,22 +227,20 @@ pub(crate) fn build_install_contract_code_tx(
source_code: &[u8],
sequence: i64,
fee: u32,
key: &ed25519_dalek::SigningKey,
key: &stellar_strkey::ed25519::PublicKey,
) -> Result<(Transaction, Hash), XdrError> {
let hash = utils::contract_hash(source_code)?;

let op = Operation {
source_account: Some(MuxedAccount::Ed25519(Uint256(
key.verifying_key().to_bytes(),
))),
source_account: Some(MuxedAccount::Ed25519(Uint256(key.0))),
body: OperationBody::InvokeHostFunction(InvokeHostFunctionOp {
host_function: HostFunction::UploadContractWasm(source_code.try_into()?),
auth: VecM::default(),
}),
};

let tx = Transaction {
source_account: MuxedAccount::Ed25519(Uint256(key.verifying_key().to_bytes())),
source_account: MuxedAccount::Ed25519(Uint256(key.0)),
fee,
seq_num: SequenceNumber(sequence),
cond: Preconditions::None,
Expand All @@ -268,8 +262,12 @@ mod tests {
b"foo",
300,
1,
&utils::parse_secret_key("SBFGFF27Y64ZUGFAIG5AMJGQODZZKV2YQKAVUUN4HNE24XZXD2OEUVUP")
.unwrap(),
&stellar_strkey::ed25519::PublicKey(
utils::parse_secret_key("SBFGFF27Y64ZUGFAIG5AMJGQODZZKV2YQKAVUUN4HNE24XZXD2OEUVUP")
.unwrap()
.verifying_key()
.to_bytes(),
),
);

assert!(result.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Cmd {
Cmd::Build(build) => build.run()?,
Cmd::Extend(extend) => extend.run().await?,
Cmd::Deploy(deploy) => deploy.run().await?,
Cmd::Id(id) => id.run()?,
Cmd::Id(id) => id.run().await?,
Cmd::Init(init) => init.run()?,
Cmd::Inspect(inspect) => inspect.run()?,
Cmd::Install(install) => install.run().await?,
Expand Down
8 changes: 3 additions & 5 deletions cmd/soroban-cli/src/commands/contract/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,14 @@ impl NetworkRunnable for Cmd {
let contract = config.resolve_contract_id(self.key.contract_id.as_ref().unwrap())?;
let entry_keys = self.key.parse_keys(contract)?;
let client = Client::new(&network.rpc_url)?;
let key = config.key_pair()?;

// Get the account sequence number
let public_strkey =
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string();
let account_details = client.get_account(&public_strkey).await?;
let public_strkey = config.public_key().await?;
let account_details = client.get_account(&public_strkey.to_string()).await?;
let sequence: i64 = account_details.seq_num.into();

let tx = Transaction {
source_account: MuxedAccount::Ed25519(Uint256(key.verifying_key().to_bytes())),
source_account: MuxedAccount::Ed25519(Uint256(public_strkey.0)),
fee: self.fee.fee,
seq_num: SequenceNumber(sequence + 1),
cond: Preconditions::None,
Expand Down
5 changes: 0 additions & 5 deletions cmd/soroban-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ impl Args {
Ok(self.sign_with.public_key().await?)
}

pub fn key_pair(&self) -> Result<ed25519_dalek::SigningKey, Error> {
let key = self.sign_with.locator.account(&self.source_account)?;
Ok(key.key_pair(self.sign_with.hd_path)?)
}

pub async fn sign(&self, tx: Transaction) -> Result<TransactionEnvelope, Error> {
Ok(self.sign_with.sign_txn(tx).await?)
}
Expand Down

0 comments on commit f099cbe

Please sign in to comment.