@tiplink/api
Version:
Api for creating and sending TipLinks
173 lines (172 loc) • 7.33 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mailEscrow = exports.mail = exports.getReceiverEmail = exports.createReceiverTipLink = void 0;
const web3_js_1 = require("@solana/web3.js");
const email_1 = require("./email");
const constants_1 = require("./escrow/constants");
const DEFAULT_ENCLAVE_ENDPOINT = "https://mailer.tiplink.io";
const ENCLAVE_ENDPOINT = process !== undefined && process.env !== undefined
? process.env.NEXT_PUBLIC_ENCLAVE_ENDPOINT_OVERRIDE ||
DEFAULT_ENCLAVE_ENDPOINT
: DEFAULT_ENCLAVE_ENDPOINT;
/**
* Asynchronously calls secure enclave to create a TipLink, store it with an associated email, and return its public key.
*
* @param apiKey - The API key to be used for the request.
* @param email - The email address to be associated with the receiver tiplink.
* @returns A promise that resolves to the PublicKey of the receiver tiplink.
* @throws Throws an error if the HTTPS request fails with a non-ok status.
*/
function createReceiverTipLink(apiKey, email) {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield (0, email_1.isEmailValid)(email))) {
throw new Error("Invalid email address");
}
const endpoint = `${ENCLAVE_ENDPOINT}/api/v1/generated-tiplinks/create`;
const res = yield fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey,
},
body: JSON.stringify({ email }),
});
if (!res.ok) {
throw new Error(`HTTP error, status: ${res.status}`);
}
const { data: { publicKey: publicKeyStr }, } = yield res.json();
return new web3_js_1.PublicKey(publicKeyStr);
});
}
exports.createReceiverTipLink = createReceiverTipLink;
/**
* Asynchronously calls secure enclave to retrieve the email associated with a receiver TipLink.
*
* @param apiKey - The API key to be used for the request.
* @param publicKey - The public key of the TipLink for which to retrieve the associated email.
* @returns A promise that resolves to the email address associated with the provided TipLink public key.
* @throws Throws an error if the HTTPS request fails with a non-ok status.
*/
function getReceiverEmail(apiKey, publicKey) {
return __awaiter(this, void 0, void 0, function* () {
// We actually no longer hit the enclave here but we'll keep in this file
// since the enclave manages this data.
const endpoint = `${constants_1.INDEXER_URL_BASE}/api/v1/generated-tiplinks/${publicKey.toString()}/email`;
const res = yield fetch(endpoint, {
method: "GET",
headers: {
"x-api-key": apiKey,
},
});
if (!res.ok) {
throw new Error(`HTTP error, status: ${res.status}`);
}
const { data: { email }, } = yield res.json();
return email;
});
}
exports.getReceiverEmail = getReceiverEmail;
/**
* @deprecated We are sunsetting this feature and will only be supporting
* emailiing EscrowTipLinks. We recommend not using this feature.
*
* Asynchronously emails a TipLink.
*
* @param apiKey - The API key to be used for the request.
* @param tipLink - The TipLink object to be sent.
* @param toEmail - The email address of the recipient.
* @param toName - Optional name of the recipient for the email.
* @param replyEmail - Optional email address for the recipient to reply to.
* @param replyName - Optional name of the sender for the email.
* @param templateName - Optional name of the template to be used for the email.
* @returns A promise that resolves when the email has been sent.
* @throws Throws an error if the HTTP request fails with a non-ok status.
*/
function mail(apiKey, tipLink, toEmail, toName, replyEmail, replyName, templateName) {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield (0, email_1.isEmailValid)(toEmail))) {
throw new Error("Invalid email address");
}
const url = `${ENCLAVE_ENDPOINT}/api/v1/email/send`;
const body = {
toEmail: toEmail,
toName,
replyEmail,
replyName,
tiplinkUrl: tipLink.url.toString(),
templateName,
};
const res = yield fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey,
},
body: JSON.stringify(body),
});
if (!res.ok) {
throw new Error(`HTTP error, status: ${res.status}`);
}
console.log("TipLink sent!", res);
});
}
exports.mail = mail;
/**
* Asynchronously emails a deposited Escrow TipLink to a pre-defined recipient.
*/
function mailEscrow(args) {
return __awaiter(this, void 0, void 0, function* () {
// TODO: Require API key / ensure deposited
const { apiKey, toName, replyEmail, replyName, templateName } = args;
const { escrowTipLink } = args;
let { toEmail, pda, receiverTipLink } = args;
if (escrowTipLink) {
toEmail = escrowTipLink.toEmail;
pda = escrowTipLink.pda;
receiverTipLink = escrowTipLink.receiverTipLink;
}
if (!(yield (0, email_1.isEmailValid)(toEmail))) {
throw new Error("Invalid email address");
}
// Sanity check; error checking occurs in the enclave and on-chain program
if (!toEmail || !pda || !receiverTipLink) {
throw new Error("Improper escrow.");
}
const url = `${ENCLAVE_ENDPOINT}/api/v1/email/send/escrow`;
const receiverUrlOverride = process !== undefined && process.env !== undefined
? process.env.NEXT_PUBLIC_ESCROW_RECEIVER_URL_OVERRIDE || undefined
: undefined;
const body = {
toEmail,
toName,
replyEmail,
replyName,
pda: pda.toString(),
tiplinkPublicKey: receiverTipLink.toString(),
receiverUrlOverride,
templateName,
};
const res = yield fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey,
},
body: JSON.stringify(body),
});
if (!res.ok) {
throw new Error(`HTTP error, status: ${res.status}`);
}
console.log("Escrow TipLink sent!", res);
});
}
exports.mailEscrow = mailEscrow;
;