Beersy
BRC-14

Bitcoin Script Binary, Hex and ASM Formats

The same script can be written as raw bytes, as hexadecimal, or in a readable assembly form, and tools disagree about the details. This documents all three so software can convert between them without surprises.

Ty Everettchanged 7 Apr 20232 min read
binaryhexadecimalasmsasm

Summary

Why
Bitcoin scripts are stored and transmitted as raw bytes, but developers need a readable and consistent way to write, inspect, and exchange those same scripts across tools.
What
BRC-14 defines the binary, hexadecimal, , and formats used to represent a Bitcoin script.
How
A developer writes or reads a script as raw bytes, as a hex string of those bytes, or as a human readable opcode string in ASM or its shorthand SASM form, converting between them as tools require.

What this lets you do

  • Read raw script bytes and know what they mean
  • Convert a script between binary, hex, and text form
  • Write a locking or by hand as readable text
  • Use the shorthand SASM form for quick typing
  • Pass scripts between tools that expect different formats

Written by claude-sonnet-5 from the specification text. Where the two differ, the original is correct.

Reference for an AI

Everything an assistant needs to answer questions about BRC-14 accurately, including what it depends on.

The specification

Abstract

Bitcoin uses scripts to control transactions, and three of the most common ways to represent a script are binary, hexadecimal, and . This standard aims to provide a detailed description of these formats to facilitate their use in BSV transactions.

Motivation

The use of scripts in Bitcoin SV transactions allows for greater flexibility and customization of transaction rules. To properly utilize these scripts, it is important to understand the different formats they can be written in. This standard aims to provide a clear and detailed description of the binary, hexadecimal, and ASM formats used in BSV scripts to facilitate their use in BSV transactions.

Specification

We specify three commonly-used Bitcoin script formats as follows:

Binary Format

The binary format is a series of bytes that represent the script. Each opcode in the script is represented by a single byte, with any arguments or data being represented by subsequent bytes. The script is executed sequentially, with each opcode performing a specific action on the stack. Bitcoin script uses reverse polish notation.

<Buffer 76 a9 14 57 f5 0e 13 db 98 a8 8e 9e 1b 8f 03 f6 5a 80 9d 6e 9c 9c ba 88 ac>

Hexadecimal Format

The hexadecimal format is a string of hexadecimal digits that represents the binary script. Each byte in the script is represented by two hexadecimal digits. The following is an example of a script in hexadecimal format:

76a91457f50e13db98a88e9e1b8f03f65a809d6e9c9cba88ac

ASM Format

Not to be confused with the full BRC-15 Assembly Language.

The ASM format is a human-readable format that represents the script in a more intuitive way. Each opcode and its arguments are represented by a single string. For example, the following script in ASM format checks that a given public key has signed the transaction:

OP_DUP OP_HASH160 57f50e13db98a88e9e1b8f03f65a809d6e9c9cba OP_EQUALVERIFY OP_CHECKSIG

SASM Format

The format is a shortening of the above ASM format which uses lowercase and removes the OP_ prefix from op codes to aid in quick typing and clearer readability. For example, the above ASM script is repeated now in SASM:

dup hash160 57f50e13db98a88e9e1b8f03f65a809d6e9c9cba equalverify checksig

Implementations

BSV scripts can be written in any of the above formats and included in a transaction as a locking script or an . To create a script in binary format, the opcodes can be written in hexadecimal and then converted to binary. To create a script in hexadecimal format, the binary script can be converted to hexadecimal. To create a script in the ASM format, the opcodes and arguments can be written as a string in the appropriate format.

There are various libraries and tools available for creating and working with BSV scripts in these formats, such as the bitcoin-cli command-line tool for the node software. These tools allow for the creation and manipulation of BSV scripts in any of the supported formats.

Was this helpful?

Search Beersy

Search standards by number, title, author or topic