Skip to content

Commit

Permalink
chore: cleanup & simplify environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
shelegdmitriy committed Jul 24, 2024
1 parent 25ea940 commit 7de40ad
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 139 deletions.
18 changes: 17 additions & 1 deletion apps/ticketing/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,21 @@ APPLE_WALLET_CERTIFICATE_PASSWORD=

# Public (Server + Client):

# testnet
NEXT_PUBLIC_HOSTNAME=http://localhost:3000
NEXT_PUBLIC_NETWORK_ID=testnet
NEXT_PUBLIC_NETWORK_ID=testnet
NEXT_PUBLIC_NETWORK_NODE_URL=https://rpc.testnet.near.org
NEXT_PUBLIC_EVENTS_WORKER_BASE=https://stripe-worker-template.keypom.workers.dev
NEXT_PUBLIC_KEYPOM_CONTRACT_ID=v2.keypom.testnet
NEXT_PUBLIC_KEYPOM_EVENTS_CONTRACT_ID=1717013187496-kp-ticketing.testnet
NEXT_PUBLIC_KEYPOM_MARKETPLACE_CONTRACT_ID=1717013187496-marketplace.testnet
NEXT_PUBLIC_CLOUDFLARE_IPFS=https://cloudflare-ipfs.com/ipfs

# mainnet
# NEXT_PUBLIC_NETWORK_ID=mainnet
# NEXT_PUBLIC_NETWORK_NODE_URL=https://rpc.mainnet.near.org
# NEXT_PUBLIC_EVENTS_WORKER_BASE=https://stripe-worker-production.keypom.workers.dev
# NEXT_PUBLIC_KEYPOM_CONTRACT_ID=v2.keypom.near
# NEXT_PUBLIC_KEYPOM_EVENTS_CONTRACT_ID=ticketing-v1.keypom.near
# NEXT_PUBLIC_KEYPOM_MARKETPLACE_CONTRACT_ID=marketplace-v1.keypom.near
# NEXT_PUBLIC_CLOUDFLARE_IPFS=https://cloudflare-ipfs.com/ipfs
2 changes: 1 addition & 1 deletion apps/ticketing/src/hooks/useDrops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { handleClientError } from '@pagoda/ui/src/utils/error';
import { useQuery } from '@tanstack/react-query';

import { useNearStore } from '@/stores/near';
import { KEYPOM_EVENTS_CONTRACT_ID } from '@/utils/common';
import { KEYPOM_EVENTS_CONTRACT_ID } from '@/utils/config';
import { EventDrop, TicketMetadataExtra } from '@/utils/helpers';
import { validateDateAndTime } from '@/utils/time';

Expand Down
2 changes: 1 addition & 1 deletion apps/ticketing/src/hooks/useEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { handleClientError } from '@pagoda/ui/src/utils/error';
import { useQuery } from '@tanstack/react-query';

import { useNearStore } from '@/stores/near';
import { KEYPOM_EVENTS_CONTRACT_ID } from '@/utils/common';
import { KEYPOM_EVENTS_CONTRACT_ID } from '@/utils/config';
import { FunderEventMetadata } from '@/utils/helpers';

