Skip to content

Commit

Permalink
feat: add support for legacy and new account encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Jan 21, 2024
1 parent 04fedaf commit 563f54d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

6 changes: 3 additions & 3 deletions scripts/demo.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- RPC = "KATANA"
-- ACCOUNT_ADDRESS = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
-- ACCOUNT_PRIVKEY = "0x1800000000300000180000000000030000000000003006001800006600"
RPC = "KATANA"
ACCOUNT_ADDRESS = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03"
ACCOUNT_PRIVKEY = "0x1800000000300000180000000000030000000000003006001800006600"

-- No args -> kipt.out, or the output filename.
-- If called several time, only the first one counts.
Expand Down
8 changes: 6 additions & 2 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub async fn setup_account(
url_network: &str,
account_address: &str,
account_privkey: &str,
is_legacy: bool,
) -> KiptResult<SingleOwnerAccount<AnyProvider, LocalWallet>> {
let provider = if url_network.starts_with("http") {
provider_from_url(url_network)?
Expand All @@ -52,8 +53,11 @@ pub async fn setup_account(

let signer = LocalWallet::from(SigningKey::from_secret_scalar(key));

let account =
SingleOwnerAccount::new(provider, signer, addr, chain_id, ExecutionEncoding::Legacy);
let account = if is_legacy {
SingleOwnerAccount::new(provider, signer, addr, chain_id, ExecutionEncoding::Legacy)
} else {
SingleOwnerAccount::new(provider, signer, addr, chain_id, ExecutionEncoding::New)
};

Ok(account)
}
Expand Down
19 changes: 10 additions & 9 deletions src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn lua_declare<'lua>(
contract_name: String,
options: Table<'lua>,
) -> LuaResult<Table<'lua>> {
let (url_network, address, privkey) = lua::get_account(lua)?;
let (url_network, address, privkey, is_legacy) = lua::get_account(lua)?;
let artifacts_path: Option<String> = options.get("artifacts_path")?;
let is_recursive: bool = options.get("artifacts_recursively")?;
let skip_if_declared: bool = options.get("skip_if_declared")?;
Expand All @@ -59,15 +59,16 @@ pub fn lua_declare<'lua>(

let data = futures::executor::block_on(async move {
RT.spawn(async move {
let account = match account::setup_account(&url_network, &address, &privkey).await {
Ok(a) => a,
Err(e) => {
return LuaOutput {
data: None,
error: format!("{:?}", e),
let account =
match account::setup_account(&url_network, &address, &privkey, is_legacy).await {
Ok(a) => a,
Err(e) => {
return LuaOutput {
data: None,
error: format!("{:?}", e),
}
}
}
};
};

let (sierra_path, casm_path) = match locate_artifacts(
&contract_name,
Expand Down
19 changes: 10 additions & 9 deletions src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn lua_deploy<'lua>(
args: Vec<String>,
options: Table<'lua>,
) -> LuaResult<Table<'lua>> {
let (url_network, address, privkey) = lua::get_account(lua)?;
let (url_network, address, privkey, is_legacy) = lua::get_account(lua)?;

let watch_interval = lua::get_watch_from_options(&options)?;
let salt: Option<String> = options.get("salt")?;
Expand All @@ -52,15 +52,16 @@ pub fn lua_deploy<'lua>(

let data = futures::executor::block_on(async move {
RT.spawn(async move {
let account = match account::setup_account(&url_network, &address, &privkey).await {
Ok(a) => a,
Err(e) => {
return LuaOutput {
data: None,
error: format!("{:?}", e),
let account =
match account::setup_account(&url_network, &address, &privkey, is_legacy).await {
Ok(a) => a,
Err(e) => {
return LuaOutput {
data: None,
error: format!("{:?}", e),
}
}
}
};
};

match deploy_tx(account, &sierra_class_hash, &args, salt, watch_interval).await {
Ok((deployed_address, depl_res)) => LuaOutput {
Expand Down
19 changes: 10 additions & 9 deletions src/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn lua_invoke<'lua>(
calls: Vec<InvokeCall>,
options: Table<'lua>,
) -> LuaResult<Table<'lua>> {
let (url_network, address, privkey) = lua::get_account(lua)?;
let (url_network, address, privkey, is_legacy) = lua::get_account(lua)?;

let watch_interval = lua::get_watch_from_options(&options)?;

Expand All @@ -74,15 +74,16 @@ pub fn lua_invoke<'lua>(

let data = futures::executor::block_on(async move {
RT.spawn(async move {
let account = match account::setup_account(&url_network, &address, &privkey).await {
Ok(a) => a,
Err(e) => {
return LuaOutput {
data: None,
error: format!("{:?}", e),
let account =
match account::setup_account(&url_network, &address, &privkey, is_legacy).await {
Ok(a) => a,
Err(e) => {
return LuaOutput {
data: None,
error: format!("{:?}", e),
}
}
}
};
};

match invoke_tx(account, calls, watch_interval).await {
Ok(invk_res) => LuaOutput {
Expand Down
11 changes: 9 additions & 2 deletions src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,20 @@ fn setup_starknet_funcs(lua: &Lua) -> LuaResult<()> {
/// # Arguments
///
/// * `lua` - Lua VM instance.
pub fn get_account(lua: &Lua) -> LuaResult<(String, String, String)> {
pub fn get_account(lua: &Lua) -> LuaResult<(String, String, String, bool)> {
let url_network: Option<String> = lua.globals().get("RPC")?;
let address: Option<String> = lua.globals().get("ACCOUNT_ADDRESS")?;
let privkey: Option<String> = lua.globals().get("ACCOUNT_PRIVKEY")?;
let is_legacy: Option<bool> = lua.globals().get("ACCOUNT_IS_LEGACY")?;

match (url_network, address, privkey) {
(Some(un), Some(a), Some(p)) => Ok((un, a, p)),
(Some(un), Some(a), Some(p)) => {
if let Some(true) = is_legacy {
Ok((un, a, p, true))
} else {
Ok((un, a, p, false))
}
}
_ => {
// Without RPC and account info, we can't send tx. Panic here.
panic!(
Expand Down

0 comments on commit 563f54d

Please sign in to comment.