(asm: string)
| 63 | } |
| 64 | |
| 65 | export 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 | |
| 88 | export function bytecodeToAsm(bytecode: Uint8Array): string { |
| 89 | // Convert the bytecode to libauth's ASM format |
no outgoing calls
no test coverage detected