MCPcopy Create free account
hub / github.com/CashScript/cashscript / asmToBytecode

Function asmToBytecode

packages/utils/src/script.ts:65–86  ·  view source on GitHub ↗
(asm: string)

Source from the content-addressed store, hash-verified

63}
64
65export function asmToBytecode(asm: string): Uint8Array {
66 // Remove any duplicate whitespace
67 asm = asm.replace(/\s+/g, ' ').trim();
68
69 if (asm === '') return new Uint8Array();
70
71 // Convert the ASM tokens to AuthenticationInstructions
72 const instructions = asm.split(' ').map((token) => {
73 // Even though the OpcodesBch type allows for { [key: number]: string }, we know that the keys are always the opcodes
74 // so we can safely cast to the AuthenticationInstruction type
75 if (token.startsWith('OP_')) {
76 return { opcode: Op[token as keyof typeof Op] } as AuthenticationInstruction;
77 }
78
79 const data = token.replace(/<|>/g, '').replace(/^0x/, '');
80
81 return decodeAuthenticationInstructions(encodeDataPush(hexToBin(data)))[0];
82 });
83
84 // Convert the AuthenticationInstructions to bytecode
85 return encodeAuthenticationInstructions(instructions);
86}
87
88export function bytecodeToAsm(bytecode: Uint8Array): string {
89 // Convert the bytecode to libauth's ASM format

Callers 4

util.test.tsFile · 0.90
script.test.tsFile · 0.85
asmToScriptFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected