Draft Inverse Bloom Filter

This commit is contained in:
Jim Hamill
2025-01-20 07:47:07 +00:00
parent d6a04f4350
commit 1aebc6da42
21 changed files with 1285 additions and 381 deletions
+26 -28
View File
@@ -1,17 +1,26 @@
import { Bytes } from './bytes.js';
import { Address } from './address.js';
import {
binToHex,
hexToBin,
isHex,
secp256k1,
sha256,
ripemd160,
utf8ToBin,
} from '@bitauth/libauth';
import { secp256k1, sha256, ripemd160, utf8ToBin } from '@bitauth/libauth';
/**
* Public Key Entity.
*
* @example
*
* ```ts
* // Import our primitives
* import { PublicKey } from '@xocash/primitives';
*
* // Create a Public Key from Hex.
* const publicKey = PublicKey.from('publicKeyHex');
*
* // Derive the address.
* const address = publicKey.deriveAddress();
*
* // Output as a CashAddr.
* console.log(address.toCashAddr());
* ```
*/
export class PublicKey {
// The raw bytes representing the Public Key.
@@ -69,18 +78,7 @@ export class PublicKey {
* @returns The created Public Key entity.
*/
public static fromHex(publicKeyHex: string): PublicKey {
// Ensure that the given string is hex.
if (!isHex(publicKeyHex)) {
throw new Error(
`The given string (${publicKeyHex}) is not valid hexadecimal.`,
);
}
// Convert the hex to binary (Uint8Array).
const publicKey = hexToBin(publicKeyHex);
// Create a Public Key Entity from the binary.
return new this(publicKey);
return new this(Bytes.fromHex(publicKeyHex).toBytes());
}
/**
@@ -102,7 +100,7 @@ export class PublicKey {
* @returns The raw bytes Public Key encoded as Hexadecimal.
*/
public toHex(): string {
return binToHex(this.bytes);
return Bytes.fromBytes(this.bytes).toHex();
}
/**
@@ -158,7 +156,7 @@ export class PublicKey {
public deriveHash160Hex(): string {
const hash160Bytes = this.deriveHash160();
return binToHex(hash160Bytes);
return Bytes.fromBytes(hash160Bytes).toHex();
}
/**
@@ -171,7 +169,7 @@ export class PublicKey {
const ripemd160Hash = this.deriveHash160();
// Return an instance of address
return Address.fromHash160(ripemd160Hash, 'P2PKH');
return Address.fromHash160Bytes(ripemd160Hash, 'P2PKH');
}
public verifyMessageSchnorr(
@@ -234,11 +232,11 @@ export class PublicKey {
signature: Uint8Array | string,
): boolean {
if (typeof messageHash === 'string') {
messageHash = hexToBin(messageHash);
messageHash = Bytes.fromHex(messageHash).toBytes();
}
if (typeof signature === 'string') {
signature = hexToBin(signature);
signature = Bytes.fromHex(signature).toBytes();
}
const validity = secp256k1.verifySignatureDER(
@@ -263,11 +261,11 @@ export class PublicKey {
signature: Uint8Array | string,
): boolean {
if (typeof messageHash === 'string') {
messageHash = hexToBin(messageHash);
messageHash = Bytes.fromHex(messageHash).toBytes();
}
if (typeof signature === 'string') {
signature = hexToBin(signature);
signature = Bytes.fromHex(signature).toBytes();
}
const validity = secp256k1.verifySignatureSchnorr(