@graphprotocol/hypergraph 0.0.14 → 0.2.0

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.
Files changed (123) hide show
  1. package/dist/Entity.d.ts +69 -0
  2. package/dist/Entity.d.ts.map +1 -0
  3. package/dist/Entity.js +174 -0
  4. package/dist/Entity.js.map +1 -0
  5. package/dist/connect/identity-encryption.d.ts +3 -3
  6. package/dist/connect/identity-encryption.d.ts.map +1 -1
  7. package/dist/connect/identity-encryption.js +18 -52
  8. package/dist/connect/identity-encryption.js.map +1 -1
  9. package/dist/connect/login.d.ts +1 -1
  10. package/dist/connect/login.d.ts.map +1 -1
  11. package/dist/connect/login.js +2 -2
  12. package/dist/connect/login.js.map +1 -1
  13. package/dist/connect/parse-auth-params.js +1 -1
  14. package/dist/connect/parse-auth-params.js.map +1 -1
  15. package/dist/connect/smart-account.d.ts.map +1 -1
  16. package/dist/connect/smart-account.js +2 -2
  17. package/dist/connect/smart-account.js.map +1 -1
  18. package/dist/connect/types.d.ts +0 -1
  19. package/dist/connect/types.d.ts.map +1 -1
  20. package/dist/connect/types.js +0 -1
  21. package/dist/connect/types.js.map +1 -1
  22. package/dist/entity/findMany.js +1 -1
  23. package/dist/entity/findMany.js.map +1 -1
  24. package/dist/entity/update.js +1 -1
  25. package/dist/entity/update.js.map +1 -1
  26. package/dist/identity/create-identity-keys.d.ts +3 -0
  27. package/dist/identity/create-identity-keys.d.ts.map +1 -0
  28. package/dist/identity/create-identity-keys.js +20 -0
  29. package/dist/identity/create-identity-keys.js.map +1 -0
  30. package/dist/identity/get-verified-identity.d.ts +1 -1
  31. package/dist/identity/get-verified-identity.d.ts.map +1 -1
  32. package/dist/identity/get-verified-identity.js +17 -4
  33. package/dist/identity/get-verified-identity.js.map +1 -1
  34. package/dist/identity/identity-encryption.js +1 -1
  35. package/dist/identity/identity-encryption.js.map +1 -1
  36. package/dist/identity/index.d.ts +1 -1
  37. package/dist/identity/index.d.ts.map +1 -1
  38. package/dist/identity/index.js +1 -1
  39. package/dist/identity/index.js.map +1 -1
  40. package/dist/identity/login.d.ts +38 -0
  41. package/dist/identity/login.d.ts.map +1 -0
  42. package/dist/identity/login.js +241 -0
  43. package/dist/identity/login.js.map +1 -0
  44. package/dist/identity/prove-ownership.d.ts +1 -1
  45. package/dist/identity/prove-ownership.d.ts.map +1 -1
  46. package/dist/identity/prove-ownership.js +1 -1
  47. package/dist/identity/prove-ownership.js.map +1 -1
  48. package/dist/inboxes/index.d.ts +1 -1
  49. package/dist/inboxes/index.d.ts.map +1 -1
  50. package/dist/inboxes/index.js +1 -1
  51. package/dist/inboxes/index.js.map +1 -1
  52. package/dist/inboxes/message-validation.d.ts.map +1 -1
  53. package/dist/inboxes/message-validation.js +2 -2
  54. package/dist/inboxes/message-validation.js.map +1 -1
  55. package/dist/index.d.ts +2 -1
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +2 -1
  58. package/dist/index.js.map +1 -1
  59. package/dist/mapping/Mapping.d.ts +418 -0
  60. package/dist/mapping/Mapping.d.ts.map +1 -0
  61. package/dist/mapping/Mapping.js +554 -0
  62. package/dist/mapping/Mapping.js.map +1 -0
  63. package/dist/mapping/Utils.d.ts +74 -0
  64. package/dist/mapping/Utils.d.ts.map +1 -0
  65. package/dist/mapping/Utils.js +144 -0
  66. package/dist/mapping/Utils.js.map +1 -0
  67. package/dist/mapping/index.d.ts +3 -0
  68. package/dist/mapping/index.d.ts.map +1 -0
  69. package/dist/mapping/index.js +3 -0
  70. package/dist/mapping/index.js.map +1 -0
  71. package/dist/messages/types.d.ts +1 -1
  72. package/dist/messages/types.d.ts.map +1 -1
  73. package/dist/messages/types.js +2 -2
  74. package/dist/messages/types.js.map +1 -1
  75. package/dist/space-events/apply-event.d.ts +1 -1
  76. package/dist/space-events/apply-event.d.ts.map +1 -1
  77. package/dist/space-events/apply-event.js +1 -1
  78. package/dist/space-events/apply-event.js.map +1 -1
  79. package/dist/space-info/decrypt-space-info.d.ts +1 -1
  80. package/dist/space-info/decrypt-space-info.d.ts.map +1 -1
  81. package/dist/space-info/decrypt-space-info.js.map +1 -1
  82. package/dist/store-connect.d.ts.map +1 -1
  83. package/dist/store-connect.js.map +1 -1
  84. package/dist/store.d.ts +4 -2
  85. package/dist/store.d.ts.map +1 -1
  86. package/dist/store.js +23 -3
  87. package/dist/store.js.map +1 -1
  88. package/dist/type/type.d.ts +0 -1
  89. package/dist/type/type.d.ts.map +1 -1
  90. package/dist/type/type.js +0 -1
  91. package/dist/type/type.js.map +1 -1
  92. package/dist/types.d.ts +0 -13
  93. package/dist/types.d.ts.map +1 -1
  94. package/dist/types.js.map +1 -1
  95. package/dist/utils/hasArrayField.d.ts +2 -0
  96. package/dist/utils/hasArrayField.d.ts.map +1 -0
  97. package/dist/utils/hasArrayField.js +5 -0
  98. package/dist/utils/hasArrayField.js.map +1 -0
  99. package/package.json +4 -3
  100. package/src/connect/identity-encryption.ts +33 -69
  101. package/src/connect/login.ts +3 -3
  102. package/src/connect/parse-auth-params.ts +1 -1
  103. package/src/connect/smart-account.ts +12 -12
  104. package/src/connect/types.ts +0 -1
  105. package/src/entity/findMany.ts +2 -2
  106. package/src/entity/update.ts +1 -1
  107. package/src/identity/get-verified-identity.ts +17 -2
  108. package/src/identity/identity-encryption.ts +1 -1
  109. package/src/identity/index.ts +1 -1
  110. package/src/identity/prove-ownership.ts +2 -3
  111. package/src/inboxes/index.ts +1 -1
  112. package/src/inboxes/message-validation.ts +4 -0
  113. package/src/index.ts +2 -1
  114. package/src/mapping/Mapping.ts +771 -0
  115. package/src/mapping/Utils.ts +156 -0
  116. package/src/mapping/index.ts +2 -0
  117. package/src/messages/types.ts +1 -1
  118. package/src/space-events/apply-event.ts +5 -2
  119. package/src/space-info/decrypt-space-info.ts +1 -7
  120. package/src/store-connect.ts +1 -1
  121. package/src/store.ts +39 -9
  122. package/src/type/type.ts +0 -1
  123. package/src/types.ts +0 -15
@@ -0,0 +1,156 @@
1
+ import { Data, String as EffectString } from 'effect';
2
+
3
+ /**
4
+ * Takes the input string and returns the camelCase equivalent
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { toCamelCase } from '@graphprotocol/hypergraph/mapping'
9
+ *
10
+ * expect(toCamelCase('Address line 1')).toEqual('addressLine1');
11
+ * expect(toCamelCase('AddressLine1')).toEqual('addressLine1');
12
+ * expect(toCamelCase('addressLine1')).toEqual('addressLine1');
13
+ * expect(toCamelCase('address_line_1')).toEqual('addressLine1');
14
+ * expect(toCamelCase('address-line-1')).toEqual('addressLine1');
15
+ * expect(toCamelCase('address-line_1')).toEqual('addressLine1');
16
+ * expect(toCamelCase('address-line 1')).toEqual('addressLine1');
17
+ * expect(toCamelCase('ADDRESS_LINE_1')).toEqual('addressLine1');
18
+ * ```
19
+ *
20
+ * @since 0.2.0
21
+ *
22
+ * @param str input string
23
+ * @returns camelCased value of the input string
24
+ */
25
+ export function toCamelCase(str: string): string {
26
+ if (EffectString.isEmpty(str)) {
27
+ throw new InvalidInputError({ input: str, cause: 'Input is empty' });
28
+ }
29
+
30
+ let result = '';
31
+ let capitalizeNext = false;
32
+ let i = 0;
33
+
34
+ // Skip leading non-alphanumeric characters
35
+ while (i < EffectString.length(str) && !/[a-zA-Z0-9]/.test(str[i])) {
36
+ i++;
37
+ }
38
+
39
+ for (; i < EffectString.length(str); i++) {
40
+ const char = str[i];
41
+
42
+ if (/[a-zA-Z0-9]/.test(char)) {
43
+ if (capitalizeNext) {
44
+ result += EffectString.toUpperCase(char);
45
+ capitalizeNext = false;
46
+ } else if (EffectString.length(result) === 0) {
47
+ // First character should always be lowercase
48
+ result += EffectString.toLowerCase(char);
49
+ } else if (/[A-Z]/.test(char) && i > 0 && /[a-z0-9]/.test(str[i - 1])) {
50
+ // Capital letter following lowercase/number - this indicates a word boundary
51
+ // So we need to capitalize this letter (it starts a new word)
52
+ result += EffectString.toUpperCase(char);
53
+ } else {
54
+ result += EffectString.toLowerCase(char);
55
+ }
56
+ } else {
57
+ // Non-alphanumeric character - set flag to capitalize next letter
58
+ capitalizeNext = EffectString.length(result) > 0; // Only capitalize if we have existing content
59
+ }
60
+ }
61
+
62
+ return result;
63
+ }
64
+
65
+ /**
66
+ * Takes the input string and returns the PascalCase equivalent
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * iimport { toPascalCase } from '@graphprotocol/hypergraph/mapping'
71
+ *
72
+ * expect(toPascalCase('Address line 1')).toEqual('AddressLine1');
73
+ * expect(toPascalCase('AddressLine1')).toEqual('AddressLine1');
74
+ * expect(toPascalCase('addressLine1')).toEqual('AddressLine1');
75
+ * expect(toPascalCase('address_line_1')).toEqual('AddressLine1');
76
+ * expect(toPascalCase('address-line-1')).toEqual('AddressLine1');
77
+ * expect(toPascalCase('address-line_1')).toEqual('AddressLine1');
78
+ * expect(toPascalCase('address-line 1')).toEqual('AddressLine1');
79
+ * expect(toPascalCase('ADDRESS_LINE_1')).toEqual('AddressLine1');
80
+ * ```
81
+ *
82
+ * @since 0.2.0
83
+ *
84
+ * @param str input string
85
+ * @returns PascalCased value of the input string
86
+ */
87
+ export function toPascalCase(str: string): string {
88
+ if (EffectString.isEmpty(str)) {
89
+ throw new InvalidInputError({ input: str, cause: 'Input is empty' });
90
+ }
91
+
92
+ let result = '';
93
+ let capitalizeNext = true; // Start with true to capitalize the first letter
94
+ let i = 0;
95
+
96
+ // Skip leading non-alphanumeric characters
97
+ while (i < EffectString.length(str) && !/[a-zA-Z0-9]/.test(str[i])) {
98
+ i++;
99
+ }
100
+
101
+ for (; i < EffectString.length(str); i++) {
102
+ const char = str[i];
103
+
104
+ if (/[a-zA-Z0-9]/.test(char)) {
105
+ if (capitalizeNext) {
106
+ result += EffectString.toUpperCase(char);
107
+ capitalizeNext = false;
108
+ } else if (/[A-Z]/.test(char) && i > 0 && /[a-z0-9]/.test(str[i - 1])) {
109
+ // Capital letter following lowercase/number - this indicates a word boundary
110
+ // So we need to capitalize this letter (it starts a new word)
111
+ result += EffectString.toUpperCase(char);
112
+ } else {
113
+ result += EffectString.toLowerCase(char);
114
+ }
115
+ } else {
116
+ // Non-alphanumeric character - set flag to capitalize next letter
117
+ capitalizeNext = true;
118
+ }
119
+ }
120
+
121
+ return result;
122
+ }
123
+
124
+ export class InvalidInputError extends Data.TaggedError('/typesync/errors/InvalidInputError')<{
125
+ readonly input: string;
126
+ readonly cause: unknown;
127
+ }> {}
128
+
129
+ /**
130
+ * Adds schema validation that the array of objects with property `name` only has unique names
131
+ *
132
+ * @example <caption>only unique names -> returns true</caption>
133
+ * ```ts
134
+ * const types = [{name:'Account'}, {name:'Event'}]
135
+ * expect(namesAreUnique(types)).toEqual(true)
136
+ * ```
137
+ *
138
+ * @example <caption>duplicate name -> returns false</caption>
139
+ * ```ts
140
+ * const types = [{name:'Account'}, {name:'Event'}, {name:'Account'}]
141
+ * expect(namesAreUnique(types)).toEqual(false)
142
+ * ```
143
+ */
144
+ export function namesAreUnique<T extends { readonly name: string }>(entries: ReadonlyArray<T>): boolean {
145
+ const names = new Set<string>();
146
+
147
+ for (const entry of entries) {
148
+ const name = EffectString.toLowerCase(entry.name);
149
+ if (names.has(name)) {
150
+ return false;
151
+ }
152
+ names.add(name);
153
+ }
154
+
155
+ return true;
156
+ }
@@ -0,0 +1,2 @@
1
+ export * from './Mapping.js';
2
+ export * from './Utils.js';
@@ -245,7 +245,6 @@ export const RequestConnectCreateAppIdentity = Schema.Struct({
245
245
  address: Schema.String,
246
246
  accountAddress: Schema.String,
247
247
  ciphertext: Schema.String,
248
- nonce: Schema.String,
249
248
  signaturePublicKey: Schema.String,
250
249
  encryptionPublicKey: Schema.String,
251
250
  accountProof: Schema.String,
@@ -450,6 +449,7 @@ export const ResponseIdentity = Schema.Struct({
450
449
  encryptionPublicKey: Schema.String,
451
450
  accountProof: Schema.String,
452
451
  keyProof: Schema.String,
452
+ appId: Schema.optional(Schema.String),
453
453
  });
454
454
 
455
455
  export type ResponseIdentity = Schema.Schema.Type<typeof ResponseIdentity>;
@@ -18,7 +18,10 @@ import {
18
18
  type Params = {
19
19
  state: SpaceState | undefined;
20
20
  event: SpaceEvent;
21
- getVerifiedIdentity: (accountAddress: string) => Effect.Effect<PublicIdentity, InvalidIdentityError>;
21
+ getVerifiedIdentity: (
22
+ accountAddress: string,
23
+ publicKey: string,
24
+ ) => Effect.Effect<PublicIdentity, InvalidIdentityError>;
22
25
  };
23
26
 
24
27
  const decodeSpaceEvent = Schema.decodeUnknownEither(SpaceEvent);
@@ -50,7 +53,7 @@ export const applyEvent = ({
50
53
  const authorPublicKey = `0x${signatureInstance.recoverPublicKey(sha256(encodedTransaction)).toHex()}`;
51
54
 
52
55
  return Effect.gen(function* () {
53
- const identity = yield* getVerifiedIdentity(event.author.accountAddress);
56
+ const identity = yield* getVerifiedIdentity(event.author.accountAddress, authorPublicKey);
54
57
  if (authorPublicKey !== identity.signaturePublicKey) {
55
58
  yield* Effect.fail(new VerifySignatureError());
56
59
  }
@@ -6,13 +6,7 @@ import { SpaceInfoContent } from './types.js';
6
6
 
7
7
  const decodeSpaceInfoContent = Schema.decodeSync(SpaceInfoContent);
8
8
 
9
- export const decryptSpaceInfo = ({
10
- spaceInfo,
11
- secretKey,
12
- }: {
13
- spaceInfo: Uint8Array;
14
- secretKey: string;
15
- }) => {
9
+ export const decryptSpaceInfo = ({ spaceInfo, secretKey }: { spaceInfo: Uint8Array; secretKey: string }) => {
16
10
  const decrypted = decryptMessage({
17
11
  nonceAndCiphertext: spaceInfo,
18
12
  secretKey: hexToBytes(secretKey),
@@ -1,5 +1,5 @@
1
1
  import type { AnyDocumentId, DocHandle, Repo } from '@automerge/automerge-repo';
2
- import { type Store, createStore } from '@xstate/store';
2
+ import { createStore, type Store } from '@xstate/store';
3
3
  import type { Address } from 'viem';
4
4
  import { mergeMessages } from './inboxes/merge-messages.js';
5
5
  import type { InboxSenderAuthPolicy } from './inboxes/types.js';
package/src/store.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import type { AnyDocumentId, DocHandle, Repo } from '@automerge/automerge-repo';
2
- import { type Store, createStore } from '@xstate/store';
2
+ import { createStore, type Store } from '@xstate/store';
3
3
  import type { PrivateAppIdentity } from './connect/types.js';
4
4
  import type { DocumentContent } from './entity/types.js';
5
5
  import { mergeMessages } from './inboxes/merge-messages.js';
6
6
  import type { InboxSenderAuthPolicy } from './inboxes/types.js';
7
+ import type { Mapping } from './mapping/Mapping.js';
7
8
  import type { Invitation, Updates } from './messages/index.js';
8
9
  import type { SpaceEvent, SpaceState } from './space-events/index.js';
9
- import type { Mapping } from './types.js';
10
10
  import { idToAutomergeId } from './utils/automergeId.js';
11
11
 
12
12
  export type InboxMessageStorageEntry = {
@@ -64,7 +64,8 @@ interface StoreContext {
64
64
  signaturePublicKey: string;
65
65
  accountProof: string;
66
66
  keyProof: string;
67
- };
67
+ appId: string | null;
68
+ }[];
68
69
  };
69
70
  authenticated: boolean;
70
71
  identity: PrivateAppIdentity | null;
@@ -104,6 +105,7 @@ type StoreEvent =
104
105
  signaturePublicKey: string;
105
106
  accountProof: string;
106
107
  keyProof: string;
108
+ appId: string | null;
107
109
  }
108
110
  | {
109
111
  type: 'setSpaceInbox';
@@ -291,18 +293,46 @@ export const store: Store<StoreContext, StoreEvent, GenericEventObject> = create
291
293
  signaturePublicKey: string;
292
294
  accountProof: string;
293
295
  keyProof: string;
296
+ appId: string | null;
294
297
  },
295
298
  ) => {
299
+ const existingIdentity = context.identities[event.accountAddress]?.find(
300
+ (identity) => identity.signaturePublicKey === event.signaturePublicKey,
301
+ );
302
+ if (existingIdentity) {
303
+ return context;
304
+ }
305
+ if (context.identities[event.accountAddress]) {
306
+ return {
307
+ ...context,
308
+ identities: {
309
+ ...context.identities,
310
+ [event.accountAddress]: [
311
+ ...context.identities[event.accountAddress],
312
+ {
313
+ encryptionPublicKey: event.encryptionPublicKey,
314
+ signaturePublicKey: event.signaturePublicKey,
315
+ accountProof: event.accountProof,
316
+ keyProof: event.keyProof,
317
+ appId: event.appId,
318
+ },
319
+ ],
320
+ },
321
+ };
322
+ }
296
323
  return {
297
324
  ...context,
298
325
  identities: {
299
326
  ...context.identities,
300
- [event.accountAddress]: {
301
- encryptionPublicKey: event.encryptionPublicKey,
302
- signaturePublicKey: event.signaturePublicKey,
303
- accountProof: event.accountProof,
304
- keyProof: event.keyProof,
305
- },
327
+ [event.accountAddress]: [
328
+ {
329
+ encryptionPublicKey: event.encryptionPublicKey,
330
+ signaturePublicKey: event.signaturePublicKey,
331
+ accountProof: event.accountProof,
332
+ keyProof: event.keyProof,
333
+ appId: event.appId,
334
+ },
335
+ ],
306
336
  },
307
337
  };
308
338
  },
package/src/type/type.ts CHANGED
@@ -8,7 +8,6 @@ export const Number = Schema.Number;
8
8
  export const Checkbox = Schema.Boolean;
9
9
  // biome-ignore lint/suspicious/noShadowRestrictedNames: is part of a namespaces module and therefor ok
10
10
  export const Date = Schema.Date;
11
- export const Url = Schema.URL;
12
11
  export const Point = Schema.transform(Schema.String, Schema.Array(Number), {
13
12
  strict: true,
14
13
  decode: (str: string) => {
package/src/types.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { Id as Grc20Id } from '@graphprotocol/grc-20';
2
1
  import * as Data from 'effect/Data';
3
2
  import * as Schema from 'effect/Schema';
4
3
 
@@ -49,17 +48,3 @@ export class FailedToParseConnectAuthUrl extends Data.TaggedError('FailedToParse
49
48
  export class FailedToParseAuthCallbackUrl extends Data.TaggedError('FailedToParseAuthCallbackUrl')<{
50
49
  message: string;
51
50
  }> {}
52
-
53
- export type MappingEntry = {
54
- typeIds: Grc20Id.Id[];
55
- properties?: {
56
- [key: string]: Grc20Id.Id;
57
- };
58
- relations?: {
59
- [key: string]: Grc20Id.Id;
60
- };
61
- };
62
-
63
- export type Mapping = {
64
- [key: string]: MappingEntry;
65
- };