message: feat(utxo): accept txBuilder in signAndVerifyWalletTransaction
diff: @@ -111,32 +111,41 @@ export class TransactionSigningError extends Error {
  * Collects and logs signing errors and verification errors, throws error in the end if any of them
  * failed.
  *
  - * @param transaction - wallet transactions to be signed
  + * @param transaction - wallet transaction (builder) to be signed
  * @param unspents - transaction unspents
  * @param walletSigner - signing parameters
  * @param isLastSignature - Returns full-signed transaction when true. Builds half-signed when false.
  */
  export function signAndVerifyWalletTransaction(
  - transaction: utxolib.bitgo.UtxoTransaction,
  + transaction: utxolib.bitgo.UtxoTransaction | utxolib.bitgo.UtxoTransactionBuilder,
  unspents: Unspent[],
  walletSigner: WalletUnspentSigner<RootWalletKeys>,
  { isLastSignature }: { isLastSignature: boolean }
  -): utxolib.Transaction {
  +): utxolib.bitgo.UtxoTransaction {
  + const network = transaction.network as utxolib.Network;
  + const prevOutputs = unspents.map((u) => toOutput(u, network));
  +
  + let txBuilder: utxolib.bitgo.UtxoTransactionBuilder;
  + if (transaction instanceof utxolib.bitgo.UtxoTransaction) {
  + txBuilder = utxolib.bitgo.createTransactionBuilderFromTransaction(transaction, prevOutputs);
  if (transaction.ins.length !== unspents.length) {
  throw new Error(`transaction inputs must match unspents`);
  }
  - const prevOutputs = unspents.map((u) => toOutput(u, transaction.network));
  - const txBuilder = utxolib.bitgo.createTransactionBuilderFromTransaction(transaction, prevOutputs);
  + } else if (transaction instanceof utxolib.bitgo.UtxoTransactionBuilder) {
  + txBuilder = transaction;
  + } else {
  + throw new Error(`must pass UtxoTransaction or UtxoTransactionBuilder`);
  + }
  const signErrors: InputSigningError[] = unspents
  .map((unspent: Unspent, inputIndex: number) => {
  try {
  - if (isReplayProtectionUnspent(unspent, transaction.network)) {
  - debug('Skipping signature for input %d of %d (RP input?)', inputIndex + 1, transaction.ins.length);
  + if (isReplayProtectionUnspent(unspent, network)) {
  + debug('Skipping signature for input %d of %d (RP input?)', inputIndex + 1, unspents.length);
  return;
  }
  signWalletTransactionWithUnspent(txBuilder, inputIndex, unspent, walletSigner);
  - debug('Successfully signed input %d of %d', inputIndex + 1, transaction.ins.length);
  + debug('Successfully signed input %d of %d', inputIndex + 1, unspents.length);
  } catch (e) {
  return new InputSigningError(inputIndex, unspent, e);
  }
  @@ -149,11 +158,11 @@ export function signAndVerifyWalletTransaction(
  .map((input, inputIndex) => {
  const unspent = unspents[inputIndex] as Unspent;
  try {
  - if (isReplayProtectionUnspent(unspent, transaction.network)) {
  + if (isReplayProtectionUnspent(unspent, network)) {
  debug(
  'Skipping input signature %d of %d (unspent from replay protection address which is platform signed only)',
  inputIndex + 1,
  - transaction.ins.length
  + unspents.length
  );
  return;
  }
  