Skip to content

Commit 13b2c85

Browse files
committed
fix: add spend htlc and support new transaction payload
1 parent 1497068 commit 13b2c85

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

examples/demo/index.html

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ <h1 class="text-3xl font-bold text-center text-blue-600 mb-6">Mintlayer Wallet D
7373
<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>
7474
<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>
7575
<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>
76+
<button onclick="toggleSection('spendHtlc')" class="bg-gray-200 text-gray-700 py-2 px-4 rounded-md hover:bg-gray-300 transition">Spend Htlc</button>
7677
<button onclick="toggleSection('extractHtlcSecret')" class="bg-gray-200 text-gray-700 py-2 px-4 rounded-md hover:bg-gray-300 transition">Extract Htlc secret</button>
7778
</div>
7879

@@ -369,9 +370,18 @@ <h3 class="text-lg font-semibold text-gray-700 mb-4">Refund HTLC</h3>
369370
</div>
370371
</div>
371372

373+
<!-- Spend HTLC Section -->
374+
<div id="spendHtlc" class="bg-white p-6 rounded-lg shadow-md hidden">
375+
<h3 class="text-lg font-semibold text-gray-700 mb-4">Spend HTLC</h3>
376+
<div class="space-y-4">
377+
<input type="text" id="spend_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" />
378+
<button onclick="spendHTLC()" class="bg-green-500 text-white py-2 px-4 rounded-md shadow hover:bg-green-600 transition w-full">Spend HTLC</button>
379+
</div>
380+
</div>
381+
372382
<!-- Extract HTLC Secret Section -->
373383
<div id="extractHtlcSecret" class="bg-white p-6 rounded-lg shadow-md hidden">
374-
<h3 class="text-lg font-semibold text-gray-700 mb-4">Refund HTLC</h3>
384+
<h3 class="text-lg font-semibold text-gray-700 mb-4">Extract Secret from HTLC</h3>
375385
<div class="space-y-4">
376386
<input type="text" id="extract_htlc_transaction_id" placeholder="Transaction ID with HTLC spend" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" />
377387
<input type="text" id="extract_htlc_transaction_hex" placeholder="Transaction HEX" class="w-full border border-gray-300 rounded-md p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" />
@@ -401,7 +411,7 @@ <h3 class="text-xl font-semibold text-gray-700 mb-4">Output</h3>
401411
'lockTokenSupply', 'changeTokenAuthority', 'changeTokenMetadata', 'createOrder',
402412
'fillOrder', 'concludeOrder', 'bridgeRequest', 'broadcastTx', 'freezeToken',
403413
'unfreezeToken', 'burn', 'dataDeposit', 'createDelegation', 'delegationStake',
404-
'delegationWithdraw', 'signChallenge', 'createHtlc', 'refundHtlc', 'requestSecretHash', 'extractHtlcSecret'
414+
'delegationWithdraw', 'signChallenge', 'createHtlc', 'refundHtlc', 'spendHtlc', 'requestSecretHash', 'extractHtlcSecret'
405415
];
406416
sections.forEach(id => {
407417
const el = document.getElementById(id);
@@ -1015,6 +1025,20 @@ <h3 class="text-xl font-semibold text-gray-700 mb-4">Output</h3>
10151025
}
10161026
}
10171027

1028+
async function spendHTLC () {
1029+
const transaction_id = document.getElementById('spend_htlc_transaction_id').value;
1030+
1031+
try {
1032+
const result = await window.mintlayer.spendHtlc({
1033+
transaction_id,
1034+
});
1035+
1036+
displayOutput(`Signature: ${JSON.stringify(result, null, 2)}`);
1037+
} catch (error) {
1038+
displayOutput(`Error: ${error.message}`);
1039+
}
1040+
}
1041+
10181042
async function extractHTLCSecret () {
10191043
const transaction_id = document.getElementById('extract_htlc_transaction_id').value;
10201044
const transaction_hex = document.getElementById('extract_htlc_transaction_hex').value;

packages/sdk/src/mintlayer-connect-sdk.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,38 @@ class Client {
30693069
return this.signTransaction(tx);
30703070
}
30713071

3072+
async spendHtlc(params: any): Promise<any> {
3073+
this.ensureInitialized();
3074+
3075+
const { transaction_id, utxo } = params;
3076+
3077+
let useHtlcUtxo: any[] = [];
3078+
3079+
if (transaction_id) {
3080+
const response = await fetch(`${this.getApiServer()}/transaction/${transaction_id}`);
3081+
if (!response.ok) {
3082+
throw new Error('Failed to fetch transaction');
3083+
}
3084+
const transaction: TransactionJSONRepresentation = await response.json();
3085+
// @ts-ignore
3086+
const { created } = this.previewUtxoChange({ JSONRepresentation: { ...transaction } } as Transaction);
3087+
// @ts-ignore
3088+
useHtlcUtxo = created.filter(({utxo}) => utxo.type === 'Htlc') || null;
3089+
}
3090+
3091+
const tx = await this.buildTransaction({
3092+
type: 'Transfer',
3093+
params: {
3094+
to: useHtlcUtxo[0].utxo.htlc.spend_key,
3095+
amount: useHtlcUtxo[0].utxo.value.amount.decimal,
3096+
},
3097+
opts: {
3098+
forceSpendUtxo: useHtlcUtxo,
3099+
}
3100+
});
3101+
return this.signTransaction(tx);
3102+
}
3103+
30723104
async extractHtlcSecret(arg: any): Promise<any> {
30733105
const {
30743106
transaction_id,
@@ -3295,14 +3327,16 @@ class Client {
32953327
return this.request({ method: 'getXPub' });
32963328
}
32973329

3298-
async broadcastTx(tx: string): Promise<any> {
3330+
async broadcastTx(tx: string | { hex: string, json: TransactionJSONRepresentation }): Promise<any> {
32993331
this.ensureInitialized();
33003332
const response = await fetch(`${this.getApiServer()}/transaction`, {
33013333
method: 'POST',
3302-
headers: {
3334+
headers: typeof tx === 'string' ? {
33033335
'Content-Type': 'text/plain',
3336+
} : {
3337+
'Content-Type': 'application/json',
33043338
},
3305-
body: tx,
3339+
body: typeof tx === 'string' ? tx : JSON.stringify({ transaction: tx.hex, json: tx.json }),
33063340
});
33073341

33083342
if (!response.ok) {

0 commit comments

Comments
 (0)