Amend Primitives/Tooling
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { Bytes } from './bytes.js';
|
||||
import { HDPublicNode } from './hd-public-node.js';
|
||||
import { Mnemonic } from './mnemonic.js';
|
||||
import { PrivateKey } from './private-key.js';
|
||||
|
||||
import {
|
||||
@@ -8,7 +10,6 @@ import {
|
||||
deriveHdPathRelative,
|
||||
deriveHdPublicNode,
|
||||
deriveHdPrivateNodeFromSeed,
|
||||
deriveSeedFromBip39Mnemonic,
|
||||
generateRandomBytes,
|
||||
} from '@bitauth/libauth';
|
||||
|
||||
@@ -30,7 +31,7 @@ export class HDPrivateNode {
|
||||
*
|
||||
* @param node The HD Private Node.
|
||||
*/
|
||||
constructor(node: HdPrivateNodeValid) {
|
||||
protected constructor(node: HdPrivateNodeValid) {
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
@@ -43,27 +44,18 @@ export class HDPrivateNode {
|
||||
*
|
||||
* @returns The created HD Private Node.
|
||||
*/
|
||||
public static fromBytes(seed: Uint8Array): HDPrivateNode {
|
||||
// Return a new HD Private Node from the given seed.
|
||||
return this.fromSeed(seed);
|
||||
}
|
||||
public static fromSeed(seed: Uint8Array | string): HDPrivateNode {
|
||||
// Cast from hex if a string was provided.
|
||||
if (typeof seed === 'string') {
|
||||
seed = Bytes.fromHex(seed).toBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a HD Private Node from a raw seed.
|
||||
*
|
||||
* @param seed The raw seed bytes.
|
||||
*
|
||||
* @throws {Error} If HD Private Node cannot be created.
|
||||
*
|
||||
* @returns The created HD Private Node.
|
||||
*/
|
||||
public static fromSeed(seed: Uint8Array): HDPrivateNode {
|
||||
// If the seed is not 32 bytes, throw an error.
|
||||
/*if (seed.length !== 32) {
|
||||
// If the seed is not 64 bytes, throw an error.
|
||||
if (seed.length !== 64) {
|
||||
throw new Error(
|
||||
`Failed to derive Private Node from seed: Seed must be 32 bytes (${seed.length} given)`
|
||||
`Failed to derive Private Node from seed: Seed must be 64 bytes (${seed.length} given)`,
|
||||
);
|
||||
}*/
|
||||
}
|
||||
|
||||
// Attempt to derive HD Private Node from the given seed.
|
||||
const deriveResult = deriveHdPrivateNodeFromSeed(seed);
|
||||
@@ -80,15 +72,15 @@ export class HDPrivateNode {
|
||||
/**
|
||||
* Creates a HD Private Node from the given mnemonic.
|
||||
*
|
||||
* @param mnemonic The mnemonic.
|
||||
* @param phrase The mnemonic phrase as a string or array of words.
|
||||
*
|
||||
* @throws {Error} If HD Private Node cannot be created.
|
||||
*
|
||||
* @returns The created HD Private Node.
|
||||
*/
|
||||
public static fromMnemonic(mnemonic: string): HDPrivateNode {
|
||||
public static fromMnemonic(phrase: string | Array<string>): HDPrivateNode {
|
||||
// Derive the seed from the mnemonic.
|
||||
const seed = deriveSeedFromBip39Mnemonic(mnemonic);
|
||||
const seed = Mnemonic.from(phrase).toSeed();
|
||||
|
||||
// Return a new HDPrivateNode from the given seed.
|
||||
return this.fromSeed(seed);
|
||||
@@ -163,14 +155,74 @@ export class HDPrivateNode {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Private Key of this node (as a Uint8Array).
|
||||
*
|
||||
* @returns The Node's Private Key (as a Uint8Array).
|
||||
*/
|
||||
public toBytes(): Uint8Array {
|
||||
// Encode the XPriv from our node info.
|
||||
return this.node.privateKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Private Key entity for this node.
|
||||
*
|
||||
* @returns The Private Key Entity for this node.
|
||||
*/
|
||||
public privateKey(): PrivateKey {
|
||||
public toPrivateKey(): PrivateKey {
|
||||
// Return a Private Key Entity from the private key of our node.
|
||||
return new PrivateKey(this.node.privateKey);
|
||||
return PrivateKey.fromRaw(this.node.privateKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this node to an XPriv Key for export.
|
||||
*
|
||||
* @param network {HdKeyNetwork} The network to encode for.
|
||||
*
|
||||
* @returns The XPriv Key.
|
||||
*/
|
||||
public toXPriv(network: HdKeyNetwork = 'mainnet'): string {
|
||||
// Create our node info structure.
|
||||
const nodeInfo: DecodedHdKey<HdPrivateNodeValid> = {
|
||||
network: network,
|
||||
node: this.node,
|
||||
};
|
||||
|
||||
// Encode the XPriv from our node info.
|
||||
return encodeHdPrivateKey(nodeInfo).hdPrivateKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the XPriv string for this node.
|
||||
*
|
||||
* @returns The XPriv string for this node.
|
||||
*/
|
||||
public toString(): string {
|
||||
// Return the Mainnet XPriv.
|
||||
return this.toXPriv();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the raw node data.
|
||||
*
|
||||
* @returns The raw node data.
|
||||
*/
|
||||
public toRaw(): HdPrivateNodeValid {
|
||||
return this.node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Derives a HD Public Node from this Private Node.
|
||||
*
|
||||
* @returns The derived HD Public Node
|
||||
*/
|
||||
public deriveHDPublicNode(): HDPublicNode {
|
||||
// Derive a HD Public Node from this HD Private Node.
|
||||
const publicNode = deriveHdPublicNode(this.node);
|
||||
|
||||
// Return a new HD Public Node entity from our derived Public Node.
|
||||
return HDPublicNode.fromRaw(publicNode);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -216,64 +268,4 @@ export class HDPrivateNode {
|
||||
// Return a new HD Private Node Entity derived from the given path.
|
||||
return new HDPrivateNode(derivePathResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Derives a HD Public Node from this Private Node.
|
||||
*
|
||||
* @returns The derived HD Public Node
|
||||
*/
|
||||
public deriveHDPublicNode(): HDPublicNode {
|
||||
// Derive a HD Public Node from this HD Private Node.
|
||||
const publicNode = deriveHdPublicNode(this.node);
|
||||
|
||||
// Return a new HD Public Node entity from our derived Public Node.
|
||||
return new HDPublicNode(publicNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Private Key of this node (as a Uint8Array).
|
||||
*
|
||||
* @returns The Node's Private Key (as a Uint8Array).
|
||||
*/
|
||||
public toBytes(): Uint8Array {
|
||||
// Encode the XPriv from our node info.
|
||||
return this.node.privateKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this node to an XPriv Key for export.
|
||||
*
|
||||
* @param network {HdKeyNetwork} The network to encode for.
|
||||
*
|
||||
* @returns The XPriv Key.
|
||||
*/
|
||||
public toXPriv(network: HdKeyNetwork = 'mainnet'): string {
|
||||
// Create our node info structure.
|
||||
const nodeInfo: DecodedHdKey<HdPrivateNodeValid> = {
|
||||
network: network,
|
||||
node: this.node,
|
||||
};
|
||||
|
||||
// Encode the XPriv from our node info.
|
||||
return encodeHdPrivateKey(nodeInfo).hdPrivateKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the XPriv string for this node.
|
||||
*
|
||||
* @returns The XPriv string for this node.
|
||||
*/
|
||||
public toString(): string {
|
||||
// Return the Mainnet XPriv.
|
||||
return this.toXPriv();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the raw node data.
|
||||
*
|
||||
* @returns The raw node data.
|
||||
*/
|
||||
public toRaw(): HdPrivateNodeValid {
|
||||
return this.node;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user