|
| 1 | +// Copyright © Aptos Foundation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { getPermissions, requestPermission, revokePermissions } from "../internal/permissions"; |
| 5 | +import { AptosConfig } from "./aptosConfig"; |
| 6 | +import { SimpleTransaction } from "../transactions/instances/simpleTransaction"; |
| 7 | +import { Permission } from "../types/permissions"; |
| 8 | +import { AccountAddress, PublicKey } from "../core"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Manages permission operations for delegated accounts. |
| 12 | + * Handles granting, revoking, and querying permissions for fungible assets, gas, and NFTs. |
| 13 | + */ |
| 14 | +export class Permissions { |
| 15 | + constructor(readonly config: AptosConfig) {} |
| 16 | + |
| 17 | + /** |
| 18 | + * Gets current permissions for a delegation key |
| 19 | + * |
| 20 | + * @example |
| 21 | + * ```typescript |
| 22 | + * const permissions = await aptos.permissions.getPermissions({ |
| 23 | + * primaryAccountAddress: AccountAddress.fromString("0x1"), |
| 24 | + * delegationPublicKey: delegatedAccount.publicKey |
| 25 | + * filter: FungibleAssetPermission |
| 26 | + * }); |
| 27 | + * ``` |
| 28 | + */ |
| 29 | + async getPermissions<T extends Permission>(args: { |
| 30 | + primaryAccountAddress: AccountAddress; |
| 31 | + delegationPublicKey: PublicKey; |
| 32 | + filter?: new (...a: any) => T; |
| 33 | + }): Promise<T[]> { |
| 34 | + return getPermissions({ |
| 35 | + aptosConfig: this.config, |
| 36 | + primaryAccountAddress: args.primaryAccountAddress, |
| 37 | + delegationPublicKey: args.delegationPublicKey, |
| 38 | + filter: args.filter, |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Requests permissions for a delegation key |
| 44 | + * |
| 45 | + * @param args |
| 46 | + * @param args.primaryAccountAddress - The primary account address |
| 47 | + * @param args.delegationPublicKey - The delegation public key |
| 48 | + * @param args.permissions - The permissions to request |
| 49 | + * @param args.expiration - The expiration time of the permissions, in epoch seconds. Defaults to 1 day from now. |
| 50 | + * @param args.refreshInterval - The refresh interval of the permissions, in seconds. Defaults to 60 seconds. |
| 51 | + * @param args.maxTransactionsPerInterval - The maximum number of transactions per interval. Defaults to 1000. |
| 52 | + * |
| 53 | + * @example |
| 54 | + * ```typescript |
| 55 | + * const txn = await aptos.permissions.requestPermissions({ |
| 56 | + * primaryAccountAddress: AccountAddress.fromString("0x1"), |
| 57 | + * delegationPublicKey: delegatedAccount.publicKey, |
| 58 | + * permissions: [ |
| 59 | + * FungibleAssetPermission.from({ asset: AccountAddress.A, amount: 100 }) |
| 60 | + * ] |
| 61 | + * }); |
| 62 | + * |
| 63 | + * await aptos.signAndSubmitTransaction({ |
| 64 | + * signer: primaryAccount, |
| 65 | + * transaction: txn, |
| 66 | + * }); |
| 67 | + * ``` |
| 68 | + */ |
| 69 | + async requestPermissions(args: { |
| 70 | + primaryAccountAddress: AccountAddress; |
| 71 | + delegationPublicKey: PublicKey; |
| 72 | + permissions: Permission[]; |
| 73 | + expiration?: number; |
| 74 | + refreshInterval?: number; |
| 75 | + maxTransactionsPerInterval?: number; |
| 76 | + }): Promise<SimpleTransaction> { |
| 77 | + return requestPermission({ |
| 78 | + aptosConfig: this.config, |
| 79 | + primaryAccountAddress: args.primaryAccountAddress, |
| 80 | + delegationPublicKey: args.delegationPublicKey, |
| 81 | + permissions: args.permissions, |
| 82 | + expiration: args.expiration ?? Date.now() + 24 * 60 * 60 * 1000, |
| 83 | + refreshInterval: args.refreshInterval ?? 60, |
| 84 | + maxTransactionsPerInterval: args.maxTransactionsPerInterval ?? 1000, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Revokes permissions from a delegation key. Note: You can pass an entire permission you get back from `getPermissions` |
| 90 | + * or call the static `revoke` function on the permission. |
| 91 | + * |
| 92 | + * @example |
| 93 | + * ```typescript |
| 94 | + * const txn = await aptos.permissions.revokePermission({ |
| 95 | + * primaryAccountAddress: AccountAddress.fromString("0x1"), |
| 96 | + * delegationPublicKey: delegatedAccount.publicKey, |
| 97 | + * permissions: [ |
| 98 | + * FungibleAssetPermission.revoke({ asset: AccountAddress.A }) |
| 99 | + * ] |
| 100 | + * }); |
| 101 | + * |
| 102 | + * await aptos.signAndSubmitTransaction({ |
| 103 | + * signer: primaryAccount, |
| 104 | + * transaction: txn, |
| 105 | + * }); |
| 106 | + * ``` |
| 107 | + */ |
| 108 | + async revokePermission(args: { |
| 109 | + primaryAccountAddress: AccountAddress; |
| 110 | + delegationPublicKey: PublicKey; |
| 111 | + permissions: Permission[]; |
| 112 | + }): Promise<SimpleTransaction> { |
| 113 | + return revokePermissions({ |
| 114 | + aptosConfig: this.config, |
| 115 | + primaryAccountAddress: args.primaryAccountAddress, |
| 116 | + delegationPublicKey: args.delegationPublicKey, |
| 117 | + permissions: args.permissions, |
| 118 | + }); |
| 119 | + } |
| 120 | +} |
0 commit comments