0xMiden / 0xMiden/rust-sdk

Fetch public account code automatically (from node?)

Abierto
#1,155 10 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Rust
Estrellas
78
Forks
129
Merge medio
4 d 14 h
PR fusionados (30 d)
52

Descripción

The way we currently interact (e.g. via a note/tx script) with an account that has custom code:
1. We locally prepare `account_code`. For an already-deployed contract, we specifically need its MASM code (locally?), which already sounds cumbersome,
2. We need to create a library out of that account code,
3. We need to setup a custom assembler with the library we just created, otherwise the tx/note script won't "understand" what to call.

It looks like:
```rust
let assembler: Assembler = TransactionKernel::assembler();
let source_manager = Arc::new(DefaultSourceManager::default());
let module = Module::parser(ModuleKind::Library).parse_str(
LibraryPath::new("external_contract::counter_contract").unwrap(),
account_code, // <------- need the contract code locally
&source_manager,
)?;
let library = assembler.clone().assemble_library([module])?;

// still setting up the assembler...
let assembler = TransactionKernel::assembler()
.with_dynamic_library(library)
.unwrap();

// and only then can we build the note script:
NoteBuilder::new(native_account.id(), client.rng())
.code(
"use.external_contract::counter_contract
begin
call.counter_contract::increment_count
end",
)
.tag(NoteTag::from_account_id(network_account.id()).into())
.build(&assembler)
.unwrap(),
```

While there might not be an easier way around step 1. for private contracts, I feel like we should allow for executing against public & network accounts in an easier fashion.

Perhaps there is an easier way to do all of this above? @Dominik1999 @bobbinth

If not, then we should probably re-think the DevEx here. Ideally, I'd want to build the script with a simple call like:
```rust
// for a public/network `Account`:
let account_code = client.fetch_code(account_id);
let tx_script = TransactionScript::against_public_contract(script_code, account_code)?;
```

I imagine the best way to go about this is for the node to "serve" the account code to the `client`, who could easily verify that the provided code matches the onchain commitment.
An alternative would be to have a public repository of smart contract codes which the users can query, but this requires an external service.

_Originally posted by @mmagician in https://github.com/0xMiden/project-template/pull/4#discussion_r2250087768_

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.