Skip to content

Commit

Permalink
Problem: people can submit invalid rocket names
Browse files Browse the repository at this point in the history
  • Loading branch information
gsovereignty committed Nov 9, 2023
1 parent 5202102 commit 4c92b99
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
33 changes: 25 additions & 8 deletions src/components/modals/CreateRocket.svelte
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
<script lang="ts">
import makeEvent from "$lib/helpers/eventMaker";
import { rocketNameValidator, simulateEvents } from "../../settings";
import { validateIdentity } from "$lib/protocol_validators/rockets";
import { currentUser } from "$lib/stores/hot_resources/current-user";
import { nameIsUnique } from "$lib/stores/nostrocket_state/hard_state/rockets";
import { Button, Form, Modal, TextInput } from "carbon-components-svelte";
import { Rocket } from "carbon-pictograms-svelte";
import { get } from "svelte/store";
import { rocketNameValidator, simulateEvents } from "../../settings";
import LoginNip07Button from "../elements/LoginNIP07Button.svelte";
let formOpen = false;
let rocketName = "";
let formValidation = true;
let disableButton = true;
let nameError = "";
let nameInvalid = false;
let nameInvalid = true;
function reset() {
rocketName = "";
nameError = "";
}
function validate() {
if (!rocketNameValidator.test(rocketName)) {
$: {
disableButton = true
if (!get(currentUser)?.pubkey) {
nameError = "You must login first"
} else if (!validateIdentity(get(currentUser)!.pubkey)) {
nameError = "You must be in the Identity Tree to launch a new Rocket"
} else if (!rocketNameValidator.test(rocketName)) {
nameInvalid = true;
nameError = "Rocket names MUST be 5-20 alphanumeric characters";
} else if (!nameIsUnique(rocketName)) {
nameInvalid = true;
nameError = "Rocket names MUST be unique";
} else {
//todo validate name is unique
nameInvalid = false;
nameError = "";
if (get(currentUser)?.pubkey) {
disableButton = false
}
}
}
function onFormSubmit() {
let e = makeEvent({kind:15171031})
if (!disableButton) {
let e = makeEvent({kind:15171031})
e.tags.push(["t", rocketName, "name"]);
if (!simulateEvents) {
e.publish()
Expand All @@ -49,6 +65,7 @@
reset();
});
}
}
}
function onFormOpen() {
Expand Down Expand Up @@ -81,6 +98,7 @@
selectorPrimaryFocus=".bx--text-input"
modalHeading="Launch a New Rocket!"
hasForm
primaryButtonDisabled={disableButton}
on:open={onFormOpen}
on:click:button--secondary={() => (formOpen = false)}
on:submit={() => (formValidation ? onFormSubmit() : null)}
Expand All @@ -90,11 +108,10 @@
<LoginNip07Button />
{/if}
<TextInput
disabled={$currentUser?false:true}
helperText="Use a name that describes the purpose of this new Rocket"
invalid={nameInvalid}
invalidText={nameError}
on:keyup={validate}
on:change={validate}
labelText="Rocket Name"
bind:value={rocketName}
required
Expand Down
7 changes: 6 additions & 1 deletion src/lib/protocol_validators/rockets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ignitionPubkey, nostrocketIgnitionEvent } from "../../settings";
import type { Nostrocket } from "$lib/stores/nostrocket_state/types";
import type { NDKEvent } from "@nostr-dev-kit/ndk";
import { consensusTipState } from "$lib/stores/nostrocket_state/master_state";
import { get } from "svelte/store";

export function validate(e: NDKEvent, state?: Nostrocket, kind?:number): boolean {
if (kind) {
Expand Down Expand Up @@ -52,7 +54,10 @@ function validate15172008(e: NDKEvent, state: Nostrocket): boolean {
}


function validateIdentity(pubkey:string, state:Nostrocket):boolean {
export function validateIdentity(pubkey:string, state?:Nostrocket):boolean {
if (!state) {
state = get(consensusTipState)
}
let success = false
if (pubkey == ignitionPubkey || state.RocketMap.get(nostrocketIgnitionEvent)?.isParticipant(pubkey)) {
success = true
Expand Down
7 changes: 6 additions & 1 deletion src/lib/stores/nostrocket_state/hard_state/rockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { rocketNameValidator } from "../../../../settings";
import type { Nostrocket } from "../types";
import { Rocket } from "../types";
import { ConsensusMode, TypeOfFailure } from "./types";
import { consensusTipState } from "../master_state";
import { get } from "svelte/store";

//kind 15171031
export function HandleRocketIgnitionNote(ev:NDKEvent, state: Nostrocket, consensusMode: ConsensusMode): [Nostrocket, TypeOfFailure, boolean] {
Expand Down Expand Up @@ -48,7 +50,10 @@ import { ConsensusMode, TypeOfFailure } from "./types";
}


export function nameIsUnique(name: string, state: Nostrocket): boolean {
export function nameIsUnique(name: string, state?: Nostrocket): boolean {
if (!state) {
state = get(consensusTipState)
}
//validate that name doesn't already exist
let unique = true
state.RocketMap.forEach((r) => {
Expand Down

0 comments on commit 4c92b99

Please sign in to comment.