Amend Primitives/Tooling

This commit is contained in:
Jim Hamill
2025-01-13 09:43:11 +00:00
parent 06c9d52b56
commit f53fc4b456
29 changed files with 817 additions and 463 deletions
+34 -12
View File
@@ -1,6 +1,7 @@
import {
generateBip39MnemonicNonStandard,
bip39WordListEnglish,
deriveSeedFromBip39Mnemonic,
generateBip39MnemonicNonStandard,
} from '@bitauth/libauth';
export type MnemonicRaw = {
@@ -20,16 +21,19 @@ export class Mnemonic {
*
* @param phrase The complete phrase (mnemonic words + optional passphrase).
* @param wordList Optional wordlist (defaults to English BIP-39 wordlist).
*
* @throws {Error} If the mnemonic is invalid.
*/
constructor(private mnemonic: MnemonicRaw) {}
protected constructor(private mnemonic: MnemonicRaw) {}
/**
* Instantiates a mnemonic from a string or array of words
*
* @param phraseOrWords The mnemonic phrase as a string or array of words
* @param dictionary Optional wordlist (defaults to English BIP-39 wordlist).
* @param wordList Optional wordlist (defaults to English BIP-39 wordlist).
*
* @throws {Error} If mnemonic could not be generated.
*
* @returns Instance of Mnemonic.
*/
public static from(
@@ -41,18 +45,20 @@ export class Mnemonic {
return this.fromWords(phraseOrWords, wordList);
}
return this.fromString(phraseOrWords, wordList);
return this.fromPhrase(phraseOrWords, wordList);
}
/**
* Instantiates a mnemonic from a string
*
* @param phrase The mnemonic phrase as a string
* @param dictionary Optional wordlist (defaults to English BIP-39 wordlist).
* @param wordList Optional wordlist (defaults to English BIP-39 wordlist).
*
* @throws {Error} If mnemonic could not be generated.
*
* @returns Instance of Mnemonic.
*/
public static fromString(
public static fromPhrase(
phrase: string,
wordList: Array<string> = bip39WordListEnglish,
) {
@@ -69,8 +75,10 @@ export class Mnemonic {
* Instantiates a mnemonic from an array of words.
*
* @param words Array of words (strings) that represent the mnemonic
* @param dictionary Optional wordlist (defaults to English BIP-39 wordlist).
* @param wordList Optional wordlist (defaults to English BIP-39 wordlist).
*
* @throws {Error} If mnemonic could not be generated.
*
* @returns Instance of Mnemonic.
*/
public static fromWords(
@@ -133,6 +141,7 @@ export class Mnemonic {
*
* @param mnemonicRaw The raw menmonic data.
* @throws {Error} If mnemonic could not be generated.
*
* @returns Instance of Mnemonic.
*/
public static fromRaw(mnemonicRaw: MnemonicRaw) {
@@ -144,6 +153,7 @@ export class Mnemonic {
*
* @param dictionary Optional wordlist (defaults to English BIP-39 wordlist).
* @throws {Error} If mnemonic could not be generated.
*
* @returns Instance of Mnemonic.
*/
public static generateRandom(
@@ -173,6 +183,7 @@ export class Mnemonic {
*
* @param phraseOrWords The mnemonic phrase or words to validate.
* @param wordList Optional wordlist (defaults to English BIP-39 wordlist).
*
* @returns boolean indicating if the phrase is valid.
*/
public static isValid(
@@ -192,6 +203,7 @@ export class Mnemonic {
*
* @param word Word to check.
* @param dictionary Optional wordlist (defaults to English BIP-39 wordlist).
*
* @returns boolean indicating if the word is valid.
*/
public static isValidWord(
@@ -202,21 +214,22 @@ export class Mnemonic {
}
/**
* Gets the passphrase if one exists.
* Returns the resulting seed for this mnemonic.
*
* @returns The passphrase or undefined if none exists.
* @returns The seed (for use with HDPrivateNodes).
*/
public getPassphrase(): string | undefined {
return this.mnemonic.passphrase;
public toSeed(): Uint8Array {
return deriveSeedFromBip39Mnemonic(this.toPhrase());
}
/**
* Returns the complete phrase as a string.
*
* @param includePassphrase Whether to include the passphrase in the output.
*
* @returns Complete phrase as a string.
*/
public toString(includePassphrase: boolean = true): string {
public toPhrase(includePassphrase: boolean = true): string {
// Join the menmonic words, separating them using a space character.
const mnemonicWords = this.mnemonic.words.join(' ');
@@ -252,4 +265,13 @@ export class Mnemonic {
public toRaw(): MnemonicRaw {
return { ...this.mnemonic };
}
/**
* Gets the passphrase if one exists.
*
* @returns The passphrase or undefined if none exists.
*/
public getPassphrase(): string | undefined {
return this.mnemonic.passphrase;
}
}