@zerochain/0chain
Advanced tools
Comparing version 1.3.72 to 1.3.73
222
index.js
@@ -27,2 +27,3 @@ /* | ||
const models = require('./models'); | ||
const wasm = require('./wasm'); | ||
"use strict"; | ||
@@ -36,3 +37,3 @@ | ||
var tokenLock; | ||
let bls; | ||
let bls, goWasm; | ||
@@ -81,3 +82,3 @@ // const StorageSmartContractAddress = "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7"; | ||
GET_MINERSC_POOL_STATS: "v1/screst/" + MinerSmartContractAddress + "/getUserPools", | ||
GET_STAKE_POOL_STAT : "v1/screst/" + StorageSmartContractAddress+"/getStakePoolStat", | ||
GET_STAKE_POOL_STAT: "v1/screst/" + StorageSmartContractAddress + "/getStakePoolStat", | ||
@@ -125,2 +126,32 @@ //BLOBBER | ||
const configJson = { | ||
"miners": [ | ||
"http://localhost:7071/", | ||
"http://localhost:7072/", | ||
"http://localhost:7073/" | ||
], | ||
"sharders": [ | ||
"http://localhost:7171/" | ||
], | ||
"preferredBlobbers": [ | ||
"http://localhost:7051/", | ||
"http://localhost:7052/", | ||
"http://localhost:7053/", | ||
"http://localhost:7054/" | ||
], | ||
"readPrice": { | ||
"min": 0, | ||
"max": 0 | ||
}, | ||
"writePrice": { | ||
"min": 0, | ||
"max": 0 | ||
}, | ||
"tokenLock": 0, | ||
"proxyServerUrl": "http://localhost:9082", | ||
"zeroBoxUrl": "http://one.0box.io:9081", | ||
"transaction_timeout": 15, | ||
"clusterName": "local" | ||
}; | ||
module.exports = { | ||
@@ -139,43 +170,17 @@ | ||
/////////////SDK Stuff below ////////////// | ||
///////////// SDK Initialization ////////////// | ||
init: async function init(configObject, bls_wasm) { | ||
var config; | ||
if (typeof configObject != "undefined" && configObject.hasOwnProperty('miners') && | ||
configObject.hasOwnProperty('sharders') | ||
&& configObject.hasOwnProperty('clusterName') | ||
&& configObject.hasOwnProperty('proxyServerUrl')) { | ||
config = configObject; | ||
const hasConfig = typeof configObject != "undefined" | ||
const hasMiners = configObject.hasOwnProperty('miners') | ||
const hasSharders = configObject.hasOwnProperty('sharders') | ||
const hasCluster = configObject.hasOwnProperty('clusterName') | ||
const hasProxyUrl = configObject.hasOwnProperty('proxyServerUrl') | ||
let config; | ||
if (hasConfig && hasMiners && hasSharders && hasCluster && hasProxyUrl) { | ||
config = configObject; | ||
} else { | ||
config = configJson; | ||
} | ||
else { | ||
const jsonContent = { | ||
"miners": [ | ||
"http://localhost:7071/", | ||
"http://localhost:7072/", | ||
"http://localhost:7073/" | ||
], | ||
"sharders": [ | ||
"http://localhost:7171/" | ||
], | ||
"preferredBlobbers": [ | ||
"http://localhost:7051/", | ||
"http://localhost:7052/", | ||
"http://localhost:7053/", | ||
"http://localhost:7054/" | ||
], | ||
"readPrice": { | ||
"min": 0, | ||
"max": 0 | ||
}, | ||
"writePrice": { | ||
"min": 0, | ||
"max": 0 | ||
}, | ||
"tokenLock": 0, | ||
"proxyServerUrl": "http://localhost:9082", | ||
"zeroBoxUrl": "http://one.0box.io:9081", | ||
"transaction_timeout": 15, | ||
"clusterName": "local" | ||
}; | ||
config = jsonContent; | ||
} | ||
@@ -185,2 +190,13 @@ bls = bls_wasm; | ||
goWasm = await wasm.create(); | ||
await goWasm.sdk.init( | ||
config.chain_id, | ||
config.block_worker, | ||
config.signature_scheme, | ||
config.min_confirmation, | ||
config.min_submit, | ||
config.confirmation_chain_length, | ||
); | ||
miners = config.miners; | ||
@@ -203,2 +219,5 @@ sharders = config.sharders; | ||
setWallet: async function (clientID, sk, pk) { | ||
await goWasm.setWallet(bls, clientID, sk, pk) | ||
}, | ||
@@ -354,3 +373,3 @@ getSdkMetadata: () => { | ||
await this.createReadPool(wallet) | ||
// console.log(wallet,"wallet") | ||
return wallet; | ||
@@ -502,3 +521,3 @@ }, | ||
readPoolInfo: function readPoolInfo(id) { | ||
const readPoolInfoRes =utils.getConsensusedInformationFromSharders( | ||
const readPoolInfoRes = utils.getConsensusedInformationFromSharders( | ||
sharders, | ||
@@ -574,10 +593,10 @@ Endpoints.SC_REST_READPOOL_STATS, | ||
const url = value.simple_miner.host + ":" + value.simple_miner.port; | ||
let check=false; | ||
let check = false; | ||
for (let val of active) { | ||
if (val.indexOf(url.slice(0, -6)) !== -1){ | ||
check=true; | ||
if (val.indexOf(url.slice(0, -6)) !== -1) { | ||
check = true; | ||
return true | ||
} | ||
} | ||
if(check) return true; | ||
if (check) return true; | ||
return false; | ||
@@ -635,11 +654,11 @@ }) | ||
getStakePoolStat:(blobber_id)=>{ | ||
getStakePoolStat: (blobber_id) => { | ||
return new Promise(async function (resolve) { // eslint-disable-line | ||
let stakePoolStat = await utils.getConsensusedInformationFromSharders( | ||
sharders,Endpoints.GET_STAKE_POOL_STAT,{blobber_id} | ||
sharders, Endpoints.GET_STAKE_POOL_STAT, { blobber_id } | ||
) | ||
.then(res => { | ||
return res | ||
}) | ||
.catch(() => null) | ||
.then(res => { | ||
return res | ||
}) | ||
.catch(() => null) | ||
@@ -675,5 +694,6 @@ if (stakePoolStat === null) { | ||
} | ||
const readPoolLockRes =await this.executeSmartContract(ae, undefined, JSON.stringify(payload), tokens) | ||
const readPoolLockRes = await this.executeSmartContract(ae, undefined, JSON.stringify(payload), tokens) | ||
if(!readPoolLockRes.transaction_output){ | ||
if (!readPoolLockRes.transaction_output) { | ||
this.createReadPool(JSON.parse(localStorage.getItem("wallet_info"))) | ||
@@ -728,9 +748,9 @@ } | ||
.then((res) => { | ||
if (res.Nodes.length > 0) { | ||
return res.Nodes.filter((value) => | ||
new Date().getTime() - new Date(value.last_health_check * 1000).getTime() < 3600000 | ||
); | ||
} else { | ||
throw "There are no nodes in blobbers array!" | ||
} | ||
if (!res.Nodes) | ||
throw "Array of Nodes (blobbers) array is NULL" | ||
if (!res.Nodes.length) | ||
throw "There are no blobbers in Nodes array" | ||
return res.Nodes.filter((value) => | ||
new Date().getTime() - new Date(value.last_health_check * 1000).getTime() < 3600000 | ||
) | ||
}); | ||
@@ -741,6 +761,6 @@ }, | ||
const currentBlobbers = await this.getAllBlobbers(); | ||
const detailedBlobbers = currentBlobbers.map((blobber)=>{ | ||
const detailedBlobbers = currentBlobbers.map((blobber) => { | ||
const blobberUrl = new URL(blobber.url) | ||
blobber.convertedUrl = 'https://'+blobberUrl.hostname +'/blobber'+ blobberUrl.port.slice(-2)+'/_statsJSON' | ||
blobber.convertedURL = 'https://'+blobberUrl.hostname +'/blobber'+ blobberUrl.port.slice(-2)+'/_stats' | ||
blobber.convertedUrl = 'https://' + blobberUrl.hostname + '/blobber' + blobberUrl.port.slice(-2) + '/_statsJSON' | ||
blobber.convertedURL = 'https://' + blobberUrl.hostname + '/blobber' + blobberUrl.port.slice(-2) + '/_stats' | ||
@@ -752,17 +772,17 @@ return blobber; | ||
return await fetch(dBl.convertedUrl) | ||
.then(data => data.json()) | ||
.then(async blobJson => { | ||
const blobStakeStats = await this.getStakePoolStat(dBl.id) | ||
blobJson.free_from_blobber_stake_stats = await blobStakeStats.free | ||
return { ...blobJson, ...dBl }; | ||
}) | ||
.catch(error => { | ||
throw { | ||
url: dBl.url, | ||
convertedUrl: dBl.convertedUrl, | ||
...error | ||
} | ||
}) | ||
})) ; | ||
}, | ||
.then(data => data.json()) | ||
.then(async blobJson => { | ||
const blobStakeStats = await this.getStakePoolStat(dBl.id) | ||
blobJson.free_from_blobber_stake_stats = await blobStakeStats.free | ||
return { ...blobJson, ...dBl }; | ||
}) | ||
.catch(error => { | ||
throw { | ||
url: dBl.url, | ||
convertedUrl: dBl.convertedUrl, | ||
...error | ||
} | ||
}) | ||
})); | ||
}, | ||
@@ -803,3 +823,3 @@ getAllocationSharedFilesFromPath: async function (allocation_id, lookup_hash, client_id, auth_token = "") { | ||
getFileMetaDataFromPath: async function (allocation_id, path, client_id, private_key, public_key) { | ||
const completeAllocationInfo = await this.allocationInfo(allocation_id); | ||
const completeAllocationInfo = await this.allocationInfo(allocation_id); | ||
const allocIdHash = sha3.sha3_256(allocation_id) | ||
@@ -823,3 +843,3 @@ const signature = this.getSign(allocIdHash, private_key) | ||
getFileStatsFromPath: async function (allocation_id, filePath, client_id, private_key, public_key) { | ||
getFileStatsFromPath: async function (allocation_id, filePath, client_id, private_key, public_key) { | ||
const completeAllocationInfo = await this.allocationInfo(allocation_id); | ||
@@ -841,5 +861,5 @@ const allocIdHash = sha3.sha3_256(allocation_id) | ||
if (!response.data) { // TODO: properly handle errors on POST reqs | ||
console.error(response) | ||
console.error(response) | ||
} else { | ||
allBlobbersResponse.push({ ...response.data, url: blobber.url }) | ||
allBlobbersResponse.push({ ...response.data, url: blobber.url }) | ||
} | ||
@@ -864,3 +884,3 @@ }) | ||
const blobber_url = blobber + Endpoints.FILE_META_ENDPOINT + allocation_id; | ||
const response = await utils.postReqToBlobber(blobber_url, {}, { path_hash: path_hash, auth_token: atob(auth_ticket) }, client_id,public_key, signature); | ||
const response = await utils.postReqToBlobber(blobber_url, {}, { path_hash: path_hash, auth_token: atob(auth_ticket) }, client_id, public_key, signature); | ||
if (response.status === 200) { | ||
@@ -907,3 +927,3 @@ const res = { | ||
updateMetaCommitToBlobbers: async function (transaction_hash, allocation, lookup_hash, client_id, auth_ticket = "", public_key="") { | ||
updateMetaCommitToBlobbers: async function (transaction_hash, allocation, lookup_hash, client_id, auth_ticket = "", public_key = "") { | ||
const completeAllocationInfo = await this.allocationInfo(allocation); | ||
@@ -982,10 +1002,5 @@ const blobber_list = completeAllocationInfo.blobbers.map(blobber => { | ||
deleteObject: async function (allocation_id, path, client_json) { | ||
const url = proxyServerUrl + Endpoints.PROXY_SERVER_DELETE_ENDPOINT | ||
const formData = new FormData(); | ||
formData.append('allocation', allocation_id); | ||
formData.append('remote_path', path); | ||
formData.append('client_json', JSON.stringify(client_json)); | ||
const response = await utils.delReq(url, formData); | ||
return response | ||
deleteObject: async function (allocation_id, path, shouldCommitMeta) { | ||
const resp = await goWasm.sdk.delete(allocation_id, path, shouldCommitMeta); | ||
return resp; | ||
}, | ||
@@ -1025,2 +1040,3 @@ | ||
return response | ||
}, | ||
@@ -1052,7 +1068,7 @@ | ||
data.append('app_id', "0x00"); | ||
const response = await utils.postMethodTo0box(url, data, activeWallet.id, activeWallet.public_key,"", tokenId); | ||
const response = await utils.postMethodTo0box(url, data, activeWallet.id, activeWallet.public_key, "", tokenId); | ||
return response | ||
}, | ||
createFree2GbAllocation: async function (id_token, phone_num, encryption_key, username, email, referrer, client_id, client_key){ | ||
createFree2GbAllocation: async function (id_token, phone_num, encryption_key, username, email, referrer, client_id, client_key) { | ||
const url = zeroBoxUrl + Endpoints.ZEROBOX_SERVER_FREE_ALLOCATION; | ||
@@ -1062,11 +1078,11 @@ const data = new FormData(); | ||
data.append('phone_num', phone_num); | ||
data.append('encryption_key',encryption_key); | ||
data.append('encryption_key', encryption_key); | ||
data.append('username', username); | ||
data.append('email', email); | ||
data.append('referrer', referrer); | ||
const response= await utils.postMethodTo0box(url, data,client_id, client_key, "", id_token); | ||
const response = await utils.postMethodTo0box(url, data, client_id, client_key, "", id_token); | ||
return response; | ||
}, | ||
deleteExistAllocation: async function (id_token, phone_num, client_id, client_key, alloc_id){ | ||
deleteExistAllocation: async function (id_token, phone_num, client_id, client_key, alloc_id) { | ||
const url = zeroBoxUrl + Endpoints.ZEROBOX_SERVER_DELETE_EXIST_ALLOCATION; | ||
@@ -1121,3 +1137,2 @@ const data = new FormData(); | ||
const url = zeroBoxUrl + Endpoints.ZEROBOX_SERVER_REFERRALS_INFO_ENDPOINT; | ||
const clientSignature = this.getSign(activeWallet.id, activeWallet.secretKey); | ||
const response = await utils.getReferrals(url); | ||
@@ -1196,3 +1211,3 @@ return response | ||
var ae = new models.Wallet(myaccount.entity); | ||
localStorage.setItem("wallet_info",JSON.stringify(ae)) | ||
localStorage.setItem("wallet_info", JSON.stringify(ae)) | ||
resolve(ae); | ||
@@ -1213,2 +1228,3 @@ }) | ||
blsSecret.setLittleEndian(buffer) | ||
const public_key = blsSecret.getPublicKey().serializeToHexStr(); | ||
@@ -1218,6 +1234,8 @@ const private_key = blsSecret.serializeToHexStr(); | ||
goWasm.jsProxy.secretKey = blsSecret; | ||
return { | ||
client_id, | ||
public_key, | ||
private_key | ||
private_key, | ||
} | ||
@@ -1224,0 +1242,0 @@ |
{ | ||
"name": "@zerochain/0chain", | ||
"version": "1.3.72", | ||
"version": "1.3.73", | ||
"description": "js client library to interact with 0chain Blockchain", | ||
@@ -24,4 +24,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"eslint": "^8.3.0" | ||
"eslint": "^8.3.0", | ||
"prettier": "^2.5.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
186493
17
1946
2
2