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
+2 -2
View File
@@ -1,8 +1,8 @@
import { Encryption, EventEmitter } from '../utils';
import { Encrypter, EventEmitter } from '../utils';
export type StorageOpts = {
syncInMemory?: boolean;
encryption?: Encryption;
encryption?: Encrypter;
};
export type StoreEvents = {
@@ -1,7 +1,7 @@
import { BaseStorage, BaseStore, StorageOpts } from './base-store.js';
import { StoreInMemorySynced } from './store-inmemory-synced.js';
import { Encryption } from '../utils';
import { Encrypter } from '../utils';
import {
encodeExtendedJsonObject,
@@ -113,7 +113,7 @@ export class StoreIndexedDB extends BaseStore {
constructor(
public db: IDBPDatabase,
public storeName: string,
public encryption?: Encryption,
public encryption?: Encrypter,
) {
super();
@@ -143,7 +143,7 @@ export class StoreIndexedDB extends BaseStore {
static async create(
db: IDBPDatabase,
storeName: string,
encryption?: Encryption,
encryption?: Encrypter,
) {
return new this(db, storeName, encryption);
}
@@ -9,7 +9,7 @@ import { utf8ToBin } from '@bitauth/libauth';
export const DEFAULT_XO_SALT =
'cash.xo.default_salt_for_encryption_aabbccddeeff00112233445566778899';
export type EncryptionOpts = {
export type EncrypterOpts = {
algorithm: string;
keyLength: number;
salt: Uint8Array;
@@ -17,13 +17,13 @@ export type EncryptionOpts = {
iterations: number;
};
export class Encryption {
export class Encrypter {
constructor(
private key: CryptoKey,
public opts: EncryptionOpts,
public opts: EncrypterOpts,
) {}
static async fromBytes(key: Uint8Array, opts: Partial<EncryptionOpts> = {}) {
static async fromBytes(key: Uint8Array, opts: Partial<EncrypterOpts> = {}) {
// Merge the provided options into our default options.
const encryptionOpts = {
algorithm: 'AES-GCM',
@@ -60,12 +60,12 @@ export class Encryption {
['encrypt', 'decrypt'],
);
return new Encryption(derivedKey, encryptionOpts);
return new Encrypter(derivedKey, encryptionOpts);
}
static async fromPassphrase(
passphrase: string,
opts: Partial<EncryptionOpts> = {},
opts: Partial<EncrypterOpts> = {},
) {
const key = utf8ToBin(passphrase);
+137
View File
@@ -0,0 +1,137 @@
import {
isHex,
isBase64,
binToHex,
base64ToBin,
hexToBin,
utf8ToBin,
ripemd160,
sha256,
} from '@bitauth/libauth';
/**
* Represents a hash primitive that provides various cryptographic hashing capabilities.
* This class handles conversions between different formats (bytes, hex, UTF-8) and
* supports multiple hashing algorithms (SHA256, RIPEMD160, Hash160, Hash256).
*/
export class Hasher {
/**
* Creates a new Hash instance.
* @param bytes - The byte array representation of the data to be hashed
*/
constructor(public bytes: Uint8Array) {}
/**
* Creates a Hash instance from a byte array.
* @param bytes - The byte array to create the hash from
* @returns A new Hash instance containing the provided bytes
*/
public static fromBytes(bytes: Uint8Array) {
return new this(bytes);
}
/**
* Creates a Hash instance from a Base64 string.
* @param base64 - The Base64 string to create the hash from
* @returns A new Hash instance containing the bytes converted from the Base64 string
* @throws {Error} If the provided string is not valid Base64
*/
public static fromBase64(base64: string) {
// Ensure that the given string is base64.
if (!isBase64(base64)) {
throw new Error('The given string is not valid Base64.');
}
return new this(base64ToBin(base64));
}
/**
* Creates a Hash instance from a hexadecimal string.
* @param hex - The hexadecimal string to create the hash from
* @returns A new Hash instance containing the bytes converted from the hex string
* @throws {Error} If the provided string is not valid hexadecimal
*/
public static fromHex(hex: string) {
// Ensure that the given string is hex.
if (!isHex(hex)) {
throw new Error('The given string is not valid hexadecimal.');
}
return new this(hexToBin(hex));
}
/**
* Creates a Hash instance from a UTF-8 string.
* @param utf8 - The UTF-8 string to create the hash from
* @returns A new Hash instance containing the bytes converted from the UTF-8 string
*/
public static fromUTF8(utf8: string) {
return new this(utf8ToBin(utf8));
}
/**
* Computes the SHA256 hash of the stored bytes.
* @returns A Uint8Array containing the SHA256 hash
*/
public toSHA256Bytes(): Uint8Array {
return sha256.hash(this.bytes);
}
/**
* Computes the SHA256 hash of the stored bytes and returns it as a hexadecimal string.
* @returns The SHA256 hash as a hexadecimal string
*/
public toSHA256Hex(): string {
return binToHex(this.toSHA256Bytes());
}
/**
* Computes the RIPEMD160 hash of the stored bytes.
* @returns A Uint8Array containing the RIPEMD160 hash
*/
public toRIPEMD160Bytes(): Uint8Array {
return ripemd160.hash(this.bytes);
}
/**
* Computes the RIPEMD160 hash of the stored bytes and returns it as a hexadecimal string.
* @returns The RIPEMD160 hash as a hexadecimal string
*/
public toRIPEMD160Hex(): string {
return binToHex(this.toRIPEMD160Bytes());
}
/**
* Computes the Hash160 (RIPEMD160(SHA256(x))) of the stored bytes.
* This is commonly used in cryptocurrency addresses.
* @returns A Uint8Array containing the Hash160 value
*/
public toHash160Bytes(): Uint8Array {
return ripemd160.hash(this.toSHA256Bytes());
}
/**
* Computes the Hash160 (RIPEMD160(SHA256(x))) of the stored bytes and returns it as a hexadecimal string.
* @returns The Hash160 value as a hexadecimal string
*/
public toHash160Hex(): string {
return binToHex(this.toHash160Bytes());
}
/**
* Computes the Hash256 (SHA256(SHA256(x))) of the stored bytes.
* This is commonly used for computing block hashes in Bitcoin.
* @returns A Uint8Array containing the Hash256 value
*/
public toHash256Bytes(): Uint8Array {
return sha256.hash(this.toSHA256Bytes());
}
/**
* Computes the Hash256 (SHA256(SHA256(x))) of the stored bytes and returns it as a hexadecimal string.
* @returns The Hash256 value as a hexadecimal string
*/
public toHash256Hex(): string {
return binToHex(sha256.hash(this.toSHA256Bytes()));
}
}
+2 -1
View File
@@ -1,6 +1,7 @@
export * from './encryption.js';
export * from './encrypter.js';
export * from './event-emitter.js';
export * from './ext-error.js';
export * from './hasher.js';
export * from './memoizer.js';
export * from './misc.js';
export * from './uuid.js';
+1 -1
View File
@@ -418,7 +418,7 @@ export class WalletHD<
// Derive the private key at the given path.
const privateKeyHex = await this.cache.getOrSet(derivationPath, () => {
const childNode = this.hdPrivateNode.derivePath(derivationPath);
return childNode.privateKey().toHex();
return childNode.toPrivateKey().toHex();
});
// Create a child P2PKH wallet for this Private Key.