blockstack
Version:
The Blockstack Javascript library for authentication, identity, and storage.
184 lines (152 loc) • 8.38 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.runNetworkTests = runNetworkTests;
var _tape = require('tape');
var _tape2 = _interopRequireDefault(_tape);
var _fetchMock = require('fetch-mock');
var _fetchMock2 = _interopRequireDefault(_fetchMock);
var _bigi = require('bigi');
var _bigi2 = _interopRequireDefault(_bigi);
var _lib = require('../../../lib');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var mynet = new _lib.network.BlockstackNetwork('https://core.blockstack.org', 'https://broadcast.blockstack.org', new _lib.network.BlockchainInfoApi());
var testAddresses = [{
skHex: '85b33fdfa5efeca980806c6ad3c8a55d67a850bd987237e7d49c967566346fbd01',
address: '1br553PVnK6F5nyBtb4ju1owwBKdsep5c'
}, {
skHex: '744196d67ed78fe39009c71fbfd53e6ecca98353fbfe81ccba21b0703a69be9c01',
address: '16xVjkJ3nY62B9t9q3N9wY6hx1duAfwRZR'
}, {
address: '1HEjCcUjZXtbiDnCYviHLVZvSQsSZoDRFa',
skHex: '12f90d1b9e34d8df56f0dc6754a97ab4a2eb962918c281b1b552162438e313c001'
}, {
address: '16TaQJi78o4A3nKDSzswqZiX3bhecNuNBQ',
skHex: '58f7b29ee4a9a8b05855591b8a5405a0647c74c0a539515173adb9a32c964a9a01'
}, {
address: '15eNSvgT3UFvHSonajxFswnmHFifJPE5LB',
skHex: 'f5360140d18c6a34fbd2c45b98c1857c3fdad5454350249688a90efe936d475101'
}, {
address: '1Lt8ajRt7i8ajkQsYQZbk3ULCVTsSn2TNV',
skHex: '6eaed28d7f26f57fac925283aa0fe49c031028212863219f1c0141e4b0de2b2d01'
}, {
address: '1GvM4xksXrQsq4xPRck11toRLXVq9UYj2B',
skHex: '4c103c5c3de544c90f18a3ed29aaeebd33feedb1bb4f026df24aa3eddae826aa01'
}];
function runNetworkTests() {
(0, _tape2.default)('prices-v1', function (t) {
t.plan(2);
_fetchMock2.default.restore();
_fetchMock2.default.get('https://core.blockstack.org/v2/prices/names/test.id', 404);
_fetchMock2.default.get('https://core.blockstack.org/v2/prices/namespaces/id', 404);
_fetchMock2.default.get('https://core.blockstack.org/v1/prices/names/test.id', { name_price: { btc: 0.0001, satoshis: 1000 } });
_fetchMock2.default.get('https://core.blockstack.org/v1/prices/namespaces/id', { satoshis: 650000 });
mynet.getNamePrice('test.id').then(function (response) {
return t.deepEqual(response, { units: 'BTC', amount: _bigi2.default.fromByteArrayUnsigned('5500') });
});
mynet.getNamespacePrice('id').then(function (response) {
return t.deepEqual(response, { units: 'BTC', amount: _bigi2.default.fromByteArrayUnsigned('650000') });
});
});
(0, _tape2.default)('prices-v2', function (t) {
t.plan(3);
_fetchMock2.default.restore();
_fetchMock2.default.get('https://core.blockstack.org/v1/prices/names/test.id', 404);
_fetchMock2.default.get('https://core.blockstack.org/v1/prices/names/test-tokens.id', 404);
_fetchMock2.default.get('https://core.blockstack.org/v1/prices/namespaces/id', 404);
_fetchMock2.default.get('https://core.blockstack.org/v2/prices/names/test.id', { name_price: { units: 'BTC', amount: '1000' } });
_fetchMock2.default.get('https://core.blockstack.org/v2/prices/names/test-tokens.id', { name_price: { units: 'STACKS', amount: '1733000' } });
_fetchMock2.default.get('https://core.blockstack.org/v2/prices/namespaces/id', { units: 'BTC', amount: '650000' });
mynet.getNamePrice('test.id').then(function (response) {
return t.deepEqual(response, { units: 'BTC', amount: _bigi2.default.fromByteArrayUnsigned('5500') });
});
mynet.getNamePrice('test-tokens.id').then(function (response) {
return t.deepEqual(response, { units: 'STACKS', amount: _bigi2.default.fromByteArrayUnsigned('1733000') });
});
mynet.getNamespacePrice('id').then(function (response) {
return t.deepEqual(response, { units: 'BTC', amount: _bigi2.default.fromByteArrayUnsigned('650000') });
});
});
(0, _tape2.default)('accounts', function (t) {
t.plan(10);
_fetchMock2.default.restore();
var addr = testAddresses[0].address;
var addr2 = testAddresses[1].address;
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr + '/tokens', ['STACKS']);
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr2 + '/tokens', 404);
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr + '/STACKS/balance', { balance: '69129' });
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr2 + '/NOPE/balance', 404);
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr + '/STACKS/status', { address: addr, credit_value: '123450', debit_value: '54321' });
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr2 + '/STACKS/status', 404);
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr + '/history?page=0', [{ address: addr, credit_value: '123450', debit_value: '54321' }]);
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr2 + '/history?page=0', 404);
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr + '/history/567890', [{ address: addr, credit_value: '123450', debit_value: '54321' }]);
_fetchMock2.default.get('https://core.blockstack.org/v1/accounts/' + addr2 + '/history/567890', 404);
mynet.getAccountTokens(addr).then(function (response) {
return t.deepEqual(response, ['STACKS']);
});
mynet.getAccountTokens(addr2).then(function () {
return t.fail('did not get exception when querying tokens of unknown address');
}).catch(function (e) {
return t.equal(e.message, 'Account not found');
});
mynet.getAccountBalance(addr, 'STACKS').then(function (response) {
return t.deepEqual(response, _bigi2.default.fromByteArrayUnsigned('69129'));
});
mynet.getAccountBalance(addr2, 'NOPE').then(function (response) {
return t.deepEqual(response, _bigi2.default.fromByteArrayUnsigned('0'));
});
mynet.getAccountStatus(addr, 'STACKS').then(function (response) {
return t.deepEqual(response, {
address: addr,
credit_value: _bigi2.default.fromByteArrayUnsigned('123450'),
debit_value: _bigi2.default.fromByteArrayUnsigned('54321')
});
});
mynet.getAccountStatus(addr2, 'STACKS').then(function () {
return t.fail('did not get exception when querying status of unknown address');
}).catch(function (e) {
return t.equal(e.message, 'Account not found');
});
mynet.getAccountHistoryPage(addr, 0).then(function (response) {
return t.deepEqual(response, [{
address: addr,
credit_value: _bigi2.default.fromByteArrayUnsigned('123450'),
debit_value: _bigi2.default.fromByteArrayUnsigned('54321')
}]);
});
mynet.getAccountHistoryPage(addr2, 0).then(function () {
return t.fail('did not get exception when querying history of unknown address');
}).catch(function (e) {
return t.equal(e.message, 'Account not found');
});
mynet.getAccountAt(addr, 567890).then(function (response) {
return t.deepEqual(response, [{
address: addr,
credit_value: _bigi2.default.fromByteArrayUnsigned('123450'),
debit_value: _bigi2.default.fromByteArrayUnsigned('54321')
}]);
});
mynet.getAccountAt(addr2, 567890).then(function () {
return t.fail('did not get exception when querying block txs of unknown address');
}).catch(function (e) {
return t.equal(e.message, 'Account not found');
});
});
(0, _tape2.default)('zonefiles', function (t) {
t.plan(2);
_fetchMock2.default.restore();
var zf = '$ORIGIN judecn.id\n$TTL 3600\n_https._tcp URI 10 1 ' + '"https://raw.githubusercontent.com/jcnelson/profile/master/judecn.id"\n_https._tcp URI ' + '10 1 "https://www.cs.princeton.edu/~jcnelson/judecn.id"\n';
var zfh = '737c631c7c5d911c6617993c21fba731363f1cfe';
var zfh2 = '737c631c7c5d911c6617993c21fba731363f1cff';
_fetchMock2.default.get('https://core.blockstack.org/v1/zonefiles/' + zfh, zf);
_fetchMock2.default.get('https://core.blockstack.org/v1/zonefiles/' + zfh2, zf);
mynet.getZonefile(zfh).then(function (response) {
return t.equal(response, zf);
});
mynet.getZonefile(zfh2).catch(function (e) {
return t.equal(e.message, 'Zone file contents hash to ' + zfh + ', not ' + zfh2);
});
});
}