Skip to content

Commit

Permalink
feat(delegated payments): add revoke approvals to SDK and CLI PE-6754
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Oct 16, 2024
1 parent 1047762 commit f2d26da
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import {
uploadFile,
uploadFolder,
} from './commands/index.js';
import { revokeApprovals } from './commands/revokeApprovals.js';
import {
createApprovalOptions,
globalOptions,
optionMap,
revokeApprovalsOptions,
uploadFileOptions,
uploadFolderOptions,
walletOptions,
Expand Down Expand Up @@ -103,6 +105,16 @@ applyOptions(
).action(async (_commandOptions, command: Command) => {
await runCommand(command, createApproval);
});
applyOptions(
program
.command('revoke-approvals')
.description(
'Revokes all Turbo delegated payment approvals for given address',
),
revokeApprovalsOptions,
).action(async (_commandOptions, command: Command) => {
await runCommand(command, revokeApprovals);
});

if (
process.argv[1].includes('bin/turbo') || // Running from global .bin
Expand Down
36 changes: 36 additions & 0 deletions src/cli/commands/revokeApprovals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { RevokeApprovalsOptions } from '../types.js';
import { turboFromOptions } from '../utils.js';

export async function revokeApprovals(
options: RevokeApprovalsOptions,
): Promise<void> {
const { address: revokedAddress } = options;
if (revokedAddress === undefined) {
throw new Error(
'Must provide an approved --address to create approval for',
);
}

const turbo = await turboFromOptions(options);

const result = await turbo.revokeDelegatedPaymentApprovals({
revokedAddress,
});

console.log('Created approval:', JSON.stringify(result, null, 2));
}
2 changes: 2 additions & 0 deletions src/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,5 @@ export const createApprovalOptions = [
optionMap.address,
optionMap.expiresBySeconds,
];

export const revokeApprovalsOptions = [...walletOptions, optionMap.address];
4 changes: 4 additions & 0 deletions src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ export type CreateApprovalOptions = WalletOptions & {
value: string | undefined;
expiresBySeconds: number | undefined;
};

export type RevokeApprovalsOptions = WalletOptions & {
address: string | undefined;
};
14 changes: 14 additions & 0 deletions src/common/turbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
TurboFundWithTokensParams,
TurboPriceResponse,
TurboRatesResponse,
TurboRevokeDelegatePaymentApprovalsParams,
TurboSignedDataItemFactory,
TurboSubmitFundTxResponse,
TurboUnauthenticatedClientConfiguration,
Expand Down Expand Up @@ -277,4 +278,17 @@ export class TurboAuthenticatedClient
): Promise<TurboUploadDataItemResponse> {
return this.uploadService.createDelegatedPaymentApproval(p);
}

Check warning on line 280 in src/common/turbo.ts

View check run for this annotation

Codecov / codecov/patch

src/common/turbo.ts#L277-L280

Added lines #L277 - L280 were not covered by tests

/**
* Creates a data item with tags that designate it as a revoke action for delegated
* payment approvals for target revokedAddress. Signs the data item and sends it to
* the Turbo Upload Service, which will verify the signature and forward the admin
* action towards the Turbo Payment Service.
*/

revokeDelegatedPaymentApprovals(
p: TurboRevokeDelegatePaymentApprovalsParams,
): Promise<TurboUploadDataItemResponse> {
return this.uploadService.revokeDelegatedPaymentApprovals(p);
}

Check warning on line 293 in src/common/turbo.ts

View check run for this annotation

Codecov / codecov/patch

src/common/turbo.ts#L290-L293

Added lines #L290 - L293 were not covered by tests
}
21 changes: 21 additions & 0 deletions src/common/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
TurboDataItemSigner,
TurboFileFactory,
TurboLogger,
TurboRevokeDelegatePaymentApprovalsParams,
TurboSignedDataItemFactory,
TurboUnauthenticatedUploadServiceConfiguration,
TurboUnauthenticatedUploadServiceInterface,
Expand Down Expand Up @@ -336,4 +337,24 @@ export abstract class TurboAuthenticatedBaseUploadService
dataItemOpts,
});
}

Check warning on line 339 in src/common/upload.ts

View check run for this annotation

Codecov / codecov/patch

src/common/upload.ts#L314-L339

Added lines #L314 - L339 were not covered by tests

public async revokeDelegatedPaymentApprovals({
revokedAddress,
}: TurboRevokeDelegatePaymentApprovalsParams): Promise<TurboUploadDataItemResponse> {
const dataItemOpts = {
tags: [
{
name: revokeDelegatePaymentApprovalTagName,
value: revokedAddress,
},
],
};

const nonceData = Buffer.from(revokedAddress + Date.now());
return this.uploadFile({
fileStreamFactory: () => Readable.from(nonceData),
fileSizeFactory: () => nonceData.byteLength,
dataItemOpts,
});
}

Check warning on line 359 in src/common/upload.ts

View check run for this annotation

Codecov / codecov/patch

src/common/upload.ts#L342-L359

Added lines #L342 - L359 were not covered by tests
}
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ export type TurboCreateDelegatedPaymentApprovalParams = {
expiresBySeconds?: number;
};

export type TurboRevokeDelegatePaymentApprovalsParams = {
revokedAddress: string;
};

export type TurboUploadFolderResponse = {
fileResponses: TurboUploadDataItemResponse[];
manifestResponse?: TurboUploadDataItemResponse;
Expand Down Expand Up @@ -586,6 +590,9 @@ export interface TurboAuthenticatedUploadServiceInterface
createDelegatedPaymentApproval(
p: TurboCreateDelegatedPaymentApprovalParams,
): Promise<TurboUploadDataItemResponse>;
revokeDelegatedPaymentApprovals(
p: TurboRevokeDelegatePaymentApprovalsParams,
): Promise<TurboUploadDataItemResponse>;
}

export interface TurboUnauthenticatedClientInterface
Expand Down

0 comments on commit f2d26da

Please sign in to comment.