Skip to content

Commit

Permalink
Bundled version for CDN (#100)
Browse files Browse the repository at this point in the history
* improve bundling, browserify:dist/browser.js

Fixes #82

* fix packages & mocha

* switch to parcel-bundler

fix import/export from class objects (parcel does not like it)

* Revert to old behaviour

* fix switch and ouput to leap-core.min.js

* Add parcel cache in .gitignore
  • Loading branch information
pinkiebell authored and sunify committed May 8, 2019
1 parent 832f255 commit d0c22a4
Show file tree
Hide file tree
Showing 13 changed files with 3,335 additions and 1,047 deletions.
11 changes: 7 additions & 4 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"presets": ["es2015", "stage-2"],
"plugins": ["add-module-exports", "array-includes"],
"retainLines": true
}
"presets": [
"@babel/preset-env"
],
"plugins": [
"add-module-exports"
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.nyc_output
coverage/

.cache
.idea
deploy.env
event.json
Expand Down
12 changes: 6 additions & 6 deletions lib/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import ethUtil, { bufferToHex } from 'ethereumjs-util';
import assert from 'assert';
import MerkleTree from './merkleTree';
import { toHexString, writeUint64, readUint64 } from './util';
import Util from './util';
import Tx from './transaction';

export default class Block {
Expand Down Expand Up @@ -58,7 +58,7 @@ export default class Block {
}

header(payload) {
writeUint64(payload, this.height, 0);
Util.writeUint64(payload, this.height, 0);
payload.write(
this.merkleRoot()
.replace('0x', '')
Expand All @@ -76,7 +76,7 @@ export default class Block {

// Returns serialized tx bytes as hex string
hex() {
return toHexString(this.toRaw());
return Util.toHexString(this.toRaw());
}

equals(another) {
Expand Down Expand Up @@ -121,7 +121,7 @@ export default class Block {
*/
toRaw() {
const payload = Buffer.alloc(this.getSize(), 0);
writeUint64(payload, this.height, 0);
Util.writeUint64(payload, this.height, 0);
payload.writeUInt32BE(this.timestamp, 8);
let offset = 12;
let rawTx;
Expand All @@ -140,7 +140,7 @@ export default class Block {
* Also: `toRaw`
*/
static fromRaw(buf) {
const height = parseInt(readUint64(buf).toString(10), 10);
const height = parseInt(Util.readUint64(buf).toString(10), 10);
const timestamp = buf.readUInt32BE(8);
const block = new Block(height, { timestamp });
let offset = 12;
Expand Down Expand Up @@ -206,7 +206,7 @@ export default class Block {
const sliceCount = Math.floor(txData.length / 32) + 1;
txPayload.writeUInt8((((txData.length % 32) > 20) ? sliceCount + 1 : sliceCount) + 1, 1);
// tx index in the block
writeUint64(txPayload, pos, 4);
Util.writeUint64(txPayload, pos, 4);
// copy tx data tighly aligned to the right boundary of the buffer
txData.copy(txPayload, txPayload.length - txData.length, 0);

Expand Down
1 change: 0 additions & 1 deletion lib/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'babel-polyfill';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import ethUtil from 'ethereumjs-util';
Expand Down
6 changes: 3 additions & 3 deletions lib/outpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import assert from 'assert';
import ethUtils from 'ethereumjs-util';
import encoding from './encoding';
import Util, { toHexString } from './util';
import Util from './util';


// 32 bytes prev tx + 1 byte output pos
Expand Down Expand Up @@ -118,7 +118,7 @@ export default class Outpoint {
dataBuf.writeInt32BE(0, 8);
dataBuf.writeInt32BE(0, 12);
dataBuf.writeUInt8(this.index, 16);
return toHexString(dataBuf);
return Util.toHexString(dataBuf);
}

/**
Expand Down Expand Up @@ -146,7 +146,7 @@ export default class Outpoint {

// Returns serialized tx bytes as hex string
hex() {
return toHexString(this.toRaw());
return Util.toHexString(this.toRaw());
}

/**
Expand Down
20 changes: 10 additions & 10 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import assert from 'assert';
import { BigInt, isBigInt, lessThan } from 'jsbi-utils';

import { toHexString, isNFT, isNST, ensureSafeValue, isBytes32 } from './util';
import Util from './util';

// 32 bytes - value, 20 bytes - address, 2 bytes color
export const OUT_LENGTH = 54;
Expand All @@ -26,7 +26,7 @@ export default class Output {

constructor(valueOrObject, address, color, data) {
if (typeof valueOrObject === 'object' && !isBigInt(valueOrObject)) {
ensureSafeValue(valueOrObject.value);
Util.ensureSafeValue(valueOrObject.value);
this.value = BigInt(valueOrObject.value);
this.color = valueOrObject.color;
this.address = valueOrObject.address;
Expand All @@ -35,7 +35,7 @@ export default class Output {
this.data = valueOrObject.data;
}
} else {
ensureSafeValue(valueOrObject);
Util.ensureSafeValue(valueOrObject);
this.value = BigInt(valueOrObject);
this.address = address;
this.color = color;
Expand All @@ -50,17 +50,17 @@ export default class Output {
if (this.color < 0 || this.color > MAX_COLOR) {
throw new Error(`Color out of range (0–${MAX_COLOR})`);
}
if (this.isNST() && !isBytes32(this.data)) {
if (this.isNST() && !Util.isBytes32(this.data)) {
throw new Error('data is not a 32 bytes hex string');
}
}

isNFT() {
return isNFT(this.color);
return Util.isNFT(this.color);
}

isNST() {
return isNST(this.color);
return Util.isNST(this.color);
}

/* eslint-disable class-methods-use-this */
Expand Down Expand Up @@ -102,13 +102,13 @@ export default class Output {
*/
static fromRaw(buf, offset = 0) {
const color = buf.readUInt16BE(offset + 32);
const valueString = toHexString(buf.slice(offset, offset + 32));
const valueString = Util.toHexString(buf.slice(offset, offset + 32));
const value = BigInt(valueString);
const address = toHexString(buf.slice(offset + 34, offset + 54));
const address = Util.toHexString(buf.slice(offset + 34, offset + 54));

let data;
if (isNST(color)) {
data = toHexString(buf.slice(offset + 54, offset + 54 + 32));
if (Util.isNST(color)) {
data = Util.toHexString(buf.slice(offset + 54, offset + 54 + 32));
}

// transfer output
Expand Down
4 changes: 2 additions & 2 deletions lib/period.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { bufferToHex, toBuffer, keccak256 } from 'ethereumjs-util';
import MerkleTree from './merkleTree';
import Block from './block';
import { range } from './util';
import Util from './util';
import { periodBlockRange } from './helpers';

export default class Period {
Expand Down Expand Up @@ -143,7 +143,7 @@ export default class Period {
*/
static periodForBlockRange(plasma, startBlock, endBlock) {
return Promise.all(
range(startBlock, endBlock).map(n => (plasma.eth || plasma).getBlock(n, true)),
Util.range(startBlock, endBlock).map(n => (plasma.eth || plasma).getBlock(n, true)),
).then((blocks) => {
const blockList = blocks
.filter(a => !!a)
Expand Down
10 changes: 5 additions & 5 deletions lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { calcInputs, calcOutputs } from './helpers';
import Input, { SPEND_INPUT_LENGTH } from './input';
import Output, { OUT_LENGTH, NST_OUT_LENGTH } from './output';
import Outpoint from './outpoint';
import { arrayToRaw, toHexString, ensureSafeValue } from './util';
import Util from './util';
import Type from './type';

const EMPTY_BUF = Buffer.alloc(32, 0);
Expand All @@ -38,7 +38,7 @@ export default class Transaction {


static minGasPrice(minGasPrice) {
ensureSafeValue(minGasPrice);
Util.ensureSafeValue(minGasPrice);
return new Transaction(Type.MIN_GAS_PRICE, [], [], { minGasPrice: BigInt(minGasPrice) });
}

Expand Down Expand Up @@ -436,7 +436,7 @@ export default class Transaction {
} else if (this.type === Type.EXIT) {
payload = Buffer.alloc(34, 0);
payload.writeUInt8(this.type, 0);
arrayToRaw(this.inputs).copy(payload, 1, 0, 33);
Util.arrayToRaw(this.inputs).copy(payload, 1, 0, 33);
} else if (this.type === Type.TRANSFER) {
payload = Buffer.alloc(2, 0);
payload.writeUInt8(this.type, 0);
Expand All @@ -452,7 +452,7 @@ export default class Transaction {
payload.writeUInt8(this.type, 0);
inputs = this.inputs;
}
return Buffer.concat([payload, arrayToRaw(inputs), arrayToRaw(this.outputs)]);
return Buffer.concat([payload, Util.arrayToRaw(inputs), Util.arrayToRaw(this.outputs)]);
}

toJSON() {
Expand Down Expand Up @@ -519,7 +519,7 @@ export default class Transaction {
if (dataBuf.length !== 10) {
throw new Error('malformed gas price tx.');
}
const gasString = toHexString(dataBuf.slice(2, 10));
const gasString = Util.toHexString(dataBuf.slice(2, 10));
const minGasPrice = BigInt(gasString);
return Transaction.minGasPrice(minGasPrice);
}
Expand Down
22 changes: 9 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"lint:eslint": "eslint --fix --ignore-path .gitignore --ignore-pattern internals/scripts --ignore-pattern dist",
"lint:js": "yarn lint:eslint . ",
"check-coverage": "nyc check-coverage --statements 50 --branches 50 --functions 50 --lines 50",
"test": "mocha lib/*.spec.js --compilers js:babel-register",
"test": "mocha --require @babel/register --require @babel/polyfill lib/*.spec.js",
"cover": "nyc --reporter=lcov yarn test",
"prebuild": "rm -rf dist",
"build": "babel --out-dir dist --ignore *.spec.js lib",
"build": "babel --ignore '**/*.spec.js' --out-dir dist lib && parcel build -t browser --global LeapCore lib/index.js -o dist/leap-core.min.js",
"prepublish": "yarn prebuild && yarn build"
},
"config": {
Expand All @@ -50,17 +50,13 @@
"author": "Leap DAO",
"license": "AGPL-3.0-or-later",
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"babel-cli": "^6.11.4",
"babel-core": "^6.10.4",
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.4",
"@babel/polyfill": "^7.4.4",
"@babel/preset-env": "^7.4.4",
"@babel/register": "^7.4.4",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.4",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-array-includes": "^2.0.3",
"babel-preset-es2015": "^6.5.0",
"babel-preset-stage-2": "^6.5.0",
"babel-register": "^6.18.0",
"babel-plugin-add-module-exports": "^1.0.2",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^3.19.0",
Expand All @@ -69,14 +65,14 @@
"eslint-plugin-import": "^2.2.0",
"mocha": "^5.2.0",
"nyc": "^11.2.1",
"parcel-bundler": "^1.12.3",
"prettier": "^1.15.3",
"sinon": "^7.2.2",
"sinon-chai": "^3.3.0",
"utf8": "^2.1.2"
},
"dependencies": {
"@types/web3": "^1.0.18",
"babel-polyfill": "^6.26.0",
"ethereumjs-util": "6.0.0",
"jsbi-utils": "^1.0.0",
"node-fetch": "^2.3.0"
Expand Down
1 change: 0 additions & 1 deletion test/babel.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/mocha.config.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/mocha.opts

This file was deleted.

Loading

0 comments on commit d0c22a4

Please sign in to comment.