Skip to content

Commit 8aa2de3

Browse files
authored
Merge pull request #515 from Oyl-Wallet/hude/use_wrap_out
fix reusing btc
2 parents 21908bc + db934ea commit 8aa2de3

File tree

4 files changed

+122
-53
lines changed

4 files changed

+122
-53
lines changed

lib/alkanes/alkanes.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export interface ProtostoneMessage {
1515
calldata: bigint[];
1616
}
1717
export declare const encodeProtostone: ({ protocolTag, edicts, pointer, refundPointer, calldata, }: ProtostoneMessage) => Buffer;
18+
export declare const addFrBtcWrapOutToPsbt: ({ frbtcWrapPsbt, account, psbt, }: {
19+
frbtcWrapPsbt: bitcoin.Psbt;
20+
account: Account;
21+
psbt: bitcoin.Psbt;
22+
}) => void;
1823
export declare const createExecutePsbt: ({ alkanesUtxos, frontendFee, feeAddress, utxos, account, protostone, provider, feeRate, fee, frbtcWrapPsbt, }: {
1924
alkanesUtxos?: FormattedUtxo[];
2025
frontendFee?: bigint;
@@ -153,7 +158,7 @@ export declare const deployReveal: ({ payload, alkanesUtxos, utxos, protostone,
153158
fee: number;
154159
satsPerVByte: string;
155160
}>;
156-
export declare const actualTransactRevealFee: ({ payload, alkanesUtxos, utxos, protostone, tweakedPublicKey, commitTxId, commitPsbt, receiverAddress, script, provider, feeRate, account, }: {
161+
export declare const actualTransactRevealFee: ({ payload, alkanesUtxos, utxos, protostone, tweakedPublicKey, commitTxId, commitPsbt, receiverAddress, script, provider, feeRate, account, frbtcWrapPsbt, }: {
157162
payload: AlkanesPayload;
158163
alkanesUtxos?: FormattedUtxo[];
159164
utxos: FormattedUtxo[];
@@ -166,6 +171,7 @@ export declare const actualTransactRevealFee: ({ payload, alkanesUtxos, utxos, p
166171
provider: Provider;
167172
feeRate?: number;
168173
account: Account;
174+
frbtcWrapPsbt?: bitcoin.Psbt;
169175
}) => Promise<{
170176
fee: number;
171177
vsize: number;
@@ -278,7 +284,7 @@ export declare const wrapBtc: ({ alkanesUtxos, utxos, account, provider, feeRate
278284
fee: number;
279285
satsPerVByte: string;
280286
}>;
281-
export declare const createTransactReveal: ({ payload, alkanesUtxos, utxos, protostone, receiverAddress, script, feeRate, tweakedPublicKey, provider, fee, commitTxId, commitPsbt, account, }: {
287+
export declare const createTransactReveal: ({ payload, alkanesUtxos, utxos, protostone, receiverAddress, script, feeRate, tweakedPublicKey, provider, fee, commitTxId, commitPsbt, account, frbtcWrapPsbt, }: {
282288
payload: AlkanesPayload;
283289
alkanesUtxos?: FormattedUtxo[];
284290
utxos: FormattedUtxo[];
@@ -292,6 +298,7 @@ export declare const createTransactReveal: ({ payload, alkanesUtxos, utxos, prot
292298
commitTxId: string;
293299
commitPsbt: bitcoin.Psbt;
294300
account: Account;
301+
frbtcWrapPsbt?: bitcoin.Psbt;
295302
}) => Promise<{
296303
psbt: string;
297304
fee: number;

lib/alkanes/alkanes.js

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

lib/alkanes/alkanes.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/alkanes/alkanes.ts

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,39 @@ export const encodeProtostone = ({
6060
}).encodedRunestone
6161
}
6262

63+
export const addFrBtcWrapOutToPsbt = ({
64+
frbtcWrapPsbt,
65+
account,
66+
psbt,
67+
}: {
68+
frbtcWrapPsbt: bitcoin.Psbt
69+
account: Account
70+
psbt: bitcoin.Psbt
71+
}) => {
72+
const frbtcWrapTxId = getUnfinalizedPsbtTxId(frbtcWrapPsbt)
73+
const output = frbtcWrapPsbt.txOutputs[0]
74+
if (account.taproot) {
75+
psbt.addInput({
76+
hash: frbtcWrapTxId,
77+
index: 0,
78+
witnessUtxo: {
79+
script: output.script,
80+
value: output.value,
81+
},
82+
tapInternalKey: toXOnly(Buffer.from(account.taproot.pubkey, 'hex')),
83+
})
84+
} else if (account.nativeSegwit) {
85+
psbt.addInput({
86+
hash: frbtcWrapTxId,
87+
index: 0,
88+
witnessUtxo: {
89+
script: output.script,
90+
value: output.value,
91+
},
92+
})
93+
}
94+
}
95+
6396
export const createExecutePsbt = async ({
6497
alkanesUtxos,
6598
frontendFee,
@@ -138,28 +171,7 @@ export const createExecutePsbt = async ({
138171
const psbt = new bitcoin.Psbt({ network: provider.network })
139172

140173
if (frbtcWrapPsbt) {
141-
const frbtcWrapTxId = getUnfinalizedPsbtTxId(frbtcWrapPsbt)
142-
const output = frbtcWrapPsbt.txOutputs[0]
143-
if (account.taproot) {
144-
psbt.addInput({
145-
hash: frbtcWrapTxId,
146-
index: 0,
147-
witnessUtxo: {
148-
script: output.script,
149-
value: output.value,
150-
},
151-
tapInternalKey: toXOnly(Buffer.from(account.taproot.pubkey, 'hex')),
152-
})
153-
} else if (account.nativeSegwit) {
154-
psbt.addInput({
155-
hash: frbtcWrapTxId,
156-
index: 0,
157-
witnessUtxo: {
158-
script: output.script,
159-
value: output.value,
160-
},
161-
})
162-
}
174+
addFrBtcWrapOutToPsbt({ frbtcWrapPsbt, account, psbt });
163175
}
164176
if (alkanesUtxos) {
165177
for (const utxo of alkanesUtxos) {
@@ -1003,6 +1015,7 @@ export const actualTransactRevealFee = async ({
10031015
provider,
10041016
feeRate,
10051017
account,
1018+
frbtcWrapPsbt,
10061019
}: {
10071020
payload: AlkanesPayload
10081021
alkanesUtxos?: FormattedUtxo[]
@@ -1016,6 +1029,7 @@ export const actualTransactRevealFee = async ({
10161029
provider: Provider
10171030
feeRate?: number
10181031
account: Account
1032+
frbtcWrapPsbt?: bitcoin.Psbt
10191033
}) => {
10201034
if (!feeRate) {
10211035
feeRate = (await provider.esplora.getFeeEstimates())['1']
@@ -1034,6 +1048,7 @@ export const actualTransactRevealFee = async ({
10341048
provider,
10351049
feeRate,
10361050
account,
1051+
frbtcWrapPsbt,
10371052
})
10381053

10391054
const { fee: estimatedFee } = await getEstimatedFee({
@@ -1056,6 +1071,7 @@ export const actualTransactRevealFee = async ({
10561071
feeRate,
10571072
fee: estimatedFee,
10581073
account,
1074+
frbtcWrapPsbt,
10591075
})
10601076

10611077
const { fee: finalFee, vsize } = await getEstimatedFee({
@@ -1228,6 +1244,18 @@ export const executeFallbackToWitnessProxy = async ({
12281244
remainingUtxos = utxos.filter(
12291245
utxo => !spentUtxos.some(spent => spent.txId === utxo.txId && spent.outputIndex === Number(utxo.outputIndex))
12301246
);
1247+
remainingUtxos.push({
1248+
txId: getUnfinalizedPsbtTxId(frbtcWrapPsbt),
1249+
outputIndex: frbtcWrapPsbt.txOutputs.length - 1,
1250+
satoshis: (frbtcWrapPsbt.txOutputs[frbtcWrapPsbt.txOutputs.length - 1] as bitcoin.PsbtTxOutput).value,
1251+
scriptPk: (frbtcWrapPsbt.txOutputs[frbtcWrapPsbt.txOutputs.length - 1] as bitcoin.PsbtTxOutput).script.toString('hex'),
1252+
address: account.nativeSegwit.address,
1253+
inscriptions: [],
1254+
runes: {},
1255+
alkanes: {},
1256+
confirmations: 0,
1257+
indexed: true, // technically not indexed but it can be used in future txs
1258+
});
12311259
if (alkanesUtxos) {
12321260
remainingAlkanesUtxos = alkanesUtxos.filter(
12331261
utxo => !spentUtxos.some(spent => spent.txId === utxo.txId && spent.outputIndex === Number(utxo.outputIndex))
@@ -1536,6 +1564,7 @@ export const createTransactReveal = async ({
15361564
commitTxId,
15371565
commitPsbt,
15381566
account,
1567+
frbtcWrapPsbt,
15391568
}: {
15401569
payload: AlkanesPayload
15411570
alkanesUtxos?: FormattedUtxo[]
@@ -1550,15 +1579,17 @@ export const createTransactReveal = async ({
15501579
commitTxId: string
15511580
commitPsbt: bitcoin.Psbt
15521581
account: Account
1582+
frbtcWrapPsbt?: bitcoin.Psbt
15531583
}) => {
15541584
try {
15551585
if (!feeRate) {
15561586
feeRate = (await provider.esplora.getFeeEstimates())['1']
15571587
}
15581588

15591589
const psbt: bitcoin.Psbt = new bitcoin.Psbt({ network: provider.network })
1590+
15601591
const minFee = minimumFee({
1561-
taprootInputCount: 1,
1592+
taprootInputCount: (frbtcWrapPsbt ? 2 : 1) + (alkanesUtxos ? alkanesUtxos.length : 0),
15621593
nonTaprootInputCount: 0,
15631594
outputCount: 2,
15641595
payload
@@ -1581,6 +1612,7 @@ export const createTransactReveal = async ({
15811612
network: provider.network,
15821613
})
15831614

1615+
// this needs to be the first input
15841616
psbt.addInput({
15851617
hash: commitTxId,
15861618
index: 0,
@@ -1597,6 +1629,10 @@ export const createTransactReveal = async ({
15971629
],
15981630
})
15991631

1632+
if (frbtcWrapPsbt) {
1633+
addFrBtcWrapOutToPsbt({ frbtcWrapPsbt, account, psbt });
1634+
}
1635+
16001636
let gatheredUtxos = {
16011637
utxos: [],
16021638
totalAmount: 0,
@@ -1760,6 +1796,7 @@ export const inscribePayloadBulk = async ({
17601796
provider,
17611797
feeRate,
17621798
account,
1799+
frbtcWrapPsbt,
17631800
})
17641801

17651802
const { psbt: finalRevealPsbt } = await createTransactReveal({
@@ -1776,6 +1813,7 @@ export const inscribePayloadBulk = async ({
17761813
feeRate,
17771814
fee: revealFee,
17781815
account,
1816+
frbtcWrapPsbt,
17791817
})
17801818

17811819
let finalReveal = bitcoin.Psbt.fromBase64(finalRevealPsbt, {

0 commit comments

Comments
 (0)