Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
describe('given a passphrase that uses a different wordlist for the passphrase', () => {
const wordlist = Mnemonic.wordlists.spanish;
const passphrase =
'model actor shallow eight glue upper seat lobster reason label enlist bridge';
const passphraseInvalidMnemonicError = [
{
code: 'INVALID_MNEMONIC',
message:
'Passphrase is not a valid mnemonic passphrase. Please check the passphrase.',
expected: true,
actual: false,
},
];
it('should return the array with the error', () => {
return expect(
getPassphraseValidationErrors(passphrase, wordlist),
).to.be.eql(passphraseInvalidMnemonicError);
describe('given a passphrase that uses the correct wordlist for the passphrase', () => {
const wordlist = Mnemonic.wordlists.english;
const passphrase =
'model actor shallow eight glue upper seat lobster reason label enlist bridge';
it('should return an empty array', () => {
return expect(
getPassphraseValidationErrors(passphrase, wordlist),
).to.be.eql(emptyErrorArray);
});
});
generateKeyPhrase() {
const wordsArray = [];
const min = 0;
const max = bip39.wordlists.EN.length;
for (let i = 0; i < 3; i++) {
wordsArray.push(
bip39.wordlists.EN[Math.floor(Math.random() * (max - min + 1)) + min]
);
}
this.secretPhrase = wordsArray.join(' ');
}
}
actual: uppercaseCharacterInPassphrase,
code: 'INVALID_AMOUNT_OF_UPPERCASE_CHARACTER',
expected: expectedUppercaseCharacterCount,
location: locateUppercaseCharacters(passphrase),
message: `Passphrase contains ${uppercaseCharacterInPassphrase} uppercase character instead of expected ${expectedUppercaseCharacterCount}. Please check the passphrase.`,
};
const validationError: PassphraseError = {
actual: false,
code: 'INVALID_MNEMONIC',
expected: true,
message:
'Passphrase is not a valid mnemonic passphrase. Please check the passphrase.',
};
const finalWordList =
wordlists !== undefined ? [...wordlists] : Mnemonic.wordlists.english;
return [
passphraseWordError,
whiteSpaceError,
uppercaseCharacterError,
validationError,
].reduce(
(errorArray: ReadonlyArray, error: PassphraseError) => {
if (
error.code === passphraseWordError.code &&
wordsInPassphrase !== expectedWords
) {
return [...errorArray, error];
}
if (
error.code === whiteSpaceError.code &&
function entropyToMnemonic(entropy, wordlist) {
if (!Buffer.isBuffer(entropy)) entropy = Buffer.from(entropy, 'hex');
wordlist = wordlist || bip39.wordlists.EN;
if (entropy.length % 4 !== 0) throw new TypeError(INVALID_ENTROPY);
var entropyBits = bytesToBinary([].slice.call(entropy));
var checksumBits = deriveChecksumBits(entropy);
var bits = entropyBits + checksumBits;
var chunks = bits.match(/(.{1,11})/g);
var words = chunks.map(function (binary) {
var index = binaryToByte(binary);
return wordlist[index];
});
return wordlist === bip39.wordlists.JA ? words.join('\u3000') : words.join(' ');
}
public promptNextWord(): void {
this.promptedWords = []
const correctWord: SingleWord = this.splittedSecret[this.emptySpot(this.currentWords)]
this.promptedWords.push(correctWord)
const wordList: string[] = bip39.wordlists.EN
for (let i: number = 0; i < ADDITIONAL_WORDS; i++) {
const filteredList: string[] = wordList.filter(
(originalWord: string) => !this.splittedSecret.find((word: SingleWord) => word === originalWord)
)
let hashedWord: string = sha3_256(correctWord)
for (let hashRuns: number = 0; hashRuns <= i; hashRuns++) {
hashedWord = sha3_256(hashedWord)
}
const additionalWord: SingleWord = filteredList[this.stringToIntHash(hashedWord, 0, filteredList.length)]
this.promptedWords.push(additionalWord)
}
public static wordLists(): any[] {
return [
bip39.wordlists.english
/*
bip39.wordlists.chinese_simplified,
bip39.wordlists.chinese_traditional,
bip39.wordlists.french,
bip39.wordlists.italian,
bip39.wordlists.japanese,
bip39.wordlists.korean,
bip39.wordlists.spanish
*/
]
}
constructor(mnemonics: string, wordlist: Array = bip39.wordlists.EN, passphrase: string = '') {
if (!hdKey.validateMnemonics(mnemonics, wordlist)) {
throw new Error('Illegal mnemonic');
}
this.rootPath = hdKey.ROOT_PATH;
this.mnemonics = mnemonics;
this.wordlist = wordlist;
this.passphrase = passphrase;
this.entropy = hdKey.getEntropyFromMnemonics(mnemonics, wordlist);
const { seed, seedHex } = hdKey.getSeedFromMnemonics(mnemonics, passphrase, wordlist);
this.seed = seed;
this.seedHex = seedHex;
this.addressList = {};
}
function deriveAddress({ mnemonics, index = 0, wordlist = bip39.wordlists.EN, passphrase = '' }: {
mnemonics: string;
index: number;
wordlist?: Array;
passphrase?: string;
}): AddressObj {
const { seedHex } = hdKey.getSeedFromMnemonics(mnemonics, passphrase, wordlist);
const { privateKey } = hdKey.deriveKeyPairByIndex(seedHex, index);
return addressLib.createAddressByPrivateKey(privateKey);
}