Skip to content

Commit

Permalink
Split NodeJS and browser setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Oct 28, 2020
1 parent 177727a commit fbc500c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 45 deletions.
7 changes: 7 additions & 0 deletions bls-demo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// Get random source
const crypto = window.crypto || window.msCrypto
const getRandomValues = x => crypto.getRandomValues(x)
// Setup BLS
const bls = {}
blsSetupFactory(createBlsModule, getRandomValues, bls)

function getValue (name) { return document.getElementsByName(name)[0].value }
function setValue (name, val) { document.getElementsByName(name)[0].value = val }
function getText (name) { return document.getElementsByName(name)[0].innerText }
Expand Down
73 changes: 29 additions & 44 deletions bls.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
(generator => {
if (typeof window === 'object') {
const exports = {}
window.bls = generator(exports, false)
} else {
generator(exports, true)
}
})((exports, isNodeJs) => {
/**
* @param createBlsModule Async factory that returns an emcc initialized Module
* In node, `const createBlsModule = require(`./bls_c.js`)`
* @param getRandomValues Function to get crypto quality random values
* @param exports Reference to an object to fill BLS methods
*/
function blsSetupFactory(createBlsModule, getRandomValues, exports) {
/* eslint-disable */
exports.BN254 = 0
exports.BN381_1 = 1
Expand All @@ -15,7 +14,7 @@
exports.ETH_MODE_DRAFT_06 = 2
exports.ETH_MODE_DRAFT_07 = 3

const setup = (exports, curveType) => {
function blsSetup(exports, curveType) {
const mod = exports.mod
const MCLBN_FP_UNIT_SIZE = 6
const MCLBN_FR_UNIT_SIZE = exports.ethMode ? 4 : 6
Expand Down Expand Up @@ -630,7 +629,8 @@
if (exports.ethMode) {
exports.setETHmode(exports.ETH_MODE_DRAFT_07)
}
} // setup()
} // blsSetup()

const _cryptoGetRandomValues = function(p, n) {
const a = new Uint8Array(n)
exports.getRandomValues(a)
Expand All @@ -642,41 +642,26 @@
exports.setRandFunc = f => {
exports.getRandomValues = f
}
exports.init = (curveType = exports.BN254) => {
exports.init = async (curveType = exports.BN254) => {
exports.curveType = curveType
const name = 'bls_c'
return new Promise(resolve => {
if (isNodeJs) {
const crypto = require('crypto')
exports.getRandomValues = crypto.randomFillSync
const path = require('path')
const js = require(`./${name}.js`)
const Module = {
cryptoGetRandomValues : _cryptoGetRandomValues,
locateFile: baseName => { return path.join(__dirname, baseName) }
}
js(Module)
.then(_mod => {
exports.mod = _mod
setup(exports, curveType)
resolve()
})
} else {
const crypto = window.crypto || window.msCrypto
exports.getRandomValues = x => crypto.getRandomValues(x)
fetch(`./${name}.wasm`) // eslint-disable-line
.then(response => response.arrayBuffer())
.then(buffer => new Uint8Array(buffer))
.then(() => {
exports.mod = Module() // eslint-disable-line
exports.mod.cryptoGetRandomValues = _cryptoGetRandomValues
exports.mod.onRuntimeInitialized = () => {
setup(exports, curveType)
resolve()
}
})
}
exports.getRandomValues = getRandomValues;
exports.mod = await createBlsModule({
cryptoGetRandomValues: _cryptoGetRandomValues,
})
blsSetup(exports, curveType)
}
return exports
})
}

// NodeJS export
if (typeof exports === 'object' && typeof module === 'object')
module.exports = blsSetupFactory;
else if (typeof define === 'function' && define['amd'])
define([], function() { return blsSetupFactory; });
else if (typeof exports === 'object')
exports["blsSetupFactory"] = blsSetupFactory;

// Browser export
if (typeof window === 'object') {
window.blsSetupFactory = blsSetupFactory;
}
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const crypto = require('crypto')
const createBlsModule = require(`./bls_c.js`)
const blsSetupFactory = require('./bls')

const bls = {}

const getRandomValues = crypto.randomFillSync
blsSetupFactory(createBlsModule, getRandomValues, bls)

module.exports = bls;
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'
const bls = require('./bls.js')
const bls = require('./index.js')
const assert = require('assert')
const fs = require('fs')
const readline = require('readline')
Expand Down

0 comments on commit fbc500c

Please sign in to comment.