Skip to content

Commit 06fb78b

Browse files
committed
feat: extract htlc secret
1 parent 5b8b263 commit 06fb78b

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

examples/demo/index.html

Lines changed: 31 additions & 3 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('extractHtlcSecret')" class="bg-gray-200 text-gray-700 py-2 px-4 rounded-md hover:bg-gray-300 transition">Extract Htlc secret</button>
7677
</div>
7778

7879
<!-- Transfer Section -->
@@ -367,6 +368,16 @@ <h3 class="text-lg font-semibold text-gray-700 mb-4">Refund HTLC</h3>
367368
<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>
368369
</div>
369370
</div>
371+
372+
<!-- Extract HTLC Secret Section -->
373+
<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>
375+
<div class="space-y-4">
376+
<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" />
377+
<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" />
378+
<button onclick="extractHTLCSecret()" class="bg-green-500 text-white py-2 px-4 rounded-md shadow hover:bg-green-600 transition w-full">Extract HTLC Secret</button>
379+
</div>
380+
</div>
370381
</div>
371382
</div>
372383

@@ -390,7 +401,7 @@ <h3 class="text-xl font-semibold text-gray-700 mb-4">Output</h3>
390401
'lockTokenSupply', 'changeTokenAuthority', 'changeTokenMetadata', 'createOrder',
391402
'fillOrder', 'concludeOrder', 'bridgeRequest', 'broadcastTx', 'freezeToken',
392403
'unfreezeToken', 'burn', 'dataDeposit', 'createDelegation', 'delegationStake',
393-
'delegationWithdraw', 'signChallenge', 'createHtlc', 'refundHtlc', 'requestSecretHash'
404+
'delegationWithdraw', 'signChallenge', 'createHtlc', 'refundHtlc', 'requestSecretHash', 'extractHtlcSecret'
394405
];
395406
sections.forEach(id => {
396407
const el = document.getElementById(id);
@@ -994,11 +1005,28 @@ <h3 class="text-xl font-semibold text-gray-700 mb-4">Output</h3>
9941005
const transaction_id = document.getElementById('refund_htlc_transaction_id').value;
9951006

9961007
try {
997-
const { signature } = await window.mintlayer.refundHtlc({
1008+
const result = await window.mintlayer.refundHtlc({
9981009
transaction_id,
9991010
});
10001011

1001-
displayOutput(`Signature: ${JSON.stringify(signature, null, 2)}`);
1012+
displayOutput(`Signature: ${JSON.stringify(result, null, 2)}`);
1013+
} catch (error) {
1014+
displayOutput(`Error: ${error.message}`);
1015+
}
1016+
}
1017+
1018+
async function extractHTLCSecret () {
1019+
const transaction_id = document.getElementById('extract_htlc_transaction_id').value;
1020+
const transaction_hex = document.getElementById('extract_htlc_transaction_hex').value;
1021+
1022+
try {
1023+
const result = await window.mintlayer.extractHtlcSecret({
1024+
transaction_id,
1025+
transaction_hex,
1026+
format: 'hex'
1027+
});
1028+
1029+
displayOutput(`Secret HEX: ${JSON.stringify(result, null, 2)}`);
10021030
} catch (error) {
10031031
displayOutput(`Error: ${error.message}`);
10041032
}

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ function hexToUint8Array(hex: any) {
8181
return array;
8282
}
8383

84+
function uint8ArrayToHex(bytes: Uint8Array): string {
85+
return Array.from(bytes)
86+
.map(b => b.toString(16).padStart(2, '0'))
87+
.join('');
88+
}
89+
8490
export function atomsToDecimal(atoms: string | number, decimals: number): string {
8591
const atomsStr = atoms.toString();
8692
const atomsLength = atomsStr.length;
@@ -2073,7 +2079,13 @@ class Client {
20732079
const data = await response.json();
20742080

20752081
// @ts-ignore
2076-
const forceSpendUtxo: UtxoEntry[] = arg?.opts?.forceSpendUtxo || [];
2082+
const forceSpendUtxo: UtxoEntry[] = arg?.opts?.forceSpendUtxo ? arg?.opts?.forceSpendUtxo.map((item: UtxoEntry) => ({
2083+
input: {
2084+
...item.outpoint,
2085+
input_type: 'UTXO',
2086+
},
2087+
utxo: item.utxo,
2088+
})) : [];
20772089

20782090
const utxos: UtxoEntry[] = data.utxos;
20792091

@@ -2478,17 +2490,18 @@ class Client {
24782490
const outputsArray = outputsArrayItems.filter((x): x is NonNullable<typeof x> => x !== undefined);
24792491

24802492
const inputAddresses: string[] = (transactionJSONrepresentation.inputs as UtxoInput[])
2481-
.filter(({ input, utxo }) => input.input_type === 'UTXO')
2493+
// @ts-ignore
2494+
.filter(({ input, utxo }) => input.input_type === 'UTXO' || utxo.htlc)
24822495
.map((input) => {
24832496
if (input.utxo.destination){
24842497
return input.utxo.destination;
24852498
}
24862499
// @ts-ignore
24872500
if (input.utxo.htlc) {
24882501
// @ts-ignore
2489-
return input.utxo.htlc.refund_key; // TODO: need to handle spend too
2502+
return [input.utxo.htlc.spend_key, input.utxo.htlc.refund_key]; // TODO: need to handle spend too
24902503
}
2491-
});
2504+
}).flat();
24922505

24932506
// @ts-ignore
24942507
if (transactionJSONrepresentation.inputs[0].input.account_type === 'DelegationBalance') {
@@ -3060,6 +3073,7 @@ class Client {
30603073
const {
30613074
transaction_id,
30623075
transaction_hex,
3076+
format = 'Uint8Array', // 'bytes' or 'hex'
30633077
} = arg;
30643078

30653079
const res = await fetch(`${this.getApiServer()}/transaction/${transaction_id}`);
@@ -3092,6 +3106,10 @@ class Client {
30923106
htlc_output_index
30933107
);
30943108

3109+
if(format === 'hex') {
3110+
return uint8ArrayToHex(secret);
3111+
}
3112+
30953113
return secret;
30963114
}
30973115

0 commit comments

Comments
 (0)