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
+18 -44
View File
@@ -1,12 +1,9 @@
import {
assembleBytecodeBCH,
disassembleBytecodeBCH,
base58AddressToLockingBytecode,
cashAddressToLockingBytecode,
binToHex,
hexToBin,
isHex,
parseScript,
} from '@bitauth/libauth';
/**
@@ -20,14 +17,29 @@ export class Script {
*
* @param bytes {Uint8Array} The raw bytes of the Script.
*/
constructor(bytes: Uint8Array) {
protected constructor(bytes: Uint8Array) {
this.bytes = bytes;
}
/**
* Create a Script Entity from the given Assembly String.
* Instantiates a Script from either Bytes or Hex.
*
* @param asm {string} String of assembly code.
* @param script The script as either Bytes or Hex.
*
* @throws {Error} If the script cannot be decoded.
*
* @returns The created Script Primitive.
*/
public static from(script: Uint8Array | string) {
return script instanceof Uint8Array
? Script.fromBytes(script)
: Script.fromHex(script);
}
/**
* Create a Script Entity from bytes.
*
* @param bytes {string} Script Bytecode
*
* @throws {Error} If script cannot be decoded.
*
@@ -86,44 +98,6 @@ export class Script {
return new Script(scriptBin);
}
/**
* Create a Script Entity from the given CashAddr.
*
* @param cashAddr {string} The Address in CashAddr format.
*
* @throws {Error} If address cannot be decoded.
*
* @returns {Script} The created Script Entity.
*/
public static fromCashAddr(cashAddr: string): Script {
const decodeResult = cashAddressToLockingBytecode(cashAddr);
if (typeof decodeResult === 'string') {
throw new Error(decodeResult);
}
return new Script(decodeResult.bytecode);
}
/**
* Create a Script Entity from the given Legacy Address.
*
* @param legacyAddress {string} The Address in Legacy format.
*
* @throws {Error} If address cannot be decoded.
*
* @returns {Script} The created Script Entity.
*/
public static fromLegacyAddress(legacyAddress: string): Script {
const decodeResult = base58AddressToLockingBytecode(legacyAddress);
if (typeof decodeResult === 'string') {
throw new Error(decodeResult);
}
return new Script(decodeResult.bytecode);
}
/**
* Convert this script to a human-readable string of assembly.
*