encoding

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

Turnkey Go SDK Encoding Module

GoDocs

The encoding module provides utilities for hexadecimal, Base58, and JSON encoding. It is primarily used as a shared dependency of the crypto module, but can also be imported independently in applications that need Turnkey-compatible encoding helpers.

Installation

go get github.com/tkhq/go-sdk/encoding

import "github.com/tkhq/go-sdk/encoding"

Key Types and Functions

HexBytes

HexBytes is a []byte type that marshals to/from a hex-encoded JSON string. Use it for fields that need to serialize as hex in JSON.

BytesToHex / HexToBytes

BytesToHex converts a byte slice to a lowercase hex string. HexToBytes converts a hex string back to a byte slice, with optional left-padding to a target length.

Bs58Encode / Bs58Decode

Bs58Encode encodes a byte slice into a Base58 string, and Bs58Decode decodes a Base58 string back into a byte slice.

HexToBase64URL

HexToBase64URL converts a hex string to a base64url-encoded string (no padding). Accepts an optional target byte length for left-padding.

Contributing + License

Contributions are welcome! Please open an issue or submit a pull request with any improvements or bug fixes.

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Documentation

Overview

Package encoding provides encoding and decoding utilities ported from the Turnkey TypeScript SDK.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base64ToBase64URL

func Base64ToBase64URL(input string) string

Base64ToBase64URL converts a standard base64 string to base64url by replacing '+' with '-', '/' with '_', and stripping '=' padding.

func Base64URLToBase64

func Base64URLToBase64(input string) string

Base64URLToBase64 converts a base64url string back to standard base64 by replacing '-' with '+', '_' with '/', and re-adding '=' padding.

func Base64URLToString

func Base64URLToString(input string) (string, error)

Base64URLToString decodes a base64url string to a plain UTF-8 string.

func Bs58CheckDecode

func Bs58CheckDecode(s string) ([]byte, error)

Bs58CheckDecode decodes a base58check string, verifies the checksum, and returns the payload.

func Bs58CheckDecodeUnsafe

func Bs58CheckDecodeUnsafe(s string) []byte

Bs58CheckDecodeUnsafe decodes a base58check string, returning nil on any error.

func Bs58CheckEncode

func Bs58CheckEncode(payload []byte) string

Bs58CheckEncode appends a 4-byte double-SHA256 checksum to payload and base58 encodes the result.

func Bs58Decode

func Bs58Decode(s string) []byte

Bs58Decode decodes a base58 string to a byte slice. Returns nil for invalid input.

func Bs58DecodeUnsafe

func Bs58DecodeUnsafe(s string) ([]byte, bool)

Bs58DecodeUnsafe decodes a base58 string, returning (nil, false) on invalid input.

func Bs58Encode

func Bs58Encode(b []byte) string

Bs58Encode encodes a byte slice to a base58 string.

func BytesToHex

func BytesToHex(input []byte) string

BytesToHex converts a byte slice into a lowercase hex string.

func Checksum

func Checksum(payload []byte) (checksum [4]byte)

Checksum returns the first four bytes of a double-SHA256 digest.

func HexToASCII

func HexToASCII(hexString string) string

HexToASCII converts a hex string to its ASCII string representation.

func HexToBase64URL

func HexToBase64URL(input string, length ...int) (string, error)

HexToBase64URL converts a hex string to a base64url string. If length is provided, the byte buffer is left-padded with zeros to that length.

func HexToBytes

func HexToBytes(hexString string, length ...int) ([]byte, error)

HexToBytes creates a byte slice from a hex string. If length is provided, the result is left-padded with zeros to that length; it is an error if the hex value exceeds length.

func NormalizePadding

func NormalizePadding(b []byte, targetLength int) ([]byte, error)

NormalizePadding pads or trims a byte slice to exactly targetLength bytes. When trimming, leading bytes must be zero or an error is returned.

func PointEncode

func PointEncode(raw []byte) ([]byte, error)

PointEncode compresses an uncompressed P-256 public key (65 bytes, 0x04 prefix) into its 33-byte compressed form (0x02 or 0x03 prefix).

func StringToBase64URL

func StringToBase64URL(input string) string

StringToBase64URL encodes a plain string to a base64url string (no padding).

func ValidateChecksum

func ValidateChecksum(payload []byte) error

ValidateChecksum validates that a payload has a valid base58check checksum in its last four bytes.

Types

type HexBytes

type HexBytes []byte

HexBytes marshals bytes as a hex-encoded JSON string.

func (HexBytes) MarshalJSON

func (bytes HexBytes) MarshalJSON() ([]byte, error)

func (*HexBytes) UnmarshalJSON

func (bytes *HexBytes) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL