Skip to content

Commit

Permalink
Changes to support mobile apps
Browse files Browse the repository at this point in the history
- Pair Device
- 2fa override below limits
- 2fa backup codes
- migrate 2fa to new device
  • Loading branch information
Administrator committed Feb 8, 2015
1 parent 6162b0f commit 5b5f8e5
Show file tree
Hide file tree
Showing 22 changed files with 7,621 additions and 3,578 deletions.
88 changes: 44 additions & 44 deletions src/bip39.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

var CryptoJS = require('crypto-js')
var crypto = require('crypto')
var CryptoJS = require('crypto-js');
var crypto = require('crypto');

function BIP39(language) {
language = language || 'en'
language = language || 'en';
this.wordlist = [
"abandon",
"ability",
Expand Down Expand Up @@ -2057,101 +2057,101 @@ function BIP39(language) {
}

BIP39.prototype.mnemonicToSeed = function(mnemonic, password) {
var options = {iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32}
var options = {iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32};
return CryptoJS.PBKDF2(mnemonic, salt(password), options).toString(CryptoJS.enc.Hex)
}
};

BIP39.prototype.entropyToMnemonic = function(entropy) {
var entropyBuffer = new Buffer(entropy, 'hex')
var entropyBits = bytesToBinary([].slice.call(entropyBuffer))
var checksum = checksumBits(entropyBuffer)
var entropyBuffer = new Buffer(entropy, 'hex');
var entropyBits = bytesToBinary([].slice.call(entropyBuffer));
var checksum = checksumBits(entropyBuffer);

var bits = entropyBits + checksum
var chunks = bits.match(/(.{1,11})/g)
var bits = entropyBits + checksum;
var chunks = bits.match(/(.{1,11})/g);

var words = chunks.map(function(binary) {
var index = parseInt(binary, 2)
var index = parseInt(binary, 2);

return this.wordlist[index]
}, this)
}, this);

return words.join(' ')
}
};


BIP39.prototype.mnemonicToHex = function (mnemonic) {
var words = mnemonic.split(' ')
var words = mnemonic.split(' ');

if (words.length % 3 !== 0) return false
if (words.length % 3 !== 0) return false;

var wordlist = this.wordlist
var wordlist = this.wordlist;
var belongToList = words.every(function (word) {
return wordlist.indexOf(word) > -1
})
});

if (!belongToList) return false
if (!belongToList) return false;

// convert word indices to 11 bit binary strings
var bits = words.map(function (word) {
var index = wordlist.indexOf(word)
var index = wordlist.indexOf(word);
return lpad(index.toString(2), '0', 11)
}).join('')
}).join('');

// split the binary string into ENT/CS
var dividerIndex = Math.floor(bits.length / 33) * 32
var entropy = bits.slice(0, dividerIndex)
var checksum = bits.slice(dividerIndex)
var dividerIndex = Math.floor(bits.length / 33) * 32;
var entropy = bits.slice(0, dividerIndex);
var checksum = bits.slice(dividerIndex);

// calculate the checksum and compare
var entropyBytes = entropy.match(/(.{1,8})/g).map(function (bin) {
return parseInt(bin, 2)
})
var entropyBuffer = new Buffer(entropyBytes)
});
var entropyBuffer = new Buffer(entropyBytes);

return entropyBuffer.toString('hex');

}
};


BIP39.prototype.validate = function(mnemonic) {
var words = mnemonic.split(' ')
var words = mnemonic.split(' ');

if (words.length % 3 !== 0) return false
if (words.length % 3 !== 0) return false;

var wordlist = this.wordlist
var wordlist = this.wordlist;
var belongToList = words.every(function(word) {
return wordlist.indexOf(word) > -1
})
});

if (!belongToList) return false
if (!belongToList) return false;

// convert word indices to 11 bit binary strings
var bits = words.map(function(word) {
var index = wordlist.indexOf(word)
var index = wordlist.indexOf(word);
return lpad(index.toString(2), '0', 11)
}).join('')
}).join('');

// split the binary string into ENT/CS
var dividerIndex = Math.floor(bits.length / 33) * 32
var entropy = bits.slice(0, dividerIndex)
var checksum = bits.slice(dividerIndex)
var dividerIndex = Math.floor(bits.length / 33) * 32;
var entropy = bits.slice(0, dividerIndex);
var checksum = bits.slice(dividerIndex);

// calculate the checksum and compare
var entropyBytes = entropy.match(/(.{1,8})/g).map(function(bin) {
return parseInt(bin, 2)
})
var entropyBuffer = new Buffer(entropyBytes)
var newChecksum = checksumBits(entropyBuffer)
});
var entropyBuffer = new Buffer(entropyBytes);
var newChecksum = checksumBits(entropyBuffer);

return newChecksum === checksum
}
};

function checksumBits(entropyBuffer) {
var hash = crypto.createHash('sha256').update(entropyBuffer).digest()
var hash = crypto.createHash('sha256').update(entropyBuffer).digest();

// Calculated constants from BIP39
var ENT = entropyBuffer.length * 8
var CS = ENT / 32
var ENT = entropyBuffer.length * 8;
var CS = ENT / 32;

return bytesToBinary([].slice.call(hash)).slice(0, CS)
}
Expand All @@ -2177,4 +2177,4 @@ function lpad(str, padString, length) {
return str;
}

module.exports = BIP39
module.exports = BIP39;
6 changes: 3 additions & 3 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ var assert = function(condition, message) {
if (!condition) {
throw message || "Assertion failed";
}
}
};

//Checks if GUID is valid. Only takes lowercase, non-bracketed GUIDs.
var isRealGuid = function(potentialGuidAsString){
if (!potentialGuidAsString) return false;
if (typeof potentialGuidAsString != 'string') return false;
if (potentialGuidAsString.length==0) return false;

var guidRegex=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
var guidRegex=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
var match = potentialGuidAsString.match(guidRegex);
return match ? true:false;
}
};

var encrypt = function (valueToEncrypt, passphrase) {
assert(valueToEncrypt, "valueToEncrypt invalid");
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var config = {
port:"1111",
basePath:""
}
}
};

config.thisServer.baseUrl=config.thisServer.protocol+'://'+
config.thisServer.hostname+':'+config.thisServer.port+
Expand Down
2 changes: 1 addition & 1 deletion src/index-mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ module.exports = {
API: require('./ninki-api'),
Engine: require('./ninki-engine'),
UI: require('./ninki-ui-mobile')
}
};
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ module.exports = {
API: require('./ninki-api'),
Engine: require('./ninki-engine'),
UI: require('./ninki-ui')
}
};
Loading

0 comments on commit 5b5f8e5

Please sign in to comment.