From a8d5289738d0128823c07c3d2aa27860614285b4 Mon Sep 17 00:00:00 2001 From: liangping <18786721@qq.com> Date: Tue, 21 Feb 2023 13:23:23 +0800 Subject: [PATCH 01/30] add multi chain support --- .gitignore | 3 +- checker.js | 10 +++-- config.js | 108 +++++++++++++++++++++++++++++++++++++---------------- faucet.js | 54 ++++++++++++++++----------- index.html | 28 +++++++++++--- 5 files changed, 139 insertions(+), 64 deletions(-) diff --git a/.gitignore b/.gitignore index 6876c43..07f193a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /node_modules /web.js -/db \ No newline at end of file +/db +.idea \ No newline at end of file diff --git a/checker.js b/checker.js index 5d186df..651a98e 100644 --- a/checker.js +++ b/checker.js @@ -24,12 +24,14 @@ export class FrequencyChecker { }) } - async checkIp(ip) { - return this.check(ip, this.conf.limit.ip) + async checkIp(ip, chain) { + const chainLimit = this.conf.blockchains.find(x => x.name === chain) + return chainLimit ? this.check(ip, chainLimit.limit.ip ) : Promise.resolve(false) } - async checkAddress(address) { - return this.check(address, this.conf.limit.address) + async checkAddress(address, chain) { + const chainLimit = this.conf.blockchains.find(x => x.name === chain) + return chainLimit ? this.check(address, chainLimit.limit.address ) : Promise.resolve(false) } async update(key) { diff --git a/config.js b/config.js index 84230b4..d7be0d2 100644 --- a/config.js +++ b/config.js @@ -11,39 +11,81 @@ export default { logo: "https://ping.pub/logo.svg", deployer: `Your Brand` }, - blockchain: { - // make sure that CORS is enabled in rpc section in config.toml - // cors_allowed_origins = ["*"] - rpc_endpoint: "https://rpc.sentry-02.theta-testnet.polypore.xyz", - - }, - sender: { - mnemonic: "surround miss nominee dream gap cross assault thank captain prosper drop duty group candy wealth weather scale put", - option: { - hdPaths: [stringToPath("m/44'/118'/0'/0/0")], - prefix: "cosmos" - } - }, - tx: { - amount: { - denom: "uatom", - amount: "10000" - }, - fee: { - amount: [ - { - amount: "1000", - denom: "uatom" + blockchains: [ + { + name: "cosmos", + endpoint: { + // make sure that CORS is enabled in rpc section in config.toml + // cors_allowed_origins = ["*"] + rpc_endpoint: "https://rpc.sentry-02.theta-testnet.polypore.xyz", + }, + sender: { + mnemonic: "surround miss nominee dream gap cross assault thank captain prosper drop duty group candy wealth weather scale put", + option: { + hdPaths: [stringToPath("m/44'/118'/0'/0/0")], + prefix: "cosmos" } - ], - gas: "200000" + }, + tx: { + amount: { + denom: "uatom", + amount: "10000" + }, + fee: { + amount: [ + { + amount: "1000", + denom: "uatom" + } + ], + gas: "200000" + }, + }, + limit: { + // how many times each wallet address is allowed in a window(24h) + address: 1, + // how many times each ip is allowed in a window(24h), + // if you use proxy, double check if the req.ip is return client's ip. + ip: 10 + } }, - }, - limit: { - // how many times each wallet address is allowed in a window(24h) - address: 1, - // how many times each ip is allowed in a window(24h), - // if you use proxy, double check if the req.ip is return client's ip. - ip: 10 - } + { + name: "juno", + endpoint: { + // make sure that CORS is enabled in rpc section in config.toml + // cors_allowed_origins = ["*"] + rpc_endpoint: "https://rpc.sentry-02.theta-testnet.polypore.xyz", + }, + sender: { + mnemonic: "surround miss nominee dream gap cross assault thank captain prosper drop duty group candy wealth weather scale put", + option: { + hdPaths: [stringToPath("m/44'/118'/0'/0/0")], + prefix: "juno" + } + }, + tx: { + amount: { + denom: "uatom", + amount: "10000" + }, + fee: { + amount: [ + { + amount: "1000", + denom: "uatom" + } + ], + gas: "200000" + }, + }, + limit: { + // how many times each wallet address is allowed in a window(24h) + address: 1, + // how many times each ip is allowed in a window(24h), + // if you use proxy, double check if the req.ip is return client's ip. + ip: 10 + } + } + + ] } \ No newline at end of file diff --git a/faucet.js b/faucet.js index 42f95c5..104ab08 100644 --- a/faucet.js +++ b/faucet.js @@ -19,23 +19,31 @@ app.get('/', (req, res) => { }) app.get('/config.json', async (req, res) => { - const wallet = await DirectSecp256k1HdWallet.fromMnemonic(conf.sender.mnemonic, conf.sender.option); - const [firstAccount] = await wallet.getAccounts(); + const sample = {} + for(let i =0; i < conf.blockchains.length; i++) { + const chainConf = conf.blockchains[i] + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(chainConf.sender.mnemonic, chainConf.sender.option); + const [firstAccount] = await wallet.getAccounts(); + sample[chainConf.name] = firstAccount.address + } + const project = conf.project - project.sample = firstAccount.address + project.sample = sample + project.blockchains = conf.blockchains.map(x => x.name) res.send(project); }) -app.get('/send/:address', async (req, res) => { - const {address} = req.params; +app.get('/send/:chain/:address', async (req, res) => { + const {chain, address} = req.params; console.log('request tokens to ', address, req.ip) - if (address) { + if (chain || address ) { try { - if (address.startsWith(conf.sender.option.prefix)) { - if( await checker.checkAddress(address) && await checker.checkIp(req.ip) ) { - checker.update(req.ip) // get ::1 on localhost - sendTx(address).then(ret => { - console.log('sent tokens to ', address) + const chainConf = conf.blockchains.find(x => x.name === chain) + if (chainConf && address.startsWith(chainConf.sender.option.prefix)) { + if( await checker.checkAddress(address, chain) && await checker.checkIp(`${chain}${req.ip}`, chain) ) { + checker.update(`${chain}${req.ip}`) // get ::1 on localhost + sendTx(address, chain).then(ret => { + checker.update(address) res.send({ result: ret }) }); @@ -61,19 +69,23 @@ app.listen(conf.port, () => { }) -async function sendTx(recipient) { +async function sendTx(recipient, chain) { // const mnemonic = "surround miss nominee dream gap cross assault thank captain prosper drop duty group candy wealth weather scale put"; - const wallet = await DirectSecp256k1HdWallet.fromMnemonic(conf.sender.mnemonic, conf.sender.option); - const [firstAccount] = await wallet.getAccounts(); + const chainConf = conf.blockchains.find(x => x.name === chain) + if(chainConf) { + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(chainConf.sender.mnemonic, chainConf.sender.option); + const [firstAccount] = await wallet.getAccounts(); - // console.log("sender", firstAccount); + // console.log("sender", firstAccount); - const rpcEndpoint = conf.blockchain.rpc_endpoint; - const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet); + const rpcEndpoint = chainConf.endpoint.rpc_endpoint; + const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet); - // const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"; - const amount = conf.tx.amount; - const fee = conf.tx.fee; - return client.sendTokens(firstAccount.address, recipient, [amount], fee); + // const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"; + const amount = chainConf.tx.amount; + const fee = chainConf.tx.fee; + return client.sendTokens(firstAccount.address, recipient, [amount], fee); + } + throw new Error(`Blockchain Config [${chain}] not found`) } diff --git a/index.html b/index.html index d726178..fd48b34 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@
-