Files
ark/pkg/client-sdk/example/covenant/wasm/index.html
Pietralberto Mazza 7f937e8418 Vars and fields renaming (#387)
* Rename asp > server

* Rename pool > round

* Consolidate naming for pubkey/prvkey vars and types

* Fix

* Fix

* Fix wasm

* Rename congestionTree > vtxoTree

* Fix wasm

* Rename payment > request

* Rename congestionTree > vtxoTree after syncing with master

* Fix Send API in SDK

* Fix wasm

* Fix wasm

* Fixes

* Fixes after review

* Fix

* Fix naming

* Fix

* Fix e2e tests
2024-11-26 15:57:16 +01:00

176 lines
6.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ark SDK WASM Example</title>
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("ark-sdk.wasm"), go.importObject).then((result) => {
go.run(result.instance);
});
function logMessage(message) {
const logArea = document.getElementById("logArea");
logArea.value += message + "\n";
logArea.scrollTop = logArea.scrollHeight;
}
async function initWallet() {
try {
const chain = "liquid";
const walletType = "singlekey";
const clientType = "rest";
const privateKey = document.getElementById("prvkey").value;
const password = document.getElementById("i_password").value;
const explorerUrl = "";
if (!password) {
logMessage("Init error: password is required");
return;
}
const serverUrl = document.getElementById("serverUrl").value;
if (!serverUrl) {
logMessage("Init error: server url is required");
return;
}
await init(walletType, clientType, serverUrl, privateKey, password, chain, explorerUrl);
logMessage("wallet initialized and connected to server");
await config();
} catch (err) {
logMessage("Init error: " + err.message);
}
}
async function receiveAddresses() {
try {
const addresses = await receive();
logMessage("Offchain address: " + addresses.offchainAddr);
logMessage("Onchain address: " + addresses.onchainAddr);
logMessage("If in regtest faucet onchain address: " + addresses.onchainAddr);
} catch (err) {
logMessage("Receive error: " + err.message);
}
}
async function getBalance() {
const bal = await balance(false);
logMessage("Onchain balance: " + bal.onchain_balance)
logMessage("Offchain balance: " + bal.offchain_balance)
}
async function send() {
const password = document.getElementById("s_password").value;
if (!password) {
logMessage("Send error: password is required");
return;
}
try {
const address = document.getElementById("sendAddress").value;
if (!address) {
logMessage("Send error: Address is required");
return;
}
const amountStr = document.getElementById("amountToSend").value;
if (!amountStr) {
logMessage("Send error: Amount is required");
return;
}
const amount = parseInt(amountStr, 10);
await unlock(password);
const txID = await sendOffChain(false, [{ To: address, Amount: amount }]);
logMessage("Sent money with tx ID: " + txID);
} catch (err) {
logMessage("Send error: " + err.message);
} finally {
await lock(password);
}
}
async function config() {
try {
const serverUrl = await getServerUrl();
logMessage("Server URL: " + serverUrl);
const serverPubkeyHex = await getServerPubkeyHex();
logMessage("Server PubKey: " + serverPubkeyHex);
const walletType = await getWalletType();
logMessage("Wallet Type: " + walletType);
const clientType = await getClientType();
logMessage("Client Type: " + clientType);
const roundLifetime = await getRoundLifetime();
logMessage("Round Lifetime: " + roundLifetime);
const unilateralExitDelay = await getUnilateralExitDelay();
logMessage("Unilateral Exit Delay: " + unilateralExitDelay);
} catch (err) {
logMessage("Config error: " + err.message);
}
}
async function board() {
const amountStr = document.getElementById("amount").value;
const amount = parseInt(amountStr, 10);
const password = document.getElementById("o_password").value;
if (!password) {
logMessage("Onboard error: password is required");
return;
}
try {
console.log("unlocking...");
await unlock(password);
console.log(amount, password);
console.log("onboarding...");
const txID = await onboard(amount);
logMessage("Onboarded with amount: " + amount + " and txID: " + txID + ", if in regtest mine a block");
} catch (err) {
logMessage("Onboard error: " + err.message);
} finally {
await lock(password);
}
}
</script>
</head>
<body>
<h1>Ark SDK WASM Example</h1>
<div>
<h2>Wallet</h2>
<div>
<button onclick="initWallet()">Init</button>
<input type="text" id="serverUrl" placeholder="http://localhost:6060">
<input type="password" id="i_password" placeholder="password">
<input type="text" id="prvkey" placeholder="Optional: privkey (hex)">
</div>
<div>
<button onclick="receiveAddresses()">Receive</button>
</div>
<div>
<button onclick="getBalance()">Balance</button>
</div>
<div>
<button onclick="board()">Onboard</button>
<input type="text" id="amount" placeholder="Amount">
<input type="password" id="o_password" placeholder="password">
</div>
<div>
<button onclick="send()">Send</button>
<input type="text" id="sendAddress" placeholder="Offchain Address">
<input type="text" id="amountToSend" placeholder="Amount">
<input type="password" id="s_password" placeholder="password">
</div>
<div>
<button onclick="config()">Config</button>
</div>
</div>
<textarea id="logArea" rows="20" cols="80" readonly></textarea>
</body>
</html>