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
+48 -35
View File
@@ -24,7 +24,7 @@ export class HDPublicNode {
*
* @param node {HdPublicNode} The HD Public Node.
*/
constructor(node: HdPublicNodeValid) {
protected constructor(node: HdPublicNodeValid) {
this.node = node;
}
@@ -50,6 +50,14 @@ export class HDPublicNode {
return new HDPublicNode(decodeResult.node);
}
/**
* Creates a HDPublicNode instance from a raw object representation (LibAuth's HdPublicNodeValid).
*
* @remarks This method is UNSAFE and MUST only be used where input is already verified or trusted.
*
* @param obj - LibAuth Object of type HdPublicNodeValid
* @returns A new HDPublicNode instance
*/
public static fromRaw(node: HdPublicNodeValid) {
return new this(node);
}
@@ -68,9 +76,46 @@ export class HDPublicNode {
*
* @returns The Public Key Entity for this node.
*/
public publicKey(): PublicKey {
public toPublicKey(): PublicKey {
// Return a Public Key Entity from the public key of our node.
return new PublicKey(this.node.publicKey);
return PublicKey.fromRaw(this.node.publicKey);
}
/**
* Converts this node to an XPub Key for export.
*
* @param network The network to encode for.
*
* @returns The XPub Key.
*/
public toXPub(network: HdKeyNetwork = 'mainnet'): string {
// Create our node info structure.
const nodeInfo = {
network: network,
node: this.node,
} as DecodedHdKey<HdPublicNodeValid>;
// Encode the XPub from our node info.
return encodeHdPublicKey(nodeInfo).hdPublicKey;
}
/**
* Returns the XPub string for this node.
*
* @returns {string} The XPub string for this node.
*/
public toString(): string {
// Return the Mainnet XPub.
return this.toXPub();
}
/**
* Converts the HDPublicKey to its raw Libauth representation (HDPublicNodeValid).
*
* @returns A LibAuth HDPublicNodeValid object
*/
public toRaw(): HdPublicNodeValid {
return { ...this.node };
}
/**
@@ -94,36 +139,4 @@ export class HDPublicNode {
// Return a new HD Public Node Entity derived from the given path.
return new HDPublicNode(derivePathResult);
}
/**
* Converts this node to an XPub Key for export.
*
* @param network The network to encode for.
*
* @returns The XPub Key.
*/
public toXPub(network: HdKeyNetwork = 'mainnet'): string {
// Create our node info structure.
const nodeInfo = {
network: network,
node: this.node,
} as DecodedHdKey<HdPublicNodeValid>;
// Encode the XPub from our node info.
return encodeHdPublicKey(nodeInfo).hdPublicKey;
}
public toRaw(): HdPublicNodeValid {
return this.node;
}
/**
* Returns the XPub string for this node.
*
* @returns {string} The XPub string for this node.
*/
public toString(): string {
// Return the Mainnet XPub.
return this.toXPub();
}
}