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

Direzione di ricerca

L'issue descrive un pattern di interazione complesso per il recupero e l'utilizzo del codice dell'account pubblico. Inizia esaminando il codice esistente in rust-sdk per le interazioni dell'account, gli script di transazione e la configurazione dell'Assembler. Cerca l'uso di `TransactionKernel::assembler()` e `NoteBuilder`. L'obiettivo è progettare un'API più semplice, forse `client.fetch_code(account_id)` e `TransactionScript::against_public_contract()`. Comprendere come il nodo potrebbe servire il codice e come vengono verificati gli impegni è fondamentale. Controlla la discussione collegata in project-template PR #4 per ulteriore contesto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
rust
Ambito
backend-api-design
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.