Substrate dApp Integration
CLV Extension Wallet uses method 'injectExtension' of @polkadot/extension-inject to inject an object into dApp's web page, and dApp can invoke the wallet by calling web3Enable(). The sample code is as follows:
1
import { web3Accounts, web3Enable } from '@polkadot/extension-dapp';
2
โ€‹
3
// find clover extension wallet injector
4
const findCloverInjected = async () => {
5
const injected = await web3Enable('clv');
6
if (!injected.length) {
7
return {
8
message: "Not found wallet",
9
status: 'error'
10
};
11
}
12
โ€‹
13
const cloverInjector = injected.find(injected, (w) => isCloverWallet(w))
14
return cloverInjector
15
}
16
โ€‹
17
// sign message
18
const polkadotSignMessage = async () => {
19
const polkadotAddress = await web3Accounts({ ss58Format: 42 });
20
const wrapped = u8aWrapBytes(polkadotAddress.toLowerCase());
21
const ret = await currentInjected.signer.signRaw({
22
data: u8aToHex(wrapped),
23
address: polkadotAddress,
24
type: "bytes",
25
});
26
}
27
28
// send transaction
29
polkadotSignTransaction = async () => {
30
const wsProvider = new WsProvider('wss://rpc.polkadot.io');
31
const api = await ApiPromise.create({provider: wsProvider});
32
const polkadotAddress = await web3Accounts({ ss58Format: 42 });
33
const currentClvAccount = polkadotAddress
34
const injected = await web3FromAddress(currentClvAccount)
35
api.setSigner(injected.signer)
36
const unsub = await api.tx.balances
37
.transfer(currentClvAccount, 0)
38
.signAndSend(currentClvAccount, (result) => {
39
โ€‹
40
if (result.status.isInBlock) {
41
// in block
42
} else if (result.status.isFinalized) {
43
unsub();
44
}
45
})
46
}
Copied!
Copy link