chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
47 lines • 1.61 kB
JavaScript
import * as bip39 from '@scure/bip39';
import { mnemonicToSeed } from '@scure/bip39';
import { WordsList } from '../../../implementations/PhraseWallet/WordsList';
import { Secret } from '../Secret';
import { Seed } from './Seed';
import { generateNewPhrase } from '../../../implementations/PhraseWallet/PhraseGenerator';
export class PhraseEncodingError extends Error {
constructor(message) {
super(message);
if (Error.captureStackTrace)
Error.captureStackTrace(this, PhraseEncodingError);
this.name = this.constructor.name;
}
}
export class Phrase extends Secret {
phrase;
constructor(phrase) {
super();
if (!Phrase.isValidPhrase(phrase))
throw new Error('Invalid phrase');
this.phrase = phrase;
}
async getSeed() {
return new Seed(await mnemonicToSeed(await this.getPhrase()));
}
async getPhrase() {
return new TextDecoder().decode(this.raw);
}
static isValidPhrase(phrase) {
for (const language of Object.keys(WordsList))
if (bip39.validateMnemonic(phrase, WordsList[language]))
return true;
return false;
}
static generateNewPhrase(language = 'english', numberOfWords = 12) {
return generateNewPhrase(language, numberOfWords);
}
get raw() {
return new TextEncoder().encode(this.phrase);
}
static async new(source) {
if (!Phrase.isValidPhrase(source))
throw new PhraseEncodingError('Invalid phrase');
return new Phrase(source);
}
}
//# sourceMappingURL=Phrase.js.map