vulpemventures / marina

Liquid Wallet browser extension
MIT License
37 stars 19 forks source link

Locking utxos #316

Closed bordalix closed 2 years ago

bordalix commented 2 years ago

After used in a transaction, utxos should be locked to prevent:

bordalix commented 2 years ago

Here is the plan:

This means that spend and send must know which utxos where selected for the transaction and lock them.

My idea is to use this to store locked utxos:

    {
      ...state,
      unspentsAndTransactions: {
        ...state.unspentsAndTransactions,
        [accountID]: {
          ...state.unspentsAndTransactions[accountID],
          [network]: {
            ...state.unspentsAndTransactions[accountID][network],
            utxosMap: {
              ...state.unspentsAndTransactions[accountID][network].utxosMap,
            },
            lockedUtxos: {
              ...state.unspentsAndTransactions[accountID][network].lockedUtxos,
              [toStringOutpoint(utxo)]: 'locked',
            },
          },
        },
      },
    }

@louisinger @tiero any thoughts?

louisinger commented 2 years ago

This means that spend and send must know which utxos where selected for the transaction and lock them.

We should use the tdex-app logic: it writes a custom coin selector from redux's Dispatch function --> each time the selector is selecting a coin, a LOCK_UTXO action is dispatched.

Note that usually, the locked unspents should be unlocked after a certain amount of time (1 min for instance): it lets to ensure we get back the utxos if the transaction is never broadcasted for instance.

    {
      ...state,
      unspentsAndTransactions: {
        ...state.unspentsAndTransactions,
        [accountID]: {
          ...state.unspentsAndTransactions[accountID],
          [network]: {
            ...state.unspentsAndTransactions[accountID][network],
            utxosMap: {
              ...state.unspentsAndTransactions[accountID][network].utxosMap,
            },
            lockedUtxos: {
              ...state.unspentsAndTransactions[accountID][network].lockedUtxos,
              [toStringOutpoint(utxo)]: 'locked',
            },
          },
        },
      },
    }

I don't think we need to store a locker for each network, 1 lockedUtxos cleaned if the network changes should be enough?

bordalix commented 2 years ago

We should use the tdex-app logic: it writes a custom coin selector from redux's Dispatch function --> each time the selector is selecting a coin, a LOCK_UTXO action is dispatched.

Nice, thanks for the tip.

bordalix commented 2 years ago

Note that usually, the locked unspents should be unlocked after a certain amount of time (1 min for instance): it lets to ensure we get back the utxos if the transaction is never broadcasted for instance.

So they should store the timestamp when they are locked, and every minute an alarm should clean up all locked utxos with timestamp less then X. It works.

bordalix commented 2 years ago

I don't think we need to store a locker for each network, 1 lockedUtxos cleaned if the network changes should be enough?

Yes, it makes sense.