Draft Inverse Bloom Filter
This commit is contained in:
@@ -1,13 +1,25 @@
|
||||
import {
|
||||
assembleBytecodeBCH,
|
||||
disassembleBytecodeBCH,
|
||||
binToHex,
|
||||
hexToBin,
|
||||
isHex,
|
||||
} from '@bitauth/libauth';
|
||||
import { Bytes } from './bytes.js';
|
||||
|
||||
import { assembleBytecodeBCH, disassembleBytecodeBCH } from '@bitauth/libauth';
|
||||
|
||||
/**
|
||||
* Script Entity.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```ts
|
||||
* // Import our primitives
|
||||
* import { Script } from '@xocash/primitives';
|
||||
*
|
||||
* // Create a script from ASM.
|
||||
* const script = Script.fromASM('OP_RETURN <0xbeef>');
|
||||
*
|
||||
* // Output as in hexadecimal representation.
|
||||
* console.log(script.toHex());
|
||||
*
|
||||
* // Output as bytecode.
|
||||
* console.log(script.toBytes());
|
||||
* ```
|
||||
*/
|
||||
export class Script {
|
||||
protected readonly bytes: Uint8Array;
|
||||
@@ -46,7 +58,11 @@ export class Script {
|
||||
* @returns {Script} The created Script Entity.
|
||||
*/
|
||||
public static fromBytes(bytes: Uint8Array): Script {
|
||||
return new this(bytes);
|
||||
// Disassemble the bytecode.
|
||||
const disassembledBytecode = disassembleBytecodeBCH(bytes);
|
||||
|
||||
// And then try to instantiate it from ASM to catch any errors.
|
||||
return this.fromASM(disassembledBytecode);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,7 +74,22 @@ export class Script {
|
||||
*
|
||||
* @returns {Script} The created Script Entity.
|
||||
*/
|
||||
public static fromASM(asm: string): Script {
|
||||
public static fromASM(asm: string, disallowUnknown = true): Script {
|
||||
// If we should disallow unknown OP codes...
|
||||
if (disallowUnknown) {
|
||||
// Check if there are any unknown OP Codes.
|
||||
const containsUnknown = asm
|
||||
.split(/\s+/)
|
||||
.some((opCode) => opCode.startsWith('OP_UNKNOWN'));
|
||||
|
||||
// Throw an error if there are unknown OP codes.
|
||||
if (containsUnknown) {
|
||||
throw new Error(
|
||||
`Failed to decode script '${asm}': Unknown OP codes detected (set disallowUnknown to false if this is expected)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to convert the given ASM to bytecode.
|
||||
const assembleResult = assembleBytecodeBCH(asm);
|
||||
|
||||
@@ -69,7 +100,9 @@ export class Script {
|
||||
|
||||
// If the success flag is not marked, this indicates an error...
|
||||
if (!assembleResult.success) {
|
||||
throw new Error('Could not assemble bytecode from assembly.');
|
||||
throw new Error(
|
||||
`Failed to decode '${asm}': Could not assemble bytecode from assembly.`,
|
||||
);
|
||||
}
|
||||
|
||||
// Return new Script Entity from the assembled bytecode.
|
||||
@@ -86,16 +119,21 @@ export class Script {
|
||||
* @returns {Script} The created Script Entity.
|
||||
*/
|
||||
public static fromHex(hex: string): Script {
|
||||
// Ensure that the given string is hex.
|
||||
if (!isHex(hex)) {
|
||||
throw new Error(`The given string (${hex}) is not valid hexadecimal.`);
|
||||
}
|
||||
|
||||
// Conver the given hex to binary (Uint8Array).
|
||||
const scriptBin = hexToBin(hex);
|
||||
|
||||
// Return new Script entity constructed from the binary.
|
||||
return new this(scriptBin);
|
||||
return this.fromBytes(Bytes.fromHex(hex).toBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,6 +166,15 @@ export class Script {
|
||||
* @returns {string} The compiled bytes as a hex string.
|
||||
*/
|
||||
public toHex(): string {
|
||||
return binToHex(this.bytes);
|
||||
return Bytes.fromBytes(this.bytes).toHex();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this primitive to a serializable raw object representation.
|
||||
*
|
||||
* @returns Serialzied primitive data.
|
||||
*/
|
||||
toRaw() {
|
||||
return this.bytes;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user