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

feat(dcellar-web-ui): support new off-chain authentication and remove… #366

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions apps/dcellar-web-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"antd": "5.11.0",
"ahooks": "3.7.7",
"hash-wasm": "4.10.0",
"@bnb-chain/greenfield-js-sdk": "1.2.0",
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.30",
"@bnb-chain/greenfield-js-sdk": "1.3.0-alpha.2",
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.31",
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@next/bundle-analyzer": "^13.1.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@ import { getSpOffChainData } from '@/store/slices/persist';
import { useAsyncEffect } from 'ahooks';
import { memo, useEffect, useMemo, useState } from 'react';

import { reverseVisibilityType } from '@/constants/legacy';
import { useOffChainAuth } from '@/context/off-chain-auth/useOffChainAuth';
import { resolve } from '@/facade/common';
import { broadcastFault, commonFault, createTxFault, simulateFault } from '@/facade/error';
import { getObjectMeta } from '@/facade/object';
import { genCreateObjectTx } from '@/modules/object/utils/genCreateObjectTx';
import { getCreateObjectTx } from '@/modules/object/utils/getCreateObjectTx';
import {
TMakePutObjectHeaders,
makePutObjectHeaders,
} from '@/modules/object/utils/generatePubObjectOptions';
import { setupAccountRecords } from '@/store/slices/accounts';
import { setupSpMeta } from '@/store/slices/sp';
import { parseErrorXml, sleep } from '@/utils/common';
import { AuthType, CreateObjectApprovalRequest } from '@bnb-chain/greenfield-js-sdk';
import {
AuthType,
Long,
RedundancyType,
VisibilityType,
bytesFromBase64,
} from '@bnb-chain/greenfield-js-sdk';
import axios from 'axios';
import { isEmpty } from 'lodash-es';
import { MsgCreateObject } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx';

interface GlobalTasksProps {}

Expand Down Expand Up @@ -122,7 +128,6 @@ export const GlobalObjectUploadManager = memo<GlobalTasksProps>(
if (authModal) return;
dispatch(updateUploadProgress({ account: loginAccount, id: task.id, progress }));
},

