I'm building a simple website with pure HTML and JS (no React, Angular, etc.). I use Nami to connect to a Cardano wallet and use the Nami's cardano.getBalance()
method to get the balance of the wallet.
This output (as most of the outputs of Nami) is CBOR encoded. I used https://cbor.me/ to decode it manually but I need to be able to do this in code to be able to work with the values.
Nami's README says to use the cardano-serialization-lib to do this. So I tried to use it but there is no documentation on how to actually use it.
Here's what I've done so far:
<html>
<head>
<!-- Cardano Serialization Lib -->
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@emurgo/[email protected]/cardano_serialization_lib_bg.min.js"
></script>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@emurgo/[email protected]/cardano_serialization_lib.asm.min.js"
></script>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@emurgo/[email protected]/cardano_serialization_lib.min.js"
></script>
</head>
<body>
<script>
function f() {
window.cardano.enable().then((a) => {
console.log(a);
window.cardano.getBalance().then((b) => console.log(b));
});
}
setTimeout(f, 1000); // wait for cardano object to be injected
</script>
</body>
</html>
I included the cardano-serialization-lib-browser using jsdelivr.com. Now the goal is to use this library to deserialize the output of cardano.getBalance()
.
My problems are:
- I don't know how to call the library in my JS code.
- I don't know what functions to use to deserialize CBOR as there is no documentation.
Any help would be highly appreciated.