Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
Merge branch '1.0.0' into xx-Delete_Dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Isabella Dell authored Aug 17, 2017
2 parents bb51f1d + 3d25199 commit b681ca3
Show file tree
Hide file tree
Showing 9 changed files with 1,161 additions and 1,188 deletions.
173 changes: 73 additions & 100 deletions src/api/liskApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ LiskAPI.prototype.getNethash = function getNethash(providedNethash) {
};

/**
* @method listPeers
* @method getPeers
* @return {object}
*/

LiskAPI.prototype.listPeers = function listPeers() {
LiskAPI.prototype.getPeers = function getPeers() {
return {
official: this.defaultPeers.map(node => ({ node })),
ssl: this.defaultSSLPeers.map(node => ({ node, ssl: true })),
Expand Down Expand Up @@ -215,7 +215,6 @@ LiskAPI.prototype.sendRequest = function sendRequest(
) {
const callback = callbackIfOptions || optionsOrCallback;
const options = typeof optionsOrCallback !== 'function' && typeof optionsOrCallback !== 'undefined' ? privateApi.checkOptions.call(this, optionsOrCallback) : {};

return privateApi.sendRequestPromise.call(this, requestMethod, requestType, options)
.then(result => result.body)
.then(handleTimestampIsInFutureFailures.bind(this, requestMethod, requestType, options))
Expand Down Expand Up @@ -243,197 +242,171 @@ LiskAPI.prototype.getAddressFromSecret = function getAddressFromSecret(secret) {
/**
* @method getAccount
* @param address
* @param callback
*
* @return API object
*/

LiskAPI.prototype.getAccount = function getAccount(address, callback) {
return this.sendRequest(GET, 'accounts', { address }, result => callback(result));
};

/**
* @method generateAccount
* @param secret
* @param callback
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.generateAccount = function generateAccount(secret, callback) {
const keys = LiskJS.crypto.getPrivateAndPublicKeyFromSecret(secret);
callback(keys);
return this;
};
LiskAPI.prototype.getAccount = privateApi.wrapSendRequest(GET, 'accounts', address => ({ address }));

/**
* @method listActiveDelegates
* @method getActiveDelegates
* @param limit
* @param callback
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.listActiveDelegates = function listActiveDelegates(limit, callback) {
this.sendRequest(GET, 'delegates/', { limit }, result => callback(result));
};
LiskAPI.prototype.getActiveDelegates = privateApi.wrapSendRequest(GET, 'delegates', limit => ({ limit }));

/**
* @method listStandbyDelegates
* @method getStandbyDelegates
* @param limit
* @param callback
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.listStandbyDelegates = function listStandbyDelegates(limit, callback) {
const standByOffset = 101;

this.sendRequest(GET, 'delegates/', { limit, orderBy: 'rate:asc', offset: standByOffset }, result => callback(result));
};
LiskAPI.prototype.getStandbyDelegates = privateApi.wrapSendRequest(GET, 'delegates', (limit, { orderBy = 'rate:asc', offset = 101 }) => ({ limit, orderBy, offset }));

/**
* @method searchDelegateByUsername
* @param username
* @param callback
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.searchDelegateByUsername = function searchDelegateByUsername(username, callback) {
this.sendRequest(GET, 'delegates/search/', { q: username }, result => callback(result));
};
LiskAPI.prototype.searchDelegatesByUsername = privateApi.wrapSendRequest(GET, 'delegates/search', username => ({ username }));

/**
* @method listBlocks
* @param amount
* @param callback
* @method getBlocks
* @param limit
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.listBlocks = function listBlocks(amount, callback) {
this.sendRequest(GET, 'blocks', { limit: amount }, result => callback(result));
};
LiskAPI.prototype.getBlocks = privateApi.wrapSendRequest(GET, 'blocks', limit => ({ limit }));

/**
* @method listForgedBlocks
* @param publicKey
* @param callback
* @method getForgedBlocks
* @param generatorPublicKey
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.listForgedBlocks = function listForgedBlocks(publicKey, callback) {
this.sendRequest(GET, 'blocks', { generatorPublicKey: publicKey }, result => callback(result));
};
LiskAPI.prototype.getForgedBlocks = privateApi.wrapSendRequest(GET, 'blocks', generatorPublicKey => ({ generatorPublicKey }));

/**
* @method getBlock
* @param block
* @param callback
* @param height
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.getBlock = function getBlock(block, callback) {
this.sendRequest(GET, 'blocks', { height: block }, result => callback(result));
};
LiskAPI.prototype.getBlock = privateApi.wrapSendRequest(GET, 'blocks', height => ({ height }));

/**
* @method listTransactions
* @param address
* @param limit
* @param offset
* @param callback
* @method getTransactions
* @param recipientId
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.listTransactions = function listTransactions(
address, limit = '20', offset = '0', callback,
) {
this.sendRequest(GET, 'transactions', { senderId: address, recipientId: address, limit, offset, orderBy: 'timestamp:desc' }, result => callback(result));
};
LiskAPI.prototype.getTransactions = privateApi.wrapSendRequest(GET, 'transactions', recipientId => ({ recipientId }));

/**
* @method getTransaction
* @param transactionId
* @param callback
* @param id
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.getTransaction = function getTransaction(transactionId, callback) {
this.sendRequest(GET, 'transactions/get', { id: transactionId }, result => callback(result));
};
LiskAPI.prototype.getTransaction = privateApi.wrapSendRequest(GET, 'transactions/get', id => ({ id }));

/**
* @method listVotes
* @method getVotes
* @param address
* @param callback
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.listVotes = function listVotes(address, callback) {
this.sendRequest(GET, 'accounts/delegates', { address }, result => callback(result));
};
LiskAPI.prototype.getVotes = privateApi.wrapSendRequest(GET, 'accounts/delegates', address => ({ address }));

/**
* @method listVoters
* @method getVoters
* @param publicKey
* @param callback
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.listVoters = function listVoters(publicKey, callback) {
this.sendRequest(GET, 'delegates/voters', { publicKey }, result => callback(result));
};
LiskAPI.prototype.getVoters = privateApi.wrapSendRequest(GET, 'delegates/voters', publicKey => ({ publicKey }));

/**
* @method sendLSK
* @param recipient
* @param amount
* @param secret
* @param secondSecret
* @param callback
* @method listMultisignatureTransactions
* @param data
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.sendLSK = function sendLSK(recipient, amount, secret, secondSecret, callback) {
this.sendRequest(POST, 'transactions', { recipientId: recipient, amount, secret, secondSecret }, response => callback(response));
};
LiskAPI.prototype.getMultisignatureTransactions = privateApi.wrapSendRequest(GET, 'transactions/multisignatures', data => data);

/**
* @method listMultisignatureTransactions
* @param callback
* @method getMultisignatureTransaction
* @param id
* @param optionsOrCallback
* @param callbackIfOptions
*
* @return API object
*/

LiskAPI.prototype.listMultisignatureTransactions = function listMultisignatureTransactions(
callback,
) {
this.sendRequest(GET, 'transactions/multisignatures', result => callback(result));
};
LiskAPI.prototype.getMultisignatureTransaction = privateApi.wrapSendRequest(GET, 'transactions/multisignatures/get', id => ({ id }));

/**
* @method getMultisignatureTransaction
* @param transactionId
* @method sendLSK
* @param recipientId
* @param amount
* @param secret
* @param secondSecret
* @param callback
*
* @return API object
*/

LiskAPI.prototype.getMultisignatureTransaction = function getMultisignatureTransaction(
transactionId, callback,
LiskAPI.prototype.sendLSK = function sendLSK(
recipientId, amount, secret, secondSecret, callback,
) {
this.sendRequest(GET, 'transactions/multisignatures/get', { id: transactionId }, result => callback(result));
return this.sendRequest(POST, 'transactions', { recipientId, amount, secret, secondSecret }, callback);
};

/**
* @method broadcastSignedTransaction
* @param transaction
* @param callback
*
* @return API object
*/

LiskAPI.prototype.broadcastSignedTransaction = function broadcastSignedTransaction(
transaction, callback,
) {
Expand Down
31 changes: 31 additions & 0 deletions src/api/privateApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,35 @@ function sendRequestPromise(requestMethod, requestType, options) {
return popsicle.request(requestObject).use(popsicle.plugins.parse(['json', 'urlencoded']));
}

/**
* @method constructRequestData
* @param providedObject
* @param optionsOrCallback
*
* @return request object
*/

const constructRequestData = (providedObject, optionsOrCallback) => {
const providedOptions = typeof optionsOrCallback !== 'function' && typeof optionsOrCallback !== 'undefined' ? optionsOrCallback : {};
return Object.assign({}, providedOptions, providedObject);
};

/**
* @method wrapSendRequest
* @param method
* @param endpoint
* @param getDataFn
*
* @return function wrappedSendRequest
*/

const wrapSendRequest = (method, endpoint, getDataFn) =>
function wrappedSendRequest(value, optionsOrCallback, callbackIfOptions) {
const callback = callbackIfOptions || optionsOrCallback;
const data = constructRequestData(getDataFn(value, optionsOrCallback), optionsOrCallback);
return this.sendRequest(method, endpoint, data, callback);
};

module.exports = {
netHashOptions,
getFullUrl,
Expand All @@ -239,4 +268,6 @@ module.exports = {
sendRequestPromise,
serialiseHttpData,
createRequestObject,
constructRequestData,
wrapSendRequest,
};
21 changes: 20 additions & 1 deletion src/transactions/dapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ import constants from '../constants';
import slots from '../time/slots';
import { prepareTransaction } from './utils';

const isInt = n => parseInt(n, 10) === n;

const validateOptions = ({ category, name, type, link }) => {
if (!isInt(category)) {
throw new Error('Dapp category must be an integer.');
}
if (typeof name !== 'string') {
throw new Error('Dapp name must be a string.');
}
if (!isInt(type)) {
throw new Error('Dapp type must be an integer.');
}
if (typeof link !== 'string') {
throw new Error('Dapp link must be a string.');
}
};

/**
* @method createDapp
* @param secret
Expand All @@ -31,7 +48,9 @@ import { prepareTransaction } from './utils';
* @return {Object}
*/

function createDapp(secret, secondSecret, options, timeOffset) {
function createDapp(secret, secondSecret, options = {}, timeOffset) {
validateOptions(options);

const keys = crypto.getKeys(secret);

const transaction = {
Expand Down
Loading

0 comments on commit b681ca3

Please sign in to comment.