generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logic): handle wasm uri on consult/1 predicate
- Loading branch information
Showing
5 changed files
with
111 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package fs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
type URIHandler interface { | ||
CanOpen(ctx context.Context, uri *url.URL) bool | ||
Open(ctx context.Context, uri *url.URL) ([]byte, error) | ||
} | ||
|
||
type Parser struct { | ||
Handlers []URIHandler | ||
} | ||
|
||
func (p *Parser) Parse(ctx context.Context, name string) ([]byte, error) { | ||
uri, err := url.Parse(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if uri.Scheme != "okp4" { | ||
return nil, fmt.Errorf("incompatible schema '%s' for %s", uri.Scheme, name) | ||
} | ||
|
||
for _, handler := range p.Handlers { | ||
if handler.CanOpen(ctx, uri) { | ||
return handler.Open(ctx, uri) | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("could not find handler for load %s file", name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package fs | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/okp4/okp4d/x/logic/types" | ||
) | ||
|
||
const queryKey = "query" | ||
const hostName = "wasm" | ||
|
||
type WasmFS struct { | ||
wasmKeeper types.WasmKeeper | ||
} | ||
|
||
func NewWasmFS(keeper types.WasmKeeper) WasmFS { | ||
return WasmFS{wasmKeeper: keeper} | ||
} | ||
|
||
func (w WasmFS) CanOpen(ctx context.Context, uri *url.URL) bool { | ||
return uri.Host == hostName | ||
} | ||
|
||
func (w WasmFS) Open(ctx context.Context, uri *url.URL) ([]byte, error) { | ||
sdkCtx := sdk.UnwrapSDKContext(ctx) | ||
|
||
paths := strings.SplitAfter(uri.Path, "/") | ||
if len(paths) != 2 { | ||
return nil, fmt.Errorf("incorect path, should contains only contract address : '://wasm/{contractAddr}?query={query}'") | ||
} | ||
|
||
contractAddr, err := sdk.AccAddressFromBech32(paths[1]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed convert path '%s' to contract address: %w", uri.Path, err) | ||
} | ||
|
||
if !uri.Query().Has(queryKey) { | ||
return nil, fmt.Errorf("uri should contains query params") | ||
} | ||
query := uri.Query().Get(queryKey) | ||
|
||
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) | ||
} | ||
|
||
decoded, err := base64.StdEncoding.DecodeString(program) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed decode wasm base64 respone: %w", err) | ||
} | ||
return decoded, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters