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cACZ,eACA,WAAY,WAKR,EADgB,KAAK,MAAM,GACC,KAElC,OADA,QAAQ,IAAI,GACL,CACL,QAGA,QAAS,EAAY,QACrB,KAAM,EAAY,KAClB,SAAU,EAAY,SAE1B,CAAE,MAAO,GAGP,OADA,QAAQ,MAAM,0BAA2B,GAClC,IACF,EACH,KAAM,mCAEV,MAIJ,OADA,EAAe,UACR,CACT,CAOA,iBAAM,CAAa,GACjB,UAGQ,KAAK,oBAAoB,EAAM,MAMvC,CAAE,MAAO,GACP,QAAQ,MAAM,wBAAyB,EACzC,CACF,EAEF,OAAO,QAAU","file":"/npm/email-tokenator@0.1.46/src/EmailTokenator.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/email-tokenator@0.1.46/src/EmailTokenator.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const PushDropTokenator = require('pushdrop-tokenator')\nconst BabbageSDK = require('@babbage/wrapped-sdk')\nconst pushdrop = require('pushdrop')\n\nconst STANDARD_EMAIL_MESSAGEBOX = 'email_inbox'\nconst STANDARD_EMAIL_BASKET = 'email'\nconst EMAIL_PROTOCOL_ID = 'email'\nconst EMAIL_KEY_ID = 1\nconst STANDARD_TOKEN_VALUE = 1\nconst EMAIL_PROTO_ADDR = '1XKdoVfVTrtNu243T44sNFVEpeTmeYitK'\n\n/**\n * Extends the Tokenator class to enable sending email messages\n * @param {object} obj All parameters are given in an object.\n * @param {String} [obj.peerServHost] The PeerServ host you want to connect to.\n * @param {String} [obj.clientPrivateKey] A private key to use for mutual authentication with Authrite. (Optional - Defaults to Babbage signing strategy).\n */\nclass EmailTokenator extends PushDropTokenator {\n  constructor ({\n    peerServHost = 'https://staging-peerserv.babbage.systems',\n    clientPrivateKey\n  } = {}) {\n    super({\n      peerServHost,\n      clientPrivateKey,\n      defaultTokenValue: STANDARD_TOKEN_VALUE,\n      protocolID: EMAIL_PROTOCOL_ID,\n      protocolKeyID: EMAIL_KEY_ID,\n      protocolBasketName: STANDARD_EMAIL_BASKET,\n      protocolMessageBox: STANDARD_EMAIL_MESSAGEBOX,\n      protocolAddress: EMAIL_PROTO_ADDR\n    })\n  }\n\n  /**\n   * Creates an email token to send in a message to PeerServ\n   * @param {Object} message The email message to send\n   * @param {String} message.recipient The recipient of this email\n   * @param {String} message.subject The subject of the email given as a public identity key\n   * @param {String} message.body The body of the email message\n   */\n  async sendEmail (message) {\n    const emailToken = {\n      recipient: message.recipient,\n      messageBox: STANDARD_EMAIL_MESSAGEBOX,\n      body: {\n        subject: message.subject,\n        body: message.body,\n        dateSent: new Date()\n      }\n    }\n    return await this.sendPushDropToken(emailToken, ['email_outgoing'])\n  }\n\n  /**\n   * Wrapper function that lists incoming emails from PeerServ, and submits them into a private basket\n   * @returns {Array} of incoming emails from PeerServ\n   */\n  async checkEmail () {\n    // Use BabbageSDK or private key for signing strategy\n    return await this.receivePushDropTokens()\n  }\n\n  /**\n   * Reads email messages from a private basket according to the standard protocol\n   * @param {Boolean} - If provided, lists only outgoing email\n   * @returns {Array} An array of email messages\n   */\n  async readEmail (outgoing) {\n    let tags\n    if (outgoing) {\n      tags = ['email_outgoing']\n    }\n    const emailFromBasket = await BabbageSDK.getTransactionOutputs({\n      // The name of the basket where the tokens are kept\n      basket: this.protocolBasketName,\n      // Only get tokens that are active on the list, not already spent\n      spendable: true,\n      includeEnvelope: true,\n      tags\n    })\n\n    // Decrypt the user's email\n    const decryptedEmail = await Promise\n      .all(emailFromBasket.map(async email => {\n        try {\n          const token = {\n            ...email.envelope,\n            lockingScript: email.outputScript,\n            txid: email.txid,\n            outputIndex: email.vout,\n            satoshis: email.amount\n          }\n\n          // Get custom instructions if provided\n          let counterparty = 'self'\n          if (email.customInstructions) {\n            token.customInstructions = JSON.parse(email.customInstructions)\n            counterparty = token.customInstructions.recipient\n          }\n\n          const decodedEmail = pushdrop.decode({ script: email.outputScript })\n          const encryptedEmail = decodedEmail.fields[1]\n          const decryptedEmail = await BabbageSDK.decrypt({\n            ciphertext: Buffer.from(encryptedEmail, 'hex'),\n            protocolID: this.protocolID,\n            keyID: this.protocolKeyID,\n            counterparty,\n            returnType: 'string'\n          })\n          // Now we can return the decrypted version of the email, along\n          // with some information about the token.\n          const parsedMessage = JSON.parse(decryptedEmail)\n          const parsedEmail = parsedMessage.body\n          console.log(decryptedEmail)\n          return {\n            token,\n            // Finally, we include the email that we've just decrypted, for\n            // showing in the email list\n            subject: parsedEmail.subject,\n            body: parsedEmail.body,\n            dateSent: parsedEmail.dateSent\n          }\n        } catch (e) {\n        // In case there are any errors, we'll handle them gracefully.\n          console.error('Error decrypting email:', e)\n          return {\n            ...email,\n            note: '[error] Unable to decrypt email!'\n          }\n        }\n      }))\n    // We reverse the list, so the newest emails show up at the top\n    decryptedEmail.reverse()\n    return decryptedEmail\n  }\n\n  /**\n   * Removes an email token from a user's private basket\n   * @param {Object} email the email object\n   * @param {Object} email.token the email token to delete\n   */\n  async deleteEmail (email) {\n    try {\n      // If the email token belongs to the current user, pushdrop.redeem will work to unbasket the output.\n      // If it is an email token you can read, but not spend, unbasketOutput would be the function to use.\n      await this.redeemPushDropToken(email.token)\n      // await BabbageSDK.unbasketOutput({\n      //   txid: email.token.txid,\n      //   basket: this.protocolBasketName,\n      //   vout: 0\n      // })\n    } catch (error) {\n      console.error('Error deleting email:', error)\n    }\n  }\n}\nmodule.exports = EmailTokenator\n"]}