headers: {
Authorization: headers.get('Authorization'),
'content-type': headers.get('content-type'),
Expand All @@ -132,6 +137,7 @@ export const GlobalObjectUploadManager = memo<GlobalTasksProps>(
'x-gnfd-expiry-timestamp': headers.get('x-gnfd-expiry-timestamp'),
'x-gnfd-txn-hash': headers.get('x-gnfd-txn-hash'),
'x-gnfd-user-address': headers.get('x-gnfd-user-address'),
'X-Gnfd-App-Reg-Public-Key': headers.get('X-Gnfd-App-Reg-Public-Key'),
},
})
.then(async () => {
Expand Down Expand Up @@ -206,20 +212,20 @@ export const GlobalObjectUploadManager = memo<GlobalTasksProps>(
const finalName = [...task.prefixFolders, task.waitObject.relativePath, task.waitObject.name]
.filter((item) => !!item)
.join('/');
const createObjectPayload: CreateObjectApprovalRequest = {
const msgCreateObject: MsgCreateObject = {
creator: tempAccount.address,
bucketName: task.bucketName,
objectName: finalName,
creator: tempAccount.address,
visibility: reverseVisibilityType[task.visibility],
fileType: task.waitObject.type || 'application/octet-stream',
contentLength: task.waitObject.size,
expectCheckSums: task.checksum,
duration: 5000,
visibility: task.visibility,
contentType: task.waitObject.type || 'application/octet-stream',
payloadSize: Long.fromInt(task.waitObject.size),
expectChecksums: task.checksum.map((x) => bytesFromBase64(x)),
redundancyType: RedundancyType.REDUNDANCY_EC_TYPE,
};
const [createObjectTx, _createError] = await genCreateObjectTx(createObjectPayload, {
type: 'ECDSA',
privateKey: tempAccount.privateKey,
}).then(resolve, createTxFault);
const [createObjectTx, _createError] = await getCreateObjectTx(msgCreateObject).then(
resolve,
createTxFault,
);
if (_createError) {
console.error('createError', _createError);
return dispatch(
Expand Down
3 changes: 0 additions & 3 deletions apps/dcellar-web-ui/src/constants/legacy.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { assetPrefix } from '@/base/env';
import { convertVisibility } from '@/utils/object';

export const getLoginLocalStorageKey = (prefix = '') => `${prefix}_GREENFIELD_LOGIN_STORAGE`;

export const USER_REJECT_STATUS_NUM = 4001;

export const REQUEST_PENDING_NUM = -32002;

export const reverseVisibilityType = convertVisibility();

export const GNFD_TESTNET = 5600;

export const GNFD_MAINNET = 1017;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export const OffChainAuthProvider: React.FC<any> = ({ children }) => {
try {
const domain = getDomain();

// If no sps selected, use all sps for welcome auth
const pruneSps = (isEmpty(authSps.current) ? allSpList : authSps.current).map(
(item: any) => ({
address: item.operatorAddress,
Expand Down
20 changes: 10 additions & 10 deletions apps/dcellar-web-ui/src/facade/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import {
QueryHeadBucketResponse,
QueryQuoteUpdateTimeResponse,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/query';
import { MsgUpdateBucketInfo } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx';
import {
MsgCreateBucket,
MsgUpdateBucketInfo,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx';
import { ResourceTags_Tag } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/types';
import {
AuthType,
CreateBucketApprovalRequest,
GRNToString,
GetBucketMetaResponse,
GetUserBucketsResponse,
Expand Down Expand Up @@ -263,12 +265,11 @@ export const getBucketQuotaUpdateTime = async (bucketName: string) => {
};

export const getCreateBucketTx = async (
params: CreateBucketApprovalRequest,
authType: AuthType,
msgCreateBucket: MsgCreateBucket,
): Promise<[TxResponse, null] | ErrorResponse> => {
const client = await getClient();
const [createBucketTx, error1] = await client.bucket
.createBucket(params, authType)
.createBucket(msgCreateBucket)
.then(resolve, createTxFault);

if (!createBucketTx) return [null, error1];
Expand All @@ -277,12 +278,11 @@ export const getCreateBucketTx = async (
};

export const simulateCreateBucket = async (
params: CreateBucketApprovalRequest,
authType: AuthType,
params: MsgCreateBucket,
): Promise<[ISimulateGasFee, null, TxResponse] | ErrorResponse> => {
const client = await getClient();
const [createBucketTx, error1] = await client.bucket
.createBucket(params, authType)
.createBucket(params)
.then(resolve, createTxFault);

if (!createBucketTx) return [null, error1];
Expand All @@ -299,11 +299,11 @@ export const simulateCreateBucket = async (
};

export const createBucket = async (
params: CreateBucketApprovalRequest,
params: MsgCreateBucket,
authType: AuthType,
connector: Connector,
): BroadcastResponse => {
const [simulateInfo, error, createBucketTx] = await simulateCreateBucket(params, authType);
const [simulateInfo, error, createBucketTx] = await simulateCreateBucket(params);
if (!simulateInfo) return [null, error];

const payload = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { useSettlementFee } from '@/hooks/useSettlementFee';
import { PaymentAccountSelector } from '@/modules/bucket/components/PaymentAccountSelector';
import { TotalFees } from '@/modules/object/components/TotalFees';
import { BUTTON_GOT_IT, WALLET_CONFIRM } from '@/modules/object/constant';
import { ChainVisibilityEnum } from '@/modules/object/type';
import { PaymentInsufficientBalance } from '@/modules/object/utils';
import { MIN_AMOUNT } from '@/modules/wallet/constants';
import { useAppDispatch, useAppSelector } from '@/store';
Expand All @@ -56,19 +55,20 @@ import {
setSignatureAction,
setupStoreFeeParams,
} from '@/store/slices/global';
import { getSpOffChainData } from '@/store/slices/persist';
import { SpEntity } from '@/store/slices/sp';
import { reportEvent } from '@/utils/gtag';
import { BN } from '@/utils/math';
import { getQuotaNetflowRate } from '@/utils/payment';
import {
CreateBucketApprovalRequest,
MsgCreateBucketTypeUrl,
MsgSetTagTypeUrl,
TxResponse,
VisibilityType,
Long,
} from '@bnb-chain/greenfield-js-sdk';
import { useAsyncEffect, useUnmount } from 'ahooks';
import { SPSelector } from './SPSelector';
import { MsgCreateBucket } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx';

type ValidateNameAndGas = {
isValidating: boolean;
Expand Down Expand Up @@ -231,23 +231,16 @@ export const CreateBucketOperation = memo<CreateBucketOperationProps>(function C
const bucketName = value;
setValidateNameAndGas({ ...validateNameAndGas, isValidating: true });
const sp = selectedSpRef.current;
const { seedString } = await dispatch(getSpOffChainData(loginAccount, sp.operatorAddress));
const createBucketPayload: CreateBucketApprovalRequest = {
bucketName,
const msgCreateBucket: MsgCreateBucket = {
creator: loginAccount,
visibility: ChainVisibilityEnum.VISIBILITY_TYPE_PUBLIC_READ,
chargedReadQuota: String(chargeQuota * G_BYTES),
spInfo: {
primarySpAddress: sp.operatorAddress,
},
bucketName,
visibility: VisibilityType.VISIBILITY_TYPE_PUBLIC_READ,
paymentAddress: loginAccount,
primarySpAddress: sp.operatorAddress,

chargedReadQuota: Long.fromNumber(chargeQuota * G_BYTES),
};
const [simulateInfo, error] = await simulateCreateBucket(createBucketPayload, {
type: 'EDDSA',
domain: window.location.origin,
seed: seedString,
address: loginAccount,
});
const [simulateInfo, error] = await simulateCreateBucket(msgCreateBucket);

if (!simulateInfo) {
if (curNonce !== nonceRef.current) return;
Expand Down Expand Up @@ -315,29 +308,19 @@ export const CreateBucketOperation = memo<CreateBucketOperationProps>(function C
dispatch(
setSignatureAction({ icon: Animates.object, title: 'Creating Bucket', desc: WALLET_CONFIRM }),
);
const { seedString } = await dispatch(
getSpOffChainData(loginAccount, selectedSpRef.current.operatorAddress),
);
const bucketName = data.bucketName;
const selectedPaAddress = selectedPaRef.current.address;
const createBucketPayload: CreateBucketApprovalRequest = {
bucketName,
const msgCreateBucket: MsgCreateBucket = {
creator: loginAccount,
bucketName,
paymentAddress: selectedPaAddress,
visibility: ChainVisibilityEnum.VISIBILITY_TYPE_PUBLIC_READ,
chargedReadQuota: String(chargeQuota * G_BYTES),
spInfo: {
primarySpAddress: selectedSpRef.current.operatorAddress,
},
visibility: VisibilityType.VISIBILITY_TYPE_PUBLIC_READ,
chargedReadQuota: Long.fromNumber(chargeQuota * G_BYTES),
primarySpAddress: selectedSpRef.current.operatorAddress,
};

const txs: TxResponse[] = [];
const [bucketTx, error1] = await getCreateBucketTx(createBucketPayload, {
type: 'EDDSA',
domain: window.location.origin,
seed: seedString,
address: loginAccount,
});
const [bucketTx, error1] = await getCreateBucketTx(msgCreateBucket);
if (!bucketTx) return errorHandler(error1);

txs.push(bucketTx);
Expand All @@ -361,9 +344,9 @@ export const CreateBucketOperation = memo<CreateBucketOperationProps>(function C
if (txRes?.code !== 0) return errorHandler((txRes as any).message || txRes?.rawLog);

await pollingGetBucket({
address: createBucketPayload.creator,
address: msgCreateBucket.creator,
endpoint: globalSP.endpoint,
bucketName: createBucketPayload.bucketName,
bucketName: msgCreateBucket.bucketName,
});

dispatch(setSignatureAction({}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
WALLET_CONFIRM,
} from '@/modules/object/constant';
import { PaymentInsufficientBalance } from '@/modules/object/utils';
import { genCreateObjectTx } from '@/modules/object/utils/genCreateObjectTx';
import { getCreateObjectTx } from '@/modules/object/utils/getCreateObjectTx';
import { useAppDispatch, useAppSelector } from '@/store';
import { AccountInfo, setupAccountRecords } from '@/store/slices/accounts';
import { TBucket } from '@/store/slices/bucket';
Expand All @@ -39,16 +39,18 @@ import {
setupStoreFeeParams,
} from '@/store/slices/global';
import { setObjectEditTagsData, setObjectOperation } from '@/store/slices/object';
import { getSpOffChainData } from '@/store/slices/persist';
import { SpEntity } from '@/store/slices/sp';
import { BN } from '@/utils/math';
import { getStoreNetflowRate } from '@/utils/payment';
import { removeTrailingSlash } from '@/utils/string';
import {
CreateObjectApprovalRequest,
MsgCreateObjectTypeUrl,
MsgSetTagTypeUrl,
TxResponse,
Long,
RedundancyType,
bytesFromBase64,
VisibilityType,
} from '@bnb-chain/greenfield-js-sdk';
import {
Box,
Expand All @@ -68,6 +70,7 @@ import { isEmpty, last, trimEnd } from 'lodash-es';
import { ChangeEvent, memo, useCallback, useEffect, useMemo, useState } from 'react';
import { useAccount } from 'wagmi';
import { TotalFees } from './TotalFees';
import { MsgCreateObject } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx';

interface CreateFolderOperationProps {
selectBucket: TBucket;
Expand Down Expand Up @@ -301,29 +304,27 @@ export const CreateFolderOperation = memo<CreateFolderOperationProps>(function C

const simulateCreateFolderTx = async (
fullFolderName: string,
visibility: any = 'VISIBILITY_TYPE_INHERIT',
visibility: VisibilityType = VisibilityType.VISIBILITY_TYPE_INHERIT,
): Promise<ErrorResponse | [TxResponse, null]> => {
// const fullPath = getPath(folderName, folders);
const file = new File([], fullFolderName, { type: 'text/plain' });
const { seedString } = await dispatch(
getSpOffChainData(loginAccount, primarySp.operatorAddress),
);
const hashResult = await checksumWorkerApi?.generateCheckSumV2(file);
const createObjectPayload: CreateObjectApprovalRequest = {
const expectCheckSums = hashResult?.expectCheckSums || [];
const msgCreateObject: MsgCreateObject = {
creator: loginAccount,
bucketName: currentBucketName,
objectName: fullFolderName,
creator: loginAccount,
visibility,
fileType: file.type,
contentLength: file.size,
expectCheckSums: hashResult?.expectCheckSums || [],
contentType: file.type,
payloadSize: Long.fromInt(file.size),
expectChecksums: expectCheckSums.map((x) => bytesFromBase64(x)),
redundancyType: RedundancyType.REDUNDANCY_EC_TYPE,
};
const [createObjectTx, createError] = await genCreateObjectTx(createObjectPayload, {
type: 'EDDSA',
domain: window.location.origin,
seed: seedString,
address: loginAccount,
}).then(resolve, createTxFault);

const [createObjectTx, createError] = await getCreateObjectTx(msgCreateObject).then(
resolve,
createTxFault,
);

if (!createObjectTx) {
return [null, createError];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,6 @@ export const ObjectList = memo<ObjectListProps>(function ObjectList({ shareMode
address: loginAccount,
};

// const operator = primarySpInfo.operatorAddress;
// const { seedString } = await dispatch(getSpOffChainData(loginAccount, operator));
const [success, opsError] = await downloadObject(params, seedString);
if (opsError) return errorHandler(opsError);
dispatch(setupBucketQuota(currentBucketName));
Expand Down
7 changes: 0 additions & 7 deletions apps/dcellar-web-ui/src/modules/object/type.ts

This file was deleted.

This file was deleted.

Loading
Loading