Anastasia-Labs / lucid-evolution

https://anastasia-labs.github.io/lucid-evolution/
22 stars 7 forks source link

Feature request: `selectWallet.fromAddress` method #130

Closed SynthLuvr closed 1 month ago

SynthLuvr commented 1 month ago

As a developer, I want to load a wallet from an address and selected UTxOs. I propose adding the following method to the LucidEvolution type. A method like the following would be useful:

type LucidEvolution = {
  selectWallet: {
    fromAddress: (address: string, utxos: UTxO[]) => void;
  };
};

This method was available in the old Lucid codebase, and would be good to include in Lucid Evolution. Currently, we have a workaround that involves manually creating a wallet instance. Here is the workaround code:

const makeWallet = (
  address: string,
  utxos: UTxO[],
  provider: Provider
): Wallet => {
  address = CML.Address.from_bech32(address).to_bech32(undefined);

  const parsedAddress = CML.BaseAddress.from_address(
    CML.Address.from_bech32(address)
  );
  invariant(parsedAddress);
  const pubKey = parsedAddress.stake().as_pub_key();
  invariant(pubKey);
  const rewardAddress = CML.RewardAddress.new(
    parsedAddress.to_address().network_id(),
    CML.Credential.new_pub_key(CML.Ed25519KeyHash.from_hex(pubKey.to_hex()))
  )
    .to_address()
    .to_bech32(undefined);

  return {
    address: async (): Promise<Address> => address,
    rewardAddress: async (): Promise<RewardAddress | null> => rewardAddress,
    getUtxos: async (): Promise<UTxO[]> => utxos,
    getUtxosCore: async (): Promise<CML.TransactionUnspentOutput[]> =>
      utxos.map(utxoToCore),
    getDelegation: async (): Promise<Delegation> =>
      provider.getDelegation(rewardAddress),
    signTx: async (
      _tx: CML.Transaction
    ): Promise<CML.TransactionWitnessSet> => {
      throw new Error("Not implemented");
    },
    signMessage: async (
      _address: Address | RewardAddress,
      _payload: Payload
    ): Promise<SignedMessage> => {
      throw new Error("Not implemented");
    },
    submitTx: async (_tx: Transaction): Promise<TxHash> => {
      throw new Error("Not implemented");
    },
  };
};
SynthLuvr commented 1 month ago

Working as expected