Skip to content

Commit 51dff0c

Browse files
authored
feat: htlc (#21)
* release: 1.0.18 * feat: htlc * feat: create htlc * in progress with htlc refund, test snapshots added, other ones fail known * in progress htlc * extract htlc method * request hash * remove old fee * cleanup logs * comment out tests
1 parent a5592f8 commit 51dff0c

File tree

5 files changed

+768
-14
lines changed

5 files changed

+768
-14
lines changed

examples/demo/index.html

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ <h1 class="text-3xl font-bold text-center text-blue-600 mb-6">Mintlayer Wallet D
7070
<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>
7171
<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>
7272
<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>
73+
<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>
74+
<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>
75+
<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>
7376
</div>
7477

7578
<!-- Transfer Section -->
@@ -328,6 +331,42 @@ <h3 class="text-lg font-semibold text-gray-700 mb-4">Sign Challenge</h3>
328331
<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>
329332
</div>
330333
</div>
334+
335+
<!-- RequestSecretHash Section -->
336+
<div id="requestSecretHash" class="bg-white p-6 rounded-lg shadow-md hidden">
337+
<h3 class="text-lg font-semibold text-gray-700 mb-4">Request Secret Hash</h3>
338+
<div class="space-y-4">
339+
<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>
340+
</div>
341+
</div>
342+
343+
<!-- Create HTLC Section -->
344+
<div id="createHtlc" class="bg-white p-6 rounded-lg shadow-md hidden">
345+
<h3 class="text-lg font-semibold text-gray-700 mb-4">Create HTLC</h3>
346+
<div class="space-y-4">
347+
<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" />
348+
<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" />
349+
<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" />
350+
<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" />
351+
<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" />
352+
<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">
353+
<option value="">Lock Type</option>
354+
<option value="ForBlockCount">ForBlockCount</option>
355+
<option value="UntilTime">UntilTime</option>
356+
</select>
357+
<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" />
358+
<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>
359+
</div>
360+
</div>
361+
362+
<!-- Refund HTLC Section -->
363+
<div id="refundHtlc" class="bg-white p-6 rounded-lg shadow-md hidden">
364+
<h3 class="text-lg font-semibold text-gray-700 mb-4">Refund HTLC</h3>
365+
<div class="space-y-4">
366+
<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" />
367+
<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>
368+
</div>
369+
</div>
331370
</div>
332371
</div>
333372

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

963+
async function createHTLC () {
964+
const amount = document.getElementById('create_htlc_amount').value;
965+
const spend_address = document.getElementById('create_htlc_spend_address').value;
966+
const refund_address = document.getElementById('create_htlc_refund_address').value;
967+
const secret_hash_hex = document.getElementById('create_htlc_secret_hash_hex').value;
968+
const lock_type = document.getElementById('create_htlc_lock_type').value;
969+
const lock_content = document.getElementById('create_htlc_lock_content').value;
970+
const token_id = document.getElementById('create_htlc_token_id').value;
971+
972+
try {
973+
const result = await window.mintlayer.createHtlc({
974+
amount: amount,
975+
spend_address: spend_address,
976+
refund_address: refund_address,
977+
refund_timelock: {
978+
type: lock_type,
979+
content: lock_content
980+
},
981+
token_id: token_id || null, // null for native token
982+
secret_hash: {
983+
hex: secret_hash_hex
984+
},
985+
});
986+
987+
displayOutput(`Transaction: ${JSON.stringify(result, null, 2)}`);
988+
} catch (error) {
989+
displayOutput(`Error: ${error.message}`);
990+
}
991+
}
992+
993+
async function refundHTLC () {
994+
const transaction_id = document.getElementById('refund_htlc_transaction_id').value;
995+
996+
try {
997+
const { signature } = await window.mintlayer.refundHtlc({
998+
transaction_id,
999+
});
1000+
1001+
displayOutput(`Signature: ${JSON.stringify(signature, null, 2)}`);
1002+
} catch (error) {
1003+
displayOutput(`Error: ${error.message}`);
1004+
}
1005+
}
1006+
1007+
async function requestSecretHash () {
1008+
try {
1009+
const response = await window.mintlayer.requestSecretHash();
1010+
1011+
displayOutput(`Secret Hash: ${JSON.stringify(response, null, 2)}`);
1012+
} catch (error) {
1013+
displayOutput(`Error: ${error.message}`);
1014+
}
1015+
}
1016+
9241017
if (window.mintlayer) displayOutput('Mintlayer Connect SDK detected!');
9251018
else displayOutput('Mintlayer Connect SDK not found.');
9261019
</script>

packages/sdk/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mintlayer/sdk",
3-
"version": "1.0.17",
3+
"version": "1.0.18",
44
"description": "",
55
"main": "dist/mintlayer-connect-sdk.js",
66
"types": "dist/mintlayer-connect-sdk.d.ts",

0 commit comments

Comments
 (0)