Commit eefe830b authored by Fabien's avatar Fabien Committed by GitHub

Merge pull request #94 from steemit/add-setConfig-fn

Add set config fn
parents b7da542b 5ab2c1ce
# Steem.js
Steem.js the JavaScript API for Steem blockchain
## Documentation
- [Install](https://github.com/steemit/steem-js/tree/dev/doc#install)
- [Browser](https://github.com/steemit/steem-js/tree/dev/doc#browser)
- [Database API](https://github.com/steemit/steem-js/tree/dev/doc#api)
- [WebSocket](https://github.com/steemit/steem-js/tree/dev/doc#websocket)
- [Subscriptions](https://github.com/steemit/steem-js/tree/dev/doc#subscriptions)
- [Tags](https://github.com/steemit/steem-js/tree/dev/doc#tags)
- [Blocks and transactions](https://github.com/steemit/steem-js/tree/dev/doc#blocks-and-transactions)
- [Globals](https://github.com/steemit/steem-js/tree/dev/doc#globals)
- [Keys](https://github.com/steemit/steem-js/tree/dev/doc#keys)
- [Accounts](https://github.com/steemit/steem-js/tree/dev/doc#accounts)
- [Market](https://github.com/steemit/steem-js/tree/dev/doc#market)
- [Authority / validation](https://github.com/steemit/steem-js/tree/dev/doc#authority--validation)
- [Votes](https://github.com/steemit/steem-js/tree/dev/doc#votes)
- [Content](https://github.com/steemit/steem-js/tree/dev/doc#content)
- [Witnesses](https://github.com/steemit/steem-js/tree/dev/doc#witnesses)
- [Login API](https://github.com/steemit/steem-js/tree/dev/doc#login)
- [Follow API](https://github.com/steemit/steem-js/tree/dev/doc#follow-api)
- [Broadcast API](https://github.com/steemit/steem-js/tree/dev/doc#broadcast-api)
- [Broadcast](https://github.com/steemit/steem-js/tree/dev/doc#broadcast)
- [Auth](https://github.com/steemit/steem-js/tree/dev/doc#auth)
# Documentation
- [Install](https://github.com/steemit/steem-js/tree/master/doc#install)
- [Browser](https://github.com/steemit/steem-js/tree/master/doc#browser)
- [Config](https://github.com/steemit/steem-js/tree/master/doc#config)
- [Database API](https://github.com/steemit/steem-js/tree/master/doc#api)
- [Subscriptions](https://github.com/steemit/steem-js/tree/master/doc#subscriptions)
- [Tags](https://github.com/steemit/steem-js/tree/master/doc#tags)
- [Blocks and transactions](https://github.com/steemit/steem-js/tree/master/doc#blocks-and-transactions)
- [Globals](https://github.com/steemit/steem-js/tree/master/doc#globals)
- [Keys](https://github.com/steemit/steem-js/tree/master/doc#keys)
- [Accounts](https://github.com/steemit/steem-js/tree/master/doc#accounts)
- [Market](https://github.com/steemit/steem-js/tree/master/doc#market)
- [Authority / validation](https://github.com/steemit/steem-js/tree/master/doc#authority--validation)
- [Votes](https://github.com/steemit/steem-js/tree/master/doc#votes)
- [Content](https://github.com/steemit/steem-js/tree/master/doc#content)
- [Witnesses](https://github.com/steemit/steem-js/tree/master/doc#witnesses)
- [Login API](https://github.com/steemit/steem-js/tree/master/doc#login)
- [Follow API](https://github.com/steemit/steem-js/tree/master/doc#follow-api)
- [Broadcast API](https://github.com/steemit/steem-js/tree/master/doc#broadcast-api)
- [Broadcast](https://github.com/steemit/steem-js/tree/master/doc#broadcast)
- [Auth](https://github.com/steemit/steem-js/tree/master/doc#auth)
Here is full documentation:
https://github.com/steemit/steem-js/tree/dev/doc
https://github.com/steemit/steem-js/tree/master/doc
## Browser
```html
......
# Documentation
- [Install](https://github.com/steemit/steem-js/tree/dev/doc#install)
- [Browser](https://github.com/steemit/steem-js/tree/dev/doc#browser)
- [Database API](https://github.com/steemit/steem-js/tree/dev/doc#api)
- [WebSocket](https://github.com/steemit/steem-js/tree/dev/doc#websocket)
- [Subscriptions](https://github.com/steemit/steem-js/tree/dev/doc#subscriptions)
- [Tags](https://github.com/steemit/steem-js/tree/dev/doc#tags)
- [Blocks and transactions](https://github.com/steemit/steem-js/tree/dev/doc#blocks-and-transactions)
- [Globals](https://github.com/steemit/steem-js/tree/dev/doc#globals)
- [Keys](https://github.com/steemit/steem-js/tree/dev/doc#keys)
- [Accounts](https://github.com/steemit/steem-js/tree/dev/doc#accounts)
- [Market](https://github.com/steemit/steem-js/tree/dev/doc#market)
- [Authority / validation](https://github.com/steemit/steem-js/tree/dev/doc#authority--validation)
- [Votes](https://github.com/steemit/steem-js/tree/dev/doc#votes)
- [Content](https://github.com/steemit/steem-js/tree/dev/doc#content)
- [Witnesses](https://github.com/steemit/steem-js/tree/dev/doc#witnesses)
- [Login API](https://github.com/steemit/steem-js/tree/dev/doc#login)
- [Follow API](https://github.com/steemit/steem-js/tree/dev/doc#follow-api)
- [Broadcast API](https://github.com/steemit/steem-js/tree/dev/doc#broadcast-api)
- [Broadcast](https://github.com/steemit/steem-js/tree/dev/doc#broadcast)
- [Auth](https://github.com/steemit/steem-js/tree/dev/doc#auth)
- [Install](https://github.com/steemit/steem-js/tree/master/doc#install)
- [Browser](https://github.com/steemit/steem-js/tree/master/doc#browser)
- [Config](https://github.com/steemit/steem-js/tree/master/doc#config)
- [Database API](https://github.com/steemit/steem-js/tree/master/doc#api)
- [Subscriptions](https://github.com/steemit/steem-js/tree/master/doc#subscriptions)
- [Tags](https://github.com/steemit/steem-js/tree/master/doc#tags)
- [Blocks and transactions](https://github.com/steemit/steem-js/tree/master/doc#blocks-and-transactions)
- [Globals](https://github.com/steemit/steem-js/tree/master/doc#globals)
- [Keys](https://github.com/steemit/steem-js/tree/master/doc#keys)
- [Accounts](https://github.com/steemit/steem-js/tree/master/doc#accounts)
- [Market](https://github.com/steemit/steem-js/tree/master/doc#market)
- [Authority / validation](https://github.com/steemit/steem-js/tree/master/doc#authority--validation)
- [Votes](https://github.com/steemit/steem-js/tree/master/doc#votes)
- [Content](https://github.com/steemit/steem-js/tree/master/doc#content)
- [Witnesses](https://github.com/steemit/steem-js/tree/master/doc#witnesses)
- [Login API](https://github.com/steemit/steem-js/tree/master/doc#login)
- [Follow API](https://github.com/steemit/steem-js/tree/master/doc#follow-api)
- [Broadcast API](https://github.com/steemit/steem-js/tree/master/doc#broadcast-api)
- [Broadcast](https://github.com/steemit/steem-js/tree/master/doc#broadcast)
- [Auth](https://github.com/steemit/steem-js/tree/master/doc#auth)
# Install
```
......@@ -36,14 +36,24 @@ steem.api.getAccounts(['ned', 'dan'], function(err, response){
</script>
```
# API
## WebSocket
### Set WebSocket
## Config
Default config should work with steem. however you can change it to work with golos
as
```js
steem.config.set('websocket','wss://ws.golos.io'); // assuming websocket is work at ws.golos.io
steem.config.set('address_prefix','GLS');
steem.config.set('chain_id','782a3039b478c839e4cb0c941ff4eaeb7df40bdd68bd441afd444b9da763de12');
```
### set
```
steem.api.setWebSocket(url);
steem.config.set('address_prefix','STM');
```
### get
```
steem.config.get('chain_id');
```
# API
## Subscriptions
......
......@@ -3,4 +3,5 @@ module.exports = {
auth: require('./lib/auth'),
broadcast: require('./lib/broadcast'),
formatter: require('./lib/formatter'),
config: require('./lib/config')
};
......@@ -4,7 +4,7 @@ import cloneDeep from 'lodash/cloneDeep';
import defaults from 'lodash/defaults';
import isNode from 'detect-node';
import newDebug from 'debug';
import config from '../../config.json';
import config from '../config';
import methods from './methods';
import { camelCase } from '../util';
......@@ -24,7 +24,7 @@ if (isNode) {
}
const DEFAULTS = {
url: config.websocket,
url: config.get('websocket'),
apiIds: {
database_api: 0,
login_api: 1,
......@@ -52,6 +52,7 @@ class Steem extends EventEmitter {
}
setWebSocket(url) {
console.warn("steem.api.setWebSocket(url) is now deprecated instead use steem.config.set('websocket',url)");
debugSetup('Setting WS', url);
this.options.url = url;
this.stop();
......@@ -226,7 +227,7 @@ class Steem extends EventEmitter {
const err = new Error(
// eslint-disable-next-line prefer-template
(errorCause.message || 'Failed to complete operation') +
' (see err.payload for the full error payload)'
' (see err.payload for the full error payload)'
);
err.payload = message;
reject(err);
......@@ -236,7 +237,7 @@ class Steem extends EventEmitter {
if (api === 'login_api' && data.method === 'login') {
debugApiIds(
'network_broadcast_api API ID depends on the WS\' session. ' +
'Triggering a refresh...'
'Triggering a refresh...'
);
this.getApiIds('network_broadcast_api', true);
}
......
......@@ -8,5 +8,5 @@ module.exports = {
brainKey: require("./src/brain_key"),
key_utils: require("./src/key_utils"),
hash: require("./src/hash"),
ecc_config: require("../../../config.json")
ecc_config: require("../../config")
}
const assert = require('assert');
const config = require('../../../../config.json');
const config = require('../../../config');
const hash = require('./hash');
const base58 = require('bs58');
......@@ -16,7 +16,7 @@ class Address {
return new Address(addy);
}
static fromString(string, address_prefix = config.address_prefix) {
static fromString(string, address_prefix = config.get('address_prefix')) {
const prefix = string.slice(0, address_prefix.length);
assert.equal(address_prefix, prefix, `Expecting key to begin with ${address_prefix}, instead got ${prefix}`);
let addy = string.slice(address_prefix.length);
......@@ -46,7 +46,7 @@ class Address {
return this.addy;
}
toString(address_prefix = config.address_prefix) {
toString(address_prefix = config.get('address_prefix')) {
const checksum = hash.ripemd160(this.addy);
const addy = Buffer.concat([this.addy, checksum.slice(0, 4)]);
return address_prefix + base58.encode(addy);
......
......@@ -4,7 +4,7 @@ var secp256k1 = ecurve.getCurveByName('secp256k1');
BigInteger = require('bigi');
var base58 = require('bs58');
var hash = require('./hash');
var config = require('../../../../config.json');
var config = require('../../../config');
var assert = require('assert');
var G = secp256k1.G
......@@ -44,7 +44,7 @@ class PublicKey {
return hash.ripemd160(pub_sha);
}
toString(address_prefix = config.address_prefix) {
toString(address_prefix = config.get('address_prefix')) {
return this.toPublicKeyString(address_prefix)
}
......@@ -52,7 +52,7 @@ class PublicKey {
Full public key
{return} string
*/
toPublicKeyString(address_prefix = config.address_prefix) {
toPublicKeyString(address_prefix = config.get('address_prefix')) {
if(this.pubdata) return address_prefix + this.pubdata
const pub_buf = this.toBuffer();
const checksum = hash.ripemd160(pub_buf);
......@@ -67,7 +67,7 @@ class PublicKey {
@return PublicKey or `null` (if the public_key string is invalid)
@deprecated fromPublicKeyString (use fromString instead)
*/
static fromString(public_key, address_prefix = config.address_prefix) {
static fromString(public_key, address_prefix = config.get('address_prefix')) {
try {
return PublicKey.fromStringOrThrow(public_key, address_prefix)
} catch (e) {
......@@ -81,7 +81,7 @@ class PublicKey {
@throws {Error} if public key is invalid
@return PublicKey
*/
static fromStringOrThrow(public_key, address_prefix = config.address_prefix) {
static fromStringOrThrow(public_key, address_prefix = config.get('address_prefix')) {
var prefix = public_key.slice(0, address_prefix.length);
assert.equal(
address_prefix, prefix,
......@@ -97,7 +97,7 @@ class PublicKey {
return PublicKey.fromBuffer(public_key);
}
toAddressString(address_prefix = config.address_prefix) {
toAddressString(address_prefix = config.get('address_prefix')) {
var pub_buf = this.toBuffer();
var pub_sha = hash.sha512(pub_buf);
var addy = hash.ripemd160(pub_sha);
......
......@@ -4,7 +4,7 @@ var bigi = require('bigi'),
ecurve = require('ecurve'),
Point = ecurve.Point,
secp256k1 = ecurve.getCurveByName('secp256k1'),
config = require('../../config.json'),
config = require('../config'),
operations = require('./serializer/src/operations'),
Signature = require('./ecc/src/signature'),
KeyPrivate = require('./ecc/src/key_private');
......@@ -40,7 +40,7 @@ Auth.generateKeys = function (name, password, roles) {
var pubBuf = point.getEncoded(toPubKey.compressed);
var checksum = crypto.createHash('rmd160').update(pubBuf).digest();
var addy = Buffer.concat([pubBuf, checksum.slice(0, 4)]);
pubKeys[role] = config.address_prefix + bs58.encode(addy);
pubKeys[role] = config.get('address_prefix') + bs58.encode(addy);
});
return pubKeys;
};
......@@ -97,7 +97,7 @@ Auth.signTransaction = function (trx, keys) {
signatures = [].concat(trx.signatures);
}
var cid = new Buffer(config.chain_id, 'hex');
var cid = new Buffer(config.get('chain_id'), 'hex');
var buf = transaction.toBuffer(trx);
for (var key in keys) {
......
......@@ -888,7 +888,7 @@ Types.public_key = {
},
toObject(object, debug = {}){
if (debug.use_default && object === undefined) {
return ecc_config.address_prefix + "859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVM";
return ecc_config.get('address_prefix') + "859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVM";
}
v.required(object);
return object.toString()
......@@ -917,7 +917,7 @@ Types.address =
},
toObject(object, debug = {}){
if (debug.use_default && object === undefined) {
return ecc_config.address_prefix + "664KmHxSuQyDsfwo4WEJvWpzg1QKdg67S";
return ecc_config.get('address_prefix') + "664KmHxSuQyDsfwo4WEJvWpzg1QKdg67S";
}
return Types.address._to_address(object).toString();
},
......
const defaultConfig = require('../config.json');
module.exports = (function () {
const config = defaultConfig;
const get = (key) => config[key];
const set = (key, value) => {
config[key] = value;
}
return { get, set };
})()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment