0xMiden / 0xMiden/rust-sdk

Fetch public account code automatically (from node?)

Aperta
#1,155 10 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
78
Fork
129
Merge medio
4g 14h
PR unite (30g)
52

Descrizione

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_

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.