WIP (BlockchainLocal working)

Signed-off-by: James Zuccon <zuccon@gmail.com>
This commit is contained in:
James Zuccon
2024-09-12 21:33:11 +10:00
parent 52da32ad4d
commit e308206d9f
33 changed files with 1274 additions and 397 deletions
+42 -24
View File
@@ -7,6 +7,7 @@ import {
deriveHdPath,
deriveHdPublicNode,
deriveHdPrivateNodeFromSeed,
deriveSeedFromBip39Mnemonic,
generateRandomBytes,
} from '@bitauth/libauth';
@@ -26,38 +27,16 @@ export class HDPrivateNode {
/**
* Construct a new HD Private Node.
*
* @param node {HdPrivateNodeValid} The HD Private Node.
* @param node The HD Private Node.
*/
constructor(node: HdPrivateNodeValid) {
this.node = node;
}
/**
* Creates a HD Private Node from the given XPriv Key.
*
* @param xpriv {string} The XPriv Key.
*
* @throws {Error} If HD Private Node cannot be created.
*
* @returns {HDPrivateNode} The created HD Private Node.
*/
public static fromXPriv(xpriv: string): HDPrivateNode {
// Attempt to decode the XPriv Key.
const decodeResult = decodeHdPrivateKey(xpriv);
// If a string is returned, this indicates an error...
if (typeof decodeResult === 'string') {
throw new Error(decodeResult);
}
// Return a new HD Private Node from the given XPriv.
return new HDPrivateNode(decodeResult.node);
}
/**
* Creates a HD Private Node from a raw seed.
*
* @param seed {Uint8Array} The raw seed bytes.
* @param seed The raw seed bytes.
*
* @throws {Error} If HD Private Node cannot be created.
*
@@ -83,6 +62,45 @@ export class HDPrivateNode {
return new HDPrivateNode(deriveResult);
}
/**
* Creates a HD Private Node from the given mnemonic.
*
* @param mnemonic The mnemonic.
*
* @throws {Error} If HD Private Node cannot be created.
*
* @returns {HDPrivateNode} The created HD Private Node.
*/
public static fromMnemonic(mnemonic: string): HDPrivateNode {
// Derive the seed from the mnemonic.
const seed = deriveSeedFromBip39Mnemonic(mnemonic);
// Return a new HDPrivateNode from the given seed.
return HDPrivateNode.fromSeed(seed);
}
/**
* Creates a HD Private Node from the given XPriv Key.
*
* @param xpriv The XPriv Key.
*
* @throws {Error} If HD Private Node cannot be created.
*
* @returns {HDPrivateNode} The created HD Private Node.
*/
public static fromXPriv(xpriv: string): HDPrivateNode {
// Attempt to decode the XPriv Key.
const decodeResult = decodeHdPrivateKey(xpriv);
// If a string is returned, this indicates an error...
if (typeof decodeResult === 'string') {
throw new Error(decodeResult);
}
// Return a new HD Private Node from the given XPriv.
return new HDPrivateNode(decodeResult.node);
}
/**
* Generates a random HD Private Node.
*