This example shows how to work with Ethereum/NEAR wallets and smart-contracts via new Core Login API.
Here is the initial code for this example:
The Core Login API is developed as a replacement of
Core.contract() functions. It provides two functions to manage Login Sessions:
Core.login()- creates login sessions for a specified authorization method, during which wallet and contract interaction will take place. It shows a pop-up window for a user where he is able to select a suitable wallet.
Core.sessions()- returns existing sessions that are not expired, to reuse previous authorization and to avoid showing a login pop-up again.
Core.login() accepts one or more login requests as an input parameter.
LoginRequest is an object with the following properties:
string) - authorization method that affects the list of wallets available for user selection. Depending on the specified authorization method, the functions will return different interfaces for interacting with wallets and contracts (
session.contract()). Possible values:
number, default: 7 days) - amount of time after which the session will become invalid in milliseconds.
string) - a name of the set of operations and the operation profile that the user intends to work in. It can be missing if the dapplet works with only one role. The role is displayed in the login pop-up and included in signing messages when the secure mode is enabled. It can be any string. For example:
string) - a link to dapplet documentation which is displayed at the message signing step when the secure mode is enabled.
Overlay) - an overlay where the login popup window will be displayed. The overlay will be blurry and blocked for user interaction while login process is going.
disabled) - a secure login mode which requires the user to sign a message to confirm wallet ownership. The valid values:
any) - a filter that defines which dapplets the login confirmation can be reused from.
Core.login() is the asynchronous function which returns
LoginSession object through which you can call a wallet, a contract, save session info or log out a user. It includes the following asynchronous functions:
wallet()- returns an object for wallet communication.
contract(address, cfg)- returns an object for contract communication.
isValid()- checks the validity of the login session according to the set timeout.
getItem(key)- gets data from the session storage.
setItem(key, value)- saves a value to the session storage. The value must be of any serializable type.
removeItem(key)- removes a value from the session storage by key.
clear()- clears all session storage.
Get all existing login sessions and log out from every of them at the beginning of
activate() function in the dapplet . In your projects you can reuse sessions using this method.
overlay.send('') method will open the overlay. An overlay must be opened before logging in if you want to show a pop-up over a blurry overlay.
If there is no valid session you should create a new one using
Core.login() function with the
ethereum/goerli authentication method.
The overlay object can be passed as
target to open a pop-up over the overlay.
To interact with the wallet within the established session call the
session.wallet() method which will return an object similar to the interface proposed in EIP-1193.
In this example we call the contract developed for Dapplets x Ethereum tutorial. You can find the source code of the contract there. The contract stores a user's tweets and returns them.
To interact with contracts you need to create wrapper object with
session.contract() method. It is similar to the legacy
The same like in the LP 2.
As in the LP 3 we create a login session but using another authentication method:
In this case the
session.wallet() returns the object, wrapping NEAR wallet communication. It uses the
near-api-js library under the hood and returns
Wallet object as described in their documentation.
The similar interface is returned here by
session.contract(). But the contract methods should be called a little differently.
Here is the result code of the example:
Run the dapplet: