@misofm/partyos 0.1.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 (58) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +50 -0
  3. package/dist/client.d.ts +84 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +109 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/contracts/partyos/deps/sui/vec_set.d.ts +21 -0
  8. package/dist/contracts/partyos/deps/sui/vec_set.d.ts.map +1 -0
  9. package/dist/contracts/partyos/deps/sui/vec_set.js +20 -0
  10. package/dist/contracts/partyos/deps/sui/vec_set.js.map +1 -0
  11. package/dist/contracts/partyos/party.d.ts +481 -0
  12. package/dist/contracts/partyos/party.d.ts.map +1 -0
  13. package/dist/contracts/partyos/party.js +527 -0
  14. package/dist/contracts/partyos/party.js.map +1 -0
  15. package/dist/contracts/utils/index.d.ts +104 -0
  16. package/dist/contracts/utils/index.d.ts.map +1 -0
  17. package/dist/contracts/utils/index.js +272 -0
  18. package/dist/contracts/utils/index.js.map +1 -0
  19. package/dist/contracts.d.ts +12 -0
  20. package/dist/contracts.d.ts.map +1 -0
  21. package/dist/contracts.js +21 -0
  22. package/dist/contracts.js.map +1 -0
  23. package/dist/deployments.d.ts +33 -0
  24. package/dist/deployments.d.ts.map +1 -0
  25. package/dist/deployments.js +63 -0
  26. package/dist/deployments.js.map +1 -0
  27. package/dist/index.d.ts +7 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +14 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/internal.d.ts +5 -0
  32. package/dist/internal.d.ts.map +1 -0
  33. package/dist/internal.js +24 -0
  34. package/dist/internal.js.map +1 -0
  35. package/dist/queries.d.ts +37 -0
  36. package/dist/queries.d.ts.map +1 -0
  37. package/dist/queries.js +123 -0
  38. package/dist/queries.js.map +1 -0
  39. package/dist/transactions.d.ts +74 -0
  40. package/dist/transactions.d.ts.map +1 -0
  41. package/dist/transactions.js +85 -0
  42. package/dist/transactions.js.map +1 -0
  43. package/dist/types.d.ts +12 -0
  44. package/dist/types.d.ts.map +1 -0
  45. package/dist/types.js +4 -0
  46. package/dist/types.js.map +1 -0
  47. package/package.json +97 -0
  48. package/src/client.ts +165 -0
  49. package/src/contracts/partyos/deps/sui/vec_set.ts +22 -0
  50. package/src/contracts/partyos/party.ts +798 -0
  51. package/src/contracts/utils/index.ts +428 -0
  52. package/src/contracts.ts +31 -0
  53. package/src/deployments.ts +90 -0
  54. package/src/index.ts +15 -0
  55. package/src/internal.ts +30 -0
  56. package/src/queries.ts +167 -0
  57. package/src/transactions.ts +167 -0
  58. package/src/types.ts +18 -0
