Skip to content

Commit bb96d47

Browse files
committed
feat: implement bitcoin onchain allocator
1 parent c921902 commit bb96d47

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

src/utils/onchain-allocator.ts

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
Address,
88
createPublicClient,
99
createWalletClient,
10+
decodeAbiParameters,
11+
encodeAbiParameters,
1012
fromHex,
1113
getContract,
1214
Hex,
@@ -287,6 +289,8 @@ export const getSignatureFromContract = async (
287289
);
288290
}
289291

292+
const allocatorChainId = chain.metadata.allocatorChainId;
293+
290294
const depository =
291295
chain.vmType === "tron-vm"
292296
? TronWeb.utils.address
@@ -297,7 +301,7 @@ export const getSignatureFromContract = async (
297301
const onchainAllocator = await getOnchainAllocator();
298302
const payloadBuilderAddress =
299303
await onchainAllocator.contract.read.payloadBuilders([
300-
BigInt(chain.metadata.allocatorChainId),
304+
BigInt(allocatorChainId),
301305
depository,
302306
]);
303307
if (payloadBuilderAddress === zeroAddress) {
@@ -311,7 +315,7 @@ export const getSignatureFromContract = async (
311315
case "hyperliquid-vm":
312316
case "tron-vm": {
313317
const hashToSign = await payloadBuilder.contract.read.hashToSign([
314-
BigInt(chain.metadata.allocatorChainId),
318+
BigInt(allocatorChainId),
315319
depository,
316320
encodedData as Hex,
317321
0,
@@ -330,7 +334,7 @@ export const getSignatureFromContract = async (
330334

331335
case "solana-vm": {
332336
const hashToSign = await payloadBuilder.contract.read.hashToSign([
333-
BigInt(chain.metadata.allocatorChainId),
337+
BigInt(allocatorChainId),
334338
depository,
335339
encodedData as Hex,
336340
0,
@@ -347,6 +351,71 @@ export const getSignatureFromContract = async (
347351
}
348352
}
349353

354+
case "bitcoin-vm": {
355+
const transactionData = decodeAbiParameters(
356+
[
357+
{
358+
type: "tuple",
359+
components: [
360+
{
361+
type: "tuple[]",
362+
name: "inputs",
363+
components: [
364+
{ type: "bytes", name: "txid" },
365+
{ type: "bytes", name: "index" },
366+
{ type: "bytes", name: "script" },
367+
{ type: "bytes", name: "value" },
368+
],
369+
},
370+
{
371+
type: "tuple[]",
372+
name: "outputs",
373+
components: [
374+
{ type: "bytes", name: "value" },
375+
{ type: "bytes", name: "script" },
376+
],
377+
},
378+
],
379+
},
380+
],
381+
encodedData as Hex,
382+
)[0] as {
383+
inputs: { txid: Hex; index: Hex; script: Hex; value: Hex }[];
384+
outputs: { value: Hex; script: Hex }[];
385+
};
386+
387+
const signatures = await Promise.all(
388+
transactionData.inputs.map(async (_, index) => {
389+
const hashToSign = await payloadBuilder.contract.read.hashToSign([
390+
BigInt(allocatorChainId),
391+
depository,
392+
encodedData as Hex,
393+
index,
394+
]);
395+
396+
const signature = await onchainAllocator.contract.read.signedPayloads(
397+
[payloadId as Hex, hashToSign],
398+
);
399+
400+
if (signature === "0x") {
401+
return undefined;
402+
}
403+
404+
return extractEcdsaSignature(signature) as Hex;
405+
}),
406+
);
407+
408+
if (signatures.some((signature) => !signature)) {
409+
return undefined;
410+
}
411+
412+
const finalizedSignatures = signatures.filter(
413+
(signature): signature is Hex => !!signature,
414+
);
415+
416+
return encodeAbiParameters([{ type: "bytes[]" }], [finalizedSignatures]);
417+
}
418+
350419
default: {
351420
throw externalError("Vm type not implemented");
352421
}

0 commit comments

Comments
 (0)