Using the Lamden Wallet Controller (LWC) #
This package should work with all javascript implementations including nodejs and vanilla javascript
What is it? #
The Lamden Wallet Controller (LWC) is a helper package for interacting with the Lamden Wallet API via a webpage. You can interact with the Lamden Wallet API without this package, but the intent here is to replace the process of managing listeners and events with a more modern callback/Promise workflow.
Install #
import WalletController from './js/walletController';
Or:
npm install lamden_wallet_controller
import WalletController from 'lamden_wallet_controller';
Create Connection Request Object #
Creating a connection request is the same as Lamden Wallet API – Create a Connection.
Below is the mandatory information for a connection request but more customization options are available, these are documented in the Lamden Wallet API docs.
const connectionRequest = {
appName: 'My Killer dApp', // Your DAPPS's name
version: '1.0.0', // any version to start, increment later versions to update connection info
logo: 'images/logo.png', // or whatever the location of your logo
contractName: 'con_killer_app', // Will never change
networkType: 'testnet', // other option is 'mainnet'
}
Create an Instance of the LWC #
const lwc = new WalletController(connectionRequest)
Respond to Events from the LWC #
// Create event handlers
const handleWalletInfo = (walletInfo) => console.log(walletInfo)
const handleTxResults = (txInfo) => console.log(txInfo)
//Connect to event emitters
lwc.events.on('newInfo', handleWalletInfo) // Wallet Info Events, including errors
lwc.events.on('txStatus', handleTxResults) // Transaction Results
- See information returned from
newInfo
here - See information returned from
txStatus
here
Check if Wallet is Installed #
Calling walletIsInstalled
will return a promise with the resolver being a boolean value as to the installed status of the wallet.
If the wallet is installed the LWC will automatically send your connection request and the user will see the New Linked Account popup process.
Once the user completes the popup process you will get the wallet Info on the newInfo
event emitter.
lwc.walletIsInstalled()
.then(installed => {
if (installed) // Connection request will automatically be sent.
else // inform user to install the wallet
})
So to recap, the walletIsInstalled
method will accomplish 3 things:
- Reports back the installed status of the wallet.
- If installed is true and a connection request was provided to the constructor then it will automatically create the
lamdenWalletConnect
event and provide your connection request to the users Lamden Wallet - If the user has a Linked Account for your DAPP you will be sent the Wallet Information on the
newInfo
listener; or else the user will compelte the New Linked Account process and you will be proveded the Wallet Information at that point.
Send Connection Request #
Not required if you have already called walletIsInstalled
sendConnection
can be used if you didn’t supply the connectionRequest information to the wallet previously or if you want to submit another connection request.
If the user has a Linked Account for your DAPP you will be sent the Wallet Information on the newInfo
listener; or else the user will compelte the New Linked Account process and you will be proveded the Wallet Information at that point.
lwc.sendConnection(connectionRequest)
Send A Transaction #
Create the transaction object as per the Lamden Wallet API specification.
contractName
is automatically filled in by the LamdenWallet with the value approved by the user in your connection request.
const txInfo = {
networkType: 'mainnet', // other option is 'testnet'
methodName: 'do_something',
kwargs: {
Str: 'awesome', //send a string
Float: {'__fixed__': '1000.000000006'} //send a float
Int: 1000 //send and int
},
stampLimit: 100
};
const handleResults = (txResults) => console.log(txResults)
lwc.sendTransaction(transaction, handleResults) // callback is optional
You always get the transaction results on the txStatus
event emitter weither a callback is defiened or not.