Skip to content

Commit

Permalink
feat: Verfify params (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 authored Feb 20, 2024
1 parent 927556e commit ec70921
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 116 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-jobs-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Verify and Assert params
38 changes: 19 additions & 19 deletions packages/js-sdk/src/api/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assertAuthType, assertStringRequire } from '@/utils/asserts/params';
import { UInt64Value } from '@bnb-chain/greenfield-cosmos-types/greenfield/common/wrapper';
import {
ActionType,
Expand Down Expand Up @@ -92,8 +93,8 @@ import type {
ReadQuotaRequest,
SpResponse,
} from '../types/sp';
import { isValidAddress, verifyBucketName, verifyUrl } from '../utils/asserts/s3';
import { decodeObjectFromHexString } from '../utils/encoding';
import { isValidAddress, isValidBucketName, isValidUrl } from '../utils/s3';
import { Sp } from './sp';
import { Storage } from './storage';

Expand Down Expand Up @@ -216,15 +217,10 @@ export class Bucket implements IBucket {
} = params;

try {
if (!spInfo.primarySpAddress) {
throw new Error('Primary sp address is missing');
}
if (!isValidBucketName(bucketName)) {
throw new Error('Error bucket name');
}
if (!creator) {
throw new Error('Empty creator address');
}
assertAuthType(authType);
assertStringRequire(spInfo.primarySpAddress, 'Primary sp address is missing');
assertStringRequire(creator, 'Empty creator address');
verifyBucketName(bucketName);

const endpoint = await this.sp.getSPUrlByPrimaryAddr(spInfo.primarySpAddress);

Expand Down Expand Up @@ -288,6 +284,7 @@ export class Bucket implements IBucket {
}

public async createBucket(params: CreateBucketApprovalRequest, authType: AuthType) {
assertAuthType(authType);
const { body } = await this.getCreateBucketApproval(params, authType);

if (!body) {
Expand Down Expand Up @@ -365,7 +362,7 @@ export class Bucket implements IBucket {
if (!isValidAddress(address)) {
throw new Error('Error address');
}
if (!isValidUrl(endpoint)) {
if (!verifyUrl(endpoint)) {
throw new Error('Invalid endpoint');
}
const { url } = getUserBucketMetaInfo(endpoint);
Expand Down Expand Up @@ -417,9 +414,10 @@ export class Bucket implements IBucket {
): Promise<SpResponse<IQuotaProps>> {
try {
const { bucketName, duration = 30000 } = params;
if (!isValidBucketName(bucketName)) {
throw new Error('Error bucket name');
}

verifyBucketName(bucketName);
assertAuthType(authType);

let endpoint = params.endpoint;
if (!endpoint) {
endpoint = await this.sp.getSPUrlByBucket(bucketName);
Expand Down Expand Up @@ -528,6 +526,7 @@ export class Bucket implements IBucket {
}

public async getMigrateBucketApproval(params: MigrateBucketApprovalRequest, authType: AuthType) {
assertAuthType(authType);
const { bucketName, operator, dstPrimarySpId } = params;

try {
Expand Down Expand Up @@ -579,8 +578,9 @@ export class Bucket implements IBucket {
}

public async migrateBucket(params: MigrateBucketApprovalRequest, authType: AuthType) {
const { signedMsg } = await this.getMigrateBucketApproval(params, authType);
assertAuthType(authType);

const { signedMsg } = await this.getMigrateBucketApproval(params, authType);
if (!signedMsg) {
throw new Error('Get migrate bucket approval error');
}
Expand Down Expand Up @@ -621,9 +621,7 @@ export class Bucket implements IBucket {

public async getBucketMeta(params: GetBucketMetaRequest) {
const { bucketName } = params;
if (!isValidBucketName(bucketName)) {
throw new Error('Error bucket name');
}
verifyBucketName(bucketName);

let endpoint = params.endpoint;
if (!endpoint) {
Expand All @@ -649,12 +647,14 @@ export class Bucket implements IBucket {

public async listBucketReadRecords(params: ListBucketReadRecordRequest, authType: AuthType) {
try {
assertAuthType(authType);

const { bucketName } = params;
let endpoint = params.endpoint;
if (!endpoint) {
endpoint = await this.sp.getSPUrlByBucket(bucketName);
}
if (!isValidUrl(endpoint)) {
if (!verifyUrl(endpoint)) {
throw new Error('Invalid endpoint');
}

Expand Down
133 changes: 58 additions & 75 deletions packages/js-sdk/src/api/objects.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,4 @@
import { encodePath, getMsgToSign, getSortQuery, secpSign } from '../clients/spclient/auth';
import { getApprovalMetaInfo } from '../clients/spclient/spApis/approval';
import { getGetObjectMetaInfo } from '../clients/spclient/spApis/getObject';
import {
getObjectMetaInfo,
parseGetObjectMetaResponse,
} from '../clients/spclient/spApis/getObjectMeta';
import {
getListObjectPoliciesMetaInfo,
parseGetListObjectPoliciesResponse,
} from '../clients/spclient/spApis/listObjectPolicies';
import { parseListObjectsByBucketNameResponse } from '../clients/spclient/spApis/listObjectsByBucket';
import {
getListObjectsByIDsMetaInfo,
parseListObjectsByIdsResponse,
} from '../clients/spclient/spApis/listObjectsByIds';
import { parseError } from '../clients/spclient/spApis/parseError';
import { getPutObjectMetaInfo } from '../clients/spclient/spApis/putObject';
import { TxClient } from '../clients/txClient';
import { METHOD_GET, NORMAL_ERROR_CODE } from '../constants/http';
import { MsgCancelCreateObjectSDKTypeEIP712 } from '../messages/greenfield/storage/MsgCancelCreateObject';
import { MsgCreateObjectSDKTypeEIP712 } from '../messages/greenfield/storage/MsgCreateObject';
import { MsgDeleteObjectSDKTypeEIP712 } from '../messages/greenfield/storage/MsgDeleteObject';
import { MsgUpdateObjectInfoSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateObjectInfo';
import { signSignatureByEddsa } from '../offchainauth';
import { GetObjectRequest } from '../types/sp/GetObject';
import { GetObjectMetaRequest, GetObjectMetaResponse } from '../types/sp/GetObjectMeta';
import { ListObjectsByBucketNameResponse } from '../types/sp/ListObjectsByBucketName';
import { PutObjectRequest } from '../types/sp/PutObject';
import { assertAuthType, assertStringRequire } from '@/utils/asserts/params';
import {
ActionType,
Principal,
Expand Down Expand Up @@ -66,7 +38,32 @@ import {
newObjectGRN,
} from '..';
import { RpcQueryClient } from '../clients/queryclient';
import { encodePath, getMsgToSign, getSortQuery, secpSign } from '../clients/spclient/auth';
import { getApprovalMetaInfo } from '../clients/spclient/spApis/approval';
import { getGetObjectMetaInfo } from '../clients/spclient/spApis/getObject';
import {
getObjectMetaInfo,
parseGetObjectMetaResponse,
} from '../clients/spclient/spApis/getObjectMeta';
import {
getListObjectPoliciesMetaInfo,
parseGetListObjectPoliciesResponse,
} from '../clients/spclient/spApis/listObjectPolicies';
import { parseListObjectsByBucketNameResponse } from '../clients/spclient/spApis/listObjectsByBucket';
import {
getListObjectsByIDsMetaInfo,
parseListObjectsByIdsResponse,
} from '../clients/spclient/spApis/listObjectsByIds';
import { parseError } from '../clients/spclient/spApis/parseError';
import { getPutObjectMetaInfo } from '../clients/spclient/spApis/putObject';
import { SpClient } from '../clients/spclient/spClient';
import { TxClient } from '../clients/txClient';
import { METHOD_GET, NORMAL_ERROR_CODE } from '../constants/http';
import { MsgCancelCreateObjectSDKTypeEIP712 } from '../messages/greenfield/storage/MsgCancelCreateObject';
import { MsgCreateObjectSDKTypeEIP712 } from '../messages/greenfield/storage/MsgCreateObject';
import { MsgDeleteObjectSDKTypeEIP712 } from '../messages/greenfield/storage/MsgDeleteObject';
import { MsgUpdateObjectInfoSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateObjectInfo';
import { signSignatureByEddsa } from '../offchainauth';
import {
AuthType,
CreateObjectApprovalRequest,
Expand All @@ -81,12 +78,16 @@ import {
SpResponse,
TxResponse,
} from '../types';
import { GetObjectRequest } from '../types/sp/GetObject';
import { GetObjectMetaRequest, GetObjectMetaResponse } from '../types/sp/GetObjectMeta';
import { ListObjectsByBucketNameResponse } from '../types/sp/ListObjectsByBucketName';
import { PutObjectRequest } from '../types/sp/PutObject';
import {
generateUrlByBucketName,
isValidBucketName,
isValidObjectName,
isValidUrl,
} from '../utils/s3';
verifyBucketName,
verifyObjectName,
verifyUrl,
} from '../utils/asserts/s3';
import { Sp } from './sp';
import { Storage } from './storage';

Expand Down Expand Up @@ -201,15 +202,10 @@ export class Objects implements IObject {
} = params;

try {
if (!isValidBucketName(bucketName)) {
throw new Error('Error bucket name');
}
if (!isValidObjectName(objectName)) {
throw new Error('Error object name');
}
if (!creator) {
throw new Error('empty creator address');
}
assertAuthType(authType);
verifyBucketName(bucketName);
verifyObjectName(objectName);
assertStringRequire(creator, 'empty creator address');

let endpoint = params.endpoint;
if (!endpoint) {
Expand Down Expand Up @@ -288,6 +284,8 @@ export class Objects implements IObject {
}

public async createObject(getApprovalParams: CreateObjectApprovalRequest, authType: AuthType) {
assertAuthType(authType);

const { signedMsg } = await this.getCreateObjectApproval(getApprovalParams, authType);
if (!signedMsg) {
throw new Error('Get create object approval error');
Expand Down Expand Up @@ -316,16 +314,11 @@ export class Objects implements IObject {
params: PutObjectRequest,
authType: AuthType,
): Promise<SpResponse<null>> {
assertAuthType(authType);
const { bucketName, objectName, txnHash, body, duration = 30000 } = params;
if (!isValidBucketName(bucketName)) {
throw new Error('Error bucket name');
}
if (!isValidObjectName(objectName)) {
throw new Error('Error object name');
}
if (!txnHash) {
throw new Error('Transaction hash is empty, please check.');
}
verifyBucketName(bucketName);
verifyObjectName(objectName);
assertStringRequire(txnHash, 'Transaction hash is empty, please check.');

let endpoint = params.endpoint;
if (!endpoint) {
Expand Down Expand Up @@ -415,13 +408,11 @@ export class Objects implements IObject {

public async getObject(params: GetObjectRequest, authType: AuthType) {
try {
assertAuthType(authType);
const { bucketName, objectName, duration = 30000 } = params;
if (!isValidBucketName(bucketName)) {
throw new Error('Error bucket name');
}
if (!isValidObjectName(objectName)) {
throw new Error('Error object name');
}
verifyBucketName(bucketName);
verifyObjectName(objectName);

let endpoint = params.endpoint;
if (!endpoint) {
endpoint = await this.sp.getSPUrlByBucket(bucketName);
Expand Down Expand Up @@ -471,13 +462,10 @@ export class Objects implements IObject {
}

public async getObjectPreviewUrl(params: GetPrivewObject, authType: AuthType) {
assertAuthType(authType);
const { bucketName, objectName, queryMap } = params;
if (!isValidBucketName(bucketName)) {
throw new Error('Error bucket name');
}
if (!isValidObjectName(objectName)) {
throw new Error('Error object name');
}
verifyBucketName(bucketName);
verifyObjectName(objectName);
let endpoint = params.endpoint;
if (!endpoint) {
endpoint = await this.sp.getSPUrlByBucket(bucketName);
Expand Down Expand Up @@ -539,12 +527,10 @@ export class Objects implements IObject {
public async listObjects(configParam: ListObjectsByBucketNameRequest) {
try {
const { bucketName, endpoint, duration = 30000, query = new URLSearchParams() } = configParam;
if (!isValidBucketName(bucketName)) {
throw new Error('Error bucket name');
}
if (!isValidUrl(endpoint)) {
throw new Error('Invalid endpoint');
}

verifyBucketName(bucketName);
verifyUrl(endpoint);

const url = `${generateUrlByBucketName(endpoint, bucketName)}?${query?.toString()}`;
const headers = new Headers();

Expand Down Expand Up @@ -592,6 +578,7 @@ export class Objects implements IObject {
>,
authType: AuthType,
) {
assertAuthType(authType);
if (!getApprovalParams.objectName.endsWith('/')) {
throw new Error(
'failed to create folder. Folder names must end with a forward slash (/) character',
Expand Down Expand Up @@ -691,12 +678,8 @@ export class Objects implements IObject {

public async getObjectMeta(params: GetObjectMetaRequest) {
const { bucketName, objectName, endpoint } = params;
if (!isValidBucketName(bucketName)) {
throw new Error('Error bucket name');
}
if (!isValidObjectName(objectName)) {
throw new Error('Error object name');
}
verifyBucketName(bucketName);
verifyObjectName(objectName);

const { url } = getObjectMetaInfo(endpoint, params);

Expand Down
2 changes: 2 additions & 0 deletions packages/js-sdk/src/api/payment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assertAuthType } from '@/utils/asserts/params';
import {
QueryAutoSettleRecordsRequest,
QueryAutoSettleRecordsResponse,
Expand Down Expand Up @@ -226,6 +227,7 @@ export class Payment implements IPayment {
config?: SpConfig,
) {
try {
assertAuthType(authType);
let endpoint = '';
if (config && config.endpoint) {
endpoint = config.endpoint;
Expand Down
2 changes: 1 addition & 1 deletion packages/js-sdk/src/clients/spclient/spApis/getObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EMPTY_STRING_SHA256, METHOD_GET } from '@/constants';
import { ReqMeta } from '@/types';
import { generateUrlByBucketName } from '@/utils/s3';
import { generateUrlByBucketName } from '@/utils/asserts/s3';
import { encodePath } from '../auth';

// https://docs.bnbchain.org/greenfield-docs/docs/api/storage-provider-rest/get_object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { convertStrToBool, formatObjectInfo } from '@/types/sp/Common';
import { GetObjectMetaRequest, GetObjectMetaResponse } from '@/types/sp/GetObjectMeta';
import { generateUrlByBucketName } from '@/utils/s3';
import { generateUrlByBucketName } from '@/utils/asserts/s3';
import { XMLParser } from 'fast-xml-parser';
import { encodePath, getSortQueryParams } from '../auth';
import type { SPMetaInfo } from './metaInfos';
Expand Down
2 changes: 1 addition & 1 deletion packages/js-sdk/src/clients/spclient/spApis/putObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EMPTY_STRING_SHA256, METHOD_PUT } from '@/constants';
import { ReqMeta } from '@/types';
import { generateUrlByBucketName } from '@/utils/s3';
import { generateUrlByBucketName } from '@/utils/asserts/s3';
import { encodePath } from '../auth';

// https://docs.bnbchain.org/greenfield-docs/docs/api/storage-provider-rest/put_object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EMPTY_STRING_SHA256, METHOD_GET } from '@/constants';
import { ReqMeta, ReadQuotaRequest } from '@/types';
import { ReadQuotaResponse } from '@/types/sp';
import { generateUrlByBucketName } from '@/utils/s3';
import { generateUrlByBucketName } from '@/utils/asserts/s3';
import { XMLParser } from 'fast-xml-parser';
import { getSortQuery } from '../auth';

Expand Down
3 changes: 3 additions & 0 deletions packages/js-sdk/src/clients/txClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { arrayify, hexlify } from '@ethersproject/bytes';
import { signTypedData, SignTypedDataVersion } from '@metamask/eth-sig-util';
import { container, inject, injectable } from 'tsyringe';
import {
assertPrivateKey,
BroadcastOptions,
CustomTx,
ISimulateGasFee,
Expand Down Expand Up @@ -228,6 +229,8 @@ export class TxClient implements ITxClient {
eip712: ReturnType<typeof createEIP712>,
privateKey: SignOptions['privateKey'],
) {
assertPrivateKey(privateKey);

const pubKey = getPubKeyByPriKey(privateKey);
const signature = signTypedData({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
2 changes: 2 additions & 0 deletions packages/js-sdk/src/utils/asserts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './params';
export * from './s3';
Loading

0 comments on commit ec70921

Please sign in to comment.