Tezos

Low-level types and encoding

Visit the GoDoc docu or read the source code on Github.

GoDoc

The Tezos package contains type definitions, constants, enums and encodings for elementary types used throughout the Tezos protocol. The main job of this package is to provide conversion functions for binary and text/JSON encoding which are utilized by all higher-level packages:

  • Typed hashes (e.g. BlockHash, OpHash, etc) and magic bytes for base58 encoded strings

  • Constants and enums for operations type/status, rights, voting, etc

  • Address type to represent all known Tezos address formats (tz1, tz2, tz3, KT1, bz1)

  • Public/private keys for Ed25519, Secp256k1 and P256 curves including support for generating and encrypting private keys

  • Signature type representing all Tezos signature schemes (edsig, spsig1, p2sig, sig)

  • Params to represent chain/protocol configuration parameters and perform basic math on cycles and voting periods

  • Zarith number types for signed (Z) and unsigned (N) numbers used in smart contracts, block headers and operations

Examples

Parsing a Tezos address

Parses/decodes an address and outputs its components. See examples/addr for more.

import "blockwatch.cc/tzgo/tezos"

// parse and panic if invalid
addr := tezos.MustParseAddress("tz3RDC3Jdn4j15J7bBHZd29EUee9gVB1CxD9")

// or parse and return error if invalid
addr, err := tezos.ParseAddress("tz3RDC3Jdn4j15J7bBHZd29EUee9gVB1CxD9")
if err != nil {
    fmt.Printf("Invalid address: %v\n", err)
}

// Do smth with the address
fmt.Printf("Address type = %s\n", addr.Type())
fmt.Printf("Address bytes = %x\n", addr.Bytes())

Generating and encrypting a private key

Creates an Ed25519 key and encrypts it with a password. See examples/key for more.

import (
    "blockwatch.cc/tzgo/tezos"
    "os"
    "log"
    "fmt"
)

// read a password from env
func readPassword() tezos.PassphraseFunc {
    return os.Getenv("TEZOS_KEY_PASSPHRASE")
}

func main() {
    // generate a new private key
    sk, err := tezos.GenerateKey(tezos.KeyTypeEd25519)
    if err != nil {
        log.Fatalln(err)
    }

    // encrypt the key
    enc, err := sk.Encrypt(readPassword())
    if err != nil {
        log.Fatalln(err)
    }

    fmt.Printf("Private Key %s\n", enc)
    fmt.Printf("Public Key  %s\n", sk.Public())
    fmt.Printf("Address     %s\n", sk.Public().Address())
}

Signing and verifying an arbitrary message

Uses a private key to sign the digest (blake2b hash) of a message and then verifies the message signature with the corresponding public key. See examples/key for more.

import (
    "blockwatch.cc/tzgo/tezos"
    "fmt"
)

var msg = []byte("Hello Tezos!")

func main() {
    // we use a hard coded key for demo purposes
    sk, _ := tezos.ParseKey("edsk3buGNhJBQTw4G44qP3gduGoa9rxSqYJLMBbi3uSbVkNFgAuNZp")
    pk := sk.Public()

    // sign message digest
    digest := tezos.Digest(msg)
    sig, _ := sk.Sign(digest[:])

    fmt.Printf("Digest   %x\n", digest[:])
    fmt.Printf("Sig      %s\n", sig)

    // verify signature using the public key and the same message digest
    err := pk.Verify(digest[:], sig);
    if  err == nil {
        fmt.Println("Signature OK")
    } else {
        fmt.Println(err)
    }
}

Last updated