package/src/queries.ts ADDED
@@ -0,0 +1,167 @@
1
+ // Copyright (c) Miso Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // Typed reads: fetch an on-chain object (or dynamic field) via the Core API, BCS-parse
5
+ // it through the generated struct, and map to the public camelCase types.
6
+
7
+ import type { ClientWithCoreApi } from "@mysten/sui/client";
8
+ import { deriveDynamicFieldID, deriveObjectID, normalizeStructTag } from "@mysten/sui/utils";
9
+ import { bcs } from "@mysten/sui/bcs";
10
+ import {
11
+ MembershipKey as MembershipKeyBcs,
12
+ Party as PartyBcs,
13
+ PendingInviteKey as PendingInviteKeyBcs,
14
+ PendingMembershipKey as PendingMembershipKeyBcs,
15
+ } from "./contracts/partyos/party.ts";
16
+ import { keyBytes, mapParty } from "./internal.ts";
17
+ import type { Party } from "./types.ts";
18
+
19
+ /** True for the Core API's "object does not exist" error (a missing dynamic field). */
20
+ export function isNotFound(e: unknown): boolean {
21
+ const msg = e instanceof Error ? e.message : String(e);
22
+ return /not\s*found|does not exist|no object/i.test(msg);
23
+ }
24
+
25
+ /**
26
+ * Reads an object's BCS content, or null when it does not exist. A missing
27
+ * object (e.g. an unset dynamic field) surfaces as a thrown "not found" from the
28
+ * Core API, not an empty result, so optional reads can treat it as absence.
29
+ */
30
+ export async function getObjectContent(client: ClientWithCoreApi, objectId: string): Promise<Uint8Array | null> {
31
+ try {
32
+ const { object } = await client.core.getObject({ objectId, include: { content: true } });
33
+ return object?.content ?? null;
34
+ } catch (e) {
35
+ if (isNotFound(e)) return null;
36
+ throw e;
37
+ }
38
+ }
39
+
40
+ /** The `Party` struct tag of one partyos deployment. */
41
+ export function partyType(partyPackageId: string): string {
42
+ return normalizeStructTag(`${partyPackageId}::party::Party`);
43
+ }
44
+
45
+ // A Party from another partyos package (a previous generation) parses
46
+ // identically but no deployed package accepts it, so the object's type is
47
+ // checked against the deployment before its content is trusted.
48
+ function assertPartyType(objectId: string, type: string | undefined, partyPackageId: string): void {
49
+ const expected = partyType(partyPackageId);
50
+ if (type === undefined || normalizeStructTag(type) !== expected) {
51
+ throw new Error(
52
+ `Object ${objectId} is ${type ?? "of unknown type"}, not a ${expected}` +
53
+ (type?.endsWith("::party::Party") ? " (a Party from a different partyos deployment)" : ""),
54
+ );
55
+ }
56
+ }
57
+
58
+ /** Fetches and parses a shared `Party` object of this deployment's partyos package. */
59
+ export async function getPartyById(
60
+ client: ClientWithCoreApi,
61
+ partyId: string,
62
+ partyPackageId: string,
63
+ ): Promise<Party> {
64
+ let object;
65
+ try {
66
+ ({ object } = await client.core.getObject({ objectId: partyId, include: { content: true } }));
67
+ } catch (e) {
68
+ if (isNotFound(e)) throw new Error(`Party not found: ${partyId}`);
69
+ throw e;
70
+ }
71
+ if (!object?.content) throw new Error(`Party not found: ${partyId}`);
72
+ assertPartyType(partyId, object.type, partyPackageId);
73
+ return mapParty(partyId, PartyBcs.parse(object.content));
74
+ }
75
+
76
+ /**
77
+ * Fetches and parses multiple shared `Party` objects in one Core request.
78
+ * Missing objects are skipped; an object of another type is an error.
79
+ */
80
+ export async function getPartiesByIds(
81
+ client: ClientWithCoreApi,
82
+ partyIds: readonly string[],
83
+ partyPackageId: string,
84
+ ): Promise<Partial<Record<string, Party>>> {
85
+ if (partyIds.length === 0) return {};
86
+ const { objects } = await client.core.getObjects({
87
+ objectIds: [...new Set(partyIds)],
88
+ include: { content: true },
89
+ });
90
+ const parties: Partial<Record<string, Party>> = {};
91
+ for (const obj of objects) {
92
+ if (obj instanceof Error) continue;
93
+ assertPartyType(obj.objectId, obj.type, partyPackageId);
94
+ parties[obj.objectId] = mapParty(obj.objectId, PartyBcs.parse(obj.content));
95
+ }
96
+ return parties;
97
+ }
98
+
99
+ /** Derives a party's `PartyAdminCap` id (it is a `derived_object` off the party UID). */
100
+ export function derivePartyAdminCapId(partyId: string, partyPackageId: string): string {
101
+ return deriveObjectID(
102
+ partyId,
103
+ `${partyPackageId}::party::PartyAdminCapKey`,
104
+ bcs.Address.serialize(partyId).toBytes(),
105
+ );
106
+ }
107
+
108
+ // === Group membership reads ===
109
+
110
+ /** Collects the ids stored in every dynamic-field key of `parentId` whose type ends with `keySuffix`. */
111
+ async function collectKeyIds(
112
+ client: ClientWithCoreApi,
113
+ parentId: string,
114
+ keySuffix: string,
115
+ codec: { parse(bytes: Uint8Array): readonly unknown[] },
116
+ ): Promise<string[]> {
117
+ const ids: string[] = [];
118
+ let cursor: string | null | undefined;
119
+ do {
120
+ const page = await client.core.listDynamicFields({ parentId, cursor: cursor ?? undefined });
121
+ for (const f of page.dynamicFields) {
122
+ if (typeof f.name?.type === "string" && f.name.type.endsWith(keySuffix) && f.name.bcs != null) {
123
+ const [id] = codec.parse(keyBytes(f.name.bcs));
124
+ ids.push(String(id));
125
+ }
126
+ }
127
+ cursor = page.hasNextPage ? page.cursor : null;
128
+ } while (cursor);
129
+ return ids;
130
+ }
131
+
132
+ /**
133
+ * The group ids a party currently belongs to, read from its `MembershipKey`
134
+ * dynamic fields (the member-side record). No indexer required.
135
+ */
136
+ export async function getMemberships(client: ClientWithCoreApi, partyId: string): Promise<string[]> {
137
+ return collectKeyIds(client, partyId, "::party::MembershipKey", MembershipKeyBcs);
138
+ }
139
+
140
+ /** The member ids invited to a group but not yet accepted (its `PendingInviteKey` fields). */
141
+ export async function getPendingInvites(client: ClientWithCoreApi, groupId: string): Promise<string[]> {
142
+ return collectKeyIds(client, groupId, "::party::PendingInviteKey", PendingInviteKeyBcs);
143
+ }
144
+
145
+ /**
146
+ * The group ids that have invited a party but are still awaiting its response.
147
+ * This reads the member-side `PendingMembershipKey` inbox index, so it only
148
+ * enumerates the target party's dynamic fields.
149
+ */
150
+ export async function getPendingMemberships(client: ClientWithCoreApi, partyId: string): Promise<string[]> {
151
+ return collectKeyIds(client, partyId, "::party::PendingMembershipKey", PendingMembershipKeyBcs);
152
+ }
153
+
154
+ /** Whether `memberId` currently holds a membership record for `groupId`. */
155
+ export async function isMember(
156
+ client: ClientWithCoreApi,
157
+ memberId: string,
158
+ groupId: string,
159
+ partyPackageId: string,
160
+ ): Promise<boolean> {
161
+ const fieldId = deriveDynamicFieldID(
162
+ memberId,
163
+ `${partyPackageId}::party::MembershipKey`,
164
+ MembershipKeyBcs.serialize([groupId]).toBytes(),
165
+ );
166
+ return (await getObjectContent(client, fieldId)) !== null;
167
+ }
@@ -0,0 +1,167 @@
1
+ // Copyright (c) Miso Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // Core PartyOS transaction builders. Every builder returns a thunk that *appends*
5
+ // commands to a caller-owned Transaction, so flows compose. Package ids are taken
6
+ // in params (the client binds them; see ./client.ts). Calls into the party package
7
+ // go through the generated typed call fns (party.*).
8
+
9
+ import type { Transaction } from "@mysten/sui/transactions";
10
+ import * as party from "./contracts/partyos/party.ts";
11
+
12
+ export type TxThunk = (tx: Transaction) => void | Promise<void>;
13
+
14
+ export interface CreatePartyParams {
15
+ /** Human-readable party name (not verified; app-layer verifies). */
16
+ name: string;
17
+ /** Address that receives the PartyAdminCap. */
18
+ recipient: string;
19
+ partyPackageId: string;
20
+ }
21
+
22
+ /** Creates an individual party, shares it, and transfers its admin cap to `recipient`. */
23
+ export function createIndividualParty(params: CreatePartyParams): TxThunk {
24
+ return (tx) => {
25
+ const kind = tx.add(party.newIndividualKind({ package: params.partyPackageId }));
26
+ const res = tx.add(party._new({ package: params.partyPackageId, arguments: [kind, params.name] }));
27
+ tx.add(party.share({ package: params.partyPackageId, arguments: [res[0]!, res[1]!] }));
28
+ tx.transferObjects([res[1]!], params.recipient);
29
+ };
30
+ }
31
+
32
+ /** Creates a group party (empty member set), shares it, transfers its admin cap. */
33
+ export function createGroupParty(params: CreatePartyParams): TxThunk {
34
+ return (tx) => {
35
+ const kind = tx.add(party.newGroupKind({ package: params.partyPackageId }));
36
+ const res = tx.add(party._new({ package: params.partyPackageId, arguments: [kind, params.name] }));
37
+ tx.add(party.share({ package: params.partyPackageId, arguments: [res[0]!, res[1]!] }));
38
+ tx.transferObjects([res[1]!], params.recipient);
39
+ };
40
+ }
41
+
42
+ // === Group membership: invite / accept / decline / revoke / leave / evict ===
43
+ // Membership is consent-based: the group admin invites, the member accepts with
44
+ // its OWN admin cap. `member`/`group`/`*Cap` params are all object ids.
45
+
46
+ export interface InvitePartyParams {
47
+ groupId: string;
48
+ groupCapId: string;
49
+ /** The individual party (shared object id) to invite. */
50
+ memberId: string;
51
+ partyPackageId: string;
52
+ }
53
+
54
+ /** Invites an individual party to a group (gated by the group's admin cap). */
55
+ export function inviteParty(params: InvitePartyParams): TxThunk {
56
+ return (tx) => {
57
+ tx.add(party.inviteParty({
58
+ package: params.partyPackageId,
59
+ arguments: [params.groupId, params.memberId, params.groupCapId],
60
+ }));
61
+ };
62
+ }
63
+
64
+ export interface AcceptInviteParams {
65
+ groupId: string;
66
+ memberId: string;
67
+ /** The member party's admin cap id (proves consent). */
68
+ memberCapId: string;
69
+ partyPackageId: string;
70
+ }
71
+
72
+ /** Accepts a pending invite, joining the member to the group (member's cap). */
73
+ export function acceptInvite(params: AcceptInviteParams): TxThunk {
74
+ return (tx) => {
75
+ tx.add(party.acceptInvite({
76
+ package: params.partyPackageId,
77
+ arguments: [params.groupId, params.memberId, params.memberCapId],
78
+ }));
79
+ };
80
+ }
81
+
82
+ export interface DeclineInviteParams {
83
+ groupId: string;
84
+ /** The invited individual party (shared object id). */
85
+ memberId: string;
86
+ memberCapId: string;
87
+ partyPackageId: string;
88
+ }
89
+
90
+ /** Declines a pending invite (member's cap). */
91
+ export function declineInvite(params: DeclineInviteParams): TxThunk {
92
+ return (tx) => {
93
+ tx.add(party.declineInvite({
94
+ package: params.partyPackageId,
95
+ arguments: [params.groupId, params.memberId, params.memberCapId],
96
+ }));
97
+ };
98
+ }
99
+
100
+ export interface RevokeInviteParams {
101
+ groupId: string;
102
+ /** The invited individual party (shared object id). */
103
+ memberId: string;
104
+ groupCapId: string;
105
+ partyPackageId: string;
106
+ }
107
+
108
+ /** Revokes a pending invite (group's admin cap). */
109
+ export function revokeInvite(params: RevokeInviteParams): TxThunk {
110
+ return (tx) => {
111
+ tx.add(party.revokeInvite({
112
+ package: params.partyPackageId,
113
+ arguments: [params.groupId, params.memberId, params.groupCapId],
114
+ }));
115
+ };
116
+ }
117
+
118
+ export interface LeaveGroupParams {
119
+ groupId: string;
120
+ memberId: string;
121
+ memberCapId: string;
122
+ partyPackageId: string;
123
+ }
124
+
125
+ /** Leaves a group, authorized by the member's own admin cap. */
126
+ export function leaveGroup(params: LeaveGroupParams): TxThunk {
127
+ return (tx) => {
128
+ tx.add(party.leave({
129
+ package: params.partyPackageId,
130
+ arguments: [params.groupId, params.memberId, params.memberCapId],
131
+ }));
132
+ };
133
+ }
134
+
135
+ export interface RemoveMemberParams {
136
+ groupId: string;
137
+ groupCapId: string;
138
+ memberId: string;
139
+ partyPackageId: string;
140
+ }
141
+
142
+ /** Evicts a member from a group (group's admin cap); scrubs the member's record too. */
143
+ export function removeMember(params: RemoveMemberParams): TxThunk {
144
+ return (tx) => {
145
+ tx.add(party.removeMember({
146
+ package: params.partyPackageId,
147
+ arguments: [params.groupId, params.groupCapId, params.memberId],
148
+ }));
149
+ };
150
+ }
151
+
152
+ export interface SetNameParams {
153
+ partyId: string;
154
+ capId: string;
155
+ name: string;
156
+ partyPackageId: string;
157
+ }
158
+
159
+ /** Sets the party's human-readable name. */
160
+ export function setName(params: SetNameParams): TxThunk {
161
+ return (tx) => {
162
+ tx.add(party.setName({
163
+ package: params.partyPackageId,
164
+ arguments: [params.partyId, params.capId, params.name],
165
+ }));
166
+ };
167
+ }
package/src/types.ts ADDED
@@ -0,0 +1,18 @@
1
+ // Copyright (c) Miso Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // Public, camelCase result types. The mappers in ./internal.ts turn the generated
5
+ // (snake_case, Move-shaped) parse output into these.
6
+
7
+ export type PartyKind = "individual" | "group";
8
+
9
+ export interface Party {
10
+ id: string;
11
+ kind: PartyKind;
12
+ /** Human-readable name (not verified). */
13
+ name: string;
14
+ /** Member party ids — present only when `kind === "group"`. */
15
+ members?: string[];
16
+ /** Unix ms when the party was created. */
17
+ createdAtMs: number;
18
+ }