diff --git a/package-lock.json b/package-lock.json index d352919..ddf1345 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,19 @@ { "name": "ontology-ts-sdk", - "version": "2.0.0-beta.0", + "version": "2.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "ontology-ts-sdk", - "version": "2.0.0-beta.0", + "version": "2.0.1", "license": "LGPL-3.0", "dependencies": { "@ont-community/html5-websocket": "^2.0.2", "@ont-dev/hdkey-secp256r1": "^1.1.2", "@ont-dev/sm.js": "^0.1.7", - "axios": "^0.19.0", + "@vespaiach/axios-fetch-adapter": "^0.3.1", + "axios": "^0.19.2", "babel-polyfill": "^6.26.0", "base-58": "^0.0.1", "base64-url": "^2.2.0", @@ -1704,6 +1705,14 @@ "integrity": "sha512-FA/BWv8t8ZWJ+gEOnLLd8ygxH/2UFbAvgEonyfN6yWGLKc7zVjbpl2Y4CTjid9h2RfgPP6SEt6uHwEOply00yw==", "dev": true }, + "node_modules/@vespaiach/axios-fetch-adapter": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/@vespaiach/axios-fetch-adapter/-/axios-fetch-adapter-0.3.1.tgz", + "integrity": "sha512-+1F52VWXmQHSRFSv4/H0wtnxfvjRMPK5531e880MIjypPdUSX6QZuoDgEVeCE1vjhzDdxCVX7rOqkub7StEUwQ==", + "peerDependencies": { + "axios": ">=0.26.0" + } + }, "node_modules/@webassemblyjs/ast": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.9.0.tgz", @@ -16689,6 +16698,12 @@ "integrity": "sha512-FA/BWv8t8ZWJ+gEOnLLd8ygxH/2UFbAvgEonyfN6yWGLKc7zVjbpl2Y4CTjid9h2RfgPP6SEt6uHwEOply00yw==", "dev": true }, + "@vespaiach/axios-fetch-adapter": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/@vespaiach/axios-fetch-adapter/-/axios-fetch-adapter-0.3.1.tgz", + "integrity": "sha512-+1F52VWXmQHSRFSv4/H0wtnxfvjRMPK5531e880MIjypPdUSX6QZuoDgEVeCE1vjhzDdxCVX7rOqkub7StEUwQ==", + "requires": {} + }, "@webassemblyjs/ast": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.9.0.tgz", diff --git a/package.json b/package.json index 7345922..49365f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ontology-ts-sdk", - "version": "2.0.1", + "version": "2.0.2", "description": "Comprehensive TypeScript library for the Ontology blockchain.", "main": "./lib/index.js", "types": "./lib/types/index.d.ts", @@ -76,7 +76,8 @@ "@ont-community/html5-websocket": "^2.0.2", "@ont-dev/hdkey-secp256r1": "^1.1.2", "@ont-dev/sm.js": "^0.1.7", - "axios": "^0.19.0", + "@vespaiach/axios-fetch-adapter": "^0.3.1", + "axios": "^0.19.2", "babel-polyfill": "^6.26.0", "base-58": "^0.0.1", "base64-url": "^2.2.0", diff --git a/src/network/rest/restClient.ts b/src/network/rest/restClient.ts index d183ab1..befc143 100644 --- a/src/network/rest/restClient.ts +++ b/src/network/rest/restClient.ts @@ -17,7 +17,8 @@ import { ERROR_CODE } from './../../error'; * along with The ontology. If not, see . */ -import axios from 'axios'; +import axios, { AxiosRequestConfig } from 'axios'; +import fetchAdapter from "@vespaiach/axios-fetch-adapter"; import { TEST_ONT_URL } from '../../consts'; import { Address } from '../../crypto/address'; import UrlConsts from './urlConsts'; @@ -31,6 +32,8 @@ export default class RestClient { */ url: string; + config: undefined | AxiosRequestConfig; + /** * Version of restful api */ @@ -41,8 +44,9 @@ export default class RestClient { */ action: string = 'sendrawtransaction'; - constructor(url ?: string) { + constructor(url ?: string, useFetch?: boolean) { this.url = url || TEST_ONT_URL.REST_URL; + this.config = useFetch ? { adapter: fetchAdapter } : undefined; if (this.url[this.url.length - 1] === '/') { this.url = this.url.substring(0, this.url.length - 1); } @@ -102,7 +106,7 @@ export default class RestClient { Data : hexData }; - return axios.post(url, body).then((res) => { + return axios.post(url, body, this.config).then((res) => { return res.data; }); } @@ -129,7 +133,8 @@ export default class RestClient { param.set('raw', '1'); let url = this.url + UrlConsts.Url_get_transaction + txHash; url += this.concatParams(param); - return axios.get(url).then((res) => { + + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -144,7 +149,8 @@ export default class RestClient { param.set('raw', '0'); let url = this.url + UrlConsts.Url_get_transaction + txHash; url += this.concatParams(param); - return axios.get(url).then((res) => { + + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -165,7 +171,8 @@ export default class RestClient { */ getNodeCount(): Promise { const url = this.url + UrlConsts.Url_get_node_count; - return axios.get(url).then((res) => { + + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -175,7 +182,8 @@ export default class RestClient { */ getBlockHeight(): Promise { const url = this.url + UrlConsts.Url_get_block_height; - return axios.get(url).then((res) => { + + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -196,7 +204,7 @@ export default class RestClient { } url += this.concatParams(params); - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -213,7 +221,7 @@ export default class RestClient { url += this.concatParams(params); // console.log('url: '+url); - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -227,7 +235,8 @@ export default class RestClient { params.set('raw', '0'); let url = this.url + UrlConsts.Url_get_contract_state + codeHash; url += this.concatParams(params); - return axios.get(url).then((res) => { + + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -245,7 +254,8 @@ export default class RestClient { } else if (typeof value === 'number') { url = this.url + UrlConsts.Url_get_smartcodeevent_txs_by_height + value; } - return axios.get(url).then((res) => { + + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -256,7 +266,8 @@ export default class RestClient { */ getBlockHeightByTxHash(hash: string): Promise { const url = this.url + UrlConsts.Url_get_block_height_by_txhash + hash; - return axios.get(url).then((res) => { + + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -268,7 +279,8 @@ export default class RestClient { */ getStorage(codeHash: string, key: string): Promise { const url = this.url + UrlConsts.Url_get_storage + codeHash + '/' + key; - return axios.get(url).then((res) => { + + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -283,7 +295,7 @@ export default class RestClient { // tslint:disable-next-line:no-console // console.log('url: ' + url); - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -295,7 +307,8 @@ export default class RestClient { */ getBalance(address: Address): Promise { const url = this.url + UrlConsts.Url_get_account_balance + address.toBase58(); - return axios.get(url).then((res) => { + + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -307,7 +320,7 @@ export default class RestClient { */ getBalanceV2(address: Address): Promise { const url = this.url + UrlConsts.Url_get_account_balance_v2 + address.toBase58(); - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -324,7 +337,7 @@ export default class RestClient { url = this.url + UrlConsts.Url_get_block_by_hash + value; } - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -342,7 +355,7 @@ export default class RestClient { } const url = this.url + UrlConsts.Url_get_allowance + asset.toLowerCase() + '/' + from.toBase58() + '/' + to.toBase58(); - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } @@ -360,56 +373,56 @@ export default class RestClient { } const url = this.url + UrlConsts.Url_get_allowance_v2 + asset.toLowerCase() + '/' + from.toBase58() + '/' + to.toBase58(); - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } getUnboundOng(address: Address): Promise { const url = this.url + UrlConsts.Url_get_unbound_ong + address.toBase58(); - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } getBlockTxsByHeight(height: number): Promise { const url = this.url + UrlConsts.Url_get_block_txs_by_height + height; - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } getGasPrice(): Promise { const url = this.url + UrlConsts.Url_get_gasprice ; - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } getGrantOng(address: Address): Promise { const url = this.url + UrlConsts.Url_get_grant_ong + address.toBase58(); - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } getMempoolTxCount(): Promise { const url = this.url + UrlConsts.Url_get_mempool_txcount; - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } getMempoolTxState(hash: string): Promise { const url = this.url + UrlConsts.Url_get_mempool_txstate + hash; - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } getVersion(): Promise { const url = this.url + UrlConsts.Url_get_version; - return axios.get(url).then((res) => { + return axios.get(url, this.config).then((res) => { return res.data; }); } diff --git a/src/network/rpc/rpcClient.ts b/src/network/rpc/rpcClient.ts index f1304ae..97cf3a0 100644 --- a/src/network/rpc/rpcClient.ts +++ b/src/network/rpc/rpcClient.ts @@ -16,7 +16,8 @@ * along with The ontology. If not, see . */ -import axios from 'axios'; +import axios, { AxiosRequestConfig } from 'axios'; +import fetchAdapter from "@vespaiach/axios-fetch-adapter"; import { TEST_ONT_URL } from '../../consts'; import { Address } from '../../crypto/address'; import { ERROR_CODE } from '../../error'; @@ -30,8 +31,12 @@ export default class RpcClient { */ url: string; - constructor( url ?: string ) { + config: undefined | AxiosRequestConfig; + + constructor( url ?: string, useFetch?: boolean ) { this.url = url || TEST_ONT_URL.RPC_URL; + + this.config = useFetch ? { adapter: fetchAdapter } : undefined; } /** @@ -65,7 +70,7 @@ export default class RpcClient { getBalance(address: Address): Promise { const req = this.makeRequest('getbalance', address.toBase58()); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -78,7 +83,7 @@ export default class RpcClient { getBalanceV2(address: Address): Promise { const req = this.makeRequest('getbalancev2', address.toBase58()); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -97,7 +102,7 @@ export default class RpcClient { req = this.makeRequest('sendrawtransaction', data); } - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -110,7 +115,7 @@ export default class RpcClient { getRawTransaction(txHash: string): Promise { const req = this.makeRequest('getrawtransaction', txHash); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -123,7 +128,7 @@ export default class RpcClient { getRawTransactionJson(txHash: string): Promise { const req = this.makeRequest('getrawtransaction', txHash, 1); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -146,7 +151,7 @@ export default class RpcClient { getNodeCount(): Promise { const req = this.makeRequest('getconnectioncount'); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -157,7 +162,7 @@ export default class RpcClient { getBlockHeight(): Promise { const req = this.makeRequest('getblockcount'); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { if (res.data && res.data.result) { return res.data.result - 1; } else { @@ -172,7 +177,7 @@ export default class RpcClient { getBlockCount(): Promise { const req = this.makeRequest('getblockcount'); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -185,7 +190,7 @@ export default class RpcClient { getBlockJson(value: string | number): Promise { const req = this.makeRequest('getblock', value, 1); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -198,7 +203,7 @@ export default class RpcClient { getContract(hash: string): Promise { const req = this.makeRequest('getcontractstate', hash); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -211,7 +216,7 @@ export default class RpcClient { getContractJson(codeHash: string): Promise { const req = this.makeRequest('getcontractstate', codeHash, 1); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -225,7 +230,7 @@ export default class RpcClient { getBlock(value: string | number): Promise { const req = this.makeRequest('getblock', value); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -240,7 +245,7 @@ export default class RpcClient { getSmartCodeEvent(value: string | number): Promise { const req = this.makeRequest('getsmartcodeevent', value); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -252,7 +257,7 @@ export default class RpcClient { getBlockHeightByTxHash(txHash: string): Promise { const req = this.makeRequest('getblockheightbytxhash', txHash); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -268,7 +273,7 @@ export default class RpcClient { // tslint:disable-next-line:no-console console.log(req); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -285,7 +290,7 @@ export default class RpcClient { // tslint:disable-next-line:no-console console.log(req); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -301,7 +306,7 @@ export default class RpcClient { throw ERROR_CODE.INVALID_PARAMS; } const req = this.makeRequest('getallowance', asset, from.toBase58(), to.toBase58()); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } @@ -317,56 +322,56 @@ export default class RpcClient { throw ERROR_CODE.INVALID_PARAMS; } const req = this.makeRequest('getallowancev2', asset, from.toBase58(), to.toBase58()); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } getUnboundOng(address: Address): Promise { const req = this.makeRequest('getunboundong', 'ong', address.toBase58(), address.toBase58()); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } getBlockTxsByHeight(height: number): Promise { const req = this.makeRequest('getblocktxsbyheight', height); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } getGasPrice(): Promise { const req = this.makeRequest('getgasprice'); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } getGrantOng(address: Address): Promise { const req = this.makeRequest('getgrantong', address.toBase58()); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } getMempoolTxCount(): Promise { const req = this.makeRequest('getmempooltxcount'); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } getMempoolTxState(txHash: string): Promise { const req = this.makeRequest('getmempooltxstate', txHash); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } getVersion(): Promise { const req = this.makeRequest('getversion'); - return axios.post(this.url, req).then((res) => { + return axios.post(this.url, req, this.config).then((res) => { return res.data; }); } diff --git a/src/smartcontract/nativevm/ontAssetTxBuilder.ts b/src/smartcontract/nativevm/ontAssetTxBuilder.ts index c8ad84c..b67f283 100644 --- a/src/smartcontract/nativevm/ontAssetTxBuilder.ts +++ b/src/smartcontract/nativevm/ontAssetTxBuilder.ts @@ -415,6 +415,7 @@ export function makeApproveTx( ): Transaction { asset = asset.toLowerCase(); if (asset !== "ont" && asset !== "ong") { + /* tslint:disable */ throw "ERROR_CODE.INVALID_PARAMS"; } diff --git a/src/utils.ts b/src/utils.ts index 81e91c5..029b4d3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -545,7 +545,16 @@ export function hash160(SignatureScript: string): string { * @param len Length of the array to generate */ export function generateRandomArray(len: number): ArrayBuffer { - return secureRandom(len); + try { + return secureRandom(len); + } catch (err) { + // 兼容在Chrome插件V3环境下使用的情况 + // 在V3下不能使用window变量,可直接访问crypto变量 + const nativeArr = new Uint8Array(len); + crypto.getRandomValues(nativeArr); + const res = [].slice.call(nativeArr) as any as ArrayBuffer; + return res; + } } /** diff --git a/tslint.json b/tslint.json index 34c4a88..194214b 100644 --- a/tslint.json +++ b/tslint.json @@ -5,8 +5,8 @@ ], "jsRules": {}, "rules": { - "quotemark": [true, "single"], - "ter-indent": [true, 4], + "quotemark": [false, "single"], + "ter-indent": [false, 4], "trailing-comma": [true, {"multiline": "never", "singleline": "never"}], "interface-name" : [true, "never-prefix"], "member-access": false, @@ -25,6 +25,7 @@ "check-type-operator", "check-preblock" ], + "ordered-imports": false, "no-bitwise": false }, "rulesDirectory": []