Files
ark/pkg/client-sdk/example/covenantless/wasm/index.html
Pietralberto Mazza 26bcbc8163 [SDK] Fix rest client and wasm (#341)
* Fixes

Co-authored-by: João Bordalo <bordalix@users.noreply.github.com>

* Fixes to rest client

* Fixes to wasm

---------

Co-authored-by: João Bordalo <bordalix@users.noreply.github.com>
2024-10-01 19:00:25 +02:00

181 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 = "bitcoin";
const walletType = "singlekey"
const clientType = "rest"
const privateKey = document.getElementById("prvkey").value;
const password = document.getElementById("i_password").value;
if (!password) {
logMessage("Init error: password is required");
return;
}
const aspUrl = document.getElementById("aspUrl").value;
if (!aspUrl) {
logMessage("Init error: asp url is required");
return;
}
await init(walletType, clientType, aspUrl, privateKey, password, chain);
logMessage("wallet initialized and connected to ASP");
await config();
} catch (err) {
logMessage("Init error: " + err.message);
}
}
async function receiveAddresses() {
try {
const addresses = await receive();
logMessage("Offchain address: " + addresses.offchainAddr);
logMessage("Boarding address: " + addresses.boardingAddr);
} catch (err) {
logMessage("Receive error: " + err.message);
}
}
async function getBalance() {
const bal = await balance(false);
logMessage("Offchain balance: " + bal.offchainBalance)
logMessage("Onchain balance: ")
logMessage(" Spendable: " + bal.onchainBalance.spendable)
logMessage(" Locked: " + bal.onchainBalance.locked)
}
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 claimVtxos() {
const password = document.getElementById("c_password").value;
if (!password) {
logMessage("Claim error: password is required");
return;
}
try {
await unlock(password);
const txID = await claim();
logMessage("Claimed money with tx ID: " + txID);
} catch (err) {
logMessage("Claim error: " + err.message);
} finally {
await lock(password);
}
}
async function history() {
try {
const history = await getTransactionHistory();
logMessage("Tx history: " + history);
} catch (err) {
logMessage("Tx history error: " + err.message);
}
}
async function config() {
try {
const aspUrl = await getAspUrl();
logMessage("ASP URL: " + aspUrl);
const aspPubKeyHex = await getAspPubKeyHex();
logMessage("ASP PubKey: " + aspPubKeyHex);
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);
}
}
</script>
</head>
<body>
<h1>Ark SDK WASM Example</h1>
<div>
<h2>Wallet</h2>
<div>
<button onclick="initWallet()">Init</button>
<input type="text" id="aspUrl" placeholder="http://localhost:7070">
<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="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="claimVtxos()">Claim</button>
<input type="password" id="c_password" placeholder="password">
</div>
<div>
<button onclick="history()">History</button>
</div>
<div>
<button onclick="config()">Config</button>
</div>
</div>
<textarea id="logArea" rows="20" cols="80" readonly></textarea>
</body>
</html>