Getting started with Go.
You add one module, connect with your service key, and your first sealed record is stored in about forty lines.
Pure Go, no cgo
1. Add the module
go get vaultdb.ch/client@v0.6.1 fetches the module directly from vaultdb.ch.
2. Get a server to talk to
To try it, run a test server on your machine: vaultdb-hosted --memory prints a card on its first line and keeps nothing on disk. For real use, write to us: you make a service key in your program (vaultdb.GenerateServiceKey(), a secret you keep), send us only its public half (vaultdb.ServicePublic(secret)), and we send you your card.
3. Run the example
It connects, creates a vault whose key you derive yourself, puts a deal and a document, searches by the start of a title, stores a file and reads it back, and reads the audit.
func example(ctx context.Context, card vaultdb.Card, serviceKey, master []byte) error {
conn, err := vaultdb.Connect(ctx, card, serviceKey)
if err != nil {
return err
}
defer conn.Close()
// The vault's key is derived on your side and never sent.
k, err := hkdf.Key(sha256.New, master, nil, "deal-4711", 32)
if err != nil {
return err
}
decl := vaultdb.Declaration{Types: map[string]vaultdb.TypeDecl{
"deal": {Parents: []string{""}},
"document": {Parents: []string{"deal"}, Fields: map[string]vaultdb.FieldDecl{"title": {}}},
}}
const actor = "user-1"
vault, err := vaultdb.CreateVault(ctx, conn, actor, k, decl)
if err != nil {
return err
}
deal, err := vault.Put(ctx, actor, vaultdb.Item{Type: "deal", App: []byte("Deal 4711")})
if err != nil {
return err
}
doc, err := vault.Put(ctx, actor, vaultdb.Item{
Type: "document",
Parent: vaultdb.ParentID(deal.ID),
Indexed: vaultdb.Indexed{"title": "Term sheet"},
App: []byte("first draft"),
})
if err != nil {
return err
}
found, err := vault.Search(ctx, actor, vaultdb.SearchQuery{
Type: "document",
Where: []vaultdb.Condition{{Field: "title", Match: vaultdb.MatchPrefix, Text: "term"}},
})
if err != nil {
return err
}
if len(found.Records) != 1 || found.Records[0].ID != doc.ID {
return fmt.Errorf("search found %d records", len(found.Records))
}
pdf := bytes.NewReader(bytes.Repeat([]byte("page "), 100000))
_, err = vault.PutFile(ctx, actor, vaultdb.FilePut{
Entry: doc.ID, RecordCRID: doc.CRID,
Name: "term-sheet.pdf", Type: "application/pdf",
Size: pdf.Size(), Source: pdf,
})
if err != nil {
return err
}
files, err := vault.ListFiles(ctx, actor, doc.ID, doc.CRID, "")
if err != nil {
return err
}
var back bytes.Buffer
if err := vault.ReadFile(ctx, actor, files.Files[0], &back); err != nil {
return err
}
events, err := vault.AuditRead(ctx, actor, vaultdb.AuditQuery{Entry: doc.ID})
if err != nil {
return err
}
fmt.Println(files.Files[0].Meta.Name, back.Len(), "bytes back,", len(events.Events), "audit events")
return nil
}4. What it prints
term-sheet.pdf 500000 bytes back, 4 audit events
Where your key comes from
You derive the vault's key from your own master secret, as the example does with hkdf.Key. We never see it. If you lose it, nobody can open the vault, us included.
Files and errors
A file up to 1 GiB goes up in 4 MiB parts. If an upload drops half way, run it again with the same file id and version and it sends only the parts the server is still missing. Errors are one type, *vaultdb.Error, with a code, a detail and a next step; use errors.As.
When a node changes its key
When a node changes its key, Connect follows it by itself — nobody needs a new card. If we switch your vault over to its replica, the client follows there too. conn.Card() returns the current card, and Options{OnCardChange: func(card []byte) {...}} is called once with the updated card JSON so you can save it; a program that never saves keeps working and learns the new key again on its next start.
Plain tables
A vault also holds plain SQLite tables of your own, for state about no one in particular: counters, queues, slots, settings. PlainQuery reads, PlainTx writes several statements all or nothing, and a statement's Expect checks and writes in one step. Only the vault's owner may call them. The SQL, its parameters and the rows are readable by the server and are not audited, and a row you delete stays in the vault's log until you shred the vault, so personal data stays in sealed records. Values are PlainNull, PlainInt, PlainReal, PlainText and PlainBlob, and come back exactly as they went in.
func examplePlain(ctx context.Context, vault *vaultdb.Vault) error {
const actor = "user-1"
one := int64(1)
_, err := vault.PlainTx(ctx, actor, []vaultdb.PlainStatement{
{SQL: "CREATE TABLE IF NOT EXISTS slots (day INTEGER PRIMARY KEY, who TEXT)"},
{
// Book day 20260301 exactly once: a second try changes no row and the call fails.
SQL: "INSERT OR IGNORE INTO slots (day, who) VALUES (?1, ?2)",
Params: []vaultdb.PlainValue{vaultdb.PlainInt(20260301), vaultdb.PlainText("desk 3")},
Expect: &vaultdb.PlainExpect{Changes: &one},
},
})
if err != nil {
return err
}
slots, err := vault.PlainQuery(ctx, actor, "SELECT day, who FROM slots WHERE day >= ?1", vaultdb.PlainInt(20260101))
if err != nil {
return err
}
fmt.Println(len(slots.Rows), "slot booked:", slots.Rows[0][1].Text)
return nil
}