Operate meets JavaScript

Released today, Operate now offers a JavaScript agent, allowing Operate to be used in the browser and in Node.js apps. This is the next step in Operate's journey, and opens Operate up to pretty much every team and developer actively working on Bitcoin today.

# What is the agent for?

To use Operate, the agent is the primary tool developers need in order to integrate Operate in their apps. It can be seen as a Bitcoin interface layer - it is responsible for loading programs (tapes) from transactions, it loads all of the necessary Ops, runs the tape in a VM and returns a result.

Operate initially launched with an agent written in Elixir. But it's always been my intention to get Operate working in the browser for pure front-end apps, and so following up with a JavaScript agent was always part of the plan.

Now Operate offers two alternative agent implementations, and we're committed to supporting both:

  • Elixir - Original implementation. Ideal for performant and scalable back-end apps.
  • JavaScript - Popular programming language. Cross-platform compatibility.

# Try Operate right now!

The great thing about JavaScript is that you can play with it in a browser. So before you start downloading npm packages, why not just try it right here, right now, on this page? Below is a fully functional embedded JavaScript console with the Operate Agent already loaded. Read on below for help getting started...

Using Firefox?

You may need to manually load the Operate library. Paste the following in to the console above.

:load //cdn.jsdelivr.net/npm/@operate/agent@latest

# Aliases

To get started, copy and paste the following line into the console to configure the agent with a number of aliases for common Bitcoin protocols. Not every transaction will be compatible with Operate, but with these common aliases you should be able to play with transactions from some popular Bitcoin apps.


# Twetch example

Next up, try loading and running a tape from any transaction. Lets try this example from Twetch:

// Load a Tape
tape = await Operate

// Run the Tape
result = await Operate.runTape(tape)

// Convert Map to Object
data = Operate.util.mapToObject(result)

At each step in the above code, try inspecting the returned data. You'll notice the result is by default returned as an instance of Map. This is because JavaScript Maps are semantically much closer to Lua Tables than Objects. The final line uses a helper method to optionally convert Maps to Objects.

# Bit.sv example

Next up, lets try the same with a Bit.sv transaction:

tape = await Operate

result = await Operate.runTape(tape)
data = Operate.util.mapToObject(result)

// Traverse the metanet node
parent = await data._META.parent()

The final line there gives a hint as to what Operate offers app developers. Not only does Operate parse transactions and implement all these data protocols, but it is returning interactive objects with functions attached. Bit.sv transactions are Metanet nodes, so we can create Ops that implement the Metanet protocol and provide functions for traversing the tree.

Wouldn't it be great if there was also an Op for the Bit.sv protocol that provides functions handling decryption of the private data, given the correct private key? This is what interoperability looks like with Operate.

# Learning more about Operate

As well as releasing the JavaScript agent today, I have also rewritten the Operate documentation from the ground up. The new documentation aims to better explain some of the key concepts behind Operate.

In addition, head over to the Github repository to browse the source code.

All Operate software is beta software. The JavaScript agent particularly has a few known issues - not least of all the massive browser bundle size (over 700kb). I think it will be possible to reduce this to below 300kb, but this will take work and time.

# Finally

In February, when I stood up in front of ~900 people at CoinGeek London and revealed that a JavaScript agent was coming soon™, I wasn't even sure if I was being honest. For a few reasons this has been a really challenging, and at times frustrating project to work on. To that point I must thank @unanon_ who joined me at CambrianSV in Lisbon, helped out with some of the dev work, and got to enjoy some of those laptop-smashingly frustrating moments with me.