Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converts permissions to a serializable class #604

Merged
merged 6 commits into from
Dec 16, 2024

Conversation

kaw2k
Copy link
Contributor

@kaw2k kaw2k commented Dec 12, 2024

Description

There will need to be more work here, for example I didn't convert the revoke permissions to be a serializable class. Looking to get feedback.

Test Plan

Tests pass

Related Links

Base PR #596

@kaw2k kaw2k requested a review from a team as a code owner December 12, 2024 20:20
src/types/permissions.ts Outdated Show resolved Hide resolved
Comment on lines +133 to +138
const [capabilityKeys, capabilityValues] = Object.entries(this.capabilities).reduce(
([keys, values], [key, value]) => [keys.concat(key), values.concat(value)],
[[] as string[], [] as boolean[]],
);
serializer.serializeStr(JSON.stringify(capabilityKeys));
serializer.serializeStr(JSON.stringify(capabilityValues));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serializing the capabilities is a bit messy. The base type is:

capabilities: {
mutate: true,
transfer: false
}

where each property is optional. Let me know of a better way.

Copy link
Contributor

@hardsetting hardsetting left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we've been doing so far, to mimic rust enums, is the following pattern:

export abstract class Permission {
  static deserialize(deserializer: Deserializer) {
    const type = deserializer.deserializeStr() as PermissionType;
    switch (type) {
      case PermissionType.FungibleAsset: return FungibleAssetPermission.load(deserializer);
      ...
      default: throw new Error('Unsupported permission type');
    }
  }
}

export class FungibleAssetPermission extends Permission {
  serialize(serializer: Serializer) { ... }
  load(deserializer: Deserializer) { ... }
}

to be honest though, this mostly makes sense / is justified only if these types are matching rust types.
Is this the case / going to be the case? Otherwise they don't need to be classes imho

Comment on lines 67 to 69
static from({ asset, balance }: { asset: AccountAddress; balance: string }): FungibleAssetPermission {
return new FungibleAssetPermission(asset, balance);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just using the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mirroring other classes in the codebase that have from as a static constructor.

src/types/permissions.ts Outdated Show resolved Hide resolved
src/types/permissions.ts Outdated Show resolved Hide resolved
@kaw2k
Copy link
Contributor Author

kaw2k commented Dec 13, 2024

These types don't mirror the rust types. The classes have helped clean up the code in a few ways, a little complications in other. Pushing another update and calling out some other changes.

Comment on lines -74 to -78
/**
* Revoke permission types
*/
export type RevokeFungibleAssetPermission = Pick<FungibleAssetPermission, "type" | "asset">;
export type RevokeNFTPermission = Pick<NFTPermission, "type" | "assetAddress">;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I removed the revoking types here in favor of just using the base class. Might add these back in but modified after using the new style in connect.

@@ -36,8 +28,8 @@ export async function getPermissions<T extends PermissionType>({
aptosConfig: AptosConfig;
primaryAccountAddress: AccountAddress;
subAccountPublicKey: Ed25519PublicKey;
filter?: T;
}): Promise<FilteredPermissions<T>> {
filter?: new (...a: any) => T;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying something different with the filtering. Eventually we will hit the indexer and we can move away from calling the node directly.

This takes the class itself, so for example FungibleAssetPermission and uses instanceof for filtering.

Copy link
Member

@chiumax chiumax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

@kaw2k kaw2k merged commit 676fb72 into sub-accounts Dec 16, 2024
2 of 5 checks passed
@kaw2k kaw2k deleted the sub-accounts-permission-serialization branch December 16, 2024 18:41
kaw2k added a commit that referenced this pull request Dec 19, 2024
* Converts permissions to a serializable class

* updates import

* Converts to single object argument approach

* More work

* Fixes dependency cycle

* Formats code
kaw2k added a commit that referenced this pull request Dec 19, 2024
* Converts permissions to a serializable class

* updates import

* Converts to single object argument approach

* More work

* Fixes dependency cycle

* Formats code
kaw2k added a commit that referenced this pull request Dec 19, 2024
* Converts permissions to a serializable class

* updates import

* Converts to single object argument approach

* More work

* Fixes dependency cycle

* Formats code
kaw2k added a commit that referenced this pull request Dec 20, 2024
* Converts permissions to a serializable class

* updates import

* Converts to single object argument approach

* More work

* Fixes dependency cycle

* Formats code
kaw2k added a commit that referenced this pull request Jan 9, 2025
* Converts permissions to a serializable class

* updates import

* Converts to single object argument approach

* More work

* Fixes dependency cycle

* Formats code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants