import albedo from "@albedo-link/intent"; import { Networks, Transaction, TransactionBuilder, } from "@stellar/stellar-sdk"; import { HandlerSignTransactionParams, KeyTypeHandler, KeyType, } from "../Types"; export const albedoHandler: KeyTypeHandler = { keyType: KeyType.albedo, async signTransaction(params: HandlerSignTransactionParams) { const { transaction, key } = params; if (key.privateKey !== "") { throw new Error( `Non-ledger key sent to ledger handler: ${JSON.stringify( key.publicKey, )}`, ); } try { const xdr = transaction.toXdr(); const response = await albedo.tx({ xdr }); if (!response.signed_envelope_xdr) { throw new Error("We couldn’t sign the transaction with Albedo."); } // fromXdr() returns type "Transaction | FeeBumpTransaction" and // signTransaction() doesn't like "| FeeBumpTransaction" type, so casting // to "Transaction" type. return TransactionBuilder.fromXdr( response.signed_envelope_xdr, Networks.PUBLIC, ) as Transaction; } catch (error) { const errorMsg = error.toString(); throw new Error( `We couldn’t sign the transaction with Albedo. ${errorMsg}.`, ); } }, };