Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update deploy subcommand to use create_contract_from_source() #167

Merged
merged 3 commits into from
Sep 30, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use jsonrpsee_http_client::{HeaderMap, HttpClientBuilder};
use rand::Rng;
use sha2::{Digest, Sha256};
use soroban_env_host::xdr::{
DecoratedSignature, Error as XdrError, Hash, HashIdPreimage, HashIdPreimageEd25519ContractId,
HostFunction, InvokeHostFunctionOp, LedgerFootprint, LedgerKey::ContractData,
LedgerKeyContractData, Memo, MuxedAccount, Operation, OperationBody, Preconditions, ScObject,
ScStatic::LedgerKeyContractCode, ScVal, SequenceNumber, Signature, SignatureHint, Transaction,
TransactionEnvelope, TransactionExt, TransactionSignaturePayload,
TransactionSignaturePayloadTaggedTransaction, TransactionV1Envelope, Uint256, VecM, WriteXdr,
AccountId, DecoratedSignature, Error as XdrError, Hash, HashIdPreimage,
HashIdPreimageSourceAccountContractId, HostFunction, InvokeHostFunctionOp, LedgerFootprint,
LedgerKey::ContractData, LedgerKeyContractData, Memo, MuxedAccount, Operation, OperationBody,
Preconditions, PublicKey, ScObject, ScStatic::LedgerKeyContractCode, ScVal, SequenceNumber,
Signature, SignatureHint, Transaction, TransactionEnvelope, TransactionExt,
TransactionSignaturePayload, TransactionSignaturePayloadTaggedTransaction,
TransactionV1Envelope, Uint256, VecM, WriteXdr,
};
use soroban_env_host::HostError;
use stellar_strkey::StrkeyPrivateKeyEd25519;
Expand Down Expand Up @@ -195,7 +196,7 @@ impl Cmd {
// TODO: create a cmdline parameter for the fee instead of simply using the minimum fee
let fee: u32 = 100;
let sequence = account_details.sequence.parse::<i64>()?;
let (tx, tx_hash) = build_create_contract_tx(
let (tx, tx_hash, contract_id) = build_create_contract_tx(
contract,
sequence,
fee,
Expand All @@ -214,7 +215,8 @@ impl Cmd {
.await?;
match response.status.as_str() {
"success" => {
println!("{}", response.status);
println!("{}", response.status.as_str());
println!("Contract ID: {}", hex::encode(contract_id.0));
2opremio marked this conversation as resolved.
Show resolved Hide resolved
return Ok(());
}
"error" => {
Expand Down Expand Up @@ -242,51 +244,33 @@ fn build_create_contract_tx(
fee: u32,
network_passphrase: &str,
key: &ed25519_dalek::Keypair,
) -> Result<(TransactionEnvelope, Hash), Error> {
) -> Result<(TransactionEnvelope, Hash, Hash), Error> {
let salt = rand::thread_rng().gen::<[u8; 32]>();

let separator =
b"create_contract_from_ed25519(contract: Vec<u8>, salt: u256, key: u256, sig: Vec<u8>)";
let contract_hash = Sha256::new()
.chain_update(separator)
.chain_update(salt)
.chain_update(contract.clone())
.finalize();

let contract_signature = key.sign(&contract_hash);

let preimage = HashIdPreimage::ContractIdFromEd25519(HashIdPreimageEd25519ContractId {
ed25519: Uint256(key.public.to_bytes()),
salt: Uint256(salt),
});
let preimage =
HashIdPreimage::ContractIdFromSourceAccount(HashIdPreimageSourceAccountContractId {
source_account: AccountId(PublicKey::PublicKeyTypeEd25519(
key.public.to_bytes().into(),
)),
salt: Uint256(salt),
});
let preimage_xdr = preimage.to_xdr()?;
let contract_id = Sha256::digest(preimage_xdr);

let contract_parameter = ScVal::Object(Some(ScObject::Bytes(contract.try_into()?)));
let salt_parameter = ScVal::Object(Some(ScObject::Bytes(salt.try_into()?)));
let public_key_parameter =
ScVal::Object(Some(ScObject::Bytes(key.public.as_bytes().try_into()?)));
let signature_parameter = ScVal::Object(Some(ScObject::Bytes(
contract_signature.to_bytes().try_into()?,
)));

let lk = ContractData(LedgerKeyContractData {
contract_id: Hash(contract_id.into()),
key: ScVal::Static(LedgerKeyContractCode),
});

let parameters: VecM<ScVal, 256_000> = vec![
contract_parameter,
salt_parameter,
public_key_parameter,
signature_parameter,
]
.try_into()?;
let parameters: VecM<ScVal, 256_000> = vec![contract_parameter, salt_parameter].try_into()?;

let op = Operation {
source_account: None,
body: OperationBody::InvokeHostFunction(InvokeHostFunctionOp {
function: HostFunction::CreateContractWithEd25519,
function: HostFunction::CreateContractWithSourceAccount,
parameters: parameters.into(),
footprint: LedgerFootprint {
read_only: VecM::default(),
Expand Down Expand Up @@ -323,7 +307,7 @@ fn build_create_contract_tx(
signatures: vec![decorated_signature].try_into()?,
});

Ok((envelope, Hash(tx_hash.into())))
Ok((envelope, Hash(tx_hash.into()), Hash(contract_id.into())))
}

fn parse_private_key(strkey: &str) -> Result<ed25519_dalek::Keypair, Error> {
Expand Down