Skip to content

Commit 56fff0f

Browse files
authored
Merge pull request #92 from mlabs-haskell/rmgaray/add-missing-docs-middleware
Add missing docs to paima-middleware
2 parents 5b91209 + c5fbcd0 commit 56fff0f

File tree

6 files changed

+128
-35
lines changed

6 files changed

+128
-35
lines changed

addons/@mlabs-haskell/cip-30-callbacks/cip_30_callbacks.gd

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,33 @@ extends Node
22
class_name Cip30Callbacks
33
## Provides a CIP-30 wrapper for a wallet in a web environment.
44
##
5-
## Used to register a CIP-30 wallet in a browser environment by adding
5+
## Used to register a godot-cardano wallet in a browser environment by adding
66
## GDScript callbacks to the global [code]window.cardano.godot[/code] Object. These callbacks
77
## defer their implementation to a [Cip30WalletApi] object, which is required for
8-
## initializing the class ([method Cip30Callbacks._init]).
8+
## initializing the class ([method Cip30Callbacks._init]).[br][br]
9+
##
10+
## A standard use of this class involves first creating a [Cip30WalletApi] object, which
11+
## implements most methods necessary for a CIP-30 wallet. In practice, you will want
12+
## to extend [Cip30WalletApi] and implement its virtual methods by deferring to an
13+
## existing godot-cardano wallet (like [OnlineWallet]).[br][br]
14+
##
15+
## With an instantiated [Cip30WalletApi], it is possible to wrap it in a [Cip30Callbacks].
16+
## The method [method Cip30Callbacks.add_to] can then be used for registering the wallet in the
17+
## browser window:[br][br]
18+
##
19+
## Example:[br][br]
20+
##
21+
## The window object is retrieved using the JavaScriptBridge interface[br]
22+
## [code]var window = JavaScriptBridge.get_interface("window")[/code][br][br]
23+
##
24+
## Then a class extending [Cip30WalletApi] is instantiated...[br]
25+
## [code]var cip_30_wallet = ...[/code][br][br]
26+
##
27+
## ... and registered in the window[br]
28+
## [code]Cip30Callbacks.new(cip_30_wallet).add_to(window)[/code][br][br]
29+
##
30+
## The result of this is that any web application can now use a godot-cardano provided wallet
31+
## via the CIP-30 interface available in the browser window.
932

1033
# NOTE
1134
# [JsCip30Api] contains a JS script that creates the `window.cardano.godot` Object
@@ -74,8 +97,8 @@ func _cb_sign_data(args):
7497
var signing_address: String = args[2]
7598
var data_hex: String = args[3]
7699

77-
# TODO: If we want proper CIP-30 support, we should parse the address (could be hex or bech32)
78-
# to pub key and differentiate between ProofGeneration and AddressNotPK errors
100+
# Possible improvement: Parse the address (could be hex or bech32) to pub key and
101+
# differentiate between ProofGeneration and AddressNotPK errors
79102

80103
# Paima framework will pass bech32 encoded Address into the sing request if the wallet is not Nami
81104
if !_cip_30_wallet.owns_address(signing_address):

addons/@mlabs-haskell/cip-30-callbacks/cip_30_js_api.gd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ class_name JsCip30Api
66
## This class is used internally by [Cip30Callbacks]. If you want to use the wallet
77
## provided by godot-cardano in a web environment, check the documentation for that
88
## class instead.[br][br]
9-
##
10-
## You should only use this class if you know what you are doing.
119

1210
## Initialize `window.cardano` (if it does not exist yet) and register the
1311
## godot-cardano wallet.

addons/@mlabs-haskell/cip-30-callbacks/cip_30_wallet_api.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ class_name Cip30WalletApi
44
## Virtual class for implementing a CIP-30 compatible wallet
55
##
66
## This class represents a CIP-30 compatible wallet. Inherit it and override
7-
## all of its methods to obtain a CIP-30 wallet that may be used to initialize
8-
## a [Cip30Callbacks] object (check [method Cip30Callbacks._init]).
7+
## all of its methods to obtain a CIP-30 wallet that may be registered in the browser
8+
## window with [Cip30Callbacks]. Check that class to see an example
9+
## of use.
910

1011
## [b]WARNING: Virtual function.[/b][br]
1112
## Return the address of the wallet as a [String].

addons/@mlabs-haskell/paima-middleware/paima_middleware.gd

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
11
extends RefCounted
22
class_name PaimaMiddleware
33

