Skip to content

Commit eefe830

Browse files
author
Fabien
authored
Merge pull request #94 from steemit/add-setConfig-fn
Add set config fn
2 parents b7da542 + 5ab2c1c commit eefe830

10 files changed

Lines changed: 90 additions & 68 deletions

File tree

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
# Steem.js
22
Steem.js the JavaScript API for Steem blockchain
33

4-
## Documentation
5-
6-
- [Install](https://github.com/steemit/steem-js/tree/dev/doc#install)
7-
- [Browser](https://github.com/steemit/steem-js/tree/dev/doc#browser)
8-
- [Database API](https://github.com/steemit/steem-js/tree/dev/doc#api)
9-
- [WebSocket](https://github.com/steemit/steem-js/tree/dev/doc#websocket)
10-
- [Subscriptions](https://github.com/steemit/steem-js/tree/dev/doc#subscriptions)
11-
- [Tags](https://github.com/steemit/steem-js/tree/dev/doc#tags)
12-
- [Blocks and transactions](https://github.com/steemit/steem-js/tree/dev/doc#blocks-and-transactions)
13-
- [Globals](https://github.com/steemit/steem-js/tree/dev/doc#globals)
14-
- [Keys](https://github.com/steemit/steem-js/tree/dev/doc#keys)
15-
- [Accounts](https://github.com/steemit/steem-js/tree/dev/doc#accounts)
16-
- [Market](https://github.com/steemit/steem-js/tree/dev/doc#market)
17-
- [Authority / validation](https://github.com/steemit/steem-js/tree/dev/doc#authority--validation)
18-
- [Votes](https://github.com/steemit/steem-js/tree/dev/doc#votes)
19-
- [Content](https://github.com/steemit/steem-js/tree/dev/doc#content)
20-
- [Witnesses](https://github.com/steemit/steem-js/tree/dev/doc#witnesses)
21-
- [Login API](https://github.com/steemit/steem-js/tree/dev/doc#login)
22-
- [Follow API](https://github.com/steemit/steem-js/tree/dev/doc#follow-api)
23-
- [Broadcast API](https://github.com/steemit/steem-js/tree/dev/doc#broadcast-api)
24-
- [Broadcast](https://github.com/steemit/steem-js/tree/dev/doc#broadcast)
25-
- [Auth](https://github.com/steemit/steem-js/tree/dev/doc#auth)
4+
# Documentation
5+
6+
- [Install](https://github.com/steemit/steem-js/tree/master/doc#install)
7+
- [Browser](https://github.com/steemit/steem-js/tree/master/doc#browser)
8+
- [Config](https://github.com/steemit/steem-js/tree/master/doc#config)
9+
- [Database API](https://github.com/steemit/steem-js/tree/master/doc#api)
10+
- [Subscriptions](https://github.com/steemit/steem-js/tree/master/doc#subscriptions)
11+
- [Tags](https://github.com/steemit/steem-js/tree/master/doc#tags)
12+
- [Blocks and transactions](https://github.com/steemit/steem-js/tree/master/doc#blocks-and-transactions)
13+
- [Globals](https://github.com/steemit/steem-js/tree/master/doc#globals)
14+
- [Keys](https://github.com/steemit/steem-js/tree/master/doc#keys)
15+
- [Accounts](https://github.com/steemit/steem-js/tree/master/doc#accounts)
16+
- [Market](https://github.com/steemit/steem-js/tree/master/doc#market)
17+
- [Authority / validation](https://github.com/steemit/steem-js/tree/master/doc#authority--validation)
18+
- [Votes](https://github.com/steemit/steem-js/tree/master/doc#votes)
19+
- [Content](https://github.com/steemit/steem-js/tree/master/doc#content)
20+
- [Witnesses](https://github.com/steemit/steem-js/tree/master/doc#witnesses)
21+
- [Login API](https://github.com/steemit/steem-js/tree/master/doc#login)
22+
- [Follow API](https://github.com/steemit/steem-js/tree/master/doc#follow-api)
23+
- [Broadcast API](https://github.com/steemit/steem-js/tree/master/doc#broadcast-api)
24+
- [Broadcast](https://github.com/steemit/steem-js/tree/master/doc#broadcast)
25+
- [Auth](https://github.com/steemit/steem-js/tree/master/doc#auth)
26+
2627

2728
Here is full documentation:
28-
https://github.com/steemit/steem-js/tree/dev/doc
29+
https://github.com/steemit/steem-js/tree/master/doc
2930

3031
## Browser
3132
```html

doc/README.md

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# Documentation
22

3-
- [Install](https://github.com/steemit/steem-js/tree/dev/doc#install)
4-
- [Browser](https://github.com/steemit/steem-js/tree/dev/doc#browser)
5-
- [Database API](https://github.com/steemit/steem-js/tree/dev/doc#api)
6-
- [WebSocket](https://github.com/steemit/steem-js/tree/dev/doc#websocket)
7-
- [Subscriptions](https://github.com/steemit/steem-js/tree/dev/doc#subscriptions)
8-
- [Tags](https://github.com/steemit/steem-js/tree/dev/doc#tags)
9-
- [Blocks and transactions](https://github.com/steemit/steem-js/tree/dev/doc#blocks-and-transactions)
10-
- [Globals](https://github.com/steemit/steem-js/tree/dev/doc#globals)
11-
- [Keys](https://github.com/steemit/steem-js/tree/dev/doc#keys)
12-
- [Accounts](https://github.com/steemit/steem-js/tree/dev/doc#accounts)
13-
- [Market](https://github.com/steemit/steem-js/tree/dev/doc#market)
14-
- [Authority / validation](https://github.com/steemit/steem-js/tree/dev/doc#authority--validation)
15-
- [Votes](https://github.com/steemit/steem-js/tree/dev/doc#votes)
16-
- [Content](https://github.com/steemit/steem-js/tree/dev/doc#content)
17-
- [Witnesses](https://github.com/steemit/steem-js/tree/dev/doc#witnesses)
18-
- [Login API](https://github.com/steemit/steem-js/tree/dev/doc#login)
19-
- [Follow API](https://github.com/steemit/steem-js/tree/dev/doc#follow-api)
20-
- [Broadcast API](https://github.com/steemit/steem-js/tree/dev/doc#broadcast-api)
21-
- [Broadcast](https://github.com/steemit/steem-js/tree/dev/doc#broadcast)
22-
- [Auth](https://github.com/steemit/steem-js/tree/dev/doc#auth)
3+
- [Install](https://github.com/steemit/steem-js/tree/master/doc#install)
4+
- [Browser](https://github.com/steemit/steem-js/tree/master/doc#browser)
5+
- [Config](https://github.com/steemit/steem-js/tree/master/doc#config)
6+
- [Database API](https://github.com/steemit/steem-js/tree/master/doc#api)
7+
- [Subscriptions](https://github.com/steemit/steem-js/tree/master/doc#subscriptions)
8+
- [Tags](https://github.com/steemit/steem-js/tree/master/doc#tags)
9+
- [Blocks and transactions](https://github.com/steemit/steem-js/tree/master/doc#blocks-and-transactions)
10+
- [Globals](https://github.com/steemit/steem-js/tree/master/doc#globals)
11+
- [Keys](https://github.com/steemit/steem-js/tree/master/doc#keys)
12+
- [Accounts](https://github.com/steemit/steem-js/tree/master/doc#accounts)
13+
- [Market](https://github.com/steemit/steem-js/tree/master/doc#market)
14+
- [Authority / validation](https://github.com/steemit/steem-js/tree/master/doc#authority--validation)
15+
- [Votes](https://github.com/steemit/steem-js/tree/master/doc#votes)
16+
- [Content](https://github.com/steemit/steem-js/tree/master/doc#content)
17+
- [Witnesses](https://github.com/steemit/steem-js/tree/master/doc#witnesses)
18+
- [Login API](https://github.com/steemit/steem-js/tree/master/doc#login)
19+
- [Follow API](https://github.com/steemit/steem-js/tree/master/doc#follow-api)
20+
- [Broadcast API](https://github.com/steemit/steem-js/tree/master/doc#broadcast-api)
21+
- [Broadcast](https://github.com/steemit/steem-js/tree/master/doc#broadcast)
22+
- [Auth](https://github.com/steemit/steem-js/tree/master/doc#auth)
2323

2424
# Install
2525
```
@@ -36,14 +36,24 @@ steem.api.getAccounts(['ned', 'dan'], function(err, response){
3636
</script>
3737
```
3838

39-
# API
40-
41-
## WebSocket
42-
43-
### Set WebSocket
39+
## Config
40+
Default config should work with steem. however you can change it to work with golos
41+
as
42+
```js
43+
steem.config.set('websocket','wss://ws.golos.io'); // assuming websocket is work at ws.golos.io
44+
steem.config.set('address_prefix','GLS');
45+
steem.config.set('chain_id','782a3039b478c839e4cb0c941ff4eaeb7df40bdd68bd441afd444b9da763de12');
46+
```
47+
### set
4448
```
45-
steem.api.setWebSocket(url);
49+
steem.config.set('address_prefix','STM');
4650
```
51+
### get
52+
```
53+
steem.config.get('chain_id');
54+
```
55+
56+
# API
4757

4858
## Subscriptions
4959

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ module.exports = {
33
auth: require('./lib/auth'),
44
broadcast: require('./lib/broadcast'),
55
formatter: require('./lib/formatter'),
6+
config: require('./lib/config')
67
};

src/api/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import cloneDeep from 'lodash/cloneDeep';
44
import defaults from 'lodash/defaults';
55
import isNode from 'detect-node';
66
import newDebug from 'debug';
7-
import config from '../../config.json';
7+
import config from '../config';
88
import methods from './methods';
99
import { camelCase } from '../util';
1010

@@ -24,7 +24,7 @@ if (isNode) {
2424
}
2525

2626
const DEFAULTS = {
27-
url: config.websocket,
27+
url: config.get('websocket'),
2828
apiIds: {
2929
database_api: 0,
3030
login_api: 1,
@@ -52,6 +52,7 @@ class Steem extends EventEmitter {
5252
}
5353

5454
setWebSocket(url) {
55+
console.warn("steem.api.setWebSocket(url) is now deprecated instead use steem.config.set('websocket',url)");
5556
debugSetup('Setting WS', url);
5657
this.options.url = url;
5758
this.stop();
@@ -226,7 +227,7 @@ class Steem extends EventEmitter {
226227
const err = new Error(
227228
// eslint-disable-next-line prefer-template
228229
(errorCause.message || 'Failed to complete operation') +
229-
' (see err.payload for the full error payload)'
230+
' (see err.payload for the full error payload)'
230231
);
231232
err.payload = message;
232233
reject(err);
@@ -236,7 +237,7 @@ class Steem extends EventEmitter {
236237
if (api === 'login_api' && data.method === 'login') {
237238
debugApiIds(
238239
'network_broadcast_api API ID depends on the WS\' session. ' +
239-
'Triggering a refresh...'
240+
'Triggering a refresh...'
240241
);
241242
this.getApiIds('network_broadcast_api', true);
242243
}

src/auth/ecc/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ module.exports = {
88
brainKey: require("./src/brain_key"),
99
key_utils: require("./src/key_utils"),
1010
hash: require("./src/hash"),
11-
ecc_config: require("../../../config.json")
11+
ecc_config: require("../../config")
1212
}

src/auth/ecc/src/address.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const assert = require('assert');
2-
const config = require('../../../../config.json');
2+
const config = require('../../../config');
33
const hash = require('./hash');
44
const base58 = require('bs58');
55

@@ -16,7 +16,7 @@ class Address {
1616
return new Address(addy);
1717
}
1818

19-
static fromString(string, address_prefix = config.address_prefix) {
19+
static fromString(string, address_prefix = config.get('address_prefix')) {
2020
const prefix = string.slice(0, address_prefix.length);
2121
assert.equal(address_prefix, prefix, `Expecting key to begin with ${address_prefix}, instead got ${prefix}`);
2222
let addy = string.slice(address_prefix.length);
@@ -46,7 +46,7 @@ class Address {
4646
return this.addy;
4747
}
4848

49-
toString(address_prefix = config.address_prefix) {
49+
toString(address_prefix = config.get('address_prefix')) {
5050
const checksum = hash.ripemd160(this.addy);
5151
const addy = Buffer.concat([this.addy, checksum.slice(0, 4)]);
5252
return address_prefix + base58.encode(addy);

src/auth/ecc/src/key_public.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var secp256k1 = ecurve.getCurveByName('secp256k1');
44
BigInteger = require('bigi');
55
var base58 = require('bs58');
66
var hash = require('./hash');
7-
var config = require('../../../../config.json');
7+
var config = require('../../../config');
88
var assert = require('assert');
99

1010
var G = secp256k1.G
@@ -44,15 +44,15 @@ class PublicKey {
4444
return hash.ripemd160(pub_sha);
4545
}
4646

47-
toString(address_prefix = config.address_prefix) {
47+
toString(address_prefix = config.get('address_prefix')) {
4848
return this.toPublicKeyString(address_prefix)
4949
}
5050

5151
/**
5252
Full public key
5353
{return} string
5454
*/
55-
toPublicKeyString(address_prefix = config.address_prefix) {
55+
toPublicKeyString(address_prefix = config.get('address_prefix')) {
5656
if(this.pubdata) return address_prefix + this.pubdata
5757
const pub_buf = this.toBuffer();
5858
const checksum = hash.ripemd160(pub_buf);
@@ -67,7 +67,7 @@ class PublicKey {
6767
@return PublicKey or `null` (if the public_key string is invalid)
6868
@deprecated fromPublicKeyString (use fromString instead)
6969
*/
70-
static fromString(public_key, address_prefix = config.address_prefix) {
70+
static fromString(public_key, address_prefix = config.get('address_prefix')) {
7171
try {
7272
return PublicKey.fromStringOrThrow(public_key, address_prefix)
7373
} catch (e) {
@@ -81,7 +81,7 @@ class PublicKey {
8181
@throws {Error} if public key is invalid
8282
@return PublicKey
8383
*/
84-
static fromStringOrThrow(public_key, address_prefix = config.address_prefix) {
84+
static fromStringOrThrow(public_key, address_prefix = config.get('address_prefix')) {
8585
var prefix = public_key.slice(0, address_prefix.length);
8686
assert.equal(
8787
address_prefix, prefix,
@@ -97,7 +97,7 @@ class PublicKey {
9797
return PublicKey.fromBuffer(public_key);
9898
}
9999

100-
toAddressString(address_prefix = config.address_prefix) {
100+
toAddressString(address_prefix = config.get('address_prefix')) {
101101
var pub_buf = this.toBuffer();
102102
var pub_sha = hash.sha512(pub_buf);
103103
var addy = hash.ripemd160(pub_sha);

src/auth/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var bigi = require('bigi'),
44
ecurve = require('ecurve'),
55
Point = ecurve.Point,
66
secp256k1 = ecurve.getCurveByName('secp256k1'),
7-
config = require('../../config.json'),
7+
config = require('../config'),
88
operations = require('./serializer/src/operations'),
99
Signature = require('./ecc/src/signature'),
1010
KeyPrivate = require('./ecc/src/key_private');
@@ -40,7 +40,7 @@ Auth.generateKeys = function (name, password, roles) {
4040
var pubBuf = point.getEncoded(toPubKey.compressed);
4141
var checksum = crypto.createHash('rmd160').update(pubBuf).digest();
4242
var addy = Buffer.concat([pubBuf, checksum.slice(0, 4)]);
43-
pubKeys[role] = config.address_prefix + bs58.encode(addy);
43+
pubKeys[role] = config.get('address_prefix') + bs58.encode(addy);
4444
});
4545
return pubKeys;
4646
};
@@ -97,7 +97,7 @@ Auth.signTransaction = function (trx, keys) {
9797
signatures = [].concat(trx.signatures);
9898
}
9999

100-
var cid = new Buffer(config.chain_id, 'hex');
100+
var cid = new Buffer(config.get('chain_id'), 'hex');
101101
var buf = transaction.toBuffer(trx);
102102

103103
for (var key in keys) {

src/auth/serializer/src/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ Types.public_key = {
888888
},
889889
toObject(object, debug = {}){
890890
if (debug.use_default && object === undefined) {
891-
return ecc_config.address_prefix + "859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVM";
891+
return ecc_config.get('address_prefix') + "859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVM";
892892
}
893893
v.required(object);
894894
return object.toString()
@@ -917,7 +917,7 @@ Types.address =
917917
},
918918
toObject(object, debug = {}){
919919
if (debug.use_default && object === undefined) {
920-
return ecc_config.address_prefix + "664KmHxSuQyDsfwo4WEJvWpzg1QKdg67S";
920+
return ecc_config.get('address_prefix') + "664KmHxSuQyDsfwo4WEJvWpzg1QKdg67S";
921921
}
922922
return Types.address._to_address(object).toString();
923923
},

src/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const defaultConfig = require('../config.json');
2+
module.exports = (function () {
3+
const config = defaultConfig;
4+
const get = (key) => config[key];
5+
const set = (key, value) => {
6+
config[key] = value;
7+
}
8+
return { get, set };
9+
})()

0 commit comments

Comments
 (0)