Browse Source

added errors catching to /content-message and /buy-message

master
Aleksandr Bautin 1 year ago
parent
commit
d8a29eebe0
No known key found for this signature in database
GPG Key ID: 9B3364A12DFE9211
  1. 86
      bin/api.js

86
bin/api.js

@ -1,52 +1,60 @@
const main = require('../contracts/main'); const main = require("../contracts/main");
const {Address, Cell, Slice} = require("ton"); const { Address, Cell, Slice } = require("ton");
const {Base64} = require('@tonconnect/protocol'); const { Base64 } = require("@tonconnect/protocol");
require("dotenv").config();
const express = require('express') const express = require("express");
const {get_tonclient, AdnlAddress} = require("../contracts/utils"); const { get_tonclient, AdnlAddress } = require("../contracts/utils");
const {getRecords} = require("../contracts/main"); const { getRecords } = require("../contracts/main");
const {sha256} = require("ton-crypto"); const { sha256 } = require("ton-crypto");
const {BN} = require("bn.js"); const { BN } = require("bn.js");
const app = express() const app = express();
const port = 5171 const port = 5171;
const debug = process.env.DEBUG === '1'; const debug = process.env.DEBUG === "1";
let tonclient = get_tonclient(debug); let tonclient = get_tonclient(debug);
app.use(function(err, req, res, next) { app.use(function (err, req, res, next) {
if(!err) return next(); // you also need this line if (!err) return next(); // you also need this line
console.log("error"); console.log("error");
res.send("error"); res.send("error");
}); });
app.get('/', async (req, res) => { app.get("/", async (req, res) => {
res.send('Agorata microservice for dealing with TON') res.send("Agorata microservice for dealing with TON");
}) });
app.get('/buy-message/:collection/:domain' /* the rest as get parameters: buyer, key */, (req, res) => { app.get("/buy-message/:collection/:domain" /* the rest as get parameters: buyer, key */, (req, res) => {
try {
let collection = Address.parse(req.params.collection); let collection = Address.parse(req.params.collection);
if (req.query.buyer !== '') { if (req.query.buyer !== "") {
let buyer = Address.parse(req.query.buyer); let buyer = Address.parse(req.query.buyer);
let key = Buffer.from(req.query.key, 'hex'); let key = Buffer.from(req.query.key, "hex");
let signature = main.signBuy(req.params.domain, collection, buyer, key); let signature = main.signBuy(req.params.domain, collection, buyer, key);
let msg = main.createItem({domain: req.params.domain, signature}) let msg = main.createItem({ domain: req.params.domain, signature });
res.send(JSON.stringify([Base64.encode(msg.toBoc()), collection.toString()])); res.send(JSON.stringify([Base64.encode(msg.toBoc()), collection.toString()]));
} else { } else {
let msg = main.createItem({domain: req.params.domain, signature: '0'}) let msg = main.createItem({ domain: req.params.domain, signature: "0" });
res.send(JSON.stringify([Base64.encode(msg.toBoc()), collection.toString()])); res.send(JSON.stringify([Base64.encode(msg.toBoc()), collection.toString()]));
} }
} catch (e) {
console.error("buy-message", e);
}
}); });
app.get('/content-message/:address/:zone/:domain', async (req, res) => { app.get("/content-message/:address/:zone/:domain", async (req, res) => {
let msg = await main.setContent({domain: req.params.domain, zone: req.params.zone}); try {
let msg = await main.setContent({ domain: req.params.domain, zone: req.params.zone });
let addr = main.getItemAddr(Address.parse(req.params.address), req.params.domain, 0); let addr = main.getItemAddr(Address.parse(req.params.address), req.params.domain, 0);
res.send(JSON.stringify([Base64.encode(msg.toBoc()), addr.toString()])); res.send(JSON.stringify([Base64.encode(msg.toBoc()), addr.toString()]));
} catch (e) {
console.error("content-message");
}
}); });
app.get('/address/:collection/:domain', (req, res) => { app.get("/address/:collection/:domain", (req, res) => {
let addr = main.getItemAddr(Address.parse(req.params.collection), req.params.domain, 0); let addr = main.getItemAddr(Address.parse(req.params.collection), req.params.domain, 0);
res.send(JSON.stringify({"address": addr.toString()})); res.send(JSON.stringify({ address: addr.toString() }));
}) });
app.get('/get-records/:address', async (req, res) => { app.get("/get-records/:address", async (req, res) => {
try { try {
let records = await getRecords(tonclient, Address.parse(req.params.address)); let records = await getRecords(tonclient, Address.parse(req.params.address));
// console.log(records) // console.log(records)
@ -55,29 +63,29 @@ app.get('/get-records/:address', async (req, res) => {
console.log(e); console.log(e);
res.send(JSON.stringify({})); res.send(JSON.stringify({}));
} }
}) });
app.get('/get-price/:address/:subdomain', async (req, res) => { app.get("/get-price/:address/:subdomain", async (req, res) => {
let price = await main.getPrice(tonclient, Address.parse(req.params.address), req.params.subdomain); let price = await main.getPrice(tonclient, Address.parse(req.params.address), req.params.subdomain);
res.send(JSON.stringify(price)); res.send(JSON.stringify(price));
}) });
app.get('/set-record/site/:site', async (req, res) => { app.get("/set-record/site/:site", async (req, res) => {
let site = new AdnlAddress(req.params.site); let site = new AdnlAddress(req.params.site);
let msg = await main.changeRecordMsg("site", await main.AdnlRecord(site)); let msg = await main.changeRecordMsg("site", await main.AdnlRecord(site));
res.send(JSON.stringify(Base64.encode(msg.toBoc()))); res.send(JSON.stringify(Base64.encode(msg.toBoc())));
}) });
app.get('/hash/:key', async (req, res) => { app.get("/hash/:key", async (req, res) => {
res.send(new BN(await sha256(req.params.key)).toString()); res.send(new BN(await sha256(req.params.key)).toString());
}) });
app.get('/set-record/wallet/:wallet', async (req, res) => { app.get("/set-record/wallet/:wallet", async (req, res) => {
let wallet = Address.parse(req.params.wallet); let wallet = Address.parse(req.params.wallet);
let msg = await main.changeRecordMsg("wallet", await main.WalletRecord(wallet)); let msg = await main.changeRecordMsg("wallet", await main.WalletRecord(wallet));
res.send(JSON.stringify(Base64.encode(msg.toBoc()))); res.send(JSON.stringify(Base64.encode(msg.toBoc())));
}) });
app.listen(port, () => { app.listen(port, () => {
console.log(`Example app listening on port ${port}`) console.log(`Example app listening on port ${port}`);
}) });

Loading…
Cancel
Save