soroban_cli/commands/contract/deploy/
wasm.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use std::array::TryFromSliceError;
use std::ffi::OsString;
use std::fmt::Debug;
use std::num::ParseIntError;

use crate::xdr::{
    AccountId, ContractExecutable, ContractIdPreimage, ContractIdPreimageFromAddress,
    CreateContractArgs, CreateContractArgsV2, Error as XdrError, Hash, HostFunction,
    InvokeContractArgs, InvokeHostFunctionOp, Limits, Memo, MuxedAccount, Operation, OperationBody,
    Preconditions, PublicKey, ScAddress, SequenceNumber, Transaction, TransactionExt, Uint256,
    VecM, WriteXdr,
};
use clap::{arg, command, Parser};
use rand::Rng;
use regex::Regex;

use soroban_spec_tools::contract as contract_spec;

use crate::{
    assembled::simulate_and_assemble_transaction,
    commands::{
        contract::{self, arg_parsing, id::wasm::get_contract_id, install},
        global,
        txn_result::{TxnEnvelopeResult, TxnResult},
        NetworkRunnable, HEADING_RPC,
    },
    config::{self, data, locator, network},
    print::Print,
    rpc,
    utils::{self, rpc::get_remote_wasm_from_hash},
    wasm,
};

pub const CONSTRUCTOR_FUNCTION_NAME: &str = "__constructor";

