Interacting with Harmony Chrome Extension Wallet
This section covers how to interact with Harmony Chrome Extension Wallet
We are assuming here that you are following the same setup as mentioned in the previous section.
Go to you src/init.js and add the following code.
1
import userWallet from './userWallet';
2
3
let but = document.getElementById("inputtButton");
4
5
but.addEventListener("click",initWallet);
6
7
async function initWallet(){
8
const wallet = new userWallet();
9
await wallet.signin();
10
}
Copied!
Here we have simply attached a "click" listener before initiating code to interact with the Wallet.

Detecting Harmony Chrome Extension Wallet and interacting with it.

When user loads a page, Harmony Chrome Extension Wallet automatically inject a onewallet object into the window object of your browser. We use this to interact with the same.
In our example, following code is implemented in src/userWallet.js
Object to interact with Harmony Chrome Extension Wallet can be accessed sing the following.
1
const isOneWallet = window.onewallet && window.onewallet.isOneWallet;
2
const onewallet = window.onewallet;
Copied!
The onewallet object needs a defaultsigner for signing the transactions, and lastly signTransaction object . We do the following using the following code snippets.
1
const getAccount = await this.onewallet.getAccount();
2
await this.connectToOneWallet(this.onewallet)
Copied!
1
async signin(){
2
const getAccount = await this.onewallet.getAccount();
3
console.log("slkdfjds")
4
console.log(getAccount)
5
6
this.address = getAccount.address;
7
this.isAuthorized = true;
8
}
Copied!
The final code we have in our example is as follows. src/userWallet.js
1
import hmy from './hmy';
2
3
class userStore {
4
5
constructor (stores) {
6
this.isOneWallet = window.onewallet && window.onewallet.isOneWallet;
7
this.onewallet = window.onewallet;
8
}
9
10
async signin(){
11
const getAccount = await this.onewallet.getAccount();
12
console.log("slkdfjds")
13
console.log(getAccount)
14
15
this.address = getAccount.address;
16
this.isAuthorized = true;
17
}
18
19
}
20
21
export default userStore
Copied!

Congratulations !!

You can now successfully interact with the Harmony Chrome Extension Wallet.
For more examples checkout this link.
Last modified 2mo ago