Skip to content

feat: htlc #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion examples/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ <h1 class="text-3xl font-bold text-center text-blue-600 mb-6">Mintlayer Wallet D
<button onclick="toggleSection('delegationStake')" class="bg-gray-200 text-gray-700 py-2 px-4 rounded-md hover:bg-gray-300 transition">Delegation Stake</button>
<button onclick="toggleSection('delegationWithdraw')" class="bg-gray-200 text-gray-700 py-2 px-4 rounded-md hover:bg-gray-300 transition">Delegation Withdraw</button>
<button onclick="toggleSection('signChallenge')" class="bg-gray-200 text-gray-700 py-2 px-4 rounded-md hover:bg-gray-300 transition">Sign Challenge</button>
<button onclick="toggleSection('requestSecretHash')" class="bg-gray-200 text-gray-700 py-2 px-4 rounded-md hover:bg-gray-300 transition">Request Secret Hash</button>
<button onclick="toggleSection('createHtlc')" class="bg-gray-200 text-gray-700 py-2 px-4 rounded-md hover:bg-gray-300 transition">Create Htlc</button>
<button onclick="toggleSection('refundHtlc')" class="bg-gray-200 text-gray-700 py-2 px-4 rounded-md hover:bg-gray-300 transition">Refund Htlc</button>
</div>

<!-- Transfer Section -->
Expand Down Expand Up @@ -328,6 +331,42 @@ <h3 class="text-lg font-semibold text-gray-700 mb-4">Sign Challenge</h3>
<button onclick="signChallenge()" class="bg-green-500 text-white py-2 px-4 rounded-md shadow hover:bg-green-600 transition w-full">Sign Challenge</button>
</div>
</div>

<!-- RequestSecretHash Section -->
<div id="requestSecretHash" class="bg-white p-6 rounded-lg shadow-md hidden">
<h3 class="text-lg font-semibold text-gray-700 mb-4">Request Secret Hash</h3>
<div class="space-y-4">
<button onclick="requestSecretHash()" class="bg-green-500 text-white py-2 px-4 rounded-md shadow hover:bg-green-600 transition w-full">Request Secret Hash</button>
</div>
</div>

<!-- Create HTLC Section -->
<div id="createHtlc" class="bg-white p-6 rounded-lg shadow-md hidden">
<h3 class="text-lg font-semibold text-gray-700 mb-4">Create HTLC</h3>
<div class="space-y-4">
<input type="text" id="create_htlc_amount" placeholder="Amount" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" />
<input type="text" id="create_htlc_token_id" placeholder="Token ID (leave empty for ML Coin)" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" />
<input type="text" id="create_htlc_spend_address" placeholder="Spend Address" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" />
<input type="text" id="create_htlc_refund_address" placeholder="Refund Address" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" />
<input type="text" id="create_htlc_secret_hash_hex" placeholder="Secret hash hex" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" />
<select name="create_htlc_lock_type" id="create_htlc_lock_type" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none">
<option value="">Lock Type</option>
<option value="ForBlockCount">ForBlockCount</option>
<option value="UntilTime">UntilTime</option>
</select>
<input type="text" id="create_htlc_lock_content" placeholder="Lock content" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" />
<button onclick="createHTLC()" class="bg-green-500 text-white py-2 px-4 rounded-md shadow hover:bg-green-600 transition w-full">Create HTLC</button>
</div>
</div>

<!-- Refund HTLC Section -->
<div id="refundHtlc" class="bg-white p-6 rounded-lg shadow-md hidden">
<h3 class="text-lg font-semibold text-gray-700 mb-4">Refund HTLC</h3>
<div class="space-y-4">
<input type="text" id="refund_htlc_transaction_id" placeholder="Transaction ID with HTLC" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" />
<button onclick="refundHTLC()" class="bg-green-500 text-white py-2 px-4 rounded-md shadow hover:bg-green-600 transition w-full">Refund HTLC</button>
</div>
</div>
</div>
</div>

Expand All @@ -351,7 +390,7 @@ <h3 class="text-xl font-semibold text-gray-700 mb-4">Output</h3>
'lockTokenSupply', 'changeTokenAuthority', 'changeTokenMetadata', 'createOrder',
'fillOrder', 'concludeOrder', 'bridgeRequest', 'broadcastTx', 'freezeToken',
'unfreezeToken', 'burn', 'dataDeposit', 'createDelegation', 'delegationStake',
'delegationWithdraw', 'signChallenge'
'delegationWithdraw', 'signChallenge', 'createHtlc', 'refundHtlc', 'requestSecretHash'
];
sections.forEach(id => {
const el = document.getElementById(id);
Expand Down Expand Up @@ -921,6 +960,60 @@ <h3 class="text-xl font-semibold text-gray-700 mb-4">Output</h3>
}
}

async function createHTLC () {
const amount = document.getElementById('create_htlc_amount').value;
const spend_address = document.getElementById('create_htlc_spend_address').value;
const refund_address = document.getElementById('create_htlc_refund_address').value;
const secret_hash_hex = document.getElementById('create_htlc_secret_hash_hex').value;
const lock_type = document.getElementById('create_htlc_lock_type').value;
const lock_content = document.getElementById('create_htlc_lock_content').value;
const token_id = document.getElementById('create_htlc_token_id').value;

try {
const result = await window.mintlayer.createHtlc({
amount: amount,
spend_address: spend_address,
refund_address: refund_address,
refund_timelock: {
type: lock_type,
content: lock_content
},
token_id: token_id || null, // null for native token
secret_hash: {
hex: secret_hash_hex
},
});

displayOutput(`Transaction: ${JSON.stringify(result, null, 2)}`);
} catch (error) {
displayOutput(`Error: ${error.message}`);
}
}

async function refundHTLC () {
const transaction_id = document.getElementById('refund_htlc_transaction_id').value;

try {
const { signature } = await window.mintlayer.refundHtlc({
transaction_id,
});

displayOutput(`Signature: ${JSON.stringify(signature, null, 2)}`);
} catch (error) {
displayOutput(`Error: ${error.message}`);
}
}

async function requestSecretHash () {
try {
const response = await window.mintlayer.requestSecretHash();

displayOutput(`Secret Hash: ${JSON.stringify(response, null, 2)}`);
} catch (error) {
displayOutput(`Error: ${error.message}`);
}
}

if (window.mintlayer) displayOutput('Mintlayer Connect SDK detected!');
else displayOutput('Mintlayer Connect SDK not found.');
</script>
Expand Down
Loading
Loading