mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 04:04:21 +01:00
177 lines
6.4 KiB
HTML
177 lines
6.4 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;
|
|
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("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 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);
|
|
|
|
const minRelayFee = await getMinRelayFee();
|
|
logMessage("Min Relay Fee: " + minRelayFee);
|
|
} 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="aspUrl" 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>
|