Open unwriter opened 5 years ago
I don't fully understanding the purpose of the bitcom syntax. Wouldn't it be cleaner and easier to query to have a separate schema definition protocol so you are not querying for "echo" "to" and "shcema.json"?
A protocol owner could publish the schema using OP_RETURN <schema protocol>
<protocol address>
<schema>
and transmit the transaction signed by the protocol private key
This way you could query any protocol schema by
{
"v": 3,
"q": {
"find": {
"in.e.a": "18SuCAXiTgcq5Wj7J91JSkKhrqQ16qPQxW",
"out.s1": "<schema protocol address>",
"out.s2": "18SuCAXiTgcq5Wj7J91JSkKhrqQ16qPQxW",
}
}
}
Bitcoin Script Schema
Define, Publish, and Consume Bitcoin Script Schema, over Bitcoin.
Problem
In the past when someone wanted to create an "overlay protocol" on top of Bitcoin script, they didn't have many options but to do it the old way of storing the protocol specification:
But this is far from ideal because:
Solution
This proposal solves above problems with a Bitcoin script schema that is:
How to Use
In this section we will walk through a workflow of how this schema can be used:
1. Define Schema
There are two things to note about Bitcoin scripts:
For a schema to work, we should be able to express what each push data means while taking above factors into account.
And in this proposal, I suggest a schema scheme that's inspired by the BitDB transaction serialization format, because BitDB has already solved all these problems for its own use case. (However anyone can come up with their own schema scheme and replace with their scheme as well, because the idea itself is applicable to any type of schema).
Example - B://
here's an example schema for the B:// protocol:
v
: The schema scheme version numbers
: Schema descriptionInside the
s
, we specify key/value pairs of Bitcoin script push data & its values. If a value is wrapped inside a{{ }}
pair, that means it's a schema attribute.In above case, the schema is saying:
out.s2
is used to representblob
.out.s3
is used to representmediatype
.2. Publish Schema to Bitcoin
So now that we've decided on the schema, how do we publish it to the blockchain itself?
You can do this using the Bitcom scheme. More specifically you can use the
$ echo
command to write a file to the protocol's root folder. (Note that this syntax is based on the upcoming "Bitcom as OS" update), and therefore the$ echo
command is embedded into the19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut
application protocol (instead of Bitcom being a standalone protocol)Here's an example of writing the schema to
B://
protocol's root folder, using the Bitcom scheme:3. Usage
Now that the schema has been published, you have opened up an "official" API endpoint for your protocol.
This means now others can look up the schema to understand what each push data means in your protocol transactions.
Here's a Bitquery to find the schema transaction:
4. Advanced Usage
The example above is a simple one because there's only a single pattern. But sometimes you may want to have a single protocol with multiple patterns. You may want this type of protocol management if you want a single Bitcoin address to control multiple "API points" for a single protocol via the Bitcom scheme.
Let's think of a hypothetical Memo.cash clone that uses this scheme. This app will have two APIs:
OP_RETURN 17yyXL4raLZFU95ixkRESa2ZBPSSYxSsS5 0x01 Johndoe
OP_RETURN 17yyXL4raLZFU95ixkRESa2ZBPSSYxSsS5 0x02 Hello
Let's describe a schema. This time we have two APIs to describe instead of one, so we will put it inside an array:
The difference here is that
h2
attributes are not variables wrapped in{{ }}
pairs. They are static values, so they are used to pattern match in the following manner:out.h2
is01
, thenout.s3
matches to"{{username}}"
out.h2
is02
, thenout.s3
matches to"{{message}}"
We can publish this schema like this, using the bitcom scheme:
Case Study: File Metadata API
Let's take a look at how this can be applied in a real world scenario. We will use a real world example protocol:
B://
.Step 1. Define a schema
A. Define B:// Schema
First we define a
B://
schema for uploading blobs to the blockchain.Since we are discussing a new way of doing things, let's assume for a moment that we are using a different version of
B://
spec where it ONLY has a single attribute:{{blob}}
.In this hypothetical version,
B://
acts purely as a "blob upload scheme". It only has one attribute:blob
.We publish this schema to Bitcoin with an OP_RETURN:
B. Define Media Header Protocol Schema
Next, we want to attach more header metadata to the blob by utilizing the Unix Pipeline concept, instead of packing all the extra attributes into the
B://
protocol.This way
B://
can stay minimal, as a purely raw blob upload protocol, and developers can build extension protocols on top of the blob protocol simply by pipingB://
into their header metadata protocols.Let's define a simple metadata protocol schema as an example:
We also publish this schema to Bitcoin using an OP_RETURN:
C. Usage
Now that we've defined two protocols:
19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut
18SuCAXiTgcq5Wj7J91JSkKhrqQ16qPQxW
let's pipe them.
Here's an actual usage of the two protocols being pipelined to:
D. Interpret
Now, blob viewer services can interpret above OP_RETURN transaction by:
To read the schema for
19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut
, we can query BitDB using:Next, to read the schema for
18SuCAXiTgcq5Wj7J91JSkKhrqQ16qPQxW
, we can query BitDB using:Above queries will return the schema files for both protocols:
and
Now we can finally go back to the original OP_RETURN and interpret what each push data means:
And the interpretations are:
[...]
is a{{blob}}
!!image/png
is a{{mediatype}}
, andlogo.png
is a{{filename}}
!!Extensibility
This schema scheme is based on how Bitcoin scripts natively work: It's based on Bitcoin push data sequencing and encoding.
This means this can be further extended in the future to describe:
Conclusion
The Bitcoin Script Schema is just one proposal to declaratively describe a Bitcoin script template, which makes it easy to:
There can be other indexing and schema description schemes, but the best ones will probably be very closely integrated with how Bitcoin script natively works.
The current proposal is one such attempt, and one that can be used Today.
The great thing about this approach is that we can easily switch out the schema to a new scheme if the community comes up with a better scheme.