Misc Primitive Fixes

This commit is contained in:
Jim Hamill
2025-01-15 03:05:53 +00:00
parent 9348648584
commit a51da6770f
15 changed files with 119 additions and 109 deletions
+39 -30
View File
@@ -1,3 +1,4 @@
import { Bytes } from './bytes.js';
import { PublicKey } from './public-key.js';
import {
@@ -5,9 +6,6 @@ import {
decodePrivateKeyWif,
encodePrivateKeyWif,
generatePrivateKey,
hexToBin,
isHex,
utf8ToBin,
bigIntToCompactUint,
secp256k1,
sha256,
@@ -23,7 +21,7 @@ import type {
*/
export class PrivateKey {
// The raw bytes representing the Private Key.
public readonly bytes: Uint8Array;
protected readonly bytes: Uint8Array;
/**
* Construct a new Private Key.
@@ -54,14 +52,14 @@ export class PrivateKey {
*/
public static from(privateKey: Uint8Array | string) {
if (privateKey instanceof Uint8Array) {
return PrivateKey.fromBytes(privateKey);
return this.fromBytes(privateKey);
}
if (PrivateKey.isWIF(privateKey)) {
return PrivateKey.fromWIF(privateKey);
if (this.isWIF(privateKey)) {
return this.fromWIF(privateKey);
}
return PrivateKey.fromHex(privateKey);
return this.fromHex(privateKey);
}
/**
@@ -74,7 +72,7 @@ export class PrivateKey {
* @returns {PrivateKey} The created Private Key Entity.
*/
public static fromBytes(privateKeyBytes: Uint8Array): PrivateKey {
return new PrivateKey(privateKeyBytes);
return new this(privateKeyBytes);
}
/**
@@ -87,18 +85,11 @@ export class PrivateKey {
* @returns {PrivateKey} The created Private Key Entity.
*/
public static fromHex(privateKeyHex: string): PrivateKey {
// Ensure that the given string is hex.
if (!isHex(privateKeyHex)) {
throw new Error(
`The given string (${privateKeyHex}) is not valid hexadecimal.`,
);
}
// Convert the hex to binary (Uint8Array).
const privateKeyBin = hexToBin(privateKeyHex);
const privateKeyBin = Bytes.fromHex(privateKeyHex).toBytes();
// Create a Private Key Entity from the binary.
return new PrivateKey(privateKeyBin);
return new this(privateKeyBin);
}
/**
@@ -142,7 +133,7 @@ export class PrivateKey {
}
// Return a new Private Key entity.
return new PrivateKey(decodeResult.privateKey);
return new this(decodeResult.privateKey);
}
/**
@@ -166,7 +157,7 @@ export class PrivateKey {
public static generateRandom(): PrivateKey {
const bytes = generatePrivateKey();
return new PrivateKey(bytes);
return new this(bytes);
}
public static isWIF(wif: string, removePrefixes = ['bitcoincash:', 'wif:']) {
@@ -261,7 +252,7 @@ export class PrivateKey {
*/
public signMessageRecoverableCompact(message: Uint8Array | string) {
if (typeof message === 'string') {
message = utf8ToBin(message);
message = Bytes.fromUtf8(message).toBytes();
}
const messageHash = sha256.hash(message);
@@ -281,7 +272,7 @@ export class PrivateKey {
*/
public signMessageCompact(message: Uint8Array | string) {
if (typeof message === 'string') {
message = utf8ToBin(message);
message = Bytes.fromUtf8(message).toBytes();
}
const messageHash = sha256.hash(message);
@@ -301,7 +292,7 @@ export class PrivateKey {
*/
public signMessageSchnorr(message: Uint8Array | string) {
if (typeof message === 'string') {
message = utf8ToBin(message);
message = Bytes.fromUtf8(message).toBytes();
}
const messageHash = sha256.hash(message);
@@ -321,7 +312,7 @@ export class PrivateKey {
*/
public signMessageDER(message: Uint8Array | string) {
if (typeof message === 'string') {
message = utf8ToBin(message);
message = Bytes.fromUtf8(message).toBytes();
}
const messageHash = sha256.hash(message);
@@ -344,8 +335,10 @@ export class PrivateKey {
*/
public signMessageElectron(message: string): Uint8Array {
// Compile the message.
const magicBytes = utf8ToBin('\x18Bitcoin Signed Message:\n');
const messageBytes = utf8ToBin(message);
const magicBytes = Bytes.fromUtf8(
'\x18Bitcoin Signed Message:\n',
).toBytes();
const messageBytes = Bytes.fromUtf8(message).toBytes();
const messageLengthBytes = bigIntToCompactUint(BigInt(messageBytes.length)); // hexToBin(messageBytes.length.toString(16)); //
const messageFinal = new Uint8Array([
...magicBytes,
@@ -385,7 +378,11 @@ export class PrivateKey {
// If a string was given, convert it from hex to bytes.
// TODO: Consider using Bytes class - it ensures valid hex.
if (typeof messageHash === 'string') {
messageHash = hexToBin(messageHash);
messageHash = Bytes.fromHex(messageHash).toBytes();
}
if (messageHash.length !== 32) {
throw new Error('messageHash must be a hash of length 32 bytes');
}
// Attempt to sign the message.
@@ -416,7 +413,11 @@ export class PrivateKey {
// If a string was given, convert it from hex to bytes.
// TODO: Consider using Bytes class - it ensures valid hex.
if (typeof messageHash === 'string') {
messageHash = hexToBin(messageHash);
messageHash = Bytes.fromHex(messageHash).toBytes();
}
if (messageHash.length !== 32) {
throw new Error('messageHash must be a hash of length 32 bytes');
}
// Attempt to sign the message.
@@ -447,7 +448,11 @@ export class PrivateKey {
// If a string was given, convert it from hex to bytes.
// TODO: Consider using Bytes class - it ensures valid hex.
if (typeof messageHash === 'string') {
messageHash = hexToBin(messageHash);
messageHash = Bytes.fromHex(messageHash).toBytes();
}
if (messageHash.length !== 32) {
throw new Error('messageHash must be a hash of length 32 bytes');
}
// Attempt to sign the message.
@@ -478,7 +483,11 @@ export class PrivateKey {
// If a string was given, convert it from hex to bytes.
// TODO: Consider using Bytes class - it ensures valid hex.
if (typeof messageHash === 'string') {
messageHash = hexToBin(messageHash);
messageHash = Bytes.fromHex(messageHash).toBytes();
}
if (messageHash.length !== 32) {
throw new Error('messageHash must be a hash of length 32 bytes');
}
const signResult = secp256k1.signMessageHashDER(this.bytes, messageHash);