Skip to content

Commit

Permalink
Merge pull request #281 from willmeister/feat/278/MergeUtilsIntoCore
Browse files Browse the repository at this point in the history
Consolidating utils into core package and updating references
  • Loading branch information
willmeister committed Jun 14, 2019
2 parents 3acb234 + 752bf0a commit 47fc275
Show file tree
Hide file tree
Showing 37 changed files with 189 additions and 370 deletions.
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './src/interfaces'
export * from './src/app'
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"body-parser": "^1.19.0",
"debug": "^4.1.1",
"eth-lib": "^0.2.8",
"ethers": "^4.0.30",
"express": "^4.17.1",
"ganache-cli": "^6.4.4",
"uuid": "^3.3.2",
"web3": "^1.0.0-beta.55",
"web3-utils": "^1.0.0-beta.55"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions packages/core/src/app/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './app'
export * from './eth'
export * from './db'
export * from './net'
export * from './utils'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Internal Imports */
import { getNullString } from './utils'
import { getNullString } from './misc'

export const NULL_ADDRESS = getNullString(32)
export const NULL_HASH = getNullString(64)
3 changes: 3 additions & 0 deletions packages/core/src/app/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export * from './codec'
export * from './constants'
export * from './debug-logger'
export * from './misc'
export * from './range-store'
export * from './state-object'
export * from './sum-tree'
export * from './type-guards'
100 changes: 99 additions & 1 deletion packages/core/src/app/common/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,104 @@ export const bnToUint256 = (bn: BigNum): Buffer => {
* @returns the transacted range's end as a Uint256 Buffer.
*/
export const getTransactionRangeEnd = (transaction: Transaction): Buffer => {
const end = transaction.stateUpdate.id.end
const end = transaction.range.end
return bnToUint256(end)
}

/**
* Sleeps for a number of milliseconds.
* @param ms Number of ms to sleep.
* @returns a promise that resolves after the number of ms.
*/
export const sleep = (ms: number): Promise<void> => {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

/**
* Removes "0x" from start of a string if it exists.
* @param str String to modify.
* @returns the string without "0x".
*/
export const remove0x = (str: string): string => {
return str.startsWith('0x') ? str.slice(2) : str
}

/**
* Adds "0x" to the start of a string if necessary.
* @param str String to modify.
* @returns the string with "0x".
*/
export const add0x = (str: string): string => {
return str.startsWith('0x') ? str : '0x' + str
}

/**
* Checks if something is an Object
* @param obj Thing that might be an Object.
* @returns `true` if the thing is a Object, `false` otherwise.
*/
export const isObject = (obj: any): boolean => {
return typeof obj === 'object' && obj !== null
}

/**
* Creates a hex string with a certain number of zeroes.
* @param n Number of zeroes.
* @returns the hex string.
*/
export const getNullString = (n: number): string => {
return '0x' + '0'.repeat(n)
}

/**
* Reverses a string in place.
* @param str String to reverse.
* @returns the reversed string.
*/
export const reverse = (str: string): string => {
return Array.from(str)
.reverse()
.join('')
}

/**
* Converts a buffer to a hex string.
* @param buf the buffer to be converted.
* @returns the buffer as a string.
*/
export const bufToHexString = (buf: Buffer): string => {
return '0x' + buf.toString('hex')
}

/**
* Converts a big number to a hex string.
* @param bn the big number to be converted.
* @returns the big number as a string.
*/
export const bnToHexString = (bn: BigNum): string => {
return '0x' + bn.toString('hex')
}

/**
* Converts either a big number or buffer to hex string
* @param value the big number or buffer to be converted
* @returns the value as a string.
*/
export const hexStringify = (value: BigNum | Buffer): string => {
if (value instanceof BigNum) {
return bnToHexString(value)
} else {
return bufToHexString(value)
}
}

/**
* Converts a hex string to a buffer
* @param hexString the hex string to be converted
* @returns the hexString as a buffer.
*/
export const hexStrToBuf = (hexString: string): Buffer => {
return Buffer.from(hexString.slice(2), 'hex')
}
10 changes: 1 addition & 9 deletions packages/core/src/app/common/utils/range-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ import BigNum = require('bn.js')

/* Internal Imports */
import { bnMax, bnMin } from './misc'

export interface Range {
start: BigNum
end: BigNum
}

export interface BlockRange extends Range {
block: BigNum
}
import { Range, BlockRange } from '../../../interfaces/common/utils'

/**
* RangeStore makes it easy to store ranges.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Internal Imports */
import { abi } from '../eth'
import { StateObject, AbiEncodable } from '../interfaces/data-types'
import { StateObject, AbiEncodable } from '../../../interfaces'

/**
* Creates a StateObject from an encoded StateObject.
Expand All @@ -18,15 +18,15 @@ const fromEncoded = (encoded: string): AbiStateObject => {
export class AbiStateObject implements StateObject, AbiEncodable {
public static abiTypes = ['address', 'bytes']

constructor(readonly predicateAddress: string, readonly data: string) {}
constructor(readonly predicate: string, readonly parameters: string) {}

/**
* @returns the abi encoded StateObject.
*/
get encoded(): string {
return abi.encode(AbiStateObject.abiTypes, [
this.predicateAddress,
this.data,
this.predicate,
this.parameters,
])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import BigNum = require('bn.js')

/* Internal Imports */
import { keccak256 } from './eth'
import { reverse } from './utils'
import { keccak256 } from '../eth'
import { reverse } from './misc'
import { NULL_HASH } from './constants'

export interface ImplicitBounds {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/interfaces/common/utils/crypto.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface EcdsaSignature {
v: string
r: string
s: string
}
1 change: 1 addition & 0 deletions packages/core/src/interfaces/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './crypto.interface'
export * from './logger.interface'
export * from './state.interface'
export * from './type.interface'
11 changes: 10 additions & 1 deletion packages/core/src/interfaces/common/utils/state.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ export interface StateUpdate {
newState: StateObject
}

export interface Range {
start: BigNum
end: BigNum
}

export interface BlockRange extends Range {
block: BigNum
}

export interface Transaction {
stateUpdate: StateUpdate
range: Range
witness: any
block: number
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/interfaces/common/utils/type.interface.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export type Type<T> = new (...args: any[]) => T

export interface AbiEncodable {
encoded: string
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '../setup'
import '../../../setup'

/* Internal Imports */
import { keccak256 } from '../../src/eth/utils'
import { keccak256 } from '../../../../src/app/common/eth/utils'

describe('Ethereum Utils', () => {
describe('keccak256', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import './setup'
import '../../../setup'

/* Internal Imports */
import { sleep, remove0x, add0x, getNullString, isObject } from '../src/utils'
import {
sleep,
remove0x,
add0x,
getNullString,
isObject,
} from '../../../../src/app/common/utils'

describe('Miscellanous Utils', () => {
describe('sleep', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { should } from './setup'
import { should } from '../../../setup'

/* External Imports */
import BigNum = require('bn.js')

/* Internal Imports */
import { MerkleSumTree, MerkleTreeNode } from '../src/sum-tree'
import { MerkleSumTree, MerkleTreeNode } from '../../../../src/app/common/utils'

describe('MerkleSumTree', () => {
describe('construction', () => {
Expand Down
6 changes: 1 addition & 5 deletions packages/core/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import chai = require('chai')
import chaiAsPromised = require('chai-as-promised')

const should = chai.should()
chai.use(chaiAsPromised)

export { should }
export const should = chai.should()
2 changes: 1 addition & 1 deletion packages/predicates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@pigi/prettier-config": "^0.0.2-alpha.2",
"@pigi/utils": "^0.0.1-alpha.2",
"@pigi/core": "^0.0.1-alpha.1",
"@types/chai": "^4.1.7",
"@types/debug": "^4.1.2",
"@types/mocha": "^5.2.6",
Expand Down
3 changes: 2 additions & 1 deletion packages/predicates/src/ownership/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
abi,
keccak256,
hexStringify,
} from '@pigi/utils'
StateUpdate,
} from '@pigi/core'

/**
* Creates a Transaction from an encoded Transaction.
Expand Down
2 changes: 1 addition & 1 deletion packages/predicates/test/ownership/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '../setup'
import debug from 'debug'
const log = debug('test:info:ownership-predicate')
import BigNum = require('bn.js')
import { AbiStateObject } from '@pigi/utils'
import { AbiStateObject } from '@pigi/core'

/* Internal Imports */
import { OwnershipTransaction } from '../..//src/ownership/transaction'
Expand Down
36 changes: 36 additions & 0 deletions packages/types/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/utils/.prettierrc.js

This file was deleted.

0 comments on commit 47fc275

Please sign in to comment.