#[derive(Parser, Debug, Clone)]
#[command(group(
    clap::ArgGroup::new("wasm_src")
        .required(true)
        .args(&["wasm", "wasm_hash"]),
))]
#[group(skip)]
pub struct Cmd {
    /// WASM file to deploy
    #[arg(long, group = "wasm_src")]
    pub wasm: Option<std::path::PathBuf>,
    /// Hash of the already installed/deployed WASM file
    #[arg(long = "wasm-hash", conflicts_with = "wasm", group = "wasm_src")]
    pub wasm_hash: Option<String>,
    /// Custom salt 32-byte salt for the token id
    #[arg(
        long,
        help_heading = HEADING_RPC,
    )]
    pub salt: Option<String>,
    #[command(flatten)]
    pub config: config::Args,
    #[command(flatten)]
    pub fee: crate::fee::Args,
    #[arg(long, short = 'i', default_value = "false")]
    /// Whether to ignore safety checks when deploying contracts
    pub ignore_checks: bool,
    /// The alias that will be used to save the contract's id.
    /// Whenever used, `--alias` will always overwrite the existing contract id
    /// configuration without asking for confirmation.
    #[arg(long, value_parser = clap::builder::ValueParser::new(alias_validator))]
    pub alias: Option<String>,
    /// If provided, will be passed to the contract's `__constructor` function with provided arguments for that function as `--arg-name value`
    #[arg(last = true, id = "CONTRACT_CONSTRUCTOR_ARGS")]
    pub slop: Vec<OsString>,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Install(#[from] install::Error),
    #[error("error parsing int: {0}")]
    ParseIntError(#[from] ParseIntError),
    #[error("internal conversion error: {0}")]
    TryFromSliceError(#[from] TryFromSliceError),
    #[error("xdr processing error: {0}")]
    Xdr(#[from] XdrError),
    #[error("jsonrpc error: {0}")]
    JsonRpc(#[from] jsonrpsee_core::Error),
    #[error("cannot parse salt: {salt}")]
    CannotParseSalt { salt: String },
    #[error("cannot parse contract ID {contract_id}: {error}")]
    CannotParseContractId {
        contract_id: String,
        error: stellar_strkey::DecodeError,
    },
    #[error("cannot parse WASM hash {wasm_hash}: {error}")]
    CannotParseWasmHash {
        wasm_hash: String,
        error: stellar_strkey::DecodeError,
    },
    #[error("Must provide either --wasm or --wash-hash")]
    WasmNotProvided,
    #[error(transparent)]
    Rpc(#[from] rpc::Error),
    #[error(transparent)]
    Config(#[from] config::Error),
    #[error(transparent)]
    StrKey(#[from] stellar_strkey::DecodeError),
    #[error(transparent)]
    Infallible(#[from] std::convert::Infallible),
    #[error(transparent)]
    WasmId(#[from] contract::id::wasm::Error),
    #[error(transparent)]
    Data(#[from] data::Error),
    #[error(transparent)]
    Network(#[from] network::Error),
    #[error(transparent)]
    Wasm(#[from] wasm::Error),
    #[error(
        "alias must be 1-30 chars long, and have only letters, numbers, underscores and dashes"
    )]
    InvalidAliasFormat { alias: String },
    #[error(transparent)]
    Locator(#[from] locator::Error),
    #[error(transparent)]
    ContractSpec(#[from] contract_spec::Error),
    #[error(transparent)]
    ArgParse(#[from] arg_parsing::Error),
    #[error("Only ed25519 accounts are allowed")]
    OnlyEd25519AccountsAllowed,
}

impl Cmd {
    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
        let res = self
            .run_against_rpc_server(Some(global_args), None)
            .await?
            .to_envelope();
        match res {
            TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?),
            TxnEnvelopeResult::Res(contract) => {
                let network = self.config.get_network()?;

                if let Some(alias) = self.alias.clone() {
                    self.config.locator.save_contract_id(
                        &network.network_passphrase,
                        &contract,
                        &alias,
                    )?;
                }

                println!("{contract}");
            }
        }
        Ok(())
    }
}

fn alias_validator(alias: &str) -> Result<String, Error> {
    let regex = Regex::new(r"^[a-zA-Z0-9_-]{1,30}$").unwrap();

    if regex.is_match(alias) {
        Ok(alias.into())
    } else {
        Err(Error::InvalidAliasFormat {
            alias: alias.into(),
        })
    }
}

#[async_trait::async_trait]
impl NetworkRunnable for Cmd {
    type Error = Error;
    type Result = TxnResult<stellar_strkey::Contract>;

    #[allow(clippy::too_many_lines)]
    async fn run_against_rpc_server(
        &self,
        global_args: Option<&global::Args>,
        config: Option<&config::Args>,
    ) -> Result<TxnResult<stellar_strkey::Contract>, Error> {
        let print = Print::new(global_args.map_or(false, |a| a.quiet));
        let config = config.unwrap_or(&self.config);
        let wasm_hash = if let Some(wasm) = &self.wasm {
            let hash = if self.fee.build_only || self.fee.sim_only {
                wasm::Args { wasm: wasm.clone() }.hash()?
            } else {
                install::Cmd {
                    wasm: wasm::Args { wasm: wasm.clone() },
                    config: config.clone(),
                    fee: self.fee.clone(),
                    ignore_checks: self.ignore_checks,
                }
                .run_against_rpc_server(global_args, Some(config))
                .await?
                .into_result()
                .expect("the value (hash) is expected because it should always be available since build-only is a shared parameter")
            };
            hex::encode(hash)
        } else {
            self.wasm_hash
                .as_ref()
                .ok_or(Error::WasmNotProvided)?
                .to_string()
        };

        let wasm_hash = Hash(
            utils::contract_id_from_str(&wasm_hash)
                .map_err(|e| Error::CannotParseWasmHash {
                    wasm_hash: wasm_hash.clone(),
                    error: e,
                })?
                .0,
        );

        print.infoln(format!("Using wasm hash {wasm_hash}").as_str());

        let network = config.get_network()?;
        let salt: [u8; 32] = match &self.salt {
            Some(h) => soroban_spec_tools::utils::padded_hex_from_str(h, 32)
                .map_err(|_| Error::CannotParseSalt { salt: h.clone() })?
                .try_into()
                .map_err(|_| Error::CannotParseSalt { salt: h.clone() })?,
            None => rand::thread_rng().gen::<[u8; 32]>(),
        };

        let client = network.rpc_client()?;
        client
            .verify_network_passphrase(Some(&network.network_passphrase))
            .await?;

        let MuxedAccount::Ed25519(bytes) = config.source_account()? else {
            return Err(Error::OnlyEd25519AccountsAllowed);
        };
        let source_account = AccountId(PublicKey::PublicKeyTypeEd25519(bytes));
        let contract_id_preimage = ContractIdPreimage::Address(ContractIdPreimageFromAddress {
            address: ScAddress::Account(source_account.clone()),
            salt: Uint256(salt),
        });
        let contract_id =
            get_contract_id(contract_id_preimage.clone(), &network.network_passphrase)?;
        let raw_wasm = if let Some(wasm) = self.wasm.as_ref() {
            wasm::Args { wasm: wasm.clone() }.read()?
        } else {
            get_remote_wasm_from_hash(&client, &wasm_hash).await?
        };
        let entries = soroban_spec_tools::contract::Spec::new(&raw_wasm)?.spec;
        let res = soroban_spec_tools::Spec::new(entries.clone());
        let constructor_params = if let Ok(func) = res.find_function(CONSTRUCTOR_FUNCTION_NAME) {
            if func.inputs.len() == 0 {
                None
            } else {
                let mut slop = vec![OsString::from(CONSTRUCTOR_FUNCTION_NAME)];
                slop.extend_from_slice(&self.slop);
                Some(
                    arg_parsing::build_host_function_parameters(
                        &stellar_strkey::Contract(contract_id.0),
                        &slop,
                        &entries,
                        config,
                    )?
                    .2,
                )
            }
        } else {
            None
        };

        // Get the account sequence number
        let account_details = client.get_account(&source_account.to_string()).await?;
        let sequence: i64 = account_details.seq_num.into();
        let txn = Box::new(build_create_contract_tx(
            wasm_hash,
            sequence + 1,
            self.fee.fee,
            source_account,
            contract_id_preimage,
            constructor_params.as_ref(),
        )?);

        if self.fee.build_only {
            print.checkln("Transaction built!");
            return Ok(TxnResult::Txn(txn));
        }

        print.infoln("Simulating deploy transaction…");

        let txn = simulate_and_assemble_transaction(&client, &txn).await?;
        let txn = Box::new(self.fee.apply_to_assembled_txn(txn).transaction().clone());

        if self.fee.sim_only {
            print.checkln("Done!");
            return Ok(TxnResult::Txn(txn));
        }

        print.globeln("Submitting deploy transaction…");
        print.log_transaction(&txn, &network, true)?;

        let get_txn_resp = client
            .send_transaction_polling(&config.sign_with_local_key(*txn).await?)
            .await?
            .try_into()?;

        if global_args.map_or(true, |a| !a.no_cache) {
            data::write(get_txn_resp, &network.rpc_uri()?)?;
        }

        if let Some(url) = utils::explorer_url_for_contract(&network, &contract_id) {
            print.linkln(url);
        }

        print.checkln("Deployed!");

        Ok(TxnResult::Res(contract_id))
    }
}

fn build_create_contract_tx(
    wasm_hash: Hash,
    sequence: i64,
    fee: u32,
    key: AccountId,
    contract_id_preimage: ContractIdPreimage,
    constructor_params: Option<&InvokeContractArgs>,
) -> Result<Transaction, Error> {
    let op = if let Some(InvokeContractArgs { args, .. }) = constructor_params {
        Operation {
            source_account: None,
            body: OperationBody::InvokeHostFunction(InvokeHostFunctionOp {
                host_function: HostFunction::CreateContractV2(CreateContractArgsV2 {
                    contract_id_preimage,
                    executable: ContractExecutable::Wasm(wasm_hash),
                    constructor_args: args.clone(),
                }),
                auth: VecM::default(),
            }),
        }
    } else {
        Operation {
            source_account: None,
            body: OperationBody::InvokeHostFunction(InvokeHostFunctionOp {
                host_function: HostFunction::CreateContract(CreateContractArgs {
                    contract_id_preimage,
                    executable: ContractExecutable::Wasm(wasm_hash),
                }),
                auth: VecM::default(),
            }),
        }
    };
    let tx = Transaction {
        source_account: key.into(),
        fee,
        seq_num: SequenceNumber(sequence),
        cond: Preconditions::None,
        memo: Memo::None,
        operations: vec![op].try_into()?,
        ext: TransactionExt::V0,
    };

    Ok(tx)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_create_contract() {
        let hash = hex::decode("0000000000000000000000000000000000000000000000000000000000000000")
            .unwrap()
            .try_into()
            .unwrap();
        let salt = [0u8; 32];
        let key =
            &utils::parse_secret_key("SBFGFF27Y64ZUGFAIG5AMJGQODZZKV2YQKAVUUN4HNE24XZXD2OEUVUP")
                .unwrap();
        let source_account = AccountId(PublicKey::PublicKeyTypeEd25519(Uint256(
            key.verifying_key().to_bytes(),
        )));

        let contract_id_preimage = ContractIdPreimage::Address(ContractIdPreimageFromAddress {
            address: ScAddress::Account(source_account.clone()),
            salt: Uint256(salt),
        });

        let result = build_create_contract_tx(
            Hash(hash),
            300,
            1,
            source_account,
            contract_id_preimage,
            None,
        );

        assert!(result.is_ok());
    }

    #[test]
    fn test_alias_validator_with_valid_inputs() {
        let valid_inputs = [
            "hello",
            "123",
            "hello123",
            "hello_123",
            "123_hello",
            "123-hello",
            "hello-123",
            "HeLlo-123",
        ];

        for input in valid_inputs {
            let result = alias_validator(input);
            assert!(result.is_ok());
            assert!(result.unwrap() == input);
        }
    }

    #[test]
    fn test_alias_validator_with_invalid_inputs() {
        let invalid_inputs = ["", "invalid!", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"];

        for input in invalid_inputs {
            let result = alias_validator(input);
            assert!(result.is_err());
        }
    }
}