Skip to content

Commit

Permalink
js/*: eslint applied
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivshti committed Apr 2, 2019
1 parent 4cd25fe commit 361ec73
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 55 deletions.
49 changes: 38 additions & 11 deletions js/Identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,36 @@ function Transaction(args) {
Transaction.prototype.hash = function() {
const buf = abi.rawEncode(
['address', 'uint256', 'address', 'uint256', 'address', 'uint256', 'bytes'],
[this.identityContract, this.nonce, this.feeTokenAddr, this.feeTokenAmount, this.to, this.value, this.data],
[
this.identityContract,
this.nonce,
this.feeTokenAddr,
this.feeTokenAmount,
this.to,
this.value,
this.data
]
)
return new Buffer(keccak256.arrayBuffer(buf))
return Buffer.from(keccak256.arrayBuffer(buf))
}

Transaction.prototype.hashHex = function() {
return '0x'+this.hash().toString('hex')
return `0x${this.hash().toString('hex')}`
}

Transaction.prototype.toSolidityTuple = function() {
// etherjs doesn't seem to want BN.js instances; hex is the lowest common denominator for web3/ethers
return [this.identityContract, '0x'+this.nonce.toString(16), this.feeTokenAddr, '0x'+this.feeTokenAmount.toString(16), this.to, '0x'+this.value.toString(16), '0x'+this.data.toString('hex')]
return [
this.identityContract,
`0x${this.nonce.toString(16)}`,
this.feeTokenAddr,
`0x${this.feeTokenAmount.toString(16)}`,
this.to,
`0x${this.value.toString(16)}`,
`0x${this.data.toString('hex')}`
]
}



function RoutineAuthorization(args) {
this.identityContract = ensure.Address(args.identityContract)
this.relayer = ensure.Address(args.relayer)
Expand All @@ -47,23 +61,36 @@ function RoutineAuthorization(args) {
RoutineAuthorization.prototype.hash = function() {
const buf = abi.rawEncode(
['address', 'address', 'address', 'uint256', 'address', 'uint256'],
[this.identityContract, this.relayer, this.outpace, this.validUntil, this.feeTokenAddr, this.feeTokenAmount],
[
this.identityContract,
this.relayer,
this.outpace,
this.validUntil,
this.feeTokenAddr,
this.feeTokenAmount
]
)
return new Buffer(keccak256.arrayBuffer(buf))
return Buffer.from(keccak256.arrayBuffer(buf))
}

RoutineAuthorization.prototype.hashHex = function() {
return '0x'+this.hash().toString('hex')
return `0x${this.hash().toString('hex')}`
}

RoutineAuthorization.prototype.toSolidityTuple = function() {
// etherjs doesn't seem to want BN.js instances; hex is the lowest common denominator for web3/ethers
return [this.identityContract, this.relayer, this.outpace, '0x'+this.validUntil.toString(16), this.feeTokenAddr, '0x'+this.feeTokenAmount.toString(16)]
return [
this.identityContract,
this.relayer,
this.outpace,
`0x${this.validUntil.toString(16)}`,
this.feeTokenAddr,
`0x${this.feeTokenAmount.toString(16)}`
]
}

RoutineAuthorization.encodeWithdraw = function(tokenAddr, to, amount) {
return abi.rawEncode(['address', 'address', 'uint256'], [tokenAddr, to, amount])
}


module.exports = { Transaction, RoutineAuthorization }
27 changes: 16 additions & 11 deletions js/IdentityProxyDeploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ const assert = require('assert')
// * unsafeERC20: true OR safeERC20Artifact
function getProxyDeployTx(
proxiedAddr,
feeTokenAddr, feeBeneficiery, feeAmnt,
feeTokenAddr,
feeBeneficiery,
feeAmnt,
registryAddr,
privLevels,
opts
) {
assert.ok(opts, 'opts not passed')
const { privSlot, registrySlot } = opts
assert.ok(typeof(privSlot) === 'number', 'privSlot is a number')
assert.ok(typeof(registrySlot) === 'number', 'registrySlot is a number')
assert.ok(typeof privSlot === 'number', 'privSlot is a number')
assert.ok(typeof registrySlot === 'number', 'registrySlot is a number')

const privLevelsCode = privLevels
.map(([addr, level]) => {
Expand All @@ -27,7 +29,8 @@ function getProxyDeployTx(
})
.join('\n')

let feeCode = ``
let erc20Header = ''
let feeCode = ''
if (feeAmnt > 0) {
// This is fine if we're only accepting whitelisted tokens
if (opts.unsafeERC20) {
Expand Down Expand Up @@ -74,28 +77,30 @@ contract IdentityProxy {
settings: {
outputSelection: {
'*': {
'*': [ 'evm.bytecode' ]
'*': ['evm.bytecode']
}
},
optimizer: {
enabled: true,
runs: 200,
runs: 200
}
}
}
const output = JSON.parse(solc.compile(JSON.stringify(input)))
assert.ifError(output.errors)
const byteCode = '0x'+output.contracts['Proxy.sol']['IdentityProxy'].evm.bytecode.object
const byteCode = `0x${output.contracts['Proxy.sol'].IdentityProxy.evm.bytecode.object}`
return { data: byteCode }
}

function getStorageSlotsFromArtifact(IdentityArtifact) {
// Find storage locations of privileges, registryAddr
const identityNode = IdentityArtifact.ast.nodes
.find(({ name, nodeType }) => nodeType === 'ContractDefinition' && name === 'Identity')
const identityNode = IdentityArtifact.ast.nodes.find(
({ name, nodeType }) => nodeType === 'ContractDefinition' && name === 'Identity'
)
assert.ok(identityNode, 'Identity contract definition not found')
const storageVariableNodes = identityNode.nodes
.filter(n => n.nodeType === 'VariableDeclaration' && !n.constant && n.stateVariable)
const storageVariableNodes = identityNode.nodes.filter(
n => n.nodeType === 'VariableDeclaration' && !n.constant && n.stateVariable
)
const privSlot = storageVariableNodes.findIndex(x => x.name === 'privileges')
const registrySlot = storageVariableNodes.findIndex(x => x.name === 'registryAddr')
assert.notEqual(privSlot, -1, 'privSlot was not found')
Expand Down
35 changes: 16 additions & 19 deletions js/MerkleTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,46 @@
const Buffer = require('buffer').Buffer
const keccak256 = require('js-sha3').keccak256

function combinedHash (first, second) {
function combinedHash(first, second) {
if (!second) {
return first
}
if (!first) {
return second
}
let sorted = Buffer.concat([first, second].sort(Buffer.compare))
const sorted = Buffer.concat([first, second].sort(Buffer.compare))

return Buffer.from(keccak256.arrayBuffer(sorted))
}

function deduplicate (buffers) {
function deduplicate(buffers) {
// NOTE: performance?
return buffers.filter((buffer, i) => {
return buffers.findIndex(e => e.equals(buffer)) === i
})
}

function getPair (index, layer) {
let pairIndex = index % 2 ? index - 1 : index + 1
function getPair(index, layer) {
const pairIndex = index % 2 ? index - 1 : index + 1
if (pairIndex < layer.length) {
return layer[pairIndex]
} else {
return null
}
return null
}

function getLayers (elements) {
function getLayers(elements) {
if (elements.length === 0) {
return [[Buffer.from('')]]
}
let layers = []
const layers = []
layers.push(elements)
while (layers[layers.length - 1].length > 1) {
layers.push(getNextLayer(layers[layers.length - 1]))
}
return layers
}

function getNextLayer (elements) {
function getNextLayer(elements) {
return elements.reduce((layer, element, index, arr) => {
if (index % 2 === 0) {
layer.push(combinedHash(element, arr[index + 1]))
Expand All @@ -52,7 +51,7 @@ function getNextLayer (elements) {
}

class MerkleTree {
constructor (_elements) {
constructor(_elements) {
if (!_elements.every(b => b.length === 32 && Buffer.isBuffer(b))) {
throw new Error('elements must be 32 byte buffers')
}
Expand All @@ -64,29 +63,27 @@ class MerkleTree {
Object.assign(this, l)
}

getRoot () {
getRoot() {
if (!this.root) {
let r = { root: this.layers[this.layers.length - 1][0] }
const r = { root: this.layers[this.layers.length - 1][0] }
Object.assign(this, r)
}
return this.root
}

verify (proof, element) {
return this.getRoot().equals(
proof.reduce((hash, pair) => combinedHash(hash, pair), element)
)
verify(proof, element) {
return this.getRoot().equals(proof.reduce((hash, pair) => combinedHash(hash, pair), element))
}

proof (element) {
proof(element) {
let index = this.elements.findIndex(e => e.equals(element))

if (index === -1) {
throw new Error('element not found in merkle tree')
}

return this.layers.reduce((proof, layer) => {
let pair = getPair(index, layer)
const pair = getPair(index, layer)
if (pair) {
proof.push(pair)
}
Expand Down
14 changes: 7 additions & 7 deletions js/ensureTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ const { BN } = require('bn.js')

function Uint256(x) {
const bn = new BN(x, 10)
if (bn.isNeg()) throw 'uint256 expected, negative number given'
if (bn.isNeg()) throw new Error('uint256 expected, negative number given')
return bn
}
function Address(x) {
if (!(typeof(x) === 'string' && x.length === 42 && x.startsWith('0x')))
throw 'invalid address: must start with a 0x and be 42 characters long'
if (!(typeof x === 'string' && x.length === 42 && x.startsWith('0x')))
throw new Error('invalid address: must start with a 0x and be 42 characters long')
return x
}
function Bytes32(b) {
if (!(b.length === 32 && Buffer.isBuffer(b))) throw '32 byte Buffer expected'
if (!(b.length === 32 && Buffer.isBuffer(b))) throw new Error('32 byte Buffer expected')
return b
}
function Bytes(b) {
if (typeof(b) === 'string' && b.startsWith('0x')) {
b = new Buffer(b.slice(2), 'hex')
if (typeof b === 'string' && b.startsWith('0x')) {
return Buffer.from(b.slice(2), 'hex')
}
if (!Buffer.isBuffer(b)) throw 'Buffer expected'
if (!Buffer.isBuffer(b)) throw new Error('Buffer expected')
return b
}

Expand Down
7 changes: 5 additions & 2 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const channelLib = require('./Channel')
const identityLib = require('./Identity')
const MerkleTree = require('./MerkleTree')
const splitSig = require('./splitSig')

module.exports = {
...channelLib,
...identityLib,
MerkleTree: require('./MerkleTree'),
splitSig: require('./splitSig'),
MerkleTree,
splitSig
}
12 changes: 7 additions & 5 deletions js/splitSig.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
function splitSig(sig) {
if (sig.startsWith('0x')) sig = sig.slice(2)
const r = '0x' + sig.substring(0, 64)
const s = '0x' + sig.substring(64, 128)
function splitSig(inputSig) {
const sig = inputSig.startsWith('0x') ? inputSig.slice(2) : inputSig
const r = `0x${sig.substring(0, 64)}`
const s = `0x${sig.substring(64, 128)}`
let v = parseInt(sig.substring(128, 130), 16)
if (v < 27) v += 27
// 02 mode is GETH
const pack = '0x'+'02'+v.toString(16)+'000000000000000000000000000000000000000000000000000000000000'
const pack = `${'0x02'}${v.toString(
16
)}000000000000000000000000000000000000000000000000000000000000`
return [pack, r, s]
}

Expand Down

0 comments on commit 361ec73

Please sign in to comment.