diff --git a/package.json b/package.json index 89a90b869b623feecd7bdf7be9e3c29f0f0534e6..7db0a451a56ceeea1335e8f1217e96954c992202 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hiveio/hive-js", - "version": "1.0.0", + "version": "1.1.0-beta.0", "description": "Hive.js the JavaScript API for Hive blockchain", "main": "lib/index.js", "scripts": { @@ -42,7 +42,7 @@ "create-hash": "^1.1.2", "create-hmac": "^1.1.4", "cross-env": "^5.0.0", - "cross-fetch": "^1.1.1", + "cross-fetch": "^3.1.4", "debug": "^2.6.8", "detect-node": "^2.0.3", "ecurve": "^1.0.5", diff --git a/src/api/index.js b/src/api/index.js index b2e82552fdce45d066bc09c7c52632796ca30d85..675d9144f6498e195fc1f3f4a354ad5eac9b2a41 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -36,9 +36,14 @@ class Hive extends EventEmitter { const methodParams = method.params || []; this[`${methodName}With`] = (options, callback) => { + const params = {} + for (let i = 0; i < methodParams.length; i++) { + params[methodParams[i]] = options[methodParams[i]] + } return this.send(method.api, { method: method.method, - params: methodParams.map(param => options[param]) + paramsCondenserApi: methodParams.map(param => options[param]), + params }, callback); }; diff --git a/src/api/methods.js b/src/api/methods.js index 5c97f2e8fb12ec3c235a4d485cf9b86c79340b60..cc238170e04b5614d78a2f8961c0f4b3787f2638 100644 --- a/src/api/methods.js +++ b/src/api/methods.js @@ -104,14 +104,14 @@ export default [ "params": ["query"] }, { - "api": "database_api", + "api": "condenser_api", "method": "get_block_header", - "params": ["blockNum"] + "params": ["block_num"] }, { - "api": "database_api", + "api": "block_api", "method": "get_block", - "params": ["blockNum"] + "params": ["block_num"] }, { "api": "database_api", @@ -431,7 +431,7 @@ export default [ "params": ["blogAccount"] }, { - "api": "network_broadcast_api", + "api": "condenser_api", "method": "broadcast_transaction", "params": ["trx"] }, @@ -455,6 +455,11 @@ export default [ "method": "set_max_block_age", "params": ["maxBlockAge"] }, + { + "api": "transaction_status_api", + "method": "find_transaction", + "params": ["transaction_id", "expiration"] + }, { "api": "market_history_api", "method": "get_ticker", diff --git a/src/api/transports/http.js b/src/api/transports/http.js index cef8638b7f2be8368818d1948b73d9ca1b24b4e0..b5370108889cf6bf063368c4afde5c66e105b98a 100644 --- a/src/api/transports/http.js +++ b/src/api/transports/http.js @@ -53,29 +53,22 @@ export function jsonRpc(uri, {method, id, params, fetchMethod=fetch}) { export default class HttpTransport extends Transport { send(api, data, callback) { - if (this.options.useAppbaseApi) { + let params = data.params + if (this.options.useAppbaseApi && api !== 'transaction_status_api') { api = 'condenser_api'; } + if (api === 'condenser_api') { + params = data.paramsCondenserApi; + } debug('Steem::send', api, data); const id = data.id || this.id++; - let params = [api, data.method, data.params]; - //SPECIAL CODE - can be removed after all API node operators upgrade to get the updated get_account_history api call - // if (this.options.uri !== 'https://api.hive.blog' && data.method === 'get_account_history' && data.params.length >= 4) - // { - // //We are experimenting with a new version of get_account_history that can now take up to 5 params - // //but this is only deployed on api.hive.blog nodes, so if this particular request is going to a different - // //backend, just strip the extra parameters off the call to avoid breaking it. Once all API nodes have upgraded - // //this code can be removed. - // while (data.params.length > 3) - // data.params.pop(); - // params = [api, data.method, data.params]; - // } - //END SPECIAL CODE + const method = api + '.' + data.method; + const retriable = this.retriable(api, data); const fetchMethod = this.options.fetchMethod; if (retriable) { retriable.attempt((currentAttempt) => { - jsonRpc(this.options.uri, { method: 'call', id, params, fetchMethod }).then( + jsonRpc(this.options.uri, { method, id, params, fetchMethod }).then( res => { callback(null, res); }, err => { if (retriable.retry(err)) { @@ -86,7 +79,7 @@ export default class HttpTransport extends Transport { ); }); } else { - jsonRpc(this.options.uri, { method: 'call', id, params, fetchMethod }).then( + jsonRpc(this.options.uri, { method, id, params, fetchMethod }).then( res => { callback(null, res); }, err => { callback(err); } ); diff --git a/src/broadcast/index.js b/src/broadcast/index.js index 2035ac28ab5e6aa91a05422f54f03d07e720b6d0..a1aa605570f3b501be75cbd43741d5731d082010 100644 --- a/src/broadcast/index.js +++ b/src/broadcast/index.js @@ -5,7 +5,8 @@ import formatterFactory from '../formatter'; import hiveApi from '../api'; import hiveAuth from '../auth'; import { camelCase } from '../utils'; - +import { transaction as trxSerializer } from '../auth/serializer/src/operations' +import { hash } from '../auth/ecc'; var operations = require('./operations'); const config = require('../config') @@ -13,6 +14,16 @@ const debug = newDebug('hive:broadcast'); const noop = function() {} const formatter = formatterFactory(hiveApi); +function getTransactionStatus(trxId, expiration, time = 3000) { + return new Promise((resolve, reject) => { + setTimeout(() => { + hiveApi.findTransactionAsync(trxId, expiration).then(res => { + resolve(res) + }) + }, time) + }) +} + const hiveBroadcast = {}; // Base transaction logic ----------------------------------------------------- @@ -22,6 +33,7 @@ const hiveBroadcast = {}; */ hiveBroadcast.send = function hiveBroadcast$send(tx, privKeys, callback) { + let trxId const resultP = hiveBroadcast._prepareTransaction(tx) .then((transaction) => { if (config.get("address_prefix") === "TST") { @@ -32,6 +44,8 @@ hiveBroadcast.send = function hiveBroadcast$send(tx, privKeys, callback) { 'Signing transaction (transaction, transaction.operations)', transaction, transaction.operations ); + const buf = trxSerializer.toBuffer(transaction); + trxId = hash.sha256(buf).toString('hex').slice(0, 40); return Promise.join( transaction, hiveAuth.signTransaction(transaction, privKeys) @@ -42,10 +56,17 @@ hiveBroadcast.send = function hiveBroadcast$send(tx, privKeys, callback) { 'Broadcasting transaction (transaction, transaction.operations)', transaction, transaction.operations ); - return hiveApi.broadcastTransactionSynchronousAsync( + return hiveApi.broadcastTransactionAsync( signedTransaction ).then((result) => { - return Object.assign({}, result, signedTransaction); + const expiration = signedTransaction.expiration + return getTransactionStatus(trxId, expiration).then(res => { + const obj = { id: trxId, status: res.status } + if (res.block_num) { + obj.block_num = res.block_num + } + return Object.assign(obj, result, signedTransaction); + }) }); });