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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/typescript/keyless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ const example = async () => {

example();


// const testPermissioned = () => {

// const subAccount = AbstractedEd25519Account.generate();
Expand Down
11 changes: 5 additions & 6 deletions src/api/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { getPermissions, requestPermission, revokePermissions } from "../internal/permissions";
import { AptosConfig } from "./aptosConfig";
import { SimpleTransaction } from "../transactions/instances/simpleTransaction";
import { FilteredPermissions, Permission, PermissionType, RevokePermission } from "../types/permissions";
import { Permission } from "../types/permissions";
import { AccountAddress, Ed25519PublicKey } from "../core";

/**
Expand Down Expand Up @@ -32,12 +32,11 @@ export class Permissions {
*
* @returns A promise that resolves to an array of current permissions and their remaining balances.
*/
async getPermissions<T extends PermissionType>(args: {
async getPermissions<T extends Permission>(args: {
primaryAccountAddress: AccountAddress;
subAccountPublicKey: Ed25519PublicKey;

filter?: T;
}): Promise<FilteredPermissions<T>> {
filter?: new (...a: any) => T;
}): Promise<T[]> {
return getPermissions({
aptosConfig: this.config,
primaryAccountAddress: args.primaryAccountAddress,
Expand Down Expand Up @@ -111,7 +110,7 @@ export class Permissions {
async revokePermission(args: {
primaryAccountAddress: AccountAddress;
subAccountPublicKey: Ed25519PublicKey;
permissions: RevokePermission[];
permissions: Permission[];
}): Promise<SimpleTransaction> {
return revokePermissions({
aptosConfig: this.config,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from "./core";
export * from "./transactions";
export * from "./transactions/management";
export * from "./types";
export * from "./types/permissions";
export * from "./utils";
147 changes: 69 additions & 78 deletions src/internal/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,12 @@
import { SimpleTransaction } from "../transactions/instances/simpleTransaction";
import { MoveString } from "../bcs";
import { AptosIntentBuilder } from "../transactions";
import {
FilteredPermissions,
PermissionType,
MoveVMPermissionType,
RevokePermission,
Permission,
buildFungibleAssetPermission,
buildNFTPermission,
} from "../types/permissions";
import { MoveVMPermissionType, Permission, FungibleAssetPermission, NFTPermission } from "../types/permissions";
import { Transaction } from "../api/transaction";
import { view } from "./view";

// functions
export async function getPermissions<T extends PermissionType>({
export async function getPermissions<T extends Permission>({
aptosConfig,
primaryAccountAddress,
subAccountPublicKey,
Expand All @@ -36,8 +28,8 @@
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.

}): Promise<T[]> {
const handle = await getHandleAddress({ aptosConfig, primaryAccountAddress, subAccountPublicKey });

const res = await fetch(`${aptosConfig.fullnode}/accounts/${handle}/resources`);
Expand All @@ -59,13 +51,13 @@
const permissions = data[0].data.perms.data.map((d) => {
switch (d.key.type_name) {
case MoveVMPermissionType.FungibleAsset: // can this be better? i dont rly like this
return buildFungibleAssetPermission({
asset: d.key.data,
return FungibleAssetPermission.from({
asset: AccountAddress.fromString(d.key.data),
balance: d.value,
});
case MoveVMPermissionType.TransferPermission:
return buildNFTPermission({
assetAddress: d.key.data,
return NFTPermission.from({
assetAddress: AccountAddress.fromString(d.key.data),
capabilities: { transfer: true, mutate: false },
});
default:
Expand All @@ -74,8 +66,8 @@
}
});

const filtered = filter ? permissions.filter((p) => filter.includes(p.type as PermissionType)) : permissions;
return filtered as FilteredPermissions<T>;
const filtered = filter ? permissions.filter((p) => p instanceof filter) : permissions;
return filtered as T[];
}

// should it return the requested permissions? on success? and when it fails it
Expand Down Expand Up @@ -108,21 +100,24 @@

// if nft permission has multiple capabilities, we need to add multiple txns
// For NFT permissions with multiple capabilities, split into separate transactions

const expandedPermissions = permissions.flatMap((permission) => {
if (permission.type === PermissionType.NFT && permission.capabilities) {
const expanded = [];
if (permission instanceof NFTPermission && permission.capabilities) {
const expanded: Permission[] = [];
if (permission.capabilities.transfer) {
expanded.push({
...permission,
capabilities: { transfer: true, mutate: false },
});
expanded.push(
NFTPermission.from({
assetAddress: permission.assetAddress,
capabilities: { transfer: true, mutate: false },
}),
);
}
if (permission.capabilities.mutate) {
expanded.push({
...permission,
capabilities: { transfer: false, mutate: true },
});
expanded.push(
NFTPermission.from({
assetAddress: permission.assetAddress,
capabilities: { transfer: false, mutate: true },
}),
);
}
return expanded;
}
Expand Down Expand Up @@ -157,7 +152,7 @@
aptosConfig: AptosConfig;
primaryAccountAddress: AccountAddress;
subAccountPublicKey: Ed25519PublicKey;
permissions: RevokePermission[];
permissions: Permission[];
}): Promise<SimpleTransaction> {
const { aptosConfig, primaryAccountAddress, subAccountPublicKey, permissions } = args;

Expand All @@ -172,27 +167,24 @@
});

const permissionPromises = permissions.map((permission) => {
switch (permission.type) {
case PermissionType.FungibleAsset: {
return builder.add_batched_calls({
function: "0x1::fungible_asset::revoke_permission",
functionArguments: [signer[0].borrow(), permission.asset],
typeArguments: [],
});
}
// TODO: object nft revoke
case PermissionType.NFT: {
return builder.add_batched_calls({
function: "0x1::object::revoke_permission",
functionArguments: [signer[0].borrow(), permission.assetAddress],
typeArguments: ["0x4::token::Token"],
});
}
default: {
console.log("Not implemented");
return Promise.resolve();
}
if (permission instanceof FungibleAssetPermission) {
return builder.add_batched_calls({
function: "0x1::fungible_asset::revoke_permission",
functionArguments: [signer[0].borrow(), permission.asset],
typeArguments: [],
});
}
// TODO: object nft revoke
if (permission instanceof NFTPermission) {
return builder.add_batched_calls({
function: "0x1::object::revoke_permission",
functionArguments: [signer[0].borrow(), permission.assetAddress],
typeArguments: ["0x4::token::Token"],
});
}

console.log("Not implemented");

Check warning on line 186 in src/internal/permissions.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
return Promise.resolve();
});

await Promise.all(permissionPromises);
Expand Down Expand Up @@ -241,42 +233,41 @@
permission: Permission;
},
) {
switch (args.permission.type) {
case PermissionType.FungibleAsset:
if (args.permission instanceof FungibleAssetPermission) {
return builder.add_batched_calls({
function: "0x1::fungible_asset::grant_permission",
functionArguments: [
BatchArgument.new_signer(0),
args.permissionedSigner[0].borrow(),
args.permission.asset, // do i need to convert this to AccountAddress? .... i guess not??
args.permission.balance,
],
typeArguments: [],
});
}
if (args.permission instanceof NFTPermission) {
const txn: Promise<BatchArgument[]>[] = [];
if (args.permission.capabilities.transfer) {
return builder.add_batched_calls({
function: "0x1::fungible_asset::grant_permission",
function: "0x1::object::grant_permission",
functionArguments: [
BatchArgument.new_signer(0),
args.permissionedSigner[0].borrow(),
args.permission.asset, // do i need to convert this to AccountAddress? .... i guess not??
args.permission.balance,
args.permission.assetAddress,
],
typeArguments: [],
typeArguments: ["0x4::token::Token"],
});
case PermissionType.NFT: {
const txn: Promise<BatchArgument[]>[] = [];
if (args.permission.capabilities.transfer) {
return builder.add_batched_calls({
function: "0x1::object::grant_permission",
functionArguments: [
BatchArgument.new_signer(0),
args.permissionedSigner[0].borrow(),
args.permission.assetAddress,
],
typeArguments: ["0x4::token::Token"],
});
}
if (args.permission.capabilities.mutate) {
console.log("mutate not implemented");
throw new Error("mutate not implemented");
}
return txn;
}
default:
console.log("Not implemented");
throw new Error(`${args.permission.type} not implemented`);
return Promise.resolve();
if (args.permission.capabilities.mutate) {
console.log("mutate not implemented");

Check warning on line 262 in src/internal/permissions.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
throw new Error("mutate not implemented");
}
return txn;
}

console.log("Not implemented");

Check warning on line 268 in src/internal/permissions.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
throw new Error(`${args.permission} not implemented`);
return Promise.resolve();
}

async function finalizeNewHandle(
Expand Down
Loading
Loading