Skip to content

Commit

Permalink
feat: add support for legacy and new account encoding (#18)
Browse files Browse the repository at this point in the history
* feat: add support for legacy and new account encoding

* docs: add ACCOUNT_IS_LEGACY to documentation

* docs: update katana account to 0.5.0

* fix: clippy
  • Loading branch information
glihm committed Jan 21, 2024
1 parent 04fedaf commit cb470c0
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 38 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.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ If you prefer a short cheatsheet, go [here](https://devhints.io/lua) or [here](h
To test Kipt, the easiest way is:

1. Install Kipt

```bash
# 1. Install
curl https://raw.githubusercontent.com/glihm/kipt/main/kiptup/install | sh
source ~/.bashrc
kiptup
```

2. Create a simple "demo.lua" script copying the example below, replacing with the name of your contract.
3. Spin up katana in an other terminal.
4. Run `kipt --lua ./demo.lua`.
Expand All @@ -37,7 +39,7 @@ kiptup

```lua
RPC = "KATANA"
ACCOUNT_ADDRESS = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
ACCOUNT_ADDRESS = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03"
ACCOUNT_PRIVKEY = "0x1800000000300000180000000000030000000000003006001800006600"

-- No args -> kipt.out
Expand Down
4 changes: 4 additions & 0 deletions book/src/globals_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ RPC = "GOERLI-1"
ACCOUNT_ADDRESS = "0x1234...."
ACCOUNT_PRIVKEY = "0x8888...."

-- If you're using a cairo 0 account, you will need legacy encoding.
-- ACCOUNT_IS_LEGACY = true

-- You can re-defined anywhere in the script a new value
RPC = "http://0.0.0.0:5050"
```
Expand All @@ -27,6 +30,7 @@ As shown, some global variables are expected by Kipt:

- `ACCOUNT_ADDRESS`: The address of the account to use to send transactions.
- `ACCOUNT_PRIVKEY`: The private key of the account to use to send transactions.
- `ACCOUNT_IS_LEGACY`: Specifies if the account is a cairo 0 account.

> ℹ️ **Note**
>
Expand Down
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
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ pub enum Error {
#[error(transparent)]
ContractJson(#[from] JsonError),
#[error(transparent)]
AccountError(#[from] AccountError<AccountSignError<SignError>>),
Account(#[from] AccountError<AccountSignError<SignError>>),
#[error(transparent)]
Anyhow(#[from] anyhow::Error),
#[error("Contract artifacts is missing: {0}")]
ArtifactsMissing(String),
#[error(transparent)]
NonAsciiNameError(#[from] NonAsciiNameError),
NonAsciiName(#[from] NonAsciiNameError),
}

impl From<Error> for LuaError {
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 cb470c0

Please sign in to comment.