Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registerAuthorizer to wasm #1275

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
//====== THIS IS AUTOGENERATED FILE. DO NOT MODIFY ========

package version
const VERSIONSTR = "v1.10.0-120-ga72a4ee1"
const VERSIONSTR = "v1.10.0-93-gf7075dc7"

55 changes: 55 additions & 0 deletions wasmsdk/auth_txn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build js && wasm
// +build js,wasm

package main

import (
"fmt"
"syscall/js"

"github.com/0chain/gosdk/core/sys"
)

// Declare a type for the callback function
type AuthCallbackFunc func(msg string) string

// Variable to store the callback function
var authCallback AuthCallbackFunc

// Register the callback function
func registerAuthorizer(this js.Value, args []js.Value) interface{} {
// Store the callback function
authCallback = parseAuthorizerCallback(args[0])

sys.Authorize = func(msg string) (string, error) {
return authCallback(msg), nil
}

return nil
}

// Use the stored callback function
func callAuth(this js.Value, args []js.Value) interface{} {
fmt.Println("callAuth is called")
if len(args) == 0 {
return nil
}

if authCallback != nil {
msg := args[0].String()
result, _ := sys.Authorize(msg)
fmt.Println("auth is called, result:", result)
return js.ValueOf(result)
}

return nil
}

// Parse the JavaScript callback function into Go AuthorizerCallback type
func parseAuthorizerCallback(jsCallback js.Value) AuthCallbackFunc {
return func(msg string) string {
// Call the JavaScript callback function from Go
result := jsCallback.Invoke(msg)
return result.String()
}
}
21 changes: 20 additions & 1 deletion wasmsdk/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ <h2>please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates
<button id="btnSplitKeys">SplitKeys</button>
<button id="btnSetWalletInfo">SetWalletInfo</button>
<button id="btnSetAuthUrl">SetAuthUrl</button>
<button id="btnRegAuth">regAuth</button>
</span>
<br>
<div id="splitKey">
Expand Down Expand Up @@ -276,7 +277,7 @@ <h2>please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates
alert(e)
}
})

let allocations = []
let blobbers = []
let files = []
Expand Down Expand Up @@ -898,5 +899,23 @@ <h2>please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates
}
})

onClick('btnRegAuth', async () => {
// Define the callback function in JavaScript
const authCallback = function (msg) {
return `Authorized: ${msg}`;
};

// Register the callback function from JavaScript to Go
const registerAuthorizer = goWasm.sdk.registerAuthorizer;
registerAuthorizer(authCallback);


// call the callAuth to see if the auth is set properly
// Use the stored callback function from Go
const message = "Hello, World!";
const result = goWasm.sdk.callAuth(message);
console.log(result); // Output: "Authorized: Hello, World!"
})

</script>
</body>
15 changes: 9 additions & 6 deletions wasmsdk/jsbridge/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ func BindFunc(global js.Value, jsFuncName string, fn interface{}) error {
func BindAsyncFuncs(global js.Value, fnList map[string]interface{}) {

for jsFuncName, fn := range fnList {
jsFunc, err := promise(fn)
if jsFuncName == "registerAuthorizer" || jsFuncName == "callAuth" {
global.Set(jsFuncName, fn)
} else {
jsFunc, err := promise(fn)

if err != nil {
log.Println(jsFuncName, err)
}
if err != nil {
log.Println("bridge promise failed:", jsFuncName, err)
}

global.Set(jsFuncName, jsFunc)
global.Set(jsFuncName, jsFunc)
}
}

}

func BindFuncs(global js.Value, fnList map[string]interface{}) {
Expand Down
8 changes: 5 additions & 3 deletions wasmsdk/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,11 @@ func main() {
"refreshJwtToken": refreshJwtToken,

//split key
"splitKeys": splitKeys,
"setWalletInfo": setWalletInfo,
"setAuthUrl": setAuthUrl,
"splitKeys": splitKeys,
"setWalletInfo": setWalletInfo,
"setAuthUrl": setAuthUrl,
"registerAuthorizer": js.FuncOf(registerAuthorizer),
"callAuth": js.FuncOf(callAuth),
})

fmt.Println("__wasm_initialized__ = true;")
Expand Down
Loading