Skip to content

Commit

Permalink
Add pubkey and VM address in avatar call
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentSalou committed Mar 16, 2021
1 parent ad65858 commit 2429b98
Show file tree
Hide file tree
Showing 7 changed files with 623 additions and 79 deletions.
11 changes: 11 additions & 0 deletions controllers/AddressCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports.Suggest = Suggest
exports.GetBalance = GetBalance
exports.ListBalances = ListBalances
exports.CountAddresses = CountAddresses
exports.GetPublicKey = GetPublicKey

function Suggest(req, res) {
let prefix = req.params.prefix
Expand Down Expand Up @@ -84,3 +85,13 @@ async function CountAddresses(req, res) {
res.status(404).json(Message(0, 'ERR_ADDRESSES_COUNT'))
})
}

async function GetPublicKey(req, res) {
let address = req.params.address
Address.getPublicKey(address)
.then(result => res.json(Message(1, undefined, result)))
.catch((error) => {
console.error(error)
res.status(404).json(Message(0, 'ERR_ADDRESSES_COUNT'))
})
}
53 changes: 35 additions & 18 deletions controllers/AvatarCtrl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';
"use strict";

const publicKeyToAddress = require('ethereum-public-key-to-address')

//Load Models
var Avatars = require('../models/avatars.js'),
Message = require('../models/message.js');
var Avatars = require("../models/avatars.js"),
Address = require("../models/address.js"),
Message = require("../models/message.js");

exports.ListAllAvatars = listavatars;
exports.Search = search;
Expand All @@ -15,14 +18,16 @@ exports.AvatarInfo = avatarinfo;
*/
function listavatars(req, res) {
var page = parseInt(req.query.page) || 0;
var items_per_page = (req.query.items_per_page) ? parseInt(req.query.items_per_page) : 100;
var items_per_page = req.query.items_per_page
? parseInt(req.query.items_per_page)
: 100;
Avatars.listavatars(page, items_per_page)
.then((avatars) => res.json(Message(1, undefined, avatars)))
.catch((error) => {
console.error(error)
res.status(404).json(Message(0, 'ERR_LIST_AVATARS'))
console.error(error);
res.status(404).json(Message(0, "ERR_LIST_AVATARS"));
});
};
}

/**
* Search for avatars names.
Expand All @@ -31,14 +36,14 @@ function listavatars(req, res) {
*/
function search(req, res) {
let prefix = req.params.prefix;
let limit = Math.min(parseInt(req.query.limit) || 10, 100)
let limit = Math.min(parseInt(req.query.limit) || 10, 100);
Avatars.suggest(prefix, limit)
.then((avatars) => res.json(Message(1, undefined, avatars)))
.catch((error) => {
console.error(error)
res.status(404).json(Message(0, 'ERR_SEARCH_AVATARS'))
console.error(error);
res.status(404).json(Message(0, "ERR_SEARCH_AVATARS"));
});
};
}

/**
* Get the information of an avatar.
Expand All @@ -47,15 +52,27 @@ function search(req, res) {
*/
function avatarinfo(req, res) {
var symbol = req.params.avatar_symbol;
let search = (isAddress(symbol)) ? Avatars.avatarInfoByAddress : Avatars.avatarinfo

let search = isAddress(symbol)
? Avatars.avatarInfoByAddress
: Avatars.avatarinfo;
search(symbol)
.then((avatars) => res.json(Message(1, undefined, avatars)))
.then(async (avatar) => {
const pubkey = await Address.getPublicKey(avatar.address);
const vmaddress = publicKeyToAddress(pubkey)
return res.json(Message(1, undefined, { ...avatar, pubkey, vmaddress }));
})
.catch((error) => {
console.error(error)
res.status(404).json(Message(0, 'ERR_GET_AVATAR'))
console.error(error);
res.status(404).json(Message(0, "ERR_GET_AVATAR"));
});
};
}

function isAddress(address) {
return (address.length == 34) && (address.charAt(0) == 'M' || address.charAt(0) == 't' || address.charAt(0) == '3');
};
return (
address.length == 34 &&
(address.charAt(0) == "M" ||
address.charAt(0) == "t" ||
address.charAt(0) == "3")
);
}
9 changes: 9 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ router.get('/suggest/tx/:prefix', mediumCacheSuccess, TxCtrl.Suggest);
*/
router.get('/address/info/:address', shortCacheSuccess, AddressCtrl.ListBalances);

/**
* Get public key on an address.
* @route GET /address/pubkey/{address}
* @param {string} address.path.required - address
* @group address - Operations about addresses
* @returns {object} 200 - Address details
*/
router.get('/address/pubkey/:address', longCacheSuccess, AddressCtrl.GetPublicKey);

/**
* Get balance of an address.
* @route GET /address/balance/{symbol}/{address}
Expand Down
172 changes: 111 additions & 61 deletions libraries/mongo.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,125 @@
let MongoClient = require('mongodb').MongoClient,
mongo_config = require('../config/mongo.js');
let MongoClient = require("mongodb").MongoClient,
mongo_config = require("../config/mongo.js");

var _db;

module.exports = {
'find': find,
'find_and_count': find_and_count,
'connect': connect
find: find,
findOne,
find_and_count: find_and_count,
connect: connect,
};

function find_and_count(args, selector, collection,sort,page,items_per_page) {
return connect()
.then((db) => Promise.all([_find_paged(db,args,selector,collection,sort,page,items_per_page), _count(db,args,collection)]))
.then((results)=> { return {result: results[0],count: results[1]}; });
};
function find_and_count(
args,
selector,
collection,
sort,
page,
items_per_page
) {
return connect()
.then((db) =>
Promise.all([
_find_paged(db, args, selector, collection, sort, page, items_per_page),
_count(db, args, collection),
])
)
.then((results) => {
return { result: results[0], count: results[1] };
});
}

function _find_paged(db, args, selector, collection, sort,page, items_per_page) {
return new Promise((resolve,reject)=>{
let col = db.collection(collection);
col.find(args, selector).sort(sort).skip(page*items_per_page).limit(items_per_page).toArray((err, docs) => {
if (err)
reject(Error(err.message));
else
resolve(docs);
});
});
};
function _find_paged(
db,
args,
selector,
collection,
sort,
page,
items_per_page
) {
return new Promise((resolve, reject) => {
let col = db.collection(collection);
col
.find(args, selector)
.sort(sort)
.skip(page * items_per_page)
.limit(items_per_page)
.toArray((err, docs) => {
if (err) reject(Error(err.message));
else resolve(docs);
});
});
}

function find(args, selector, collection,sort) {
if(sort===undefined)
sort={};
return connect()
.then((db) => _find(db,args,selector,collection,sort));
};
function find(args, selector, collection, sort) {
if (sort === undefined) sort = {};
return connect().then((db) => _find(db, args, selector, collection, sort));
}

function findOne(args, selector, collection, sort){
if (sort === undefined) sort = {};
return connect().then((db) => _findOne(db, args, selector, collection, sort));
}

function _find(db,args,selector,collection,sort){
return new Promise((resolve,reject)=>{
let col = db.collection(collection);
col.find(args,selector).sort(sort).toArray((err, docs) => {
if (err)
reject(Error(err.message));
else
resolve(docs);
});
});
function _findOne(db, args, selector, collection, sort) {
return new Promise((resolve, reject) => {
let col = db.collection(collection);
col
.find(args, selector)
.sort(sort)
.limit(1)
.toArray((err, docs) => {
if (err) reject(Error(err.message));
else resolve(docs.length ? docs[0] : null);
});
});
}
function _find(db, args, selector, collection, sort) {
return new Promise((resolve, reject) => {
let col = db.collection(collection);
col
.find(args, selector)
.sort(sort)
.toArray((err, docs) => {
if (err) reject(Error(err.message));
else resolve(docs);
});
});
}

function _count(db,args,collection,sort){
return new Promise((resolve,reject)=>{
let col = db.collection(collection);
col.find(args).sort(sort).count((err, count) => {
if (err)
reject(Error(err.message));
else
resolve(count);
});
});
function _count(db, args, collection, sort) {
return new Promise((resolve, reject) => {
let col = db.collection(collection);
col
.find(args)
.sort(sort)
.count((err, count) => {
if (err) reject(Error(err.message));
else resolve(count);
});
});
}
function connect() {
return new Promise((resolve, reject) => {
if (_db !== undefined)
resolve(_db);
else {
let url = (mongo_config.url)?mongo_config.url:'mongodb://' + mongo_config.host + ':' + mongo_config.port + '/' + mongo_config.database;
MongoClient.connect(url, function(err, db) {
if (err) throw Error(err.message);
else {
resolve(db);
_db = db;
}
});
}
});
return new Promise((resolve, reject) => {
if (_db !== undefined) resolve(_db);
else {
let url = mongo_config.url
? mongo_config.url
: "mongodb://" +
mongo_config.host +
":" +
mongo_config.port +
"/" +
mongo_config.database;
MongoClient.connect(url, function (err, db) {
if (err) throw Error(err.message);
else {
resolve(db);
_db = db;
}
});
}
});
}
20 changes: 20 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,29 @@ module.exports = {
balances: listBalances,
suggest: suggest,
listAddressIds: listAddressIds,
getPublicKey,
countaddresses: countaddresses
};

async function getPublicKey(address){
const pubkeyRegex = /\[ .{144} \] \[ (.{66}) \]/
const tx = await mongo.findOne({
'inputs.address': address,
"orphan": 0,
}, {
'inputs.address': 1,
'inputs.script': 1
}, 'tx')
if(tx != null){
for(let input of tx.inputs){
if(input.address===address && pubkeyRegex.test(input.script)) {
return input.script.match(pubkeyRegex)[1]
}
}
}
return null
}

function listTxsData(address, from, to) {
return mongo.find({
$or: [{
Expand Down
Loading

0 comments on commit 2429b98

Please sign in to comment.