From 8a73a2f8a5b0faf876fcf0185a780fc4671ad37d Mon Sep 17 00:00:00 2001 From: David Vennik Date: Mon, 28 Feb 2022 08:52:07 +0200 Subject: [PATCH] updating structure and documentation --- .gitignore | 1 + README.md | 2 +- {codec/bech32 => bech32}/bech32.go | 29 ++++++++++++++++++++++++++- codec/README.md | 8 -------- codec/doc.go | 17 ---------------- {codec/codecer => codecer}/codecer.go | 0 codec/general.go => general.go | 6 +++--- codec/types.go => types.go | 2 +- 8 files changed, 34 insertions(+), 31 deletions(-) rename {codec/bech32 => bech32}/bech32.go (51%) delete mode 100644 codec/README.md delete mode 100644 codec/doc.go rename {codec/codecer => codecer}/codecer.go (100%) rename codec/general.go => general.go (80%) rename codec/types.go => types.go (99%) diff --git a/.gitignore b/.gitignore index dff6331..273bf8e 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ /.idea/modules.xml /.idea/transcribe.iml /.idea/vcs.xml +/.idea/ diff --git a/README.md b/README.md index 551cb3d..4da97e4 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ microservice, including simple concurrency This tutorial demonstrates the use of almost every possible and important feature of Go. A "toy" implementation of a gRPC/protobuf microservice is added -in order to illustrate almost everything. +in order to illustrate almost everything else. In order to demonstrate synchronisation primitives, waitgroups, atomics and mutexes, the service will keep track of the number of invocations, print this diff --git a/codec/bech32/bech32.go b/bech32/bech32.go similarity index 51% rename from codec/bech32/bech32.go rename to bech32/bech32.go index 0ecce68..0c3ffc8 100644 --- a/codec/bech32/bech32.go +++ b/bech32/bech32.go @@ -8,7 +8,9 @@ package bech32 import ( - "github.com/cybriq/transcribe/codec" + "fmt" + "github.com/cosmos/btcutil/bech32" + codec "github.com/quanterall/kitchensink" ) // Spec is the collection of elements derived from the codec type definition @@ -30,3 +32,28 @@ var Spec = codec.Codec{ return false }, } + +// ConvertAndEncode converts from a base64 encoded byte string to base32 encoded byte string and then to bech32. +func ConvertAndEncode(hrp string, data []byte) (string, error) { + converted, err := bech32.ConvertBits(data, 8, 5, true) + if err != nil { + return "", fmt.Errorf("encoding bech32 failed: %w", err) + } + + return bech32.Encode(hrp, converted) +} + +// DecodeAndConvert decodes a bech32 encoded string and converts to base64 encoded bytes. +func DecodeAndConvert(bech string) (string, []byte, error) { + hrp, data, err := bech32.Decode(bech, 1023) + if err != nil { + return "", nil, fmt.Errorf("decoding bech32 failed: %w", err) + } + + converted, err := bech32.ConvertBits(data, 5, 8, false) + if err != nil { + return "", nil, fmt.Errorf("decoding bech32 failed: %w", err) + } + + return hrp, converted, nil +} diff --git a/codec/README.md b/codec/README.md deleted file mode 100644 index cc9cf78..0000000 --- a/codec/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# codec - -codec is an example and framework for building custom human readable binary -transcription encoders. - -The type definition found in [types.go](./types.go) is intended to be used -by external definitions that fill in all the fields with the concrete -implementation details. diff --git a/codec/doc.go b/codec/doc.go deleted file mode 100644 index 5189e36..0000000 --- a/codec/doc.go +++ /dev/null @@ -1,17 +0,0 @@ -// Package codec is a generalised encoder and decoder for encoding short -// binary values such as used in cryptocurrency addresses, accounts and -// transaction hashes. -// -// This codec is intended to be extended via implementations adding the -// functions specific to an implementation to the generalised implementation -// provided in this package that invokes these implementation specific features -// and configurations. -// -// This is part of a tutorial for teaching the correct way to create a Go -// library, which includes this file that provides the header for the godoc -// output. -// -// All exported symbols in Go code should have proper and informative comments -// added above them as this adds to the head of the package identifier. No -// released code should be without these on every identifier. -package codec diff --git a/codec/codecer/codecer.go b/codecer/codecer.go similarity index 100% rename from codec/codecer/codecer.go rename to codecer/codecer.go diff --git a/codec/general.go b/general.go similarity index 80% rename from codec/general.go rename to general.go index 4736b0e..a2e40c9 100644 --- a/codec/general.go +++ b/general.go @@ -1,10 +1,10 @@ -package codec +package transcribe import ( - . "github.com/cybriq/transcribe/codec/codecer" + "github.com/quanterall/kitchensink/codecer" ) -func NewCodec(cdc *Codec) Codecer { +func NewCodec(cdc *Codec) codecer.Codecer { // Make sure the provided codec has all the parts that are used in the // interface if cdc.Encoder == nil || diff --git a/codec/types.go b/types.go similarity index 99% rename from codec/types.go rename to types.go index d17b49d..99af986 100644 --- a/codec/types.go +++ b/types.go @@ -1,4 +1,4 @@ -package codec +package transcribe import ( "github.com/cybriq/transcribe/codec/codecer"