Factom Protocol Docs
  • Welcome Developers
  • Protocol Overview
  • Getting Started
    • Installing Factom
    • Developer Sandbox
    • Hello-World Examples
      • Golang
      • JavaScript
      • Python
    • API Reference
      • Factomd
      • Factom-walletd
      • Debug
    • CLI Reference
      • Run Factom Federation
      • Common Tasks
      • Factom CLI Commands
    • Data Structures
      • Building blocks
      • User elements
      • Block types
    • SDKs
      • Open Source SDKs
      • Commercial SDKs
    • Testnet
      • Installation
      • Node monitoring
      • Maintenance
      • Interacting with Factom blockchain
      • Faucet
      • Frequently Asked Questions (FAQ)
      • Useful links
  • Infrastructure
    • Control Panel
    • Explorer
      • Overview
      • Block types
      • Usage
    • Open Node
  • Technologies
    • FAT Tokenization
    • Voting Protocols
  • Protocol development
    • Factom Improvement Proposals
    • Hacking Factom
  • Wallets
    • Ledger Nano S Wallet
    • Enterprise Wallet
      • Install
      • Create FCT Address
      • Send and Receive FCT
      • Backup Wallet
      • Restore From a Backup Seed
      • Importing/Exporting your private key(s)
      • Select Your Factomd Node
      • Upgrade to a Secure Wallet
    • FAT Wallet
    • MyFactomWallet
    • Paper Wallet
  • Authority Node Operators
    • ANO Guides & Tutorials
      • Getting started
      • Generating your server identity
      • Setting the coinbase address and node efficiency
      • Joining the docker swarm
      • Securing your node with SSH and iptables
      • Updating and brainswapping your node
    • ANO Tools and Resources
Powered by GitBook
On this page
  • Prerequisites
  • Getting Started with Development
  • Connect to a factomd server node
  • Writing data to a chain
  • Creating your own chain

Was this helpful?

  1. Getting Started
  2. Hello-World Examples

Golang

Factom "Hello world" in golang.

PreviousHello-World ExamplesNextJavaScript

Last updated 6 years ago

Was this helpful?

Prerequisites

  • A valid Entry Credit address with a few EC (see how to do that )

  • A working Go environment (get that )

  • 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 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.

hello-world-factom
package main


import(
    "github.com/FactomProject/factom"
)

func init() {
    // Connect to Factom Open Node
    factom.SetFactomdServer("https://api.factomd.net")
}

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:

  1. Directory Layer -- Organizes the Merkle Roots of Entry Blocks

  2. Entry Block Layer -- Organizes references to Entries

  3. Entries -- Contains an Application's raw data or a hash of its private data

  4. 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

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

hello-world-factom
...


func main() {
    // TODO: don't store key in plaintext, use a more secure way
    ecSecret := "YOUR EC PRIVATE KEY HERE"
    ecAddress, err := factom.GetECAddress(ecSecret)
    if err != nil {
        log.Fatal(err)
    }

    // create a new Factom entry
    e := factom.Entry{}
    e.ChainID = "18de01b1ba2cea239c701501df630e93936c658561a33d1c0d08ee5281790aee"
    e.ExtIDs = [][]byte{[]byte("ExampleTag")}
    e.Content = []byte("Hello, Factom!")
}

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.

hello-world-factom
func main() {
    ...

    // Commit the entry, then reveal it
    txID, err := factom.CommitEntry(&e, ecAddress)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Transaction ID: %s", txID)

    if _, err := factom.RevealEntry(&e); err != nil {
        log.Fatal(err)
    }
}

Build and run the program, and it should print your transaction ID.

go build
go ./introduction

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.

hello-world-factom
func main() {
    // TODO: don't store key in plaintext, use a more secure way
    ecSecret := "YOUR EC PRIVATE KEY HERE"
    ecAddress, err := factom.GetECAddress(ecSecret)
    if err != nil {
        log.Fatal(err)
    }

    chainName := []string{"Factom Tutorial", "Intro", "YOUR TEST CHAIN NAME"}
    chainID := ConstructChainID(chainName)

    // create a new Factom entry
    e := factom.Entry{}
    e.ChainID = chainID
    e.Content = []byte("Something important...")

    if !factom.ChainExists(chainID) {
        // Chain doesn't exist yet. Create, commit, and reveal it.
        for _, nameSegment := range chainName {
            e.ExtIDs = append(e.ExtIDs, []byte(nameSegment))
        }
        c := factom.NewChain(&e)

        if _, err := factom.CommitChain(c, ecAddress); err != nil {
            log.Fatal(err)
        }
        if _, err := factom.RevealChain(c); err != nil {
            log.Fatal(err)
        }
        fmt.Printf("Chain Created: %s", chainID)
        return
    }

    // The chain exists already. So ExtID is no longer the chain name.
    // Treat them however you want. Tags, flags, names, priorities, etc.
    e.ExtIDs = [][]byte{[]byte("ExampleTag")}

    // Commit the entry, then reveal it
    txID, err := factom.CommitEntry(&e, ecAddress)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Transaction ID: %s", txID)

    if _, err := factom.RevealEntry(&e); err != nil {
        log.Fatal(err)
    }
}

// ConstructChainID takes the ChainName as a string array and returns its ChainID
// see: https://developers.factomprotocol.org/start/factom-data-structures
func ConstructChainID(chainName []string) string {
    e := factom.Entry{}
    for _, nameSegment := range chainName {
        e.ExtIDs = append(e.ExtIDs, []byte(nameSegment))
    }
    chain := factom.NewChain(&e)
    return chain.ChainID
}

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:

Copy that and go to the 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 for more info.)

See for more specific details on what makes up an Entry.

Pick your own ChainName, run it a first time, and you should be able to 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.

here
here
developer sandbox setup guide
factom explorer
data structures doc
here
find your newly created chain
Proof of Life: an Intro to Client-Side Validation
Proof of Life (part 2): Structured Chains
Block Explorer
First Entry