@moneypot/hub 1.16.2 → 1.16.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,11 @@
1
1
  import { DbCasino, DbExperience, DbHash, DbHashChain, DbUser } from "../db/types.js";
2
2
  import { PgClientInTransaction } from "../db/index.js";
3
- export declare function dbLockHubHashChain(pgClient: PgClientInTransaction, { userId, experienceId, casinoId, hashChainId, }: {
3
+ export declare function dbLockHubHashChain(pgClient: PgClientInTransaction, { userId, experienceId, casinoId, hashChainId, active, }: {
4
4
  userId: DbUser["id"];
5
5
  experienceId: DbExperience["id"];
6
6
  casinoId: DbCasino["id"];
7
7
  hashChainId: DbHashChain["id"];
8
+ active: "must-be-active" | "active-or-inactive";
8
9
  }): Promise<DbHashChain | null>;
9
10
  export declare function dbInsertHubHash(pgClient: PgClientInTransaction, { hashChainId, kind, digest, iteration, clientSeed, metadata, }: {
10
11
  hashChainId: DbHashChain["id"];
@@ -2,25 +2,20 @@ import { DbHashKind, } from "../db/types.js";
2
2
  import { exactlyOneRow, maybeOneRow, } from "../db/index.js";
3
3
  import { assert } from "tsafe";
4
4
  import { logger } from "../logger.js";
5
- export async function dbLockHubHashChain(pgClient, { userId, experienceId, casinoId, hashChainId, }) {
6
- const hashChain = await pgClient
5
+ export async function dbLockHubHashChain(pgClient, { userId, experienceId, casinoId, hashChainId, active, }) {
6
+ const dbLockedHashChain = await pgClient
7
7
  .query(`
8
- SELECT *
9
- FROM hub.hash_chain
10
- WHERE id = $1
11
- AND user_id = $2
12
- AND experience_id = $3
13
- AND casino_id = $4
14
- AND active = TRUE
15
-
16
- FOR UPDATE
17
- `, [hashChainId, userId, experienceId, casinoId])
8
+ SELECT *
9
+ FROM hub.hash_chain
10
+ WHERE id = $1
11
+ AND user_id = $2
12
+ AND experience_id = $3
13
+ AND casino_id = $4
14
+ AND ($5::text = 'active-or-inactive' OR active = TRUE)
15
+ FOR UPDATE`, [hashChainId, userId, experienceId, casinoId, active])
18
16
  .then(maybeOneRow)
19
17
  .then((row) => row ?? null);
20
- if (hashChain) {
21
- assert(hashChain.current_iteration >= 1, "Hash chain iteration must be >= 1");
22
- }
23
- return hashChain;
18
+ return dbLockedHashChain;
24
19
  }
25
20
  export async function dbInsertHubHash(pgClient, { hashChainId, kind, digest, iteration, clientSeed, metadata = {}, }) {
26
21
  logger.debug({
@@ -1,14 +1,13 @@
1
1
  import { context, object, sideEffect } from "postgraphile/grafast";
2
- import { gql, makeExtendSchemaPlugin } from "postgraphile/utils";
2
+ import { gql, extendSchema } from "postgraphile/utils";
3
3
  import { dbLockHubHashChain, withPgPoolTransaction } from "../../db/index.js";
4
4
  import z, { prettifyError } from "zod/v4";
5
5
  import { GraphQLError } from "graphql";
6
- import { assert } from "tsafe";
7
6
  import { dbRevealHashChain } from "../reveal-hash-chain.js";
8
7
  const InputSchema = z.object({
9
8
  hashChainId: z.uuidv7("Invalid hash chain ID"),
10
9
  });
11
- export const HubRevealHashChainPlugin = makeExtendSchemaPlugin((build) => {
10
+ export const HubRevealHashChainPlugin = extendSchema((build) => {
12
11
  const hashTable = build.input.pgRegistry.pgResources.hub_hash;
13
12
  return {
14
13
  typeDefs: gql `
@@ -47,20 +46,20 @@ export const HubRevealHashChainPlugin = makeExtendSchemaPlugin((build) => {
47
46
  throw e;
48
47
  }
49
48
  return withPgPoolTransaction(superuserPool, async (pgClient) => {
50
- const hashChain = await dbLockHubHashChain(pgClient, {
49
+ const dbLockedHashChain = await dbLockHubHashChain(pgClient, {
51
50
  userId: identity.session.user_id,
52
51
  experienceId: identity.session.experience_id,
53
52
  casinoId: identity.session.casino_id,
54
53
  hashChainId: input.hashChainId,
54
+ active: "active-or-inactive",
55
55
  });
56
- if (!hashChain || !hashChain.active) {
57
- throw new GraphQLError("Active hash chain not found");
56
+ if (!dbLockedHashChain) {
57
+ throw new GraphQLError("Hash chain not found");
58
58
  }
59
- assert(hashChain.current_iteration >= 1, "Invalid hash chain iteration");
60
- const dbPreimageHash = await dbRevealHashChain(pgClient, {
61
- hashChainId: input.hashChainId,
59
+ const dbPreimageHashId = await dbRevealHashChain(pgClient, {
60
+ hashChainId: dbLockedHashChain.id,
62
61
  });
63
- return dbPreimageHash.id;
62
+ return dbPreimageHashId;
64
63
  });
65
64
  });
66
65
  return object({
@@ -1,5 +1,5 @@
1
- import { DbHash } from "../db/types.js";
1
+ import { DbHash, DbHashChain } from "../db/types.js";
2
2
  import { PgClientInTransaction } from "../db/index.js";
3
3
  export declare function dbRevealHashChain(pgClient: PgClientInTransaction, { hashChainId, }: {
4
- hashChainId: string;
5
- }): Promise<DbHash>;
4
+ hashChainId: DbHashChain["id"];
5
+ }): Promise<DbHash["id"]>;
@@ -1,16 +1,39 @@
1
1
  import { logger } from "../logger.js";
2
2
  import { getPreimageHash } from "./get-hash.js";
3
3
  import { DbHashKind } from "../db/types.js";
4
- import { dbInsertHubHash } from "../db/index.js";
4
+ import { dbInsertHubHash, maybeOneRow, } from "../db/index.js";
5
5
  export async function dbRevealHashChain(pgClient, { hashChainId, }) {
6
6
  logger.debug({ hashChainId }, "Revealing hash chain");
7
+ const dbLockedHashChain = await pgClient
8
+ .query(`
9
+ SELECT *
10
+ FROM hub.hash_chain
11
+ WHERE id = $1
12
+ FOR UPDATE
13
+ `, [hashChainId])
14
+ .then(maybeOneRow);
15
+ if (!dbLockedHashChain) {
16
+ throw new Error("Hash chain not found");
17
+ }
18
+ const dbPreimageHash = await pgClient
19
+ .query(`
20
+ SELECT id
21
+ FROM hub.hash
22
+ WHERE hash_chain_id = $1
23
+ AND kind = 'PREIMAGE'
24
+ AND iteration = 0
25
+ `, [hashChainId])
26
+ .then(maybeOneRow);
27
+ if (dbPreimageHash) {
28
+ return dbPreimageHash.id;
29
+ }
7
30
  const preimageHashResult = await getPreimageHash({
8
- hashChainId,
31
+ hashChainId: dbLockedHashChain.id,
9
32
  });
10
33
  logger.debug({ preimageHashResult }, "Preimage hash result");
11
34
  if (preimageHashResult.type === "success") {
12
35
  const dbPreimageHash = await dbInsertHubHash(pgClient, {
13
- hashChainId,
36
+ hashChainId: dbLockedHashChain.id,
14
37
  kind: DbHashKind.PREIMAGE,
15
38
  digest: preimageHashResult.hash,
16
39
  iteration: 0,
@@ -20,11 +43,11 @@ export async function dbRevealHashChain(pgClient, { hashChainId, }) {
20
43
  SET current_iteration = 0,
21
44
  active = false
22
45
  WHERE id = $1
23
- `, [hashChainId]);
46
+ `, [dbLockedHashChain.id]);
24
47
  if (result.rowCount !== 1) {
25
48
  throw new Error("Failed to update hash chain iteration");
26
49
  }
27
- return dbPreimageHash;
50
+ return dbPreimageHash.id;
28
51
  }
29
52
  else {
30
53
  logger.warn({ hashChainId, error: preimageHashResult }, "Failed to reveal hash chain");
@@ -4,7 +4,7 @@ import { assert } from "tsafe";
4
4
  import { GET_USER_FROM_USER_TOKEN } from "../graphql-queries.js";
5
5
  import { exactlyOneRow, maybeOneRow } from "../db/util.js";
6
6
  import { createGraphqlClient } from "../graphql-client.js";
7
- import { constant, context, error, object, sideEffect, } from "postgraphile/grafast";
7
+ import { constant, context, object, sideEffect } from "postgraphile/grafast";
8
8
  import { withPgPoolTransaction, } from "../db/index.js";
9
9
  import { logger } from "../logger.js";
10
10
  import * as jwtService from "../services/jwt-service.js";
@@ -114,7 +114,7 @@ export const HubAuthenticatePlugin = extendSchema(() => {
114
114
  throw new GraphQLError(errorInfo.message);
115
115
  }
116
116
  }
117
- throw error;
117
+ throw e;
118
118
  }
119
119
  const result = res.userFromUserToken;
120
120
  if (!result || !result.user || !result.experience) {
@@ -199,6 +199,7 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
199
199
  experienceId: session.experience_id,
200
200
  casinoId: session.casino_id,
201
201
  hashChainId: input.hashChainId,
202
+ active: "must-be-active",
202
203
  });
203
204
  if (!dbHashChain || !dbHashChain.active) {
204
205
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moneypot/hub",
3
- "version": "1.16.2",
3
+ "version": "1.16.4",
4
4
  "author": "moneypot.com",
5
5
  "homepage": "https://moneypot.com/hub",
6
6
  "keywords": [