Amend Primitives/Tooling
This commit is contained in:
@@ -32,7 +32,7 @@ export class PrivateKey {
|
||||
*
|
||||
* @param bytes {Uint8Array} The raw bytes (32) for the Private Key.
|
||||
*/
|
||||
constructor(bytes: Uint8Array) {
|
||||
protected constructor(bytes: Uint8Array) {
|
||||
// Ensure that the Private Key is valid.
|
||||
if (!secp256k1.validatePrivateKey(bytes)) {
|
||||
throw new Error(
|
||||
@@ -43,6 +43,27 @@ export class PrivateKey {
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a Private Key from either Bytes, Hex or WIF.
|
||||
*
|
||||
* @param privateKey The private key as either Bytes, Hex or WIF.
|
||||
*
|
||||
* @throws {Error} If the private key cannot be decoded.
|
||||
*
|
||||
* @returns The created Private Key Primitive.
|
||||
*/
|
||||
public static from(privateKey: Uint8Array | string) {
|
||||
if (privateKey instanceof Uint8Array) {
|
||||
return PrivateKey.fromBytes(privateKey);
|
||||
}
|
||||
|
||||
if (PrivateKey.isWIF(privateKey)) {
|
||||
return PrivateKey.fromWIF(privateKey);
|
||||
}
|
||||
|
||||
return PrivateKey.fromHex(privateKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Private Key Entity from the given bytes.
|
||||
*
|
||||
@@ -124,6 +145,19 @@ export class PrivateKey {
|
||||
return new PrivateKey(decodeResult.privateKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random Private Key.
|
||||
*
|
||||
@@ -144,6 +178,57 @@ export class PrivateKey {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the raw bytes of this Private Key to hexadecimal encoding.
|
||||
*
|
||||
* @returns {string} The raw bytes of the Private Key as a hexadecimal string.
|
||||
*/
|
||||
public toHex(): string {
|
||||
// Return the hexadecimal encoded bytes of this Private Key.
|
||||
return binToHex(this.bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert this Private Key to a raw 32 byte representation.
|
||||
*
|
||||
* @returns {Uint8Array} The raw Private Key as a Uint8Array.
|
||||
*/
|
||||
public toBytes(): Uint8Array {
|
||||
// Return the raw bytes for this Private Key.
|
||||
return this.bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert this Private Key to a WIF format.
|
||||
*
|
||||
* @param type {WalletImportFormatType} The type of WIF to create.
|
||||
*
|
||||
* @returns {string} The Private Key in WIF format.
|
||||
*/
|
||||
public toWif(type: WalletImportFormatType = 'mainnet'): string {
|
||||
// Return the encoded Private Key WIF.
|
||||
return encodePrivateKeyWif(this.bytes, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Private Key as a mainnet WIF.
|
||||
*
|
||||
* @returns {string} The Private Key as a mainnet WIF.
|
||||
*/
|
||||
public toString(): string {
|
||||
// Return the encoded Mainnet Private Key WIF.
|
||||
return this.toWif();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this primitive to a serializable raw object representation.
|
||||
*
|
||||
* @returns Serialzied primitive data.
|
||||
*/
|
||||
toRaw() {
|
||||
return this.bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive the public key from this private key.
|
||||
*
|
||||
@@ -161,7 +246,7 @@ export class PrivateKey {
|
||||
}
|
||||
|
||||
// Return the derived Public Key.
|
||||
return new PublicKey(deriveResult);
|
||||
return PublicKey.fromRaw(deriveResult);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -406,46 +491,4 @@ export class PrivateKey {
|
||||
// Return the signature.
|
||||
return signResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the raw bytes of this Private Key to hexadecimal encoding.
|
||||
*
|
||||
* @returns {string} The raw bytes of the Private Key as a hexadecimal string.
|
||||
*/
|
||||
public toHex(): string {
|
||||
// Return the hexadecimal encoded bytes of this Private Key.
|
||||
return binToHex(this.bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert this Private Key to a raw 32 byte representation.
|
||||
*
|
||||
* @returns {Uint8Array} The raw Private Key as a Uint8Array.
|
||||
*/
|
||||
public toBytes(): Uint8Array {
|
||||
// Return the raw bytes for this Private Key.
|
||||
return this.bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert this Private Key to a WIF format.
|
||||
*
|
||||
* @param type {WalletImportFormatType} The type of WIF to create.
|
||||
*
|
||||
* @returns {string} The Private Key in WIF format.
|
||||
*/
|
||||
public toWif(type: WalletImportFormatType = 'mainnet'): string {
|
||||
// Return the encoded Private Key WIF.
|
||||
return encodePrivateKeyWif(this.bytes, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Private Key as a mainnet WIF.
|
||||
*
|
||||
* @returns {string} The Private Key as a mainnet WIF.
|
||||
*/
|
||||
public toString(): string {
|
||||
// Return the encoded Mainnet Private Key WIF.
|
||||
return this.toWif();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user