{"version":3,"file":"Signer.cjs","sources":["../../src/Signer.ts"],"sourcesContent":["import {\n  PublicKey,\n  PublicKeyInput,\n} from '@metaplex-foundation/umi-public-keys';\nimport { Transaction } from './Transaction';\nimport { uniqueBy } from './utils';\n\n/**\n * Defines a public key that can sign transactions and messages.\n * @category Context and Interfaces\n */\nexport interface Signer {\n  /** The public key of the Signer. */\n  readonly publicKey: PublicKey;\n  /** Signs the given message. */\n  readonly signMessage: (message: Uint8Array) => Promise<Uint8Array>;\n  /** Signs the given transaction. */\n  readonly signTransaction: (transaction: Transaction) => Promise<Transaction>;\n  /** Signs all the given transactions at once. */\n  readonly signAllTransactions: (\n    transactions: Transaction[]\n  ) => Promise<Transaction[]>;\n}\n\n/**\n * Signs a transaction using the provided signers.\n * @category Signers and PublicKeys\n */\nexport const signTransaction = async (\n  transaction: Transaction,\n  signers: Signer[]\n): Promise<Transaction> =>\n  signers.reduce(async (promise, signer) => {\n    const unsigned = await promise;\n    return signer.signTransaction(unsigned);\n  }, Promise.resolve(transaction));\n\n/**\n * Signs multiple transactions using the provided signers\n * such that signers that need to sign multiple transactions\n * sign them all at once using the `signAllTransactions` method.\n *\n * @category Signers and PublicKeys\n */\nexport const signAllTransactions = async (\n  transactionsWithSigners: {\n    transaction: Transaction;\n    signers: Signer[];\n  }[]\n): Promise<Transaction[]> => {\n  const transactions = transactionsWithSigners.map((item) => item.transaction);\n  const signersWithTransactions = transactionsWithSigners.reduce(\n    (all, { signers }, index) => {\n      signers.forEach((signer) => {\n        const item = all.find(\n          (item) => item.signer.publicKey === signer.publicKey\n        );\n        if (item) {\n          item.indices.push(index);\n        } else {\n          all.push({ signer, indices: [index] });\n        }\n      });\n      return all;\n    },\n    [] as { signer: Signer; indices: number[] }[]\n  );\n\n  return signersWithTransactions.reduce(\n    async (promise, { signer, indices }) => {\n      const transactions = await promise;\n      if (indices.length === 1) {\n        const unsigned = transactions[indices[0]];\n        transactions[indices[0]] = await signer.signTransaction(unsigned);\n        return transactions;\n      }\n      const unsigned = indices.map((index) => transactions[index]);\n      const signed = await signer.signAllTransactions(unsigned);\n      indices.forEach((index, position) => {\n        transactions[index] = signed[position];\n      });\n      return transactions;\n    },\n    Promise.resolve(transactions)\n  );\n};\n\n/**\n * Whether the provided value is a `Signer`.\n * @category Signers and PublicKeys\n */\nexport const isSigner = (value: PublicKeyInput | Signer): value is Signer =>\n  typeof value === 'object' && 'publicKey' in value && 'signMessage' in value;\n\n/**\n * Deduplicates the provided signers by public key.\n * @category Signers and PublicKeys\n */\nexport const uniqueSigners = (signers: Signer[]): Signer[] =>\n  uniqueBy(signers, (a, b) => a.publicKey === b.publicKey);\n\n/**\n * Creates a `Signer` that, when required to sign, does nothing.\n * This can be useful when libraries require a `Signer` but\n * we don't have one in the current environment. For example,\n * if the transaction will then be signed in a backend server.\n *\n * @category Signers and PublicKeys\n */\nexport const createNoopSigner = (publicKey: PublicKey): Signer => ({\n  publicKey,\n  async signMessage(message: Uint8Array): Promise<Uint8Array> {\n    return message;\n  },\n  async signTransaction(transaction: Transaction): Promise<Transaction> {\n    return transaction;\n  },\n  async signAllTransactions(\n    transactions: Transaction[]\n  ): Promise<Transaction[]> {\n    return transactions;\n  },\n});\n\n/**\n * Creates a `Signer` that, when required to sign, throws an error.\n * @category Signers and PublicKeys\n */\nexport function createNullSigner(): Signer {\n  const error = new Error(\n    'Trying to use a NullSigner. ' +\n      'Did you forget to set a Signer on your Umi instance? ' +\n      'See the `signerIdentity` method for more information.'\n  );\n  const errorHandler = () => {\n    throw error;\n  };\n  return {\n    get publicKey(): PublicKey {\n      throw error;\n    },\n    signMessage: errorHandler,\n    signTransaction: errorHandler,\n    signAllTransactions: errorHandler,\n  };\n}\n"],"names":["signTransaction","transaction","signers","reduce","promise","signer","unsigned","Promise","resolve","signAllTransactions","transactionsWithSigners","transactions","map","item","signersWithTransactions","all","index","forEach","find","publicKey","indices","push","length","signed","position","isSigner","value","uniqueSigners","uniqueBy","a","b","createNoopSigner","signMessage","message","createNullSigner","error","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