Skip to content

Commit

Permalink
Add hex_to_base58 function
Browse files Browse the repository at this point in the history
  • Loading branch information
sapience committed Mar 13, 2024
1 parent 2897983 commit 55c6aaf
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions go/extension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package extension

import (
"context"
"encoding/hex"
"errors"
"fmt"
"log"
Expand All @@ -10,6 +11,8 @@ import (

"github.com/kwilteam/kwil-extensions/server"
"github.com/kwilteam/kwil-extensions/types"

"github.com/mr-tron/base58"
)

const (
Expand Down Expand Up @@ -63,6 +66,7 @@ func (e *FractalExt) BuildServer(logger *log.Logger) (*server.ExtensionServer, e
map[string]server.MethodFunc{
"get_block_height": server.WithOutputsCheck(e.BlockHeight, 1),
"has_grants": server.WithInputsCheck(server.WithOutputsCheck(e.GrantsFor, 1), 2),
"hex_to_base58": server.WithInputsCheck(server.WithOutputsCheck(e.HexToBase58, 1), 1),
}).
Build()
}
Expand Down Expand Up @@ -141,6 +145,20 @@ func (e *FractalExt) GrantsFor(ctx *types.ExecutionContext, values ...*types.Sca
return encodeScalarValues(exist)
}

func (e *FractalExt) HexToBase58(ctx *types.ExecutionContext, values ...*types.ScalarValue) ([]*types.ScalarValue, error) {
inputHex, err := values[0].String()
if err != nil {
return nil, fmt.Errorf("convert value to string failed: %w", err)
}
fmt.Println(inputHex)
binaryString, _ := hex.DecodeString(inputHex)
base58 := base58.Encode(binaryString)
public_key := fmt.Sprintf("ed25519:%s", base58)
fmt.Println(public_key)

return encodeScalarValues(public_key)
}

// initialize checks that the meta data includes all required fields and applies
// any default values.
func initialize(ctx context.Context, metadata map[string]string) (map[string]string, error) {
Expand Down

0 comments on commit 55c6aaf

Please sign in to comment.