Start adding NutsDB

This commit is contained in:
Netscape Navigator 2020-08-20 08:13:38 -05:00
parent 81c0d66f54
commit 6d1231dcde
5 changed files with 112 additions and 18 deletions

1
go.mod
View File

@ -14,6 +14,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.0
github.com/xujiajun/nutsdb v0.5.0
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
golang.org/x/text v0.3.3
gopkg.in/ini.v1 v1.57.0 // indirect

10
go.sum
View File

@ -24,6 +24,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
@ -200,6 +202,13 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/xujiajun/gorouter v1.2.0/go.mod h1:yJrIta+bTNpBM/2UT8hLOaEAFckO+m/qmR3luMIQygM=
github.com/xujiajun/mmap-go v1.0.1 h1:7Se7ss1fLPPRW+ePgqGpCkfGIZzJV6JPq9Wq9iv/WHc=
github.com/xujiajun/mmap-go v1.0.1/go.mod h1:CNN6Sw4SL69Sui00p0zEzcZKbt+5HtEnYUsc6BKKRMg=
github.com/xujiajun/nutsdb v0.5.0 h1:j/jM3Zw7Chg8WK7bAcKR0Xr7Mal47U1oJAMgySfDn9E=
github.com/xujiajun/nutsdb v0.5.0/go.mod h1:owdwN0tW084RxEodABLbO7h4Z2s9WiAjZGZFhRh0/1Q=
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b h1:jKG9OiL4T4xQN3IUrhUpc1tG+HfDXppkgVcrAiiaI/0=
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b/go.mod h1:AZd87GYJlUzl82Yab2kTjx1EyXSQCAfZDhpTo1SQC4k=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
@ -259,6 +268,7 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

26
pigeon/b32.go Normal file
View File

@ -0,0 +1,26 @@
package pigeon
import (
"encoding/base32"
"fmt"
)
var alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
var encoder = base32.NewEncoding(alphabet).WithPadding(base32.NoPadding)
// B32Encode does Crockford 32 encoding on a string.
func B32Encode(data []byte) string {
return encoder.EncodeToString(data)
}
// B32Decode takes a Crockford Base32 string and converts it
// to a byte array.
func B32Decode(input string) []byte {
output, error := encoder.DecodeString(input)
if error != nil {
msg := fmt.Sprintf("Error decoding Base32 string %s", input)
panic(msg)
}
return output
}

62
pigeon/db.go Normal file
View File

@ -0,0 +1,62 @@
package pigeon
import (
"log"
"github.com/xujiajun/nutsdb"
)
// Database will open the NutsDB database.
func createDB() *nutsdb.DB {
// Open the database located in the /tmp/nutsdb directory.
// It will be created if it doesn't exist.
opt := nutsdb.DefaultOptions
opt.Dir = "./nutsdb"
db, err := nutsdb.Open(opt)
if err != nil {
log.Fatal(err)
}
return db
}
var database = createDB()
var configBucket = "config"
// A ConfigKey represents a key used within the NutsDB
// "config" bucket.
type ConfigKey string
const (
// ConfigSecret is the binary representation of the users
// ED25519 secret key.
ConfigSecret ConfigKey = "secret"
)
// PutConfig writes a configuration value to NutsDB
func PutConfig(k ConfigKey, v []byte) {
database.Update(func(tx *nutsdb.Tx) error {
err1 := tx.Put(configBucket, []byte(k), v, 0)
if err1 != nil {
log.Fatal(err1)
}
err2 := tx.Commit()
if err2 != nil {
log.Fatal(err2)
}
return nil
})
}
func getConfig(k ConfigKey) []byte {
var output []byte
database.View(
func(tx *nutsdb.Tx) error {
val, err1 := tx.Get(configBucket, []byte(k))
if err1 != nil {
log.Fatal(err1)
}
output = val.Value
return nil
})
return output
}

View File

@ -2,7 +2,6 @@ package pigeon
import (
"crypto/ed25519"
"encoding/base32"
"fmt"
"os"
)
@ -12,7 +11,7 @@ const Version = "0.0.0"
// CreateKeypair makes a new ED25519 key pair. Just a thin
// wrapper around crypto/ed25519.
func CreateKeypair() (ed25519.PublicKey, ed25519.PrivateKey) {
func createKeypair() (ed25519.PublicKey, ed25519.PrivateKey) {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
fmt.Println(err)
@ -22,22 +21,18 @@ func CreateKeypair() (ed25519.PublicKey, ed25519.PrivateKey) {
return pub, priv
}
// Encoder is an Encoder
var Encoder = base32.NewEncoding("0123456789ABCDEFGHJKMNPQRSTVWXYZ").WithPadding(base32.NoPadding)
// B32Encode does Crockford 32 encoding on a string.
func B32Encode(data []byte) string {
return Encoder.EncodeToString(data)
// CreateIdentity is used by the CLI to create an ED25519
// keypair and store it to disk. It returns the private key
// as a Base32 encoded string
func CreateIdentity() (ed25519.PublicKey, ed25519.PrivateKey) {
pub, priv := createKeypair()
PutConfig(ConfigSecret, priv)
return pub, priv
}
// B32Decode takes a Crockford Base32 string and converts it
// to a byte array.
func B32Decode(input string) []byte {
output, error := Encoder.DecodeString(input)
if error != nil {
msg := fmt.Sprintf("Error decoding Base32 string %s", input)
panic(msg)
}
return output
// GetIdentity retrieves the user's signing key
func GetIdentity() []byte {
return getConfig(ConfigSecret)
}
func EncodeUserMhash() {}