Skip to content

Commit

Permalink
Allow short contract IDs (#49)
Browse files Browse the repository at this point in the history
Co-authored-by: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com>
  • Loading branch information
2opremio and leighmcculloch authored Jul 29, 2022
1 parent b46e126 commit 111f4c7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt::Debug, fs, io};
use clap::Parser;
use soroban_env_host::xdr::Error as XdrError;

use hex::{FromHex, FromHexError};
use hex::FromHexError;

use crate::snapshot;
use crate::utils;
Expand Down Expand Up @@ -35,7 +35,7 @@ pub enum Error {

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
let contract_id: [u8; 32] = FromHex::from_hex(&self.contract_id)?;
let contract_id: [u8; 32] = utils::contract_id_from_str(&self.contract_id)?;
let contract = fs::read(&self.file).unwrap();

let mut ledger_entries = snapshot::read(&self.snapshot_file)?;
Expand Down
4 changes: 2 additions & 2 deletions src/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use soroban_env_host::{
Host, HostError, Vm,
};

use hex::{FromHex, FromHexError};
use hex::FromHexError;

use crate::snapshot;
use crate::strval::{self, StrValError};
Expand Down Expand Up @@ -67,7 +67,7 @@ pub enum Error {

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
let contract_id: [u8; 32] = FromHex::from_hex(&self.contract_id)?;
let contract_id: [u8; 32] = utils::contract_id_from_str(&self.contract_id)?;

// Initialize storage and host
// TODO: allow option to separate input and output file
Expand Down
8 changes: 8 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use hex::FromHexError;
use soroban_env_host::{
im_rc::OrdMap,
storage::Storage,
Expand Down Expand Up @@ -34,6 +35,13 @@ pub fn add_contract_to_ledger_entries(
Ok(())
}

pub fn contract_id_from_str(contract_id: &String) -> Result<[u8; 32], FromHexError> {
let mut decoded = [0u8; 32];
let padded = format!("{:0>width$}", contract_id, width = decoded.len() * 2);
hex::decode_to_slice(padded, &mut decoded)?;
Ok(decoded)
}

pub fn get_contract_wasm_from_storage(
storage: &mut Storage,
contract_id: [u8; 32],
Expand Down

0 comments on commit 111f4c7

Please sign in to comment.