export function useEvents(accountId: string | undefined) {
Expand Down
3 changes: 1 addition & 2 deletions apps/ticketing/src/hooks/useNearInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { useRef } from 'react';
import { useEffect } from 'react';

import { useNearStore } from '@/stores/near';
import { KEYPOM_CONTRACT_ID } from '@/utils/common';
import { NETWORK_ID, NETWORK_NODE_URL } from '@/utils/config';
import { KEYPOM_CONTRACT_ID, NETWORK_ID, NETWORK_NODE_URL } from '@/utils/config';

export function useNearInitializer() {
const connectPromise = useRef<Promise<Near> | null>(null);
Expand Down
2 changes: 1 addition & 1 deletion apps/ticketing/src/hooks/usePurchasedTickets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useQuery } from '@tanstack/react-query';
import { Account } from 'near-api-js';

import { useNearStore } from '@/stores/near';
import { KEYPOM_EVENTS_CONTRACT_ID } from '@/utils/common';
import { KEYPOM_EVENTS_CONTRACT_ID } from '@/utils/config';
import { EventDrop, FunderEventMetadata, TicketMetadataExtra } from '@/utils/helpers';

export function usePurchasedTickets(secretKeys: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion apps/ticketing/src/hooks/useStripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { v4 as uuidv4 } from 'uuid';

import { useNearStore } from '@/stores/near';
import { useStripeStore } from '@/stores/stripe';
import { EVENTS_WORKER_BASE, KEYPOM_MARKETPLACE_CONTRACT_ID } from '@/utils/common';
import { EVENTS_WORKER_BASE, KEYPOM_MARKETPLACE_CONTRACT_ID } from '@/utils/config';

export function useStripe(accountId: string | undefined, stripeCheckout: boolean) {
const viewAccount = useNearStore((store) => store.viewAccount);
Expand Down
2 changes: 1 addition & 1 deletion apps/ticketing/src/hooks/useWalletInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRef } from 'react';
import { useEffect } from 'react';

import { useWalletStore } from '@/stores/wallet';
import { KEYPOM_CONTRACT_ID } from '@/utils/common';
import { KEYPOM_CONTRACT_ID } from '@/utils/config';
import { WALLET_SELECTOR_PARAMS } from '@/utils/wallet';

export function useWalletInitializer() {
Expand Down
6 changes: 3 additions & 3 deletions apps/ticketing/src/pages/events/[eventId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useRouter } from 'next/router';

import { useDefaultLayout } from '@/hooks/useLayout';
import { useWalletStore } from '@/stores/wallet';
import { CLOUDFLARE_IPFS } from '@/utils/common';
import { CLOUDFLARE_IPFS } from '@/utils/config';
import { HOSTNAME } from '@/utils/config';
import { displayEventDate } from '@/utils/date';
import { parseEventIdQueryParam } from '@/utils/event-id';
Expand All @@ -24,8 +24,8 @@ import { fetchEventFromJsonRpc } from '@/utils/rpc';
import type { EventDetails, NextPageWithLayout } from '@/utils/types';

/*
NOTE: This page is a bit of an outlier. Due to this particular page being frequently shared
via social media, it's important to fetch the event data on the server side so that we can
NOTE: This page is a bit of an outlier. Due to this particular page being frequently shared
via social media, it's important to fetch the event data on the server side so that we can
properly render meta tags to populate dynamic preview content when shared.
*/

Expand Down
2 changes: 1 addition & 1 deletion apps/ticketing/src/pages/events/[eventId]/tickets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { useDrops } from '@/hooks/useDrops';
import { useEvent } from '@/hooks/useEvents';
import { useDefaultLayout } from '@/hooks/useLayout';
import { useNearStore } from '@/stores/near';
import { CLOUDFLARE_IPFS } from '@/utils/common';
import { CLOUDFLARE_IPFS } from '@/utils/config';
import { displayEventDate } from '@/utils/date';
import { formatDollar, formatTicketPrice } from '@/utils/dollar';
import { formatEventIdQueryParam, parseEventIdQueryParam } from '@/utils/event-id';
Expand Down
2 changes: 1 addition & 1 deletion apps/ticketing/src/pages/events/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { useStripe } from '@/hooks/useStripe';
import { useNearStore } from '@/stores/near';
import { useStripeStore } from '@/stores/stripe';
import { useWalletStore } from '@/stores/wallet';
import { EVENTS_WORKER_BASE, KEYPOM_EVENTS_CONTRACT_ID } from '@/utils/common';
import { EVENTS_WORKER_BASE, KEYPOM_EVENTS_CONTRACT_ID } from '@/utils/config';
import {
createPayload,
estimateCosts,
Expand Down
10 changes: 5 additions & 5 deletions apps/ticketing/src/pages/tickets/purchased.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
NOTE: This page supports rendering single or multiple purchased tickets based on
NOTE: This page supports rendering single or multiple purchased tickets based on
the number of comma separated values that exist in the URL hash:
- Single ticket: /tickets/purchased#1
Expand Down Expand Up @@ -29,7 +29,7 @@ import { AddToAppleWallet } from '@/components/AddToAppleWallet';
import { AddToGoogleWallet } from '@/components/AddToGoogleWallet';
import { useDefaultLayout } from '@/hooks/useLayout';
import { usePurchasedTickets } from '@/hooks/usePurchasedTickets';
import { CLOUDFLARE_IPFS } from '@/utils/common';
import { CLOUDFLARE_IPFS } from '@/utils/config';
import { displayEventDate } from '@/utils/date';
import { formatEventIdQueryParam } from '@/utils/event-id';
import { convertToSafeFilename } from '@/utils/file';
Expand All @@ -45,10 +45,10 @@ const PurchasedTickets: NextPageWithLayout = () => {

useEffect(() => {
/*
NOTE: The secret keys are stored and read from "location.hash" instead of
a query param to help provide an extra layer of privacy since URI fragments
NOTE: The secret keys are stored and read from "location.hash" instead of
a query param to help provide an extra layer of privacy since URI fragments
are never sent to the server (they're client side only).
https://en.wikipedia.org/wiki/URI_fragment
*/

Expand Down
2 changes: 1 addition & 1 deletion apps/ticketing/src/stores/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useQuery } from '@tanstack/react-query';
import { v4 as uuidv4 } from 'uuid';
import { create } from 'zustand';

import { EVENTS_WORKER_BASE, KEYPOM_MARKETPLACE_CONTRACT_ID } from '@/utils/common';
import { EVENTS_WORKER_BASE, KEYPOM_MARKETPLACE_CONTRACT_ID } from '@/utils/config';

import { useNearStore } from './near';

Expand Down
62 changes: 0 additions & 62 deletions apps/ticketing/src/utils/common.ts

This file was deleted.

11 changes: 9 additions & 2 deletions apps/ticketing/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ export const APPLE_WALLET_CERTIFICATE_PASSWORD = process.env.APPLE_WALLET_CERTIF
export const APPLE_WALLET_CERTIFICATE_PEM = process.env.PASS_COM_PAGODA_TICKETING_PEM;
export const HOSTNAME = process.env.NEXT_PUBLIC_HOSTNAME || 'http://localhost:3000';
export const NETWORK_ID: NetworkId = (process.env.NEXT_PUBLIC_NETWORK_ID as NetworkId) || 'testnet';
export const NETWORK_NODE_URL =
NETWORK_ID === 'mainnet' ? 'https://rpc.mainnet.near.org' : 'https://rpc.testnet.near.org';
export const NETWORK_NODE_URL = process.env.NEXT_PUBLIC_NETWORK_NODE_URL || 'https://rpc.testnet.near.org';
export const EVENTS_WORKER_BASE =
process.env.NEXT_PUBLIC_EVENTS_WORKER_BASE || 'https://stripe-worker-template.keypom.workers.dev';
export const KEYPOM_CONTRACT_ID = process.env.NEXT_PUBLIC_KEYPOM_CONTRACT_ID || 'v2.keypom.testnet';
export const KEYPOM_EVENTS_CONTRACT_ID =
process.env.NEXT_PUBLIC_KEYPOM_EVENTS_CONTRACT_ID || '1717013187496-kp-ticketing.testnet';
export const KEYPOM_MARKETPLACE_CONTRACT_ID =
process.env.NEXT_PUBLIC_KEYPOM_MARKETPLACE_CONTRACT_ID || '1717013187496-marketplace.testnet';
export const CLOUDFLARE_IPFS = process.env.NEXT_PUBLIC_CLOUDFLARE_IPFS || 'https://cloudflare-ipfs.com/ipfs';
34 changes: 1 addition & 33 deletions apps/ticketing/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { type Action } from '@near-wallet-selector/core';
import { utils } from 'near-api-js';
import { parseNearAmount } from 'near-api-js/lib/utils/format';

import { KEYPOM_MARKETPLACE_CONTRACT_ID } from './common';
import { KEYPOM_MARKETPLACE_CONTRACT_ID } from './config';
import { getByteSize } from './crypto-helpers';
import { localStorageGet } from './local-storage';

export interface DateAndTimeInfo {
startDate: number; // Milliseconds from Unix Epoch
Expand Down Expand Up @@ -232,11 +231,6 @@ function arrayBufferToBase64(buffer: any) {
export const estimateCosts = ({ formData }: { formData: FormSchema }) => {
const eventId = Date.now().toString();

const masterKey = localStorageGet('MASTER_KEY') ?? '';
if (!masterKey) {
console.warn('Missing local storage value MASTER_KEY inside createPayload()');
}

const eventMetadata: FunderEventMetadata = {
name: formData.name,
dateCreated: Date.now().toString(),
Expand Down Expand Up @@ -293,11 +287,6 @@ export const createPayload = async ({
eventId: string;
stripeAccountId: string;
}): Promise<{ actions: Action[]; dropIds: string[] }> => {
const masterKey = localStorageGet('MASTER_KEY') ?? '';
if (!masterKey) {
console.warn('Missing local storage value MASTER_KEY inside createPayload()');
}

const funderMetadata: FunderMetadata = {};

const eventMetadata: FunderEventMetadata = {
Expand All @@ -313,26 +302,9 @@ export const createPayload = async ({
},
artwork: eventArtworkCid,
sellable: formData.sellable,
// questions: formData.questions.map((question: { question: any; isRequired: any; }) => ({
// question: question.question,
// required: question.isRequired || false,
// })),
id: eventId.toString(),
};

// if (formData.questions.length > 0) {
// const { publicKey, privateKey } = await generateKeyPair();
// const saltBytes = window.crypto.getRandomValues(new Uint8Array(16));
// const saltBase64 = uint8ArrayToBase64(saltBytes);
// const symmetricKey = await deriveKeyFromPassword(masterKey, saltBase64);
// const { encryptedPrivateKeyBase64, ivBase64 } = await encryptPrivateKey(privateKey, symmetricKey);

// eventMetadata.pubKey = await exportPublicKeyToBase64(publicKey);
// eventMetadata.encPrivKey = encryptedPrivateKeyBase64;
// eventMetadata.iv = ivBase64;
// eventMetadata.salt = saltBase64;
// }

funderMetadata[eventId] = eventMetadata;

const drop_ids: string[] = [];
Expand All @@ -356,7 +328,6 @@ export const createPayload = async ({

const ticketExtra: TicketMetadataExtra = {
dateCreated: Date.now().toString(),
// price: parseNearAmount(ticket.price)!.toString(),
priceNear: ticket.priceNear,
priceFiat: ticket.priceFiat,
salesValidThrough: ticket.salesValidThrough,
Expand All @@ -378,9 +349,6 @@ export const createPayload = async ({
price: parseNearAmount(ticket.priceNear || '0')!.toString(),
sale_start: Date.now() || undefined,
sale_end: Date.parse(formData.date) || undefined,
// ------------ pattern for allowing start and end sales date individually for each ticket
// sale_start: ticket.salesValidThrough.startDate || undefined,
// sale_end: ticket.salesValidThrough.endDate || undefined,
};

const dropConfig = {
Expand Down
13 changes: 0 additions & 13 deletions apps/ticketing/src/utils/local-storage.ts

This file was deleted.

13 changes: 5 additions & 8 deletions apps/ticketing/src/utils/purchase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import assert from 'assert';
import { Account } from 'near-api-js';

import { DropsByEventId } from '@/hooks/useDrops';
import { KEYPOM_MARKETPLACE_CONTRACT_ID } from '@/utils/common';

import { botCheck } from './bot-check';
import { EVENTS_WORKER_BASE } from './common';
import { CLOUDFLARE_IPFS, EVENTS_WORKER_BASE, KEYPOM_MARKETPLACE_CONTRACT_ID } from './config';
import { FunderEventMetadata } from './helpers';

type PurchaseWorkerPayload = {
Expand Down Expand Up @@ -89,10 +88,8 @@ export async function purchaseTickets({
//const publicKey = await base64ToPublicKey(publicKeyBase64);
//const buyerAnswers = await encryptWithPublicKey(JSON.stringify({ questions: {} }), publicKey); // TODO: Eventually support questions in our UI and record answers here

const eventImageUrl = event.artwork ? `https://cloudflare-ipfs.com/ipfs/${event.artwork}` : '';
const ticketImageUrl = drop.ticket.artwork
? `https://cloudflare-ipfs.com/ipfs/${drop.ticket.artwork}`
: eventImageUrl ?? '';
const eventImageUrl = event.artwork ? `${CLOUDFLARE_IPFS}/${event.artwork}` : '';
const ticketImageUrl = drop.ticket.artwork ? `${CLOUDFLARE_IPFS}/${drop.ticket.artwork}` : eventImageUrl ?? '';

const workerPayload: PurchaseWorkerPayload = {
name: null,
Expand Down Expand Up @@ -142,8 +139,8 @@ export async function purchaseTickets({
data.tickets.forEach((t) => purchases.push({ secretKey: t.secret_key }));
} else {
/*
TODO: We'll need to think through how we redirect to Stripe after exiting this loop.
We need to make sure any and all free tickets are handled first before redirecting
TODO: We'll need to think through how we redirect to Stripe after exiting this loop.
We need to make sure any and all free tickets are handled first before redirecting
to Stripe.
*/
console.error(response);
Expand Down
3 changes: 1 addition & 2 deletions apps/ticketing/src/utils/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { KEYPOM_EVENTS_CONTRACT_ID } from './common';
import { NETWORK_NODE_URL } from './config';
import { KEYPOM_EVENTS_CONTRACT_ID, NETWORK_NODE_URL } from './config';
import { FunderEventMetadata } from './helpers';

function encodeJsonRpcArgs(args: Record<any, any>) {
Expand Down

0 comments on commit 7de40ad

Please sign in to comment.