spacebudz / lucid

Lucid is a library, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript, Deno and Node.js.
https://lucid.spacebudz.io
MIT License
339 stars 139 forks source link

Lucid Multi Sig Transaction Example #193

Closed pablosymc closed 1 year ago

pablosymc commented 1 year ago

I'm coming from the Mesh SDK where doing a multi sig transaction is pretty simple. Essentially on the backend the steps to do a simple multi sig transaction are:

1) Get the user's stake -> Look up all the UTXO's using Blockfrost (for each address)

const address_utxos = await blockfrostProvider.fetchAddressUTxOs(
  address
);

2) Find x number of UTXO's that combined have 2 ADA in them

const lovelace_utxos = largestFirst(
  "2000000",
  address_utxos,
  true
);

3) Then I set the tx inputs to use in the transaction:

tx.setTxInputs(lovelace_utxos);

4) Send the 2 ADA to my wallet and send the change back to the user

// Send 2 ADA to the site wallet address
tx.sendLovelace(site_address, "2000000");

// User will receive change back to their wallet
tx.setChangeAddress(address);

5) Set the required signers and build the tx

// Set the required signers
tx.setRequiredSigners([address, site_address]);

// Generate the CBOR to send to the user
const unsignedTx = await tx.build();

At this point you would send the unsignedTx CBOR to the user for them to sign and then send that CBOR back to the backend to also sign, then submit the tx.

What would this look like with Lucid? I haven't been able to find any substantial examples of how I would accomplish this with Lucid (without using a Contract).

Any guidance would be appreciated!

alessandrokonrad commented 1 year ago

I don't understand step 2. Is this coin selection? In Lucid this happens automatically. Here's an example:

// .. instantiated Lucid

lucid.selectWalletFrom({address}); 

const tx = await lucid.newTx()
  .payToAddress(site_address, {lovelace: 2000000n})
  .addSigner(address)
  .addSigner(site_address)
  .complete();

const cborTx = tx.toString();

// .. sending cbor to signer (address)

const signature = await lucid.fromTx(cborTx).partialSign();

// .. sending cbor to signer (site_address)

const signature2 = await lucid.fromTx(cborTx).partialSign();

// .. assemble and submit

const txHash = await lucid.fromTx(cborTx).assemble([signature, signature2])
  .complete()
  .then(tx => tx.submit())
pablosymc commented 1 year ago

Thanks so much @alessandrokonrad. Appreciate your time! :)