Skip to content
This repository has been archived by the owner on Jun 6, 2019. It is now read-only.

Commit

Permalink
Refactor to use brave-crypto for passphrases (#52)
Browse files Browse the repository at this point in the history
fix #50
  • Loading branch information
diracdeltas authored Apr 13, 2018
1 parent dd36177 commit 75f0aa4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 48 deletions.
36 changes: 5 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ const path = require('path')
const querystring = require('querystring')
const url = require('url')
const braveCrypto = require('brave-crypto')
const passphraseUtil = braveCrypto.passphrase

const anonize = require('node-anonize2-relic-emscripten')
const backoff = require('@ambassify/backoff-strategies')
const balance = require('bat-balance')
const bg = require('bitgo')
const Joi = require('joi')
const niceware = require('niceware')
const bip39 = require('bip39')
const random = require('random-lib')
const { sign } = require('http-request-signature')
const stringify = require('json-stable-stringify')
Expand All @@ -21,8 +20,6 @@ const uuid = require('uuid')

const batPublisher = require('bat-publisher')

const NICEWARE_PASSPHRASE_LENGTH = 16
const BIP39_PASSPHRASE_LENGTH = 24
const SEED_LENGTH = 32
const HKDF_SALT = new Uint8Array([ 126, 244, 99, 158, 51, 68, 253, 80, 133, 183, 51, 180, 77, 62, 74, 252, 62, 106, 96, 125, 241, 110, 134, 87, 190, 208, 158, 84, 125, 69, 246, 207, 162, 247, 107, 172, 37, 34, 53, 246, 105, 20, 215, 5, 248, 154, 179, 191, 46, 17, 6, 72, 210, 91, 10, 169, 145, 248, 22, 147, 117, 24, 105, 12 ])
const LEDGER_SERVERS = {
Expand Down Expand Up @@ -525,29 +522,17 @@ Client.prototype.getWalletPassphrase = function (state, options) {

if ((wallet.keyinfo) && (wallet.keyinfo.seed)) {
const seed = Buffer.from(wallet.keyinfo.seed)
const passPhrase = passphraseUtil.fromBytesOrHex(seed, options && options.useNiceware)

if (options && options.useNiceware === true) {
return niceware.bytesToPassphrase(seed)
}

const passPhrase = bip39.entropyToMnemonic(seed.toString('hex'))
return passPhrase.split(' ')
return passPhrase && passPhrase.split(' ')
}
}

Client.prototype.recoverKeypair = function (passPhrase) {
var seed
this._log('recoverKeypair')
const passPhraseWords = passPhrase.split(' ')
if (![NICEWARE_PASSPHRASE_LENGTH, BIP39_PASSPHRASE_LENGTH].includes(passPhraseWords.length)) {
throw new Error(`invalid passphrase: must be either ${BIP39_PASSPHRASE_LENGTH} or ${NICEWARE_PASSPHRASE_LENGTH} words`)
}
try {
if (passPhraseWords.length === BIP39_PASSPHRASE_LENGTH) {
seed = Buffer.from(bip39.mnemonicToEntropy(passPhrase), 'hex')
} else {
seed = niceware.passphraseToBytes(passPhraseWords)
}
seed = Buffer.from(passphraseUtil.toBytes32(passPhrase))
} catch (ex) {
throw new Error('invalid passphrase:' + ex.toString())
}
Expand All @@ -567,19 +552,8 @@ Client.prototype.isValidPassPhrase = function (passPhrase) {
return false
}

const passPhraseWords = passPhrase.split(' ')

if (![NICEWARE_PASSPHRASE_LENGTH, BIP39_PASSPHRASE_LENGTH].includes(passPhraseWords.length)) {
this.memo('isValidPassPhrase', `invalid passphrase: must be either ${BIP39_PASSPHRASE_LENGTH} or ${NICEWARE_PASSPHRASE_LENGTH} words`)
return false
}

try {
if (passPhraseWords.length === BIP39_PASSPHRASE_LENGTH) {
Buffer.from(bip39.mnemonicToEntropy(passPhrase), 'hex')
} else {
niceware.passphraseToBytes(passPhraseWords)
}
passphraseUtil.toBytes32(passPhrase)
} catch (ex) {
this.memo('isValidPassPhrase', ex.toString())
return false
Expand Down
28 changes: 15 additions & 13 deletions package-lock.json

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

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bat-client",
"version": "2.2.1",
"version": "2.2.2",
"description": "An example of client code for the BAT.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,13 +32,11 @@
"@ambassify/backoff-strategies": "1.0.0",
"bat-balance": "^1.0.7",
"bat-publisher": "^2.0.9",
"bip39": "^2.5.0",
"bitgo": "^4.15.0",
"brave-crypto": "0.0.1",
"brave-crypto": "^0.1.0",
"http-request-signature": "0.0.2",
"joi": "^13.1.2",
"json-stable-stringify": "1.0.1",
"niceware": "1.0.4",
"node-anonize2-relic-emscripten": "^0.3.3",
"random-lib": "2.1.0",
"underscore": "1.8.3",
Expand Down

0 comments on commit 75f0aa4

Please sign in to comment.