Remove npm dependency: secretjs

This commit is contained in:
g1nt0ki
2022-05-20 14:42:32 +05:30
parent 090df093ca
commit 81e6df0d06
6 changed files with 276 additions and 713 deletions

795
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -12,6 +12,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@cosmjs/encoding": "^0.28.4",
"@defillama/sdk": "^2.3.62",
"@polkadot/api": "^8.0.0",
"@project-serum/anchor": "^0.18.2",
@@ -24,13 +25,16 @@
"bignumber.js": "^9.0.1",
"blakejs": "^1.2.1",
"borsh": "^0.7.0",
"curve25519-js": "^0.0.4",
"dotenv": "^8.6.0",
"ethers": "^5.6.5",
"graphql": "^15.5.0",
"graphql-request": "^3.6.1",
"hi-base32": "^0.5.1",
"js-crypto-hkdf": "^1.0.4",
"js-sha512": "^0.8.0",
"secretjs": "^0.17.5",
"miscreant": "^0.3.2",
"secure-random": "^1.1.2",
"tron-format-address": "^0.1.8",
"underscore": "^1.13.1"
},

View File

@@ -1,20 +1,3 @@
const retry = require('./helper/retry')
const axios = require("axios");
const BigNumber = require("bignumber.js");
async function fetch() {
return 0
var price_feed = await retry(async bail => await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=thorchain&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true'))
var res = await retry(async bail => await axios.get('https://chaosnet-midgard.bepswap.com/v1/network'))
var tvl = await new BigNumber((parseFloat(res.data.totalStaked) * 2) + parseFloat(res.data.bondMetrics.totalActiveBond) + parseFloat(res.data.bondMetrics.totalStandbyBond)).div(10 ** 8).toFixed(2);
tvl = tvl * price_feed.data.thorchain.usd;
return tvl;
}
module.exports = {
fetch
fetch: () => 0
}

View File

@@ -0,0 +1,60 @@
const { get } = require('./http')
const EnigmaUtils = require('./utils/enigma')
const { toHex, toUtf8, toBase64, fromUtf8, fromBase64 } = require('@cosmjs/encoding')
class CosmWasmClient {
constructor(nodeURL) {
this.nodeURL = nodeURL
this.codeHashCache = {}
this.enigmautils = new EnigmaUtils(nodeURL)
}
async getHeight() {
const latest = await this.get('/blocks/latest')
return parseInt(latest.block.header.height, 10)
}
async queryContractSmart(contractAddress, query, addedParams) {
const contractCodeHash = await this.getCodeHashByContractAddr(contractAddress);
const encrypted = await this.enigmautils.encrypt(contractCodeHash, query);
const nonce = encrypted.slice(0, 32);
const encoded = toHex(toUtf8(toBase64(encrypted)));
// @ts-ignore
const paramString = new URLSearchParams(addedParams).toString();
const path = `/wasm/contract/${contractAddress}/query/${encoded}?encoding=hex&${paramString}`;
let responseData = (await this.get(path))
// By convention, smart queries must return a valid JSON document (see https://github.com/CosmWasm/cosmwasm/issues/144)
return JSON.parse(fromUtf8(fromBase64(fromUtf8(await this.enigmautils.decrypt(fromBase64(responseData.result.smart), nonce)))));
}
async get(api) {
return get(this.nodeURL + api)
}
async getContracts(codeId) {
const path = `/wasm/code/${codeId}/contracts`;
const responseData = (await this.get(path));
const result = responseData.result || [];
return result.map((entry) => ({
address: entry.address,
codeId: entry.code_id,
creator: entry.creator,
label: entry.label,
}));
}
async getCodeHashByContractAddr(addr) {
const codeHashFromCache = this.codeHashCache[addr];
if (typeof codeHashFromCache === "string")
return codeHashFromCache;
const path = `/wasm/contract/${addr}/code-hash`;
const responseData = await this.get(path);
this.codeHashCache[addr] = responseData.result
return responseData.result;
}
}
module.exports = CosmWasmClient

View File

@@ -0,0 +1,105 @@
const miscreant = require("miscreant");
const { toUtf8, fromBase64 } = require('@cosmjs/encoding')
const curve25519_js_1 = require("curve25519-js");
const secureRandom = require("secure-random");
const hkdf = require("js-crypto-hkdf");
const { get } = require('../http')
const cryptoProvider = new miscreant.PolyfillCryptoProvider();
const hkdfSalt = Uint8Array.from([
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x02,
0x4b,
0xea,
0xd8,
0xdf,
0x69,
0x99,
0x08,
0x52,
0xc2,
0x02,
0xdb,
0x0e,
0x00,
0x97,
0xc1,
0xa1,
0x2e,
0xa6,
0x37,
0xd7,
0xe9,
0x6d,
]);
class EnigmaUtils {
constructor(apiUrl) {
this.consensusIoPubKey = new Uint8Array(); // cache
this.apiUrl = apiUrl;
this.seed = EnigmaUtils.GenerateNewSeed();
const { privkey, pubkey } = EnigmaUtils.GenerateNewKeyPairFromSeed(this.seed);
this.privkey = privkey;
this.pubkey = pubkey;
}
static GenerateNewKeyPair() {
return EnigmaUtils.GenerateNewKeyPairFromSeed(EnigmaUtils.GenerateNewSeed());
}
static GenerateNewSeed() {
return secureRandom(32, { type: "Uint8Array" });
}
static GenerateNewKeyPairFromSeed(seed) {
const { private: privkey, public: pubkey } = curve25519_js_1.generateKeyPair(seed);
return { privkey, pubkey };
}
async getConsensusIoPubKey() {
if (this.consensusIoPubKey.length === 32) {
return this.consensusIoPubKey;
}
const { result: { TxKey }, } = await get(this.apiUrl + "/reg/tx-key");
this.consensusIoPubKey = fromBase64(TxKey);
return this.consensusIoPubKey;
}
async getTxEncryptionKey(nonce) {
const consensusIoPubKey = await this.getConsensusIoPubKey();
const txEncryptionIkm = curve25519_js_1.sharedKey(this.privkey, consensusIoPubKey);
const { key: txEncryptionKey } = await hkdf.compute(Uint8Array.from([...txEncryptionIkm, ...nonce]), "SHA-256", 32, "", hkdfSalt);
return txEncryptionKey;
}
async encrypt(contractCodeHash, msg) {
const nonce = secureRandom(32, {
type: "Uint8Array",
});
const txEncryptionKey = await this.getTxEncryptionKey(nonce);
const siv = await miscreant.SIV.importKey(txEncryptionKey, "AES-SIV", cryptoProvider);
const plaintext = toUtf8(contractCodeHash + JSON.stringify(msg));
const ciphertext = await siv.seal(plaintext, [new Uint8Array()]);
// ciphertext = nonce(32) || wallet_pubkey(32) || ciphertext
return Uint8Array.from([...nonce, ...this.pubkey, ...ciphertext]);
}
async decrypt(ciphertext, nonce) {
var _a;
if (!((_a = ciphertext) === null || _a === void 0 ? void 0 : _a.length)) {
return new Uint8Array();
}
const txEncryptionKey = await this.getTxEncryptionKey(nonce);
//console.log(`decrypt tx encryption key: ${Encoding.toHex(txEncryptionKey)}`);
const siv = await miscreant.SIV.importKey(txEncryptionKey, "AES-SIV", cryptoProvider);
const plaintext = await siv.open(ciphertext, [new Uint8Array()]);
return plaintext;
}
getPubkey() {
return Promise.resolve(this.pubkey);
}
}
module.exports = EnigmaUtils

View File

@@ -1,4 +1,4 @@
const { CosmWasmClient } = require("secretjs");
const CosmWasm = require("../helper/CosmWasm");
const BigNumber = require("bignumber.js");
const sdk = require('@defillama/sdk');
const utils = require("./utils");
@@ -21,7 +21,7 @@ const SIENNA_TOKEN_ADDRESS = "secret1rgm2m5t530tdzyd99775n6vzumxa5luxcllml4";
const LEND_OVERSEER_CONTRACT = "secret1pf88n9hm64mn58aw48jxfs2fsvzr07svnrrdlv";
const SECRET_NODE_URL = "https://bridgeapi.azure-api.net/node10";
const queryClient = new CosmWasmClient(SECRET_NODE_URL)
const queryClient = new CosmWasm(SECRET_NODE_URL)
const CACHED_TOKENS = {};