1 line
63 KiB
Plaintext
1 line
63 KiB
Plaintext
|
{"version":3,"file":"index.mjs","sources":["../src/crypto.ts","../src/httpClient.ts","../src/wssocket.ts","../src/wsClient.ts"],"sourcesContent":["// export function aesDecrypt(data: base64) {\n// const dataAscii = cryptico.b64to256(data)\n// const encryptedBlocks = cryptico.string2bytes(dataAscii)\n// const exkey = global.aesKey.slice(0)\n// aes.ExpandKey(exkey)\n// aes.Decrypt(encryptedBlocks, exkey)\n// return cryptico.bytes2string(encryptedBlocks)\n// }\n\nimport type { AESKey } from '@daotl/cryptico'\nimport { aes, cryptico, RSAKey } from '@daotl/cryptico'\n\nexport function aesEncrypt(data: string, aesKey: AESKey): string {\n const exkey = aesKey.slice(0)\n aes.ExpandKey(exkey)\n let blocks = cryptico.string2bytes(data)\n blocks = cryptico.pad16(blocks)\n aes.Encrypt(blocks, exkey)\n let ciphertext = cryptico.bytes2string(blocks)\n ciphertext = cryptico.b256to64(ciphertext)\n return ciphertext\n}\n\nexport function rsaEncrypt(\n data: string,\n rsaKey: { n: string; e1: string },\n): string {\n const rsa = new RSAKey()\n rsa.setPublic(rsaKey.n, rsaKey.e1)\n return rsa.encrypt(data)\n}\n\nexport function loadRSAKey(rsaKey: string): RSAKey {\n const str = cryptico.b64to256(rsaKey)\n const strs = str.split(',')\n const key = new RSAKey()\n key.setPrivate(strs[0], strs[0], strs[0])\n return key\n}\n\n// function testRSA() {\n// pubKey = loadRSAKey(global.privKey)\n// reqContent = {}\n// reqContent.action = 'main'\n// reqContent.arg = '[{\"score\":20},{\"score\":20}]'\n// reqContent.contractID = 'abc'\n// eReq = encryptReq(reqContent, pubKey)\n// url =\n// 'http://localhost:8080/SCIDE/SCManager?action=executeContractEncrypted&contractRequest=' +\n// encodeURIComponent(JSON.stringify(eReq))\n// }\n\nexport function encryptReq(\n reqContent: { contractID: string },\n pubKey: RSAKey,\n): {\n action: string | null\n contractID: string\n arg: string\n requester: string\n} {\n const aes = {\n key: cryptico.generateAESKey(),\n }\n const aesObj = JSON.stringify(aes)\n const { contractID } = reqContent\n const reqContentNoId = { ...reqContent, contractID: undefined }\n const encrypedReq = {\n action: pubKey.decrypt(aesObj),\n contractID,\n arg: aesEncrypt(JSON.stringify(reqContentNoId), aes.key),\n requester: cryptico.b256to64(\n `${pubKey.n.toString(16)},${pubKey.e.toString(16)},0`,\n ),\n }\n return encrypedReq\n}\n","import { buildAxiosFetch } from '@lifeomic/axios-fetch'\nimport axios, { type AxiosRequestConfig } from 'axios'\nimport type { KeyPairHex } from 'sm-crypto'\nimport { sm2 } from 'sm-crypto'\n\nimport type {\n ClientResponse,\n ConfigNodeArgs,\n ExecuteContractArgs,\n ListAllUsersResponseData,\n ListNodesResponse,\n ListProjectPermissionRequest,\n ListProjectPermissionResponseData,\n LoadNodeConfigResponseData,\n ExecuteContractResponse,\n PingResponse,\n SaveFileRequest,\n StartContractByYpkRequest,\n} from './types'\n\nconst genUrlParamsFromObject = (obj: Record<string, unknown>): string => {\n return Object.entries(obj)\n .map(([key, value]) => `${key}=${String(value)}`)\n .join('&')\n}\n\nexport * from './types'\n\nexport class HttpClient {\n private fetch: (input: RequestInfo, init?: RequestInit) => Promise<Response>\n\n constructor(\n private baseUrl: string,\n private sm2Key: KeyPairHex,\n config = {} as AxiosRequestConfig,\n ) {\n const axiosInstance = axios.create({\n baseURL: 'https://some-domain.com/api/',\n timeout: 10000,\n // headers: {'X-Custom-Header': 'foobar'}\n ...config,\n })\n\n this.fetch = buildAxiosFetch(\n axiosInstance /* , function (config) {\n config.timeout = 1000;\n return config;\n } */,\n )\n }\n\n async requestWithSignature<Data>(\n path: string,\n init?: Partial<RequestInit>,\n sm2Key?: KeyPairHex,\n ): Promise<ClientResponse<Data>> {\n return new Promise<ClientResponse<Data>>((resolve, reject) => {\n const rawUrl = this.baseUrl + path\n const url = `${rawUrl}${path.includes('?') ? '&' : '
|