Skip to content

Commit

Permalink
Add bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Aug 17, 2020
1 parent e766054 commit da54a5a
Show file tree
Hide file tree
Showing 10 changed files with 1,716 additions and 102 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# zkSync changelog

### zksync.js 0.6.0
- Upgrade ethers to ^5.0.0
### zksync.js 0.6.1

- Bundled version added.

### zksync.js 0.6.0

- Upgrade ethers to ^5.0.0

### zkSync

Expand Down
1 change: 1 addition & 0 deletions core/server/src/api_server/rpc_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pub fn start_ws_server(
|context: &RequestContext| Arc::new(Session::new(context.sender())),
)
.request_middleware(super::loggers::ws_rpc::request_middleware)
.max_connections(1000)
.event_loop_executor(task_executor.executor())
.start(&addr)
.expect("Unable to start RPC ws server");
Expand Down
2 changes: 1 addition & 1 deletion js/tests/simple-integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ async function checkFailedTransactionResending(contract: Contract, depositWallet
try {
await testTransfer(syncWallet1, syncWallet2, "ETH", amount);
} catch (e) {
assert(e.value.failReason == `Not enough balance`);
assert(e?.value?.failReason == `Not enough balance`);
console.log('Transfer failed (expected)');
}

Expand Down
9 changes: 6 additions & 3 deletions js/zksync.js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zksync",
"version": "0.6.0",
"version": "0.6.1",
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand All @@ -24,14 +24,17 @@
"prettier": "1.18.2",
"ts-node": "^8.3.0",
"tslint": "^6.0.0-beta0",
"typescript": "^3.5.3"
"typescript": "^3.5.3",
"webpack": "^5.0.0-beta.26",
"webpack-cli": "^3.3.12"
},
"scripts": {
"lint:ts": "tslint -c tslint.json {examples,test,src}/**/*.ts",
"lint:ts-fix": "tslint -c tslint.json --fix {examples,test,src}/**/*.ts",
"test": "mocha -r ts-node/register tests/**/*.test.ts",
"fmt": "prettier --tab-width 4 --parser typescript --write \"{src,tests,examples}/*.ts\"",
"build": "tsc",
"watch": "tsc --watch"
"watch": "tsc --watch",
"prepublish": "yarn build && webpack"
}
}
82 changes: 6 additions & 76 deletions js/zksync.js/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
} from "./crypto";
import {BigNumber, BigNumberish, ethers} from "ethers";
import {
packAmountChecked,
packFeeChecked,
getEthSignatureType,
signMessagePersonalAPI,
getSignedBytesFromMessage
getSignedBytesFromMessage,
serializeAccountId,
serializeAddress,
serializeTokenId,
serializeAmountPacked,
serializeFeePacked, serializeNonce, serializeAmountFull
} from "./utils";
import BN = require("bn.js");
import {
Address,
EthSignerType,
Expand All @@ -20,9 +22,6 @@ import {
Withdraw
} from "./types";

const MAX_NUMBER_OF_TOKENS = 128;
const MAX_NUMBER_OF_ACCOUNTS = Math.pow(2, 24);

export class Signer {
readonly privateKey: Uint8Array;

Expand Down Expand Up @@ -151,72 +150,3 @@ export class Signer {
}
}

function removeAddressPrefix(address: Address | PubKeyHash): string {
if (address.startsWith("0x")) return address.substr(2);

if (address.startsWith("sync:")) return address.substr(5);

throw new Error(
"ETH address must start with '0x' and PubKeyHash must start with 'sync:'"
);
}

// PubKeyHash or eth address
export function serializeAddress(address: Address | PubKeyHash): Buffer {
const prefixlessAddress = removeAddressPrefix(address);

const addressBytes = Buffer.from(prefixlessAddress, "hex");
if (addressBytes.length != 20) {
throw new Error("Address must be 20 bytes long");
}

return addressBytes;
}

export function serializeAccountId(accountId: number): Buffer {
if (accountId < 0) {
throw new Error("Negative account id");
}
if (accountId >= MAX_NUMBER_OF_ACCOUNTS) {
throw new Error("AccountId is too big");
}
const buffer = Buffer.alloc(4);
buffer.writeUInt32BE(accountId, 0);
return buffer;
}

export function serializeTokenId(tokenId: number): Buffer {
if (tokenId < 0) {
throw new Error("Negative tokenId");
}
if (tokenId >= MAX_NUMBER_OF_TOKENS) {
throw new Error("TokenId is too big");
}
const buffer = Buffer.alloc(2);
buffer.writeUInt16BE(tokenId, 0);
return buffer;
}

export function serializeAmountPacked(amount: BigNumberish): Buffer {
const bnAmount = new BN(BigNumber.from(amount).toString());
return packAmountChecked(bnAmount);
}

export function serializeAmountFull(amount: BigNumberish): Buffer {
const bnAmount = new BN(BigNumber.from(amount).toString());
return bnAmount.toArrayLike(Buffer, "be", 16);
}

export function serializeFeePacked(fee: BigNumberish): Buffer {
const bnFee = new BN(BigNumber.from(fee).toString());
return packFeeChecked(bnFee);
}

export function serializeNonce(nonce: number): Buffer {
if (nonce < 0) {
throw new Error("Negative nonce");
}
const buff = Buffer.alloc(4);
buff.writeUInt32BE(nonce, 0);
return buff;
}
3 changes: 2 additions & 1 deletion js/zksync.js/src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Axios from "axios";
import WebSocketAsPromised = require("websocket-as-promised");
const W3CWebSocket = require("websocket").w3cwebsocket;
import * as websocket from "websocket";
const W3CWebSocket = websocket.w3cwebsocket;

export abstract class AbstractJSONRPCTransport {
abstract async request(method: string, params): Promise<any>;
Expand Down
80 changes: 76 additions & 4 deletions js/zksync.js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
TokenLike,
Tokens,
TokenSymbol,
EthSignerType
EthSignerType, Address
} from "./types";
import { serializeAccountId, serializeNonce } from "./signer";

const MAX_NUMBER_OF_TOKENS = 128;
const MAX_NUMBER_OF_ACCOUNTS = Math.pow(2, 24);

export const IERC20_INTERFACE = new utils.Interface(
require("../abi/IERC20.json").abi
Expand Down Expand Up @@ -44,7 +46,7 @@ export function floatToInteger(
mantissaBits: number,
expBaseNumber: number
): BN {
if (floatBytes.length * 8 != mantissaBits + expBits) {
if (floatBytes.length * 8 !== mantissaBits + expBits) {
throw new Error("Float unpacking, incorrect input length");
}

Expand Down Expand Up @@ -72,7 +74,7 @@ export function floatToInteger(
}

export function bitsIntoBytesInBEOrder(bits: boolean[]): Buffer {
if (bits.length % 8 != 0) {
if (bits.length % 8 !== 0) {
throw new Error("wrong number of bits to pack");
}
const nBytes = bits.length / 8;
Expand Down Expand Up @@ -486,3 +488,73 @@ export async function getEthSignatureType(
isSignedMsgPrefixed: true
};
}

function removeAddressPrefix(address: Address | PubKeyHash): string {
if (address.startsWith("0x")) return address.substr(2);

if (address.startsWith("sync:")) return address.substr(5);

throw new Error(
"ETH address must start with '0x' and PubKeyHash must start with 'sync:'"
);
}

// PubKeyHash or eth address
export function serializeAddress(address: Address | PubKeyHash): Buffer {
const prefixlessAddress = removeAddressPrefix(address);

const addressBytes = Buffer.from(prefixlessAddress, "hex");
if (addressBytes.length !== 20) {
throw new Error("Address must be 20 bytes long");
}

return addressBytes;
}

export function serializeAccountId(accountId: number): Buffer {
if (accountId < 0) {
throw new Error("Negative account id");
}
if (accountId >= MAX_NUMBER_OF_ACCOUNTS) {
throw new Error("AccountId is too big");
}
const buffer = Buffer.alloc(4);
buffer.writeUInt32BE(accountId, 0);
return buffer;
}

export function serializeTokenId(tokenId: number): Buffer {
if (tokenId < 0) {
throw new Error("Negative tokenId");
}
if (tokenId >= MAX_NUMBER_OF_TOKENS) {
throw new Error("TokenId is too big");
}
const buffer = Buffer.alloc(2);
buffer.writeUInt16BE(tokenId, 0);
return buffer;
}

export function serializeAmountPacked(amount: BigNumberish): Buffer {
const bnAmount = new BN(BigNumber.from(amount).toString());
return packAmountChecked(bnAmount);
}

export function serializeAmountFull(amount: BigNumberish): Buffer {
const bnAmount = new BN(BigNumber.from(amount).toString());
return bnAmount.toArrayLike(Buffer, "be", 16);
}

export function serializeFeePacked(fee: BigNumberish): Buffer {
const bnFee = new BN(BigNumber.from(fee).toString());
return packFeeChecked(bnFee);
}

export function serializeNonce(nonce: number): Buffer {
if (nonce < 0) {
throw new Error("Negative nonce");
}
const buff = Buffer.alloc(4);
buff.writeUInt32BE(nonce, 0);
return buff;
}
2 changes: 0 additions & 2 deletions js/zksync.js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "commonjs",
"target": "es6",
"moduleResolution": "node",

"outDir": "./build",

Expand Down
11 changes: 11 additions & 0 deletions js/zksync.js/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

module.exports = {
entry: './build/index.js',
mode: 'production',
experiments: {
asyncWebAssembly: true
},
resolve: {
alias: { util: false },
},
};
Loading

0 comments on commit da54a5a

Please sign in to comment.