Skip to content

Commit

Permalink
Feat/new msg tag (#413)
Browse files Browse the repository at this point in the history
* feat: Upgrade types to 0.4.0-alpha-25

* feat: Create Group with tags

* feat: SetTag tx

* chore(example): Update Group Tag

* feat: Create Bucket with tags

* chore(example): Create bucket with tags

* refactor: Rename Object class name

* feat: Create Object with tags

* chore(example): Create Object with tags

* chore: Enter pre changeset

* chore: Minor Changeset
  • Loading branch information
rrr523 authored Dec 7, 2023
1 parent e79ef17 commit 47d59a1
Show file tree
Hide file tree
Showing 23 changed files with 814 additions and 178 deletions.
14 changes: 14 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"mode": "pre",
"tag": "alpha",
"initialVersions": {
"greenfield-js-sdk-docs": "0.0.0",
"@demo/wallet": "0.0.16",
"@demo/nodejs": "0.0.19",
"@bnb-chain/create-gnfd-app": "1.0.0",
"@bnb-chain/greenfiled-file-handle": "1.0.2",
"@bnb-chain/greenfield-js-sdk": "1.0.6",
"@bnb-chain/greenfield-zk-crypto": "1.0.0"
},
"changesets": []
}
6 changes: 6 additions & 0 deletions .changeset/tricky-chicken-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@bnb-chain/greenfield-js-sdk': minor
---

feat(BREAKING CHANGE): create group / bucket / object must have `tags` arguments and supply `setMsg`
method to update tags.
2 changes: 1 addition & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint": "next lint"
},
"dependencies": {
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.23",
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.25",
"@bnb-chain/greenfield-js-sdk": "workspace:*",
"@bnb-chain/greenfiled-file-handle": "workspace:*",
"@cosmjs/encoding": "^0.32.0",
Expand Down
2 changes: 2 additions & 0 deletions examples/nextjs/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const getSps = async () => {
const sps = await client.sp.getStorageProviders();
const finalSps = (sps ?? []).filter((v: any) => v.endpoint.includes('nodereal'));

// const finalSps = sps.filter((v) => v.id === 4);

return finalSps;
};

Expand Down
15 changes: 14 additions & 1 deletion examples/nextjs/src/components/bucket/create/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { client, selectSp } from '@/client';
import { ACCOUNT_PRIVATEKEY } from '@/config/env';
import { getOffchainAuthKeys } from '@/utils/offchainAuth';
import { useState } from 'react';
import { useAccount } from 'wagmi';
Expand Down Expand Up @@ -48,6 +47,20 @@ export const CreateBucket = () => {
primarySpAddress: spInfo.primarySpAddress,
},
paymentAddress: address,
// empty tags
tags: {
tags: [],
},

// with tags:
// tags: {
// tags: [
// {
// key: 'test',
// value: 'test',
// },
// ],
// },
},
{
// type: 'ECDSA',
Expand Down
59 changes: 52 additions & 7 deletions examples/nextjs/src/components/group/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export const CreateGroup = () => {
creator: address,
groupName: createGroupInfo.groupName,
extra: 'extra info',
tags: {
tags: [],
},
});

const simulateInfo = await createGroupTx.simulate({
Expand All @@ -42,23 +45,65 @@ export const CreateGroup = () => {
gasPrice: simulateInfo.gasPrice,
payer: address,
granter: '',
signTypedDataCallback: async (addr: string, message: string) => {
const provider = await connector?.getProvider();
return await provider?.request({
method: 'eth_signTypedData_v4',
params: [addr, message],
});
// signTypedDataCallback: async (addr: string, message: string) => {
// const provider = await connector?.getProvider();
// return await provider?.request({
// method: 'eth_signTypedData_v4',
// params: [addr, message],
// });
// },
});

if (res.code === 0) {
alert('create group success');
}

console.log(res);
}}
>
create group with empty tags
</button>

<button
onClick={async () => {
if (!address) return;

const createGroupTx = await client.group.createGroup({
creator: address,
groupName: createGroupInfo.groupName,
extra: 'extra info',
tags: {
tags: [
{
key: 'key',
value: 'value',
},
],
},
});

const simulateInfo = await createGroupTx.simulate({
denom: 'BNB',
});

console.log(simulateInfo);

const res = await createGroupTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo.gasLimit),
gasPrice: simulateInfo.gasPrice,
payer: address,
granter: '',
});

if (res.code === 0) {
alert('create group success');
}

console.log(res);
}}
>
create group
create group with tags
</button>
</div>
);
Expand Down
44 changes: 43 additions & 1 deletion examples/nextjs/src/components/group/update/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { client } from '@/client';
import { toTimestamp } from '@bnb-chain/greenfield-js-sdk';
import { GRNToString, newBucketGRN, newGroupGRN, toTimestamp } from '@bnb-chain/greenfield-js-sdk';
import { useState } from 'react';
import { zeroAddress } from 'viem';
import { useAccount } from 'wagmi';
Expand All @@ -17,6 +17,7 @@ export const GroupUpdate = () => {
setGroupName(e.target.value);
}}
/>
<br />
<button
onClick={async () => {
if (!address) return;
Expand Down Expand Up @@ -56,6 +57,7 @@ export const GroupUpdate = () => {
>
update group (add)
</button>
<br />
<button
onClick={async () => {
if (!address) return;
Expand Down Expand Up @@ -87,6 +89,7 @@ export const GroupUpdate = () => {
>
update group (delete)
</button>
<br />
<button
onClick={async () => {
if (!address) return;
Expand Down Expand Up @@ -117,6 +120,45 @@ export const GroupUpdate = () => {
>
updateGroupExtra
</button>
<br />
<button
onClick={async () => {
if (!address) return;

const resource = GRNToString(newGroupGRN(address, 'zzxc1'));

const updateGroupTx = await client.storage.setTag({
operator: address,
resource,
tags: {
tags: [
{
key: 'x',
value: 'xx',
},
],
},
});

const simulateInfo = await updateGroupTx.simulate({
denom: 'BNB',
});

console.log(simulateInfo);

const res = await updateGroupTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo.gasLimit),
gasPrice: simulateInfo.gasPrice,
payer: address,
granter: '',
});

console.log('res', res);
}}
>
update group tags
</button>
</div>
);
};
24 changes: 24 additions & 0 deletions examples/nextjs/src/components/object/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ export const CreateObject = () => {
redundancyType: 'REDUNDANCY_EC_TYPE',
contentLength,
expectCheckSums: JSON.parse(expectCheckSums),
// empty tags
// tags: {
// tags: [],
// },
tags: {
tags: [
{
key: 'test',
value: 'test',
},
],
},
},
{
type: 'EDDSA',
Expand Down Expand Up @@ -172,6 +184,18 @@ export const CreateObject = () => {
bucketName: createObjectInfo.bucketName,
objectName: createObjectInfo.objectName + '/',
creator: address,
// empty tags
// tags: {
// tags: [],
// },
tags: {
tags: [
{
key: 'test',
value: 'test',
},
],
},
},
{
type: 'EDDSA',
Expand Down
2 changes: 1 addition & 1 deletion packages/js-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
]
},
"dependencies": {
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.23",
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.25",
"@bnb-chain/greenfield-zk-crypto": "workspace:*",
"@cosmjs/proto-signing": "^0.32.0",
"@cosmjs/stargate": "^0.32.0",
Expand Down
10 changes: 9 additions & 1 deletion packages/js-sdk/src/api/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
MsgPutPolicy,
MsgUpdateBucketInfo,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx';
import { ResourceTags } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/types';
import { bytesFromBase64 } from '@bnb-chain/greenfield-cosmos-types/helpers';
import { Headers } from 'cross-fetch';
import { bytesToUtf8, hexToBytes } from 'ethereum-cryptography/utils';
Expand Down Expand Up @@ -68,7 +69,7 @@ import {
import { AuthType, SpClient } from '../clients/spclient/spClient';
import { TxClient } from '../clients/txClient';
import { METHOD_GET, NORMAL_ERROR_CODE } from '../constants/http';
import { MsgCreateBucketSDKTypeEIP712 } from '../messages/greenfield/storage/MsgCreateBucket';
import { getMsgCreateBucketSDKTypeEIP712 } from '../messages/greenfield/storage/MsgCreateBucket';
import { MsgDeleteBucketSDKTypeEIP712 } from '../messages/greenfield/storage/MsgDeleteBucket';
import { MsgMigrateBucketSDKTypeEIP712 } from '../messages/greenfield/storage/MsgMigrateBucket';
import { MsgUpdateBucketInfoSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateBucketInfo';
Expand Down Expand Up @@ -212,6 +213,7 @@ export class Bucket implements IBucket {
spInfo,
duration,
paymentAddress,
tags,
} = params;

try {
Expand Down Expand Up @@ -240,6 +242,7 @@ export class Bucket implements IBucket {
},
charged_read_quota: chargedReadQuota,
payment_address: paymentAddress,
tags: tags,
});

const signHeaders = await this.spClient.signHeaders(reqMeta, authType);
Expand Down Expand Up @@ -271,6 +274,10 @@ export class Bucket implements IBucket {
}

private async createBucketTx(msg: MsgCreateBucket, signedMsg: CreateBucketApprovalResponse) {
const isTagsEmpty = msg?.tags?.tags?.length === 0;

const MsgCreateBucketSDKTypeEIP712 = getMsgCreateBucketSDKTypeEIP712(isTagsEmpty);

return await this.txClient.tx(
MsgCreateBucketTypeUrl,
msg.creator,
Expand Down Expand Up @@ -307,6 +314,7 @@ export class Bucket implements IBucket {
},
chargedReadQuota: Long.fromString(signedMsg.charged_read_quota),
paymentAddress: signedMsg.payment_address,
tags: ResourceTags.fromJSON(signedMsg.tags),
};

return await this.createBucketTx(msg, signedMsg);
Expand Down
18 changes: 11 additions & 7 deletions packages/js-sdk/src/api/group.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import { TxClient } from '../clients/txClient';
import { MsgCreateGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgCreateGroup';
import { MsgDeleteGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgDeleteGroup';
import { MsgLeaveGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgLeaveGroup';
import { MsgUpdateGroupExtraSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateGroupExtra';
import { getMsgUpdateGroupMemberSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateGroupMember';
import { GRNToString, newBucketGRN, newGroupGRN, newObjectGRN } from '../utils/grn';
import {
QueryGroupNFTResponse,
QueryHeadGroupMemberResponse,
Expand Down Expand Up @@ -34,6 +27,13 @@ import {
TxResponse,
} from '..';
import { RpcQueryClient } from '../clients/queryclient';
import { TxClient } from '../clients/txClient';
import { getMsgCreateGroupSDKTypeWithTagEIP712 } from '../messages/greenfield/storage/MsgCreateGroup';
import { MsgDeleteGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgDeleteGroup';
import { MsgLeaveGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgLeaveGroup';
import { MsgUpdateGroupExtraSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateGroupExtra';
import { getMsgUpdateGroupMemberSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateGroupMember';
import { GRNToString, newBucketGRN, newGroupGRN, newObjectGRN } from '../utils/grn';
import { Storage } from './storage';

export interface IGroup {
Expand Down Expand Up @@ -111,6 +111,10 @@ export class Group implements IGroup {
private queryClient: RpcQueryClient = container.resolve(RpcQueryClient);

public async createGroup(msg: MsgCreateGroup) {
const isTagsEmpty = msg?.tags?.tags?.length === 0;

const MsgCreateGroupSDKTypeEIP712 = getMsgCreateGroupSDKTypeWithTagEIP712(isTagsEmpty);

return await this.txClient.tx(
MsgCreateGroupTypeUrl,
msg.creator,
Expand Down
Loading

0 comments on commit 47d59a1

Please sign in to comment.