Skip to content

Commit

Permalink
feat(logic)!: add base64Decode uri key on cosmwasm uri
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Jun 22, 2023
1 parent bd6c780 commit 0516290
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions x/logic/fs/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io/fs"
"net/url"
"strconv"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -15,8 +16,9 @@ import (
)

const (
queryKey = "query"
scheme = "cosmwasm"
queryKey = "query"
base64DecodeKey = "base64Decode"
scheme = "cosmwasm"
)

type WasmHandler struct {
Expand Down Expand Up @@ -57,21 +59,33 @@ func (w WasmHandler) Open(ctx context.Context, uri *url.URL) (fs.File, error) {
}
query := uri.Query().Get(queryKey)

var base64Decode = true
if uri.Query().Has(base64DecodeKey) {
if base64Decode, err = strconv.ParseBool(uri.Query().Get(base64DecodeKey)); err != nil {
return nil, fmt.Errorf("failed convert 'base64Decode' query value to boolean: %w", err)
}
}

data, err := w.wasmKeeper.QuerySmart(sdkCtx, contractAddr, []byte(query))
if err != nil {
return nil, fmt.Errorf("failed query wasm keeper: %w", err)
}

var program string
err = json.Unmarshal(data, &program)
if err != nil {
return nil, fmt.Errorf("failed unmarshal json wasm response to string: %w", err)
}
if base64Decode {
var program string
err = json.Unmarshal(data, &program)
if err != nil {
return nil, fmt.Errorf("failed unmarshal json wasm response to string: %w", err)
}

decoded, err := base64.StdEncoding.DecodeString(program)
if err != nil {
return nil, fmt.Errorf("failed decode wasm base64 respone: %w", err)
decoded, err := base64.StdEncoding.DecodeString(program)
if err != nil {
return nil, fmt.Errorf("failed decode wasm base64 respone: %w", err)
}

return NewVirtualFile(decoded, uri, sdkCtx.BlockTime()), nil
}

return NewVirtualFile(decoded, uri, sdkCtx.BlockTime()), nil
return NewVirtualFile(data, uri, sdkCtx.BlockTime()), nil

}

0 comments on commit 0516290

Please sign in to comment.