4-
## Wrapper for core of Paima middleware.
4+
## Wrapper for the core of Paima middleware.
55
##
6-
## Provides API for logging-in and querying `RoundExecutor`.
7-
## All calls wrap JS object with Paima middleware. Handling of JS Promises is done
8-
## via signals and `await`s.
9-
6+
## Provides an API for logging-in and querying the `RoundExecutor`.[br][br]
7+
##
8+
## This API deals mostly with objects from the Paima middleware, and hence uses
9+
## [JavaScriptObject]s for many different things. Most importantly, the API is
10+
## initialized with the [code]paima_endpoints[/code] object.[br][br]
11+
##
12+
## Most methods exposed in this API are asynchronous. As such, they can be [code]await[/code]ed
13+
## or a callback can be bound to their respective signals.[br][br]
14+
##
15+
## [b]How to obtain [code]paima_endpoints[/code][/b][br][br]
16+
##
17+
## This object is provided by the Paima middleware ([code]paimaMiddleware.js[/code]), and in Javascript
18+
## it can be easily brought into scope with an import statement:[br][br]
19+
##
20+
## [code]import endpoints from './paima/paimaMiddleware.js'[/code][br][br]
21+
##
22+
## Unfortunately, in GDScript it is not as easy. Import statements are not available,
23+
## so only global objects may be accessed, such as [code]window[/code].[br][br]
24+
##
25+
## Hence, the main way of obtaining [code]paima_endpoints[/code] is by attaching it
26+
## to the global [code]window[/code] object when the web page is loaded. In order
27+
## to do this, the web export template for the project must be modified accordingly:[br][br]
28+
##
29+
## [code][preset.0.options][/code][br]
30+
## [code]...[/code][br]
31+
## [code]html/head_include="<script type=\"module\">[/code][br]
32+
## [code]import endpoints from './paima/paimaMiddleware.js';[/code][br]
33+
## [code]window.paima_endpoints = endpoints;[/code][br][br]
34+
##
35+
## In GDScript, the [code]window[/code] (and thus [code]paima_endpoints[/code])
36+
## can be easily retrieved (check [method _init] docs).[br][br]
37+
##
38+
## [b]How to use the class[/b][br][br]
39+
##
40+
## The core methods available in [code]paima_endpoints[/code] are game-independent
41+
## and may be used directly from [PaimaMiddleware] without calling directly into
42+
## Javascript code (e.g: [method login], [method queryRoundExecutor]).[br][br]
43+
##
44+
## However, for method that are specific to a given game, the [code]paima_endpoints[/code]
45+
## object must be used directly and calls into Javascript are necessary. To retrieve
46+
## the endpoints object, use [method get_endpoints]. Consult this method's documentation
47+
## to learn how to call into Javascript from GDScript.
48+
49+
## Wallet mode
50+
##
51+
## The kind of wallet to be used when logging into Paima.
1052
enum WalletMode {
1153
EVM_INJECTED = 0,
1254
EVM_ETHERS = 1,
@@ -16,53 +58,82 @@ enum WalletMode {
1658
ALGORAND =5
1759
}
1860

61+
## Login information
62+
##
63+
## Used in [method PaimaMiddleware.login]
1964
class LoginInfo:
2065
var _wallet_name: String
21-
var _mode: int
66+
var _mode: WalletMode
2267
var _prefer_batcher: bool
2368

24-
## For Cardano wallets `prefer_batcher` should be set to `true`
25-
## as they can work only through Paima barcher
69+
## Create [PaimaMiddleware.LoginInfo] by providing a [param wallet_name], a [param mode] and a
70+
## [param prefer_batcher_flag].[br][br]
71+
## For Cardano wallets, [param prefer_batcher] should always be set
72+
## to [code]true[/code], as they can only work through the Paima batcher.
2673
func _init(
2774
wallet_name: String,
28-
mode: int,
75+
mode: WalletMode,
2976
prefer_batcher: bool
3077
) -> void:
3178
_wallet_name = wallet_name
32-
_mode = mode
79+
_mode = mode
3380
_prefer_batcher = prefer_batcher
3481

3582
var _endpoints: JavaScriptObject
3683
var _paima_wallet: JavaScriptObject
3784

38-
var console = JavaScriptBridge.get_interface("console")
85+
## The browser console, provided here for convenience
86+
var console := JavaScriptBridge.get_interface("console")
3987

88+
## A [PaimaMiddleware] is initialized by providing an [param endpoints]
89+
## object, which is normally made available under the [code]window[/code] object[br][br]
90+
## [code]var endpoints = JavaScriptBridge.get_interface("window").paima_endpoints[/code][br]
91+
## [code]var middleware = PaimaMiddleware(endpoints)
4092
func _init(endpoints: JavaScriptObject) -> void:
4193
_endpoints = endpoints
4294
assert(_endpoints)
4395

96+
## Get the middleware endpoints, which can be used for executing middleware functions.[br]
97+
## The core functions are already wrapped by [PaimaMiddleware] in a more convenient form,
98+
## use this function for wrapping endpoints [b]not[/b] provided by the class (i.e: endpoints specific to
99+
## your game).[br][br]
100+
##
101+
## For example, if you want to execute a [code]join_match[/code] function:[br][br]
102+
##
103+
## First, you define a GDScript callback you want to run after joining the match[br]
104+
## [code]function join_match_cb(join_result: JavaScriptObject) -> void:[/code][br]
105+
## [code] ...[/code][br][br]
106+
##
107+
## Then, you wrap the GDScript callback in a Javascript callback using [method JavasScriptBridge.create_callback][br]
108+
## [code]var join_match_cb_js := JavaScriptBridge.create_callback(join_match_cb)[/code][br][br]
109+
##
110+
## Finally, you get the the endpoints object and execute [code]join_match[/code]. The return value
111+
## will be a promise to which you can attach your Javascript callback using the Promise API.[br]
112+
## [code]middleware.get_endpoints().join_match(...).then()[/code]
44113
func get_endpoints() -> JavaScriptObject:
45114
return _endpoints
46-
115+
116+
## Get the wallet being used by the Paima middleware
47117
func get_wallet() -> JavaScriptObject:
48118
return _paima_wallet
49119

120+
## Get the wallet address
121+
# TODO: Provide a return type for this function?
50122
func get_wallet_address():
51123
return _paima_wallet.result.walletAddress
52124

53-
## Login
54-
### The func
55-
func login(login_info: LoginInfo, on_login_cb = null) -> bool:
125+
## Log into Paima using [param login_info].
126+
func login(login_info: LoginInfo) -> bool:
56127
var js_login_info = _to_js_login_info(login_info)
57128
console.log("GD:Paima: login_info: ", js_login_info)
58129
_endpoints.userWalletLogin(js_login_info).then(_on_login_js)
59130
var login_successful = await on_paima_login
60131
return login_successful
61132

62-
### Signal for `await`
133+
## Emitted by [method login]
63134
signal on_paima_login(success: bool)
64135

65-
### Callback
136+
# Callback
66137
var _on_login_js = JavaScriptBridge.create_callback(_on_login)
67138
func _on_login(args) -> void:
68139
var wallet = args[0]
@@ -75,6 +146,8 @@ func _on_login(args) -> void:
75146
console.log("Paima wallet login result:", wallet)
76147
on_paima_login.emit(false)
77148

149+
## Returns [code]true[/code] if a wallet was successfully set for the Paima
150+
## middleware.
78151
func wallet_is_set() -> bool:
79152
return _paima_wallet && _paima_wallet.success
80153

@@ -87,8 +160,8 @@ func _to_js_login_info(login_info: LoginInfo) -> JavaScriptObject:
87160
info.preference = pref
88161
return info
89162

90-
## Query round executor
91-
### The func
163+
## Query the state of the round executor by providing a [parama lobby_id] and
164+
## a [param round_number].
92165
func query_round_executor(lobby_id: String, round_number: int) -> RoundExecutor:
93166
_endpoints.getRoundExecutor(lobby_id, round_number).then(_on_executor_query_response_js)
94167
var re = await on_executor_response
@@ -100,12 +173,12 @@ func query_round_executor(lobby_id: String, round_number: int) -> RoundExecutor:
100173
executor = null
101174
return executor
102175

103-
### JS callback
104-
var _on_executor_query_response_js = JavaScriptBridge.create_callback(on_executor_query_response)
105-
func on_executor_query_response(args) -> void:
176+
# JS callback
177+
var _on_executor_query_response_js = JavaScriptBridge.create_callback(_on_executor_query_response)
178+
func _on_executor_query_response(args) -> void:
106179
on_executor_response.emit(args[0])
107180

108-
### Signal for `await`
181+
## Emitted by [method query_round_executor]
109182
signal on_executor_response(re_result: JavaScriptObject)
110183

111184
func _new_js_obj() -> JavaScriptObject:

paima-demo/godot-cip-30-frontend/game_middleware.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extends RefCounted
66
## Internal state is then used in the `_process(...)` of the parent node to adjust UI.
77
class_name GameMiddleware
88

9-
var console = JavaScriptBridge.get_interface("console")
9+
var console: JavaScriptObject = JavaScriptBridge.get_interface("console")
1010
var _player_stats: JavaScriptObject
1111
var _middleware: PaimaMiddleware
1212
var _endpoints: JavaScriptObject
@@ -47,7 +47,7 @@ func update_player_stats():
4747
else:
4848
print("GD:Paima: wallet login was not successful, check wallet by `show wallet` button")
4949

50-
### Callback
50+
# Callback
5151
var _on_stats_received_js = JavaScriptBridge.create_callback(_on_stats_received)
5252
func _on_stats_received(args):
5353
_player_stats = args[0]

paima-demo/godot-cip-30-frontend/main.gd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ var buttons_grid
99
var ui_grid
1010
var walet_picker: HBoxContainer
1111

12-
13-
1412
# Called when the node enters the scene tree for the first time.
1513
func _ready():
1614
print("GD: starting cardano-godot Paima demo")

0 commit comments

Comments
 (0)