Skip to content

Commit

Permalink
Init config for argon2 in browser (#368)
Browse files Browse the repository at this point in the history
argon2 browser support
  • Loading branch information
nduchak authored and ricricucit committed May 10, 2019
1 parent 956e59e commit 5fe6247
Show file tree
Hide file tree
Showing 11 changed files with 28,777 additions and 138 deletions.
14 changes: 4 additions & 10 deletions es/contract/aci.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ const SOPHIA_TYPES = [
'list',
'map',
'record'
].reduce((acc, type, i) => {
acc[type] = type
return acc
}, {})
].reduce((acc, type) => ({ ...acc, [type]: type }), {})

function encodeAddress (address, prefix = 'ak') {
const addressBuffer = Buffer.from(address, 'hex')
Expand Down Expand Up @@ -118,11 +115,8 @@ function transform (type, value) {
return `#${decode(value).toString('hex')}`
case SOPHIA_TYPES.record:
return `{${generic.reduce(
(acc, { name, type }, i) => {
if (i !== 0) acc += ','
acc += `${name} = ${transform(type[0], value[name])}`
return acc
},
(acc, { name, type }, i) =>
(acc += `${i !== 0 ? ',' : ''}${name} = ${transform(type[0], value[name])}`),
''
)}}`
case SOPHIA_TYPES.map:
Expand Down Expand Up @@ -162,7 +156,7 @@ function readType (type, returnType = false) {
// Base types
if (typeof t === 'string') return { t }

// Map, Tuple, List
// Map, Tuple, List, Record
if (typeof t === 'object') {
const [[baseType, generic]] = Object.entries(t)
return { t: baseType, generic }
Expand Down
27 changes: 24 additions & 3 deletions es/utils/keystore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import nacl from 'tweetnacl'
import * as argon2 from 'argon2'
import uuid from 'uuid'

import { encodeBase58Check, isBase64 } from './crypto'
Expand Down Expand Up @@ -29,9 +28,31 @@ const DERIVED_KEY_FUNCTIONS = {
'argon2id': deriveKeyUsingArgon2id
}

async function deriveKeyUsingArgon2id (password, salt, options) {
export async function deriveKeyUsingArgon2id (password, salt, options) {
const { memlimit_kib: memoryCost, parallelism, opslimit: timeCost } = options.kdf_params
return argon2.hash(password, { timeCost, memoryCost, parallelism, type: argon2.argon2id, raw: true, salt })
const isBrowser = !(typeof module !== 'undefined' && module.exports)

if (isBrowser) {
const _sodium = require('libsodium-wrappers-sumo')

return _sodium.ready.then(async () => {
// tslint:disable-next-line:typedef
const sodium = _sodium

const result = sodium.crypto_pwhash(
32,
password,
salt,
timeCost,
memoryCost * 1024,
sodium.crypto_pwhash_ALG_ARGON2ID13
)
return Buffer.from(result)
})
} else {
const argon2 = require('argon2')
return argon2.hash(password, { timeCost, memoryCost, parallelism, type: argon2.argon2id, raw: true, salt })
}
}

// CRYPTO PART
Expand Down
Loading

0 comments on commit 5fe6247

Please sign in to comment.