Commit 1ea2183a authored by Jason Salyers's avatar Jason Salyers
Browse files

[JES] Don't worry about unit tests for the temp library

parent 652fabaa
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;
toObject = toObject.bind(AllTypes);
fromObject = fromObject.bind(AllTypes);
toBuffer = toBuffer.bind(AllTypes);
fromBuffer = fromBuffer.bind(AllTypes);
it('from object', () => {
assert(fromObject(allTypes), 'serializable');
assert(fromObject(fromObject(allTypes)), 'non-serializable');
});
it('to object', () => {
assert(toObject(allTypes), 'serializable');
assert.deepEqual(
toObject(allTypes),
toObject(allTypes),
'serializable (single to)'
);
assert.deepEqual(
toObject(toObject(allTypes)),
toObject(allTypes),
'serializable (double to)'
);
assert.deepEqual(
toObject(fromObject(allTypes)),
toObject(allTypes),
'non-serializable'
);
assert.deepEqual(
toObject(fromObject(fromObject(allTypes))),
toObject(allTypes),
'non-serializable (double from)'
);
});
it('to buffer', () => {
assert(toBuffer(allTypes), 'serializable');
assert(toBuffer(fromObject(allTypes)), 'non-serializable');
assert.equal(
toBuffer(allTypes).toString('hex'), // serializable
toBuffer(fromObject(allTypes)).toString('hex'), // non-serializable
'serializable and non-serializable'
);
});
it('from buffer', () => {
assert.deepEqual(
toObject(fromBuffer(toBuffer(allTypes))),
toObject(allTypes),
'serializable'
);
assert.deepEqual(
toObject(fromBuffer(toBuffer(fromObject(allTypes)))),
toObject(allTypes),
'non-serializable'
);
});
it('template', () => {
assert(toObject(allTypes, { use_default: true }));
assert(toObject(allTypes, { annotate: true }));
assert(toObject({}, { use_default: true }));
assert(toObject({}, { use_default: true, annotate: true }));
});
// keep last
it('visual check', () => {
console.log(toObject(fromObject(allTypes)));
});
});
require('babel-polyfill');
import assert from 'assert';
import should from 'should';
import testPost from './test-post.json';
import steem from '../src';
import api from '../src/api';
describe('steem.api:', function () {
this.timeout(30 * 1000);
describe('setOptions', () => {
it('works', () => {
let url = steem.config.get('uri');
if (!url) url = steem.config.get('websocket');
steem.api.setOptions({ url: url, useAppbaseApi: true });
});
});
describe('getFollowers', () => {
describe("getting ned's followers", () => {
it('works', async () => {
const result = await steem.api.getFollowersAsync(
'ned',
0,
'blog',
5
);
assert(result, 'getFollowersAsync resoved to null?');
result.should.have.lengthOf(5);
});
it('the startFollower parameter has an impact on the result', async () => {
// Get the first 5
const result1 = await steem.api.getFollowersAsync(
'ned',
0,
'blog',
5
);
result1.should.have.lengthOf(5);
const result2 = await steem.api.getFollowersAsync(
'ned',
result1[result1.length - 1].follower,
'blog',
5
);
result2.should.have.lengthOf(5);
result1.should.not.be.eql(result2);
});
it('clears listeners', async () => {
steem.api.listeners('message').should.have.lengthOf(0);
});
});
});
describe('getContent', () => {
describe('getting a random post', () => {
it('works', async () => {
const result = await steem.api.getContentAsync(
'yamadapc',
'test-1-2-3-4-5-6-7-9'
);
result.should.have.properties(testPost);
});
it('clears listeners', async () => {
steem.api.listeners('message').should.have.lengthOf(0);
});
});
});
describe('streamBlockNumber', () => {
it('streams steem transactions', (done) => {
let i = 0;
const release = steem.api.streamBlockNumber((err, block) => {
should.exist(block);
block.should.be.instanceOf(Number);
i++;
if (i === 2) {
release();
done();
}
});
});
});
describe('streamBlock', () => {
it('streams steem blocks', (done) => {
let i = 0;
const release = steem.api.streamBlock((err, block) => {
try {
should.exist(block);
block.should.have.properties([
'previous',
'transactions',
'timestamp',
]);
} catch (err2) {
release();
done(err2);
return;
}
i++;
if (i === 2) {
release();
done();
}
});
});
});
describe('streamTransactions', () => {
it('streams steem transactions', (done) => {
let i = 0;
const release = steem.api.streamTransactions((err, transaction) => {
try {
should.exist(transaction);
transaction.should.have.properties([
'ref_block_num',
'operations',
'extensions',
]);
} catch (err2) {
release();
done(err2);
return;
}
i++;
if (i === 2) {
release();
done();
}
});
});
});
describe('streamOperations', () => {
it('streams steem operations', (done) => {
let i = 0;
const release = steem.api.streamOperations((err, operation) => {
try {
should.exist(operation);
} catch (err2) {
release();
done(err2);
return;
}
i++;
if (i === 2) {
release();
done();
}
});
});
});
describe('useApiOptions', () => {
it('works ok with the prod instances', async () => {
steem.api.setOptions({
useAppbaseApi: true,
url: steem.config.get('uri'),
});
const result = await steem.api.getContentAsync(
'yamadapc',
'test-1-2-3-4-5-6-7-9'
);
steem.api.setOptions({
useAppbaseApi: false,
url: steem.config.get('uri'),
});
result.should.have.properties(testPost);
});
});
describe('with retry', () => {
let steemApi;
beforeEach(() => {
steemApi = new api.Steem({});
});
it('works by default', async function () {
let attempts = 0;
steemApi.setOptions({
url: 'https://api.steemit.com',
fetchMethod: (uri, req) =>
new Promise((res, rej) => {
const data = JSON.parse(req.body);
res({
ok: true,