Add fromCashASM function
This commit is contained in:
@@ -595,6 +595,15 @@ export const script = {
|
|||||||
models: [''],
|
models: [''],
|
||||||
callback: (args: [string]) => Script.fromASM(...args),
|
callback: (args: [string]) => Script.fromASM(...args),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
label: 'From CashASM',
|
||||||
|
inputs: ['CashASM'],
|
||||||
|
models: [
|
||||||
|
'// Script evaluations are also supported.\n<$(<"evaluateThisFirst"> OP_SHA256)> OP_SHA256',
|
||||||
|
],
|
||||||
|
callback: (args: [string]) => Script.fromCashASM(...args),
|
||||||
|
extendedInput: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
label: 'From Hex',
|
label: 'From Hex',
|
||||||
inputs: ['Hex'],
|
inputs: ['Hex'],
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import { Bytes } from './bytes.js';
|
import { Bytes } from './bytes.js';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
type WalletTemplateVariable,
|
||||||
assembleBytecodeBCH,
|
assembleBytecodeBCH,
|
||||||
disassembleBytecodeBCH,
|
disassembleBytecodeBCH,
|
||||||
compileCashAssembly,
|
createCompilerBCH,
|
||||||
|
stringify,
|
||||||
} from '@bitauth/libauth';
|
} from '@bitauth/libauth';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -113,20 +115,46 @@ export class Script {
|
|||||||
return new this(assembleResult.bytecode);
|
return new this(assembleResult.bytecode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: In its current form, this is pretty useless because it does not allow OP_CODES.
|
public static fromCashASM(
|
||||||
// Disabling because it will probably just cause confusion.
|
cashASM: string,
|
||||||
/*
|
data?: Record<string, Uint8Array>,
|
||||||
public static fromCashASM(cashASM: string) {
|
) {
|
||||||
const result = compileCashAssembly(cashASM);
|
// Create a BCH Compilation context.
|
||||||
|
const compiler = createCompilerBCH({
|
||||||
|
scripts: {
|
||||||
|
// NOTE: We need to name the script, so we give it the name "_main".
|
||||||
|
_main: cashASM,
|
||||||
|
},
|
||||||
|
variables: Object.keys(data || {}).reduce<{
|
||||||
|
[name: string]: WalletTemplateVariable;
|
||||||
|
}>(
|
||||||
|
(acc, variableName: string) => {
|
||||||
|
acc[variableName] = { type: 'AddressData' };
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{}, // Initial value
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
if (typeof result === 'string') {
|
// Attempt to compile the script called "main"
|
||||||
// TODO: Make error more detailed.
|
const compilationResult = compiler.generateBytecode<true>({
|
||||||
throw new Error(`Failed to parse CashASM: ${result}`);
|
data: {
|
||||||
|
...data,
|
||||||
|
},
|
||||||
|
scriptId: '_main',
|
||||||
|
debug: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
// If compilation failed...
|
||||||
|
if (!compilationResult.success) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to resolve CashASM: ${stringify(compilationResult)}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new this(result);
|
// Return a new script instance containing the compiled bytecode.
|
||||||
|
return new Script(compilationResult.bytecode);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a Script Entity from the given bytes (as hexadecimal string).
|
* Create a Script Entity from the given bytes (as hexadecimal string).
|
||||||
|
|||||||
Reference in New Issue
Block a user