token-gallery-backend/url.js

52 lines
1.4 KiB
JavaScript

const Web3Util = require('web3-utils');
const { Client } = require('pg');
const https = require('https');
const { argv } = require('process');
const account = Web3Util.toChecksumAddress(process.argv[2]);
const method = process.argv[3].toUpperCase();
const url = process.argv[4];
const path = new URL(url).pathname;
const settings = require('./test-settings.json');
const client = new Client(settings.db);
client.connect().then(async () => {
const result = await client.query("select token from account where id = $1", [ account ]);
const token = result.rows[0].token;
const now = new Date();
const hashString = `${now} ${account} ${token} ${path}`;
console.log(hashString);
const hash = Web3Util.keccak256(hashString);
const headers = {
'X-NftStore-Now': now,
'X-NftStore-Account': account,
'Authorization': `Bearer ${hash}`
}
if (argv.length > 5) {
var body = process.argv[5];
headers['Content-Type'] = 'application/json';
headers['Content-Length'] = body.length;
}
const req = https.request(url, { method, headers }, res => {
console.log(res.headers);
res.on('data', d => {
console.log(d.toString());
});
res.on('error', e => {
console.error(e);
process.exit(1);
});
res.on('end', () => {
process.exit(res.statusCode);
});
});
if (argv.length > 5) {
req.write(body);
}
req.end();
});