Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create user object and user endpoint #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 111 additions & 2 deletions packages/bitcore-wallet-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/bitcore-wallet-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"moment": "^2.10.3",
"mongodb": "^2.0.27",
"morgan": "^1.9.1",
"multer": "^1.4.2",
"mustache": "^2.1.0",
"nodemailer": "^5.1.1",
"npmlog": "^0.1.1",
Expand Down
11 changes: 11 additions & 0 deletions packages/bitcore-wallet-service/src/lib/expressapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,17 @@ export class ExpressApp {
});
});

router.post('/v1/user/new/', (req, res) => {
getServerWithAuth(req, res, server => {
server.createUser(req, (err, user) => {
if (err) return returnError(err, res, req);
res.json(user);
})
})
});

//router.get('v1/user/:id')

router.get('/v2/txproposals/', (req, res) => {
getServerWithAuth(req, res, server => {
server.getPendingTxs({}, (err, pendings) => {
Expand Down
1 change: 1 addition & 0 deletions packages/bitcore-wallet-service/src/lib/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { TxConfirmationSub } from './txconfirmationsub';
export { TxNote } from './txnote';
export { ITxProposal, TxProposal } from './txproposal';
export { IWallet, Wallet } from './wallet';
export { IUser, User } from './user' ;
61 changes: 61 additions & 0 deletions packages/bitcore-wallet-service/src/lib/model/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export interface IUser {
id: number | string;
version: string;
createdOn: number;
name: string;
dateOfBirth: Date;
address: string;
mobileNumber: number;
document: string;
verified: boolean;
publicSharingKey: string;
}

export class User {
id: number | string;
version: string;
createdOn: number;
name: string;
dateOfBirth: Date;
address: string;
mobileNumber: number;
document: string;
verified: boolean;
publicSharingKey: string;

static create(opts) {
opts = opts || {};

const x = new User();

x.version = '1.0.0';
x.createdOn = Math.floor(Date.now() / 1000);
x.id = opts.id;
x.name = opts.name;
x.dateOfBirth = opts.dateOfBirth;
x.address = opts.address;
x.mobileNumber = opts.mobileNumber;
x.document = opts.document;
x.verified = opts.verified;
x.publicSharingKey = opts.publicSharingKey;

return x;
}

static fromObj(obj) {
const x = new User();

x.version = obj.version;
x.createdOn = obj.createdOn;
x.id = obj.id;
x.name = obj.name;
x.dateOfBirth = obj.dateOfBirth;
x.address = obj.address;
x.mobileNumber = obj.mobileNumber;
x.document = obj.document;
x.verified = obj.verified;
x.publicSharingKey = obj.publicSharingKey;

return x;
}
}
105 changes: 104 additions & 1 deletion packages/bitcore-wallet-service/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,32 @@ import {
TxConfirmationSub,
TxNote,
TxProposal,
Wallet
Wallet,
User
} from './model';
import { Storage } from './storage';

const path = require('path');
const config = require('../config');
const Uuid = require('uuid');
const $ = require('preconditions').singleton();
const deprecatedServerMessage = require('../deprecated-serverMessages');
const serverMessages = require('../serverMessages');
const BCHAddressTranslator = require('./bchaddresstranslator');

const multer = require('multer');

const fileStorage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'uploads/');
},

// By default, multer removes file extensions so let's add them back
filename: function(req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});

log.debug = log.verbose;
log.disableColor();
log.level = 'error';
Expand Down Expand Up @@ -1208,6 +1223,94 @@ export class WalletService {
});
}



/*** Start New */


/**
* Creates a new transaction proposal.
* @param {Object} opts
* @returns {User} User Info
*/
createUser(opts, cb) {
let obj = opts.body;
let file = opts.file;

let upload = multer({ fileStorage: fileStorage }).single('document');

const checkUserAlreadyExists = (id, cb) => {
if (!id) return cb();
this.storage.fetchUser(id, cb);
}

this._runLocked(
cb,
cb => {
this.getWallet({}, (err, wallet) => {
if (err) return cb(err);
if (!wallet.isComplete()) return cb(Errors.WALLET_NOT_COMPLETE);
if (wallet.scanStatus == 'error') return cb(Errors.WALLET_NEED_SCAN);

checkUserAlreadyExists(obj.id, (err, user) => {
if (err) return cb(err);
if (user) return cb(null, user);

async.series(
[
next => {
upload(file, cb, function(err) {
if (err instanceof multer.MulterError) return next(err);
else if (err) return next(err);
next()
});
},
next => {
const userOpts = {
id: obj.id,
version: obj.version,
name: obj.name,
dateOfBirth: obj.dateOfBirth,
address: obj.address,
mobileNumber: obj.mobileNumber,
document: file.fieldname,
verified: obj.verified,
publicSharingKey: obj.publicSharingKey
};
user = User.create(userOpts);
next();
}
],
err => {
if (err) return cb(err);
return cb(null, user)
}
)
})
})
}
)
}

/**
* Retrieves a preferences for the current wallet/copayer pair.
* @param {Object} opts
* @returns {Object} preferences
*/
getUser(opts, cb) {
this.storage.fetchUser(opts.id, (err, user) => {
if (err) return cb(err);
return cb(null, user || {});
});
}


/*** End New */





_canCreateAddress(ignoreMaxGap, cb) {
if (ignoreMaxGap) return cb(null, true);

Expand Down
Loading