Skip to content

Commit

Permalink
fix: Backport #be42db3e46d240871350630012f63eb2f3c2cd7e
Browse files Browse the repository at this point in the history
  • Loading branch information
Eengineer1 committed Sep 12, 2023
1 parent 4e73d20 commit 249164d
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 32 deletions.
149 changes: 120 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"cosmjs-types": "^0.5.2",
"did-jwt": "^6.11.6",
"did-resolver": "^4.1.0",
"file-type": "^16.5.4",
"multiformats": "^9.9.0",
"uuid": "^9.0.0"
},
Expand All @@ -77,7 +78,7 @@
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.3",
"uint8arrays": "^4.0.3"
"uint8arrays": "^3.1.1"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down
6 changes: 5 additions & 1 deletion src/modules/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ import {
createPagination,
createProtobufRpcClient
} from "@cosmjs/stargate"
import { toString } from 'uint8arrays';
import FileType from 'file-type/browser';
import { SignInfo } from "@cheqd/ts-proto/cheqd/did/v2/index";
import { assert } from '@cosmjs/utils';
import { PageRequest } from '@cheqd/ts-proto/cosmos/base/query/v1beta1/pagination';
import { CheqdQuerier } from '../querier';
import { isJSON } from '../utils';

export const defaultResourceExtensionKey = 'resource' as const

Expand Down Expand Up @@ -208,7 +211,8 @@ export class ResourceModule extends AbstractCheqdSDKModule {
}

static async readMimeType(content: Uint8Array): Promise<string> {
return 'application/octet-stream'
if (isJSON(toString(content, 'utf-8'))) return 'application/json'
return (await FileType.fromBuffer(content))?.mime ?? 'application/octet-stream'
}

static async generateCreateResourceImageFees(feePayer: string, granter?: string): Promise<DidStdFee> {
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,14 @@ export function createMsgResourcePayloadToSign(payload: Partial<MsgCreateResourc
return MsgCreateResourcePayload.encode(
MsgCreateResourcePayload.fromPartial(payload)
).finish()
}

export function isJSON(input: any): boolean {
if (typeof input !== 'string') return false
try {
JSON.parse(input)
return true
} catch (e) {
return false
}
}
45 changes: 44 additions & 1 deletion tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
TImportableEd25519Key,
createSignInputsFromImportableEd25519Key
createSignInputsFromImportableEd25519Key,
isJSON
} from '../src/utils'
import {
createDidVerificationMethod,
Expand Down Expand Up @@ -74,4 +75,46 @@ describe('createSignInputsFromImportableEd25519Key', () => {

expect(signInput).toEqual({ verificationMethodId: verificationKeys.keyId, privateKeyHex: importableEd25519Key.privateKeyHex })
})

it('should return valid json', async () => {
// define invalid cases
const invalid = [
'invalid',
'{invalid: json}',
'{"invalid": "json"',
'"invalid": "json"}',
'{""}',
0,
1,
true,
null,
undefined,
]

// define valid cases
const valid = [
'{"valid": "json"}',
'{"valid": "json", "with": "multiple", "keys": "and", "values": "of", "different": "types"}',
'{"valid": "json", "with": "multiple", "keys": "and", "values": "of", "different": "types", "and": {"nested": "objects"}}',
'{"valid": "json", "with": "multiple", "keys": "and", "values": "of", "different": "types", "and": {"nested": "objects", "and": {"even": {"more": {"nested": "objects"}}}}}',
'{"": ""}',
'{"boolean": true}',
'{"boolean": false}',
'{"number": 0}',
'{"nullish": null}',
'{"array": []}',
'{"array": [1, 2, 3]}',
'{"array": [1, 2, 3], "with": ["multiple", "arrays"]}',
]

// check invalid cases
invalid.forEach((invalidCase) => {
expect(isJSON(invalidCase)).toBe(false)
})

// check valid cases
valid.forEach((validCase) => {
expect(isJSON(validCase)).toBe(true)
})
})
})

0 comments on commit 249164d

Please sign in to comment.