Skip to content

Commit

Permalink
fix: EncodePath (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 authored Aug 25, 2023
1 parent b2fff6e commit 7824130
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-suits-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

fix: EncodePath
6 changes: 3 additions & 3 deletions packages/chain-sdk/src/api/objectt.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { encodePath } from '@/clients/spclient/auth';
import { getGetObjectMetaInfo } from '@/clients/spclient/spApis/getObject';
import { parseGetObjectMetaResponse } from '@/clients/spclient/spApis/getObjectMeta';
import { parseListObjectsByBucketNameResponse } from '@/clients/spclient/spApis/listObjectsByBucket';
import { getObjectApprovalMetaInfo } from '@/clients/spclient/spApis/objectApproval';
import { parseError } from '@/clients/spclient/spApis/parseError';
import { getPutObjectMetaInfo } from '@/clients/spclient/spApis/putObject';
import { METHOD_GET, METHOD_PUT, NORMAL_ERROR_CODE } from '@/constants/http';
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';
Expand Down Expand Up @@ -60,7 +61,6 @@ import {
TxResponse,
} from '../types';
import {
encodeObjectName,
generateUrlByBucketName,
isValidBucketName,
isValidObjectName,
Expand Down Expand Up @@ -613,7 +613,7 @@ export class Objectt implements IObject {
}

const query = 'object-meta';
const path = encodeObjectName(objectName);
const path = encodePath(objectName);
const url = `${generateUrlByBucketName(endpoint, bucketName)}/${path}?${query}`;
const result = await this.spClient.callApi(url, {
method: METHOD_GET,
Expand Down
41 changes: 41 additions & 0 deletions packages/chain-sdk/src/clients/spclient/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,44 @@ export const getMsgToSign = (unsignedBytes: Uint8Array): Uint8Array => {
const res = keccak256(unsignedBytes);
return res;
};

export const encodePath = (pathName: string) => {
const reservedNames = /^[a-zA-Z0-9-_.~/]+$/;
if (reservedNames.test(pathName)) {
return pathName;
}

let encodedPathName = '';
for (let i = 0; i < pathName.length; i++) {
const s = pathName[i];

// soft characters
if (('A' <= s && s <= 'Z') || ('a' <= s && s <= 'z') || ('0' <= s && s <= '9')) {
encodedPathName += s;
continue;
}

switch (s) {
// special characters are allowed
case '-':
case '_':
case '.':
case '~':
case '/':
encodedPathName += s;
continue;

// others characters need to be encoded
default:
const length = encodeURIComponent(s).length;
if (length < 0) {
// if encodeURIComponent cannot convert return the same string as is
return pathName;
}

const hexStr = s.charCodeAt(0).toString(16);
encodedPathName += '%' + hexStr.toUpperCase();
}
}
return encodedPathName;
};
4 changes: 3 additions & 1 deletion packages/chain-sdk/src/clients/spclient/spApis/putObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EMPTY_STRING_SHA256, METHOD_PUT } from '@/constants';
import { ReqMeta } from '@/types';
import { generateUrlByBucketName } from '@/utils/s3';
import { encodePath } from '../auth';

// https://docs.bnbchain.org/greenfield-docs/docs/api/storgae-provider-rest/put_object
export const getPutObjectMetaInfo = async (
Expand All @@ -14,7 +15,8 @@ export const getPutObjectMetaInfo = async (
},
) => {
const { bucketName, objectName, txnHash, contentType, body } = params;
const path = `/${objectName}`;
const path = `/${encodePath(objectName)}`;

const query = '';
const url = `${generateUrlByBucketName(endpoint, bucketName)}${path}`;

Expand Down
5 changes: 0 additions & 5 deletions packages/chain-sdk/src/utils/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,11 @@ const generateUrlByBucketName = (endpoint = '', bucketName: string) => {
return endpoint.replace(`${protocol}//`, `${protocol}//${bucketName}.`);
};

const encodeObjectName = (obj: string) => {
return obj.split('/').map(encodeURIComponent).join('/');
};

export {
isValidBucketName,
isValidObjectName,
isValidAddress,
trimString,
isValidUrl,
generateUrlByBucketName,
encodeObjectName,
};

0 comments on commit 7824130

Please sign in to comment.