Golang
Factom "Hello world" in golang.
Prerequisites
A valid Entry Credit address with a few EC (see how to do that here)
A working Go environment (get that here)
A copy of the Factom package (run:
go get github.com/FactomProject/factom
)Internet connection
Getting Started with Development
Connect to a factomd server node
If you want to setup your own factomd node, head over to the developer sandbox setup guide and follow the instructions there, but for the sake of simplicity, it'd be easier to use the courtesy node from Factom Inc. They host a remote node for the community to access.
Create a new .go
file and import the FactomProject/factom package. Now in the init()
function, you'll set the url for the live factomd
instance you want to work with.
Writing data to a chain
Bitcoin has a single longest chain that everyone agrees to build upon. In Factom there isn't just one big stack everyone keeps throwing information on top of, but rather, many smaller independent chains that are organized into one large chain (which is anchored into Bitcoin every 10 minutes). The organizational layers/concepts in the Factom system are:
Directory Layer -- Organizes the Merkle Roots of Entry Blocks
Entry Block Layer -- Organizes references to Entries
Entries -- Contains an Application's raw data or a hash of its private data
Chains -- Grouping of entries specific to an Application
To access a specific chain, you usually will use its ChainName or ChainID. Here's a sample ChainID:18de01b1ba2cea239c701501df630e93936c658561a33d1c0d08ee5281790aee
Copy that and go to the factom explorer to search for it. You'll find
Click the first entry in the chain. This is what the Chain Name points to. The ChainName is the set of External IDs of the first entry in a chain, this set of ExtIDs must be unique among all other first entries in Factom. The ChainID is formed by a hashing the series of ChainName segments, then those hashes are concatenated, and are hashed again into a single 32 byte ChainID. (see the data structures doc for more info.)
ChainID = SHA256( SHA256(ChainName[0]) | SHA256(ChainName[1]) | ... | SHA256(ChainName[n]) )
To write your own data to the chain, construct a new Factom Entry with the following:
ChainID - the chain you want to write to
ExtIDs - optional IDs or tags that are interpretted client side, in some application specific manner
Content - the raw or hashed data you want to put on the blockchain
See here for more specific details on what makes up an Entry.
Note that:
ExtIDs & Content are optional for Entries
Content is optional for Chains
Now you'll have to commit the Entry. This is signed with the specified EC address and broadcast to the network. At this point, no one else in the factom network knows what your Entry actually contains, they can only see that you paid for the ability to publish content and that you provided a hash of that content. When you reveal the Entry, your content then becomes public and able to be checked against what you committed to publishing just before.
Build and run the program, and it should print your transaction ID.
Copy the transaction ID and paste it into the block explorer. It'll show up in the next directory block within 10 minutes or so.
Creating your own chain
Just like we did for writing something to an existing chain, we'll use the same commit-reveal scheme. Create an Entry, and give it a set of ExtIDs. Remember from earlier that since this is the first entry of a chain, the ExtIDs are used as the ChainName, which is in turn used to calculate the ChainID.
Pick your own ChainName, run it a first time, and you should be able to find your newly created chain at the ChainID that is printed. If you run it a second time, another entry will be added to the chain that was just created.
Congrats! You've learned the basic building blocks of Factom in go and are ready to start building applications with tamper-proof data.
If you want to continue with a more in-depth tutorial, check out the following great tutorials that build on the one you just went through:
Last updated