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
+72 -35
View File
@@ -22,15 +22,25 @@ export class PublicKey {
*
* @param bytes The raw bytes (33) for the Public Key.
*/
constructor(bytes: Uint8Array) {
// Ensure that exactly 33 Bytes are given.
if (!secp256k1.validatePublicKey(bytes)) {
throw new Error(`Invalid Public Key.`);
}
protected constructor(bytes: Uint8Array) {
this.bytes = bytes;
}
/**
* Instantiates a Public Key from either Public Key Hex or Public Key Bytes.
*
* @param publicKey The public key in either hex or bytes.
*
* @throws {Error} If the public key cannot be decoded.
*
* @returns The created Public Key Primitive.
*/
public static from(publicKey: Uint8Array | string) {
return publicKey instanceof Uint8Array
? PublicKey.fromBytes(publicKey)
: PublicKey.fromHex(publicKey);
}
/**
* Create a Public Key entity from the given hexadecimal string.
*
@@ -41,6 +51,11 @@ export class PublicKey {
* @returns The created Public Key entity.
*/
public static fromBytes(publicKeyBytes: Uint8Array): PublicKey {
// Ensure that exactly 33 Bytes are given.
if (!secp256k1.validatePublicKey(publicKeyBytes)) {
throw new Error(`Invalid Public Key.`);
}
return new PublicKey(publicKeyBytes);
}
@@ -68,6 +83,57 @@ export class PublicKey {
return new PublicKey(publicKey);
}
/**
* Creates an instance from serialized data.
*
* @remarks This method is UNSAFE and MUST only be used where input is already verified or trusted.
*
* @param bytes - Serialized Primitive Data
*
* @returns A new instance of this primitive.
*/
public static fromRaw(bytes: Uint8Array) {
return new this(bytes);
}
/**
* Encode the raw bytes of this Public Key to a hexadecimal representation.
*
* @returns The raw bytes Public Key encoded as Hexadecimal.
*/
public toHex(): string {
return binToHex(this.bytes);
}
/**
* Get the raw bytes of this Public Key.
*
* @returns The raw bytes of the Public Key.
*/
public toBytes(): Uint8Array {
// Return the raw bytes for this Public Key.
return this.bytes;
}
/**
* Return the hexadecimal representation of the raw bytes of the Public Key.
*
* @returns The raw bytes of the Public Key as hexadecimal.
*/
public toString(): string {
// Return the raw Public Key byes encoded as a hexadecimal string.
return this.toHex();
}
/**
* Converts this primitive to a serializable representation.
*
* @returns Serialzied primitive data.
*/
toRaw() {
return this.bytes;
}
/**
* Derive the Hash160 for this Public Key.
*
@@ -212,33 +278,4 @@ export class PublicKey {
return validity;
}
/**
* Encode the raw bytes of this Public Key to a hexadecimal representation.
*
* @returns The raw bytes Public Key encoded as Hexadecimal.
*/
public toHex(): string {
return binToHex(this.bytes);
}
/**
* Get the raw bytes of this Public Key.
*
* @returns The raw bytes of the Public Key.
*/
public toBytes(): Uint8Array {
// Return the raw bytes for this Public Key.
return this.bytes;
}
/**
* Return the hexadecimal representation of the raw bytes of the Public Key.
*
* @returns The raw bytes of the Public Key as hexadecimal.
*/
public toString(): string {
// Return the raw Public Key byes encoded as a hexadecimal string.
return this.toHex();
}
}