Skip to content

Commit

Permalink
Fix/statment expiratime (#524)
Browse files Browse the repository at this point in the history
* fix: Fetch timeout

* fix: Statement's expirationTime compatibility
  • Loading branch information
rrr523 authored Apr 21, 2024
1 parent e746b6c commit 5a3de3f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-rice-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

fix: Statment's expiratime compatibility
12 changes: 11 additions & 1 deletion examples/nextjs/src/components/policy/bucketPolicy/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { client } from '@/client';
import { GRNToString, newBucketGRN, PermissionTypes } from '@bnb-chain/greenfield-js-sdk';
import {
GRNToString,
newBucketGRN,
PermissionTypes,
toTimestamp,
} from '@bnb-chain/greenfield-js-sdk';
import { useState } from 'react';
import { useAccount } from 'wagmi';

Expand All @@ -25,10 +30,14 @@ export const BucketPolicy = () => {
onClick={async () => {
if (!address) return;

const date = new Date();
date.setDate(date.getMinutes() + 10);

const statement: PermissionTypes.Statement = {
effect: PermissionTypes.Effect.EFFECT_ALLOW,
actions: [PermissionTypes.ActionType.ACTION_UPDATE_BUCKET_INFO],
resources: [GRNToString(newBucketGRN(policyBucketInfo.bucketName))],
expirationTime: toTimestamp(date),
};

const tx = await client.bucket.putBucketPolicy(policyBucketInfo.bucketName, {
Expand All @@ -38,6 +47,7 @@ export const BucketPolicy = () => {
type: PermissionTypes.PrincipalType.PRINCIPAL_TYPE_GNFD_ACCOUNT,
value: '0x0000000000000000000000000000000000000001',
},
expirationTime: toTimestamp(date),
});

const simulateInfo = await tx.simulate({
Expand Down
6 changes: 5 additions & 1 deletion examples/nextjs/src/components/policy/objectPolicy/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { client } from '@/client';
import { PermissionTypes, TimestampTypes } from '@bnb-chain/greenfield-js-sdk';
import { PermissionTypes, TimestampTypes, toTimestamp } from '@bnb-chain/greenfield-js-sdk';
import { useState } from 'react';
import { useAccount } from 'wagmi';

Expand Down Expand Up @@ -35,10 +35,13 @@ export const ObjectPolicy = () => {
onClick={async () => {
if (!address) return;

const date = new Date();
date.setDate(date.getMinutes() + 10);
const statement: PermissionTypes.Statement = {
effect: PermissionTypes.Effect.EFFECT_ALLOW,
actions: [PermissionTypes.ActionType.ACTION_GET_OBJECT],
resources: [],
// expirationTime: toTimestamp(date),
};

const tx = await client.object.putObjectPolicy(
Expand All @@ -51,6 +54,7 @@ export const ObjectPolicy = () => {
type: PermissionTypes.PrincipalType.PRINCIPAL_TYPE_GNFD_ACCOUNT,
value: '0x0000000000000000000000000000000000000001',
},
expirationTime: toTimestamp(date),
},
);

Expand Down
4 changes: 2 additions & 2 deletions packages/js-sdk/src/api/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export interface IObject {
bucketName: string,
objectName: string,
// expirationTime: Date,
srcMsg: Omit<MsgPutPolicy, 'resource' | 'expirationTime'>,
srcMsg: Omit<MsgPutPolicy, 'resource'>,
): Promise<TxResponse>;

deleteObjectPolicy(
Expand Down Expand Up @@ -618,7 +618,7 @@ export class Objects implements IObject {
bucketName: string,
objectName: string,
// expirationTime: Date,
srcMsg: Omit<MsgPutPolicy, 'resource' | 'expirationTime'>,
srcMsg: Omit<MsgPutPolicy, 'resource'>,
) {
const resource = GRNToString(newObjectGRN(bucketName, objectName));
const msg: MsgPutPolicy = {
Expand Down
9 changes: 7 additions & 2 deletions packages/js-sdk/src/api/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ export class Storage implements IStorage {
...toSdk,
expiration_time: msg.expirationTime ? fromTimestamp(msg.expirationTime) : '',
statements: toSdk.statements.map((e) => {
// @ts-ignore
e.expiration_time = '';
if (e.expiration_time) {
// @ts-ignore
e.expiration_time = fromTimestamp(e.expiration_time);
} else {
// @ts-ignore
e.expiration_time = '';
}

if (e.resources.length == 0) {
// @ts-ignore
Expand Down
1 change: 1 addition & 0 deletions packages/js-sdk/src/clients/txClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export class TxClient implements ITxClient {
gasLimit,
granter,
payer,
// @ts-ignore
712,
);

Expand Down

0 comments on commit 5a3de3f

Please sign in to comment.