From ecb6a6872f012b9361041482a0370e98fa1df9eb Mon Sep 17 00:00:00 2001 From: David Vennik Date: Sun, 27 Feb 2022 09:55:28 +0200 Subject: [PATCH] filled out neccessaries for implementing interface into base type --- codec/README.md | 6 ++++++ codec/bech32/bech32.go | 28 +++++++++++++++++++++++++--- codec/const.go | 8 -------- codec/general.go | 20 ++++++++++++++++++++ codec/types.go | 29 ++++++++++++++++++++++++----- 5 files changed, 75 insertions(+), 16 deletions(-) delete mode 100644 codec/const.go create mode 100644 codec/general.go diff --git a/codec/README.md b/codec/README.md index 51d7e67..4554382 100644 --- a/codec/README.md +++ b/codec/README.md @@ -2,3 +2,9 @@ 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. + +The Bech32 implemnation can be found in [bech32/bech32.go](./bech32/bech32.go) diff --git a/codec/bech32/bech32.go b/codec/bech32/bech32.go index 14bad98..c06ffd0 100644 --- a/codec/bech32/bech32.go +++ b/codec/bech32/bech32.go @@ -1,10 +1,32 @@ // Package bech32 provides a (somewhat simplified) version of the standard // Bech32 human readable binary codec for rendering hashes into a form that can -// be read and transcribed by humans +// be read and transcribed by humans, as used in Bitcoin Segregated Witness +// addresses and the Cosmos SDK framework, amongst others. +// +// BIP 0173 https://en.bitcoin.it/wiki/BIP_0173 is the specification for this +// encoding. package bech32 import ( - "github.com/cybriq/transcribe/codec" + . "github.com/cybriq/transcribe/codec" ) -var Spec = codec.Codec{} +// Spec is the collection of elements derived from the codec type definition +// that creates the concrete implementation of a 'generic' functionality. +var Spec = Codec{ + Name: "Bech32", + HRP: "cybriq", + Charset: "qpzry9x8gf2tvdw0s3jn54khce6mua7l", + Encoder: func(input []byte) (output string) { + return "" + }, + Decoder: func(input string) (valid bool, output []byte) { + return true, nil + }, + MakeCheck: func(input []byte) (output []byte) { + return nil + }, + Check: func(input string) (valid bool) { + return false + }, +} diff --git a/codec/const.go b/codec/const.go deleted file mode 100644 index 9726290..0000000 --- a/codec/const.go +++ /dev/null @@ -1,8 +0,0 @@ -package codec - -const ( - // Base58 is the modified base58 character set used by Bitcoin. - Base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" - // Bech32 is the character set used by the Bech32 standard (cosmos, segwit) - Bech32 = "qpzry9x8gf2tvdw0s3jn54khce6mua7l" -) diff --git a/codec/general.go b/codec/general.go new file mode 100644 index 0000000..4736b0e --- /dev/null +++ b/codec/general.go @@ -0,0 +1,20 @@ +package codec + +import ( + . "github.com/cybriq/transcribe/codec/codecer" +) + +func NewCodec(cdc *Codec) Codecer { + // Make sure the provided codec has all the parts that are used in the + // interface + if cdc.Encoder == nil || + cdc.Decoder == nil { + // panic should not be in production code execution paths, but SHOULD be + // in execution paths that fail due to programmer errors, that must be + // fixed before production release. + panic("Programmer Error: " + + "codec does not have all necessary functions implemented", + ) + } + return cdc +} diff --git a/codec/types.go b/codec/types.go index 2ad8f6a..a52308b 100644 --- a/codec/types.go +++ b/codec/types.go @@ -4,7 +4,8 @@ package codec // Codec // // This is an example of the use of a structure definition to encapsulate and -// logically connect together all of the elements of an implementation, while also permitting this to be used by external code without further dependencies +// logically connect together all of the elements of an implementation, while +// also permitting this to be used by external code without further dependencies type Codec struct { // Name is the human readable name given to this encoder Name string @@ -21,10 +22,28 @@ type Codec struct { Charset string // Encode takes an arbitrary length byte input and returns the output as // defined for the codec - Encode func(input []byte) (output string) + Encoder func(input []byte) (output string) // Decode takes an encoded string and returns if the encoding is valid and // the value passes any check function defined for the type - Decode func(input string) (valid bool, output []byte) - // AddCheck is used by Encode to add extra bytes for the - AddCheck func(input []byte) (output []byte) + Decoder func(input string) (valid bool, output []byte) + // AddCheck is used by Encode to add extra bytes for the checksum to ensure + // correct input so user does not send to a wrong address by mistake, for + // example. + MakeCheck func(input []byte) (output []byte) + // Check returns whether the check is valid + Check func(input string) (valid bool) +} + +// Encode implements the Codecer.Encoder by calling the provided function, and allows +// the concrete Codec type to always satisfy the interface, while allowing it to +// be implemented entirely differently +func (c Codec) Encode(input []byte) (output string) { + return c.Encoder(input) +} + +// Decode implements the Codecer.Decoder by calling the provided function, and allows +// the concrete Codec type to always satisfy the interface, while allowing it to +// be implemented entirely differently +func (c Codec) Decode(input string) (valid bool, output []byte) { + return c.Decoder(input) }