Commit 0acae2fa authored by Jason Salyers's avatar Jason Salyers
Browse files

[JES] Doing a test of a new get_account_history call. Merging to develop so we...

[JES] Doing a test of a new get_account_history call. Merging to develop so we can push to staging, but this code IS NOT READY FOR MASTER
parent f9296bc4
MIT License
Copyright (c) 2017 Hive.js
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://gitlab.syncad.com/hive/hive-js/blob/master/LICENSE)
# Hive.js
Hive.js the Official JavaScript API for Hive blockchain
# Documentation
- [Install](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#install)
- [Browser](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#browser)
- [Config](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#config)
- [Database API](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#api)
- [Subscriptions](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#subscriptions)
- [Tags](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#tags)
- [Blocks and transactions](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#blocks-and-transactions)
- [Globals](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#globals)
- [Keys](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#keys)
- [Accounts](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#accounts)
- [Market](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#market)
- [Authority / validation](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#authority--validation)
- [Votes](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#votes)
- [Content](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#content)
- [Witnesses](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#witnesses)
- [Login API](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#login)
- [Follow API](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#follow-api)
- [Broadcast API](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#broadcast-api)
- [Broadcast](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#broadcast)
- [Auth](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#auth)
- [Formatter](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#formatter)
- [Utils](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#utils)
- [Tutorials](https://gitlab.syncad.com/hive/hive-js/tree/master/doc#tutorials)
Here is full documentation:
https://gitlab.syncad.com/hive/hive-js/tree/master/doc
## Browser
```html
<script src="./hive.min.js"></script>
<script>
hive.api.getAccounts(['mahdiyari', 'hiveio'], function(err, response){
console.log(err, response);
});
</script>
```
## CDN
https://cdn.jsdelivr.net/npm/@hiveio/hive-js/dist/hive.min.js<br/>
```html
<script src="https://cdn.jsdelivr.net/npm/@hiveio/hive-js/dist/hive.min.js"></script>
```
## Webpack
[Please have a look at the webpack usage example.](https://gitlab.syncad.com/hive/hive-js/blob/master/examples/webpack-example)
## Server
## Install
```
$ npm install @hiveio/hive-js --save
```
## RPC Servers
https://api.hive.blog By Default<br/>
https://anyx.io<br/>
https://api.openhive.network<br/>
https://api.hivekings.com<br/>
https://api.pharesim.me<br/>
https://hived.privex.io<br/>
https://rpc.ausbit.dev<br/>
https://rpc.esteem.app<br/>
<sub>[List of Hive nodes](https://hivekings.com/nodes)</sub><br/>
## Examples
### Broadcast Vote
```js
var hive = require('@hiveio/hive-js');
var wif = hive.auth.toWif(username, password, 'posting');
hive.broadcast.vote(wif, voter, author, permlink, weight, function(err, result) {
console.log(err, result);
});
```
### Get Accounts
```js
hive.api.getAccounts(['mahdiyari', 'hiveio'], function(err, result) {
console.log(err, result);
});
```
### Get State
```js
hive.api.getState('/trends/funny', function(err, result) {
console.log(err, result);
});
```
### Reputation Formatter
```js
var reputation = hive.formatter.reputation(user.reputation);
console.log(reputation);
```
### Hive Testnet
Hive-js requires some configuration to work on the public Hive testnet.
You need to set two Hive API options, `address_prefix` and `chain_id`.
```js
hive.api.setOptions({
address_prefix: 'TST',
chain_id: '46d82ab7d8db682eb1959aed0ada039a6d49afa1602491f93dde9cac3e8e6c32',
useTestNet: true,
});
```
The Chain ID could change. If it does, it may not be reflected here, but will be documented on any testnet launch announcements.
## Contributions
Patches are welcome! Contributors are listed in the package.json file. Please run the tests before opening a pull request and make sure that you are passing all of them.
## Issues
When you find issues, please report them!
## License
MIT
machine:
node:
version: 6
{
"transport": "http",
"websocket": "wss://api.hive.blog:8090",
"uri": "https://api.hive.blog",
"url": "https://api.hive.blog",
"dev_uri": "",
"stage_uri": "",
"address_prefix": "STM",
"chain_id": "beeab0de00000000000000000000000000000000000000000000000000000000",
"alternative_api_endpoints": ["https://api.hive.blog", "https://anyx.io"],
"failover_threshold": 3,
"rebranded_api": true
}
FROM node:4
ADD ./package.json /steemjs/package.json
WORKDIR /steemjs
RUN npm install
ADD . /steemjs
RUN npm test
FROM node:6
ADD ./package.json /steemjs/package.json
WORKDIR /steemjs
RUN npm install
ADD . /steemjs
RUN npm test
{
"name": "@hiveio/hive-js",
"version": "0.8.5",
"description": "Hive.js the JavaScript API for Hive blockchain",
"main": "lib/index.js",
"scripts": {
"test": "eslint --quiet src test; mocha -t 40000 --require babel-polyfill --require babel-register",
"test-auth": "npm test -- --grep 'steem.auth'",
"build": "npm run build-browser && npm run build-node",
"build-browser": "rm -rf dist && NODE_ENV=production node ./node_modules/webpack/bin/webpack.js && gzip -k -f ./dist/*.js && du -h ./dist/*",
"build-node": "mkdir -p ./lib && cp -r ./src/* ./lib/ && babel ./src --out-dir ./lib",
"prepublish": "npm run build"
},
"browser": {
"ws": false,
"crypto": false
},
"repository": {
"type": "git",
"url": "git+https://gitlab.syncad.com/hive/hive-js.git"
},
"keywords": [
"hive",
"hiveblog",
"blockchain",
"hivejs"
],
"author": "Fabien (https://github.com/bonustrack)",
"license": "MIT",
"bugs": {
"url": "https://gitlab.syncad.com/hive/hive-js/issues"
},
"homepage": "https://gitlab.syncad.com/hive/hive-js#readme",
"dependencies": {
"@steemit/rpc-auth": "^1.1.1",
"bigi": "^1.4.2",
"bluebird": "^3.4.6",
"browserify-aes": "^1.0.6",
"bs58": "^4.0.0",
"buffer": "^5.0.6",
"bytebuffer": "^5.0.1",
"create-hash": "^1.1.2",
"create-hmac": "^1.1.4",
"cross-env": "^5.0.0",
"cross-fetch": "^1.1.1",
"debug": "^2.6.8",
"detect-node": "^2.0.3",
"ecurve": "^1.0.5",
"lodash": "^4.16.4",
"retry": "^0.12.0",
"secure-random": "^1.1.2",
"ws": "^3.3.2"
},
"devDependencies": {
"babel-cli": "^6.16.0",
"babel-eslint": "^7.1.1",
"babel-loader": "^6.2.5",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-es2017": "^6.16.0",
"babel-register": "^6.14.0",
"bluebird": "^3.4.6",
"eslint": "^3.5.0",
"eslint-plugin-import": "^1.15.0",
"eslint-plugin-jsx-a11y": "^2.2.2",
"eslint-plugin-react": "^6.2.1",
"json-loader": "^0.5.4",
"mocha": "^3.0.2",
"mocha-make-stub": "^2.3.2",
"should": "^11.1.0",
"webpack": "^1.13.2",
"webpack-visualizer-plugin": "^0.1.5"
},
"contributors": [
"Fabien (https://github.com/bonustrack)",
"James Calfee (https://github.com/jcalfee)",
"Nilesh Suthar (https://github.com/nil1511)",
"Pedro Tacla Yamada (https://github.com/yamadapc)",
"MahdiYari (https://github.com/mahdiyari)"
],
"directories": {
"doc": "doc",
"example": "examples",
"lib": "lib",
"test": "test"
}
}
import config from '../src/config';
import { Aes, PrivateKey, PublicKey, Signature } from '../src/auth/ecc';
import assert from 'assert';
var secureRandom = require('secure-random');
var hash = require('../src/auth/ecc/src/hash');
var key = require('../src/auth/ecc/src/key_utils');
describe('steem.auth: Crypto', function () {
/*it "Computes public key", ->
private_key = PrivateKey.fromHex decrypted_key.substring 0, 64
public_key = private_key.toPublicKey()
console.log public_key.toHex());*/
it('sign', function () {
this.timeout(10000);
var private_key = PrivateKey.fromSeed('1');
return (() => {
var result = [];
for (var i = 0; i < 10; i++) {
result.push(Signature.signBuffer(new Buffer(i), private_key));
}
return result;
})();
});
});
describe('steem.auth: derives', () => {
let prefix = config.get('address_prefix');
let one_time_private = PrivateKey.fromHex(
'8fdfdde486f696fd7c6313325e14d3ff0c34b6e2c390d1944cbfe150f4457168'
);
let to_public = PublicKey.fromStringOrThrow(
prefix + '7vbxtK1WaZqXsiCHPcjVFBewVj8HFRd5Z5XZDpN6Pvb2dZcMqK'
);
let secret = one_time_private.get_shared_secret(to_public);
let child = hash.sha256(secret);
// Check everything above with `wdump((child));` from the witness_node:
assert.equal(
child.toString('hex'),
'1f296fa48172d9af63ef3fb6da8e369e6cc33c1fb7c164207a3549b39e8ef698'
);
let nonce = hash.sha256(one_time_private.toBuffer());
assert.equal(
nonce.toString('hex'),
'462f6c19ece033b5a3dba09f1e1d7935a5302e4d1eac0a84489cdc8339233fbf'
);
it('child from public', () =>
assert.equal(
to_public.child(child).toString(),
'STM6XA72XARQCain961PCJnXiKYdEMrndNGago2PV5bcUiVyzJ6iL',
'derive child public key'
));
// child = hash.sha256( one_time_private.get_secret( to_public ))
it('child from private', () =>
assert.equal(
PrivateKey.fromSeed('alice-brain-key')
.child(child)
.toPublicKey()
.toString(),
'STM6XA72XARQCain961PCJnXiKYdEMrndNGago2PV5bcUiVyzJ6iL',
'derive child from private key'
));
// "many keys" works, not really needed
// it("many keys", function() {
// this.timeout(10 * 1000)
// for (var i = 0; i < 10; i++) {
// let privkey1 = key.get_random_key()
// let privkey2 = key.get_random_key()
// let secret1 = one_time_private.get_shared_secret( privkey1.toPublicKey() )
// let child1 = hash.sha256( secret1 )
// let secret2 = privkey2.get_shared_secret( privkey2.toPublicKey() )
// let child2 = hash.sha256( secret2 )
// it("child from public", ()=> assert.equal(
// privkey1.toPublicKey().child(child1).toString(),
// privkey2.toPublicKey().child(child2).toString(),
// "derive child public key"
// ))
// it("child from private", ()=> assert.equal(
// privkey1.child(child1).toString(),
// privkey2.child(child2).toString(),
// "derive child private key"
// ))
// }
// })
});
var min_time_elapsed = function (f) {
var start_t = Date.now();
var ret = f();
var elapsed = Date.now() - start_t;
assert.equal(
// repeat operations may take less time
elapsed >= 250 * 0.8,
true,
`minimum time requirement was not met, instead only ${
elapsed / 1000.0
} elapsed`
);
return ret;
};
import { PrivateKey, PublicKey, Address } from '../src/auth/ecc';
import assert from 'assert';
var test = function (key) {
describe('steem.auth: key_formats', function () {
it('Calcualtes public key from private key', function () {
var private_key = PrivateKey.fromHex(key.private_key);
var public_key = private_key.toPublicKey();
assert.equal(key.public_key, public_key.toPublicKeyString());
});
it('Create BTS short address', function () {
var public_key = PublicKey.fromString(key.public_key);
assert.equal(key.bts_address, public_key.toAddressString());
});
it('Blockchain Address', function () {
var public_key = PublicKey.fromString(key.public_key);
assert.equal(
key.blockchain_address,
public_key.toBlockchainAddress().toString('hex')
);
});
it('BTS public key import / export', function () {
var public_key = PublicKey.fromString(key.public_key);
assert.equal(key.public_key, public_key.toPublicKeyString());
});
it('PTS', function () {
var private_key = PrivateKey.fromHex(key.private_key);
var public_key = private_key.toPublicKey();
assert.equal(key.pts_address, public_key.toPtsAddy());
});
it('To WIF', function () {
var private_key = PrivateKey.fromHex(key.private_key);
assert.equal(key.private_key_WIF_format, private_key.toWif());
});
it('From WIF', function () {
var private_key = PrivateKey.fromWif(key.private_key_WIF_format);
assert.equal(private_key.toHex(), key.private_key);
});
it('Calc public key', function () {
var private_key = PrivateKey.fromHex(key.private_key);
var public_key = private_key.toPublicKey();
assert.equal(key.bts_address, public_key.toAddressString());
});
it('BTS/BTC uncompressed', function () {
var public_key = PublicKey.fromString(key.public_key);
var address = Address.fromPublic(public_key, false, 0);
assert.equal(key.Uncompressed_BTC, address.toString());
});
it('BTS/BTC compressed', function () {
var public_key = PublicKey.fromString(key.public_key);
var address = Address.fromPublic(public_key, true, 0);
assert.equal(key.Compressed_BTC, address.toString());
});
it('BTS/PTS uncompressed', function () {
var public_key = PublicKey.fromString(key.public_key);
var address = Address.fromPublic(public_key, false, 56);
assert.equal(key.Uncompressed_PTS, address.toString());
});
it('BTS/PTS compressed', function () {
var public_key = PublicKey.fromString(key.public_key);
var address = Address.fromPublic(public_key, true, 56);
assert.equal(key.Compressed_PTS, address.toString());
});
it('null hex to pubkey', function () {
var public_key = PublicKey.fromHex(key.null_hex);
assert.equal(key.null_address, public_key.toPublicKeyString());
});
it('null pubkey to hex', function () {
var public_key = PublicKey.fromString(key.null_address);
assert.equal(key.null_hex, public_key.toHex());
});
});
};
test({
// delegate0
// sourced from: ./bitshares/programs/utils/bts_create_key
public_key: 'STM7jDPoMwyjVH5obFmqzFNp4Ffp7G2nvC7FKFkrMBpo7Sy4uq5Mj',
private_key:
'20991828d456b389d0768ed7fb69bf26b9bb87208dd699ef49f10481c20d3e18',
private_key_WIF_format:
'5J4eFhjREJA7hKG6KcvHofHMXyGQZCDpQE463PAaKo9xXY6UDPq',
bts_address: 'STM8DvGQqzbgCR5FHiNsFf8kotEXr8VKD3mR',
pts_address: 'Po3mqkgMzBL4F1VXJArwQxeWf3fWEpxUf3',
encrypted_private_key:
'5e1ae410919c450dce1c476ae3ed3e5fe779ad248081d85b3dcf2888e698744d0a4b60efb7e854453bec3f6883bcbd1d',
blockchain_address: '4f3a560442a05e4fbb257e8dc5859b736306bace',
// https://github.com/BitShares/bitshares/blob/2602504998dcd63788e106260895769697f62b07/libraries/wallet/wallet_db.cpp#L103-L108
Uncompressed_BTC: 'STMLAFmEtM8as1mbmjVcj5dphLdPguXquimn',
Compressed_BTC: 'STMANNTSEaUviJgWLzJBersPmyFZBY4jJETY',
Uncompressed_PTS: 'STMEgj7RM6FBwSoccGaESJLC3Mi18785bM3T',
Compressed_PTS: 'STMD5rYtofD6D4UHJH6mo953P5wpBfMhdMEi',
// https://github.com/steemit/steem-js/issues/267
null_hex:
'000000000000000000000000000000000000000000000000000000000000000000',
null_address: 'STM1111111111111111111111111111111114T1Anm',
});
import { PrivateKey, PublicKey, Address } from '../src/auth/ecc';
var assert = require('assert');
var Serilizer = require('../src/auth/serializer/src/serializer');
var types = require('../src/auth/serializer/src/types');
var ops = require('../src/auth/serializer/src/operations');
var {
//varint32,
uint8,
uint16,
uint32,
int16,
int64,
uint64,
string,
string_binary,
bytes,
bool,
array,
fixed_array,
protocol_id_type,
object_id_type,
vote_id,
// future_extensions,
static_variant,
map,
set,
public_key,
address,
time_point_sec,
optional,
asset,
} = types;
var { price, transfer } = ops;
// Must stay in sync with allTypes below.
let AllTypes = new Serilizer('all_types', {
uint8,
uint16,
uint32,
int16,
int64,
uint64,
string,
string_binary,
bytes: bytes(1),
bool,
array: array(uint8),
fixed_array: fixed_array(2, uint8),
protocol_id_type: protocol_id_type('base'),
object_id_type, //vote_id,
static_variant: array(static_variant([transfer, price])),
map: map(uint8, uint8),
set: set(uint8),
public_key,
address,
time_optional: optional(time_point_sec),
time_point_sec1: time_point_sec,
time_point_sec2: time_point_sec,
time_point_sec3: time_point_sec,
});
// Must stay in sync with AllTypes above.
let allTypes = {
uint8: Math.pow(2, 8) - 1,
uint16: Math.pow(2, 16) - 1,
uint32: Math.pow(2, 32) - 1,
int16: 30000,
int64: '9223372036854775807',
uint64: '9223372036854775807',
string: '‘Quote’',
string_binary: '\u0001',
bytes: 'ff',
bool: true,
array: [2, 1],
fixed_array: [1, 0],
protocol_id_type: '1.1.1',
object_id_type: '1.1.1', //vote_id: "2:1",
static_variant: [
[
'transfer',
{ from: 'alice', to: 'bob', amount: '1.000 STEEM', memo: '' },
],
['price', { base: '1.000 STEEM', quote: '1.000 STEEM' }],
],
map: [
[4, 3],
[2, 1],
],
set: [2, 1],
public_key: PrivateKey.fromSeed('').toPublicKey().toString(),
address: Address.fromPublic(
PrivateKey.fromSeed('').toPublicKey()
).toString(),
time_optional: undefined,
time_point_sec1: new Date(),
time_point_sec2: Math.floor(Date.now() / 1000),
time_point_sec3: '2017-02-16T20:27:12',
};
describe('steem.auth: all types', () => {
let { toObject, fromObject, toBuffer, fromBuffer } = AllTypes;