filled out neccessaries for implementing interface into base type

This commit is contained in:
David Vennik
2022-02-27 09:55:28 +02:00
parent cb803b8482
commit ecb6a6872f
5 changed files with 75 additions and 16 deletions

View File

@@ -2,3 +2,9 @@
codec is an example and framework for building custom human readable binary codec is an example and framework for building custom human readable binary
transcription encoders. 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)

View File

@@ -1,10 +1,32 @@
// Package bech32 provides a (somewhat simplified) version of the standard // Package bech32 provides a (somewhat simplified) version of the standard
// Bech32 human readable binary codec for rendering hashes into a form that can // 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 package bech32
import ( 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
},
}

View File

@@ -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"
)

20
codec/general.go Normal file
View File

@@ -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
}

View File

@@ -4,7 +4,8 @@ package codec
// Codec // Codec
// //
// This is an example of the use of a structure definition to encapsulate and // 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 { type Codec struct {
// Name is the human readable name given to this encoder // Name is the human readable name given to this encoder
Name string Name string
@@ -21,10 +22,28 @@ type Codec struct {
Charset string Charset string
// Encode takes an arbitrary length byte input and returns the output as // Encode takes an arbitrary length byte input and returns the output as
// defined for the codec // 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 // Decode takes an encoded string and returns if the encoding is valid and
// the value passes any check function defined for the type // the value passes any check function defined for the type
Decode func(input string) (valid bool, output []byte) Decoder func(input string) (valid bool, output []byte)
// AddCheck is used by Encode to add extra bytes for the // AddCheck is used by Encode to add extra bytes for the checksum to ensure
AddCheck func(input []byte) (output []byte) // 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)
} }