github.com/aergoio/aergo@v1.3.1/cmd/aergoluac/encoding/codeEncoding.go (about)

     1  package encoding
     2  
     3  import (
     4  	"encoding/hex"
     5  	"errors"
     6  	"fmt"
     7  	"github.com/anaskhan96/base58check"
     8  )
     9  
    10  const CodeVersion = 0xC0
    11  
    12  func EncodeCode(code []byte) string {
    13  	encoded, _ := base58check.Encode(fmt.Sprintf("%x", CodeVersion), hex.EncodeToString(code))
    14  	return encoded
    15  }
    16  
    17  func DecodeCode(encodedCode string) ([]byte, error) {
    18  	decodedString, err := base58check.Decode(encodedCode)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	decodedBytes, err := hex.DecodeString(decodedString)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	version := decodedBytes[0]
    27  	if version != CodeVersion {
    28  		return nil, errors.New("Invalid code version")
    29  	}
    30  	decoded := decodedBytes[1:]
    31  	return decoded, nil
    32  }