();
diff --git a/src/lib/event_helpers/rockets.ts b/src/lib/event_helpers/rockets.ts
index 03a83b2..04f4530 100644
--- a/src/lib/event_helpers/rockets.ts
+++ b/src/lib/event_helpers/rockets.ts
@@ -175,7 +175,7 @@ export class Rocket {
let _products = new Map();
for (let p of this.Event.getMatchingTags('product')) {
let rp = new RocketProduct(p);
- _products.set(rp.ID, rp);
+ _products.set(rp.ID(), rp);
}
return _products;
}
@@ -551,28 +551,52 @@ export class RocketAMR {
}
export class RocketProduct {
- ID: string;
- Price: number;
- ValidAfter: number; //unix time
- MaxPurchases: number;
- Purchases: Map;
+ tag: NDKTag;
+ ID(): string {
+ return this.tag[1].split(':')[0];
+ }
+ Price(): number {
+ return parseInt(this.tag[1].split(':')[1], 10);
+ }
+ ValidAfter(): number {
+ return parseInt(this.tag[1].split(':')[2], 10);
+ }
+ MaxPurchases(): number {
+ return parseInt(this.tag[1].split(':')[3], 10);
+ }
+ Purchases(): Map {
+ let result: Map = new Map();
+ let purchases = JSON.parse(this.tag[3]);
+ for (let p of purchases) {
+ let payment = new ProductPayment(p);
+ result.set(payment.ZapID, payment);
+ }
+ return result;
+ }
PurchasesJSON(): string {
let purchases = [];
- for (let [_, p] of this.Purchases) {
+ for (let [_, p] of this.Purchases()) {
purchases.push(`${p.ZapID}:${p.BuyerPubkey}:${p.WitnessedAt}`);
}
return JSON.stringify(purchases);
}
+ Validate(): boolean {
+ try {
+ this.ID();
+ this.Price();
+ this.ValidAfter();
+ this.MaxPurchases();
+ this.Purchases();
+ this.PurchasesJSON();
+ return true;
+ } catch {
+ return false;
+ }
+ }
constructor(tag: NDKTag) {
- this.Purchases = new Map();
- this.ID = tag[1].split(':')[0];
- this.Price = parseInt(tag[1].split(':')[1], 10);
- this.ValidAfter = parseInt(tag[1].split(':')[2], 10);
- this.MaxPurchases = parseInt(tag[1].split(':')[3], 10);
- let purchases = JSON.parse(tag[3]);
- for (let p of purchases) {
- let payment = new ProductPayment(p);
- this.Purchases.set(payment.ZapID, payment);
+ this.tag = tag;
+ if (!this.Validate()) {
+ throw new Error('bug!');
}
}
}
@@ -610,7 +634,7 @@ export class ZapPurchase {
IncludedInRocketState(rocket: NDKEvent): boolean {
let thisProduct = this.ProductFromRocket(rocket);
if (thisProduct) {
- return thisProduct.Purchases.get(this.ZapReceipt.id) ? true : false;
+ return thisProduct.Purchases().get(this.ZapReceipt.id) ? true : false;
} else {
return false;
}
@@ -627,7 +651,7 @@ export class ZapPurchase {
return true;
}
let product = this.ProductFromRocket(rocket);
- if (product && this.Amount / 1000 >= product.Price) {
+ if (product && this.Amount / 1000 >= product.Price()) {
return true;
}
return false;
diff --git a/src/lib/stores/session.ts b/src/lib/stores/session.ts
index ecc80b3..e27e7e5 100644
--- a/src/lib/stores/session.ts
+++ b/src/lib/stores/session.ts
@@ -9,3 +9,5 @@ export async function prepareUserSession(ndk: NDKSvelte, user: NDKUser): Promise
//implement any session set up stuff here
});
}
+
+export const devmode = writable(false);