Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/alkanes/alkanes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export interface ProtostoneMessage {
calldata: bigint[];
}
export declare const encodeProtostone: ({ protocolTag, edicts, pointer, refundPointer, calldata, }: ProtostoneMessage) => Buffer;
export declare const addFrBtcWrapOutToPsbt: ({ frbtcWrapPsbt, account, psbt, }: {
frbtcWrapPsbt: bitcoin.Psbt;
account: Account;
psbt: bitcoin.Psbt;
}) => void;
export declare const createExecutePsbt: ({ alkanesUtxos, frontendFee, feeAddress, utxos, account, protostone, provider, feeRate, fee, frbtcWrapPsbt, }: {
alkanesUtxos?: FormattedUtxo[];
frontendFee?: bigint;
Expand Down Expand Up @@ -153,7 +158,7 @@ export declare const deployReveal: ({ payload, alkanesUtxos, utxos, protostone,
fee: number;
satsPerVByte: string;
}>;
export declare const actualTransactRevealFee: ({ payload, alkanesUtxos, utxos, protostone, tweakedPublicKey, commitTxId, commitPsbt, receiverAddress, script, provider, feeRate, account, }: {
export declare const actualTransactRevealFee: ({ payload, alkanesUtxos, utxos, protostone, tweakedPublicKey, commitTxId, commitPsbt, receiverAddress, script, provider, feeRate, account, frbtcWrapPsbt, }: {
payload: AlkanesPayload;
alkanesUtxos?: FormattedUtxo[];
utxos: FormattedUtxo[];
Expand All @@ -166,6 +171,7 @@ export declare const actualTransactRevealFee: ({ payload, alkanesUtxos, utxos, p
provider: Provider;
feeRate?: number;
account: Account;
frbtcWrapPsbt?: bitcoin.Psbt;
}) => Promise<{
fee: number;
vsize: number;
Expand Down Expand Up @@ -278,7 +284,7 @@ export declare const wrapBtc: ({ alkanesUtxos, utxos, account, provider, feeRate
fee: number;
satsPerVByte: string;
}>;
export declare const createTransactReveal: ({ payload, alkanesUtxos, utxos, protostone, receiverAddress, script, feeRate, tweakedPublicKey, provider, fee, commitTxId, commitPsbt, account, }: {
export declare const createTransactReveal: ({ payload, alkanesUtxos, utxos, protostone, receiverAddress, script, feeRate, tweakedPublicKey, provider, fee, commitTxId, commitPsbt, account, frbtcWrapPsbt, }: {
payload: AlkanesPayload;
alkanesUtxos?: FormattedUtxo[];
utxos: FormattedUtxo[];
Expand All @@ -292,6 +298,7 @@ export declare const createTransactReveal: ({ payload, alkanesUtxos, utxos, prot
commitTxId: string;
commitPsbt: bitcoin.Psbt;
account: Account;
frbtcWrapPsbt?: bitcoin.Psbt;
}) => Promise<{
psbt: string;
fee: number;
Expand Down
78 changes: 51 additions & 27 deletions lib/alkanes/alkanes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/alkanes/alkanes.js.map

Large diffs are not rendered by default.

84 changes: 61 additions & 23 deletions src/alkanes/alkanes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ export const encodeProtostone = ({
}).encodedRunestone
}

export const addFrBtcWrapOutToPsbt = ({
frbtcWrapPsbt,
account,
psbt,
}: {
frbtcWrapPsbt: bitcoin.Psbt
account: Account
psbt: bitcoin.Psbt
}) => {
const frbtcWrapTxId = getUnfinalizedPsbtTxId(frbtcWrapPsbt)
const output = frbtcWrapPsbt.txOutputs[0]
if (account.taproot) {
psbt.addInput({
hash: frbtcWrapTxId,
index: 0,
witnessUtxo: {
script: output.script,
value: output.value,
},
tapInternalKey: toXOnly(Buffer.from(account.taproot.pubkey, 'hex')),
})
} else if (account.nativeSegwit) {
psbt.addInput({
hash: frbtcWrapTxId,
index: 0,
witnessUtxo: {
script: output.script,
value: output.value,
},
})
}
}

export const createExecutePsbt = async ({
alkanesUtxos,
frontendFee,
Expand Down Expand Up @@ -138,28 +171,7 @@ export const createExecutePsbt = async ({
const psbt = new bitcoin.Psbt({ network: provider.network })

if (frbtcWrapPsbt) {
const frbtcWrapTxId = getUnfinalizedPsbtTxId(frbtcWrapPsbt)
const output = frbtcWrapPsbt.txOutputs[0]
if (account.taproot) {
psbt.addInput({
hash: frbtcWrapTxId,
index: 0,
witnessUtxo: {
script: output.script,
value: output.value,
},
tapInternalKey: toXOnly(Buffer.from(account.taproot.pubkey, 'hex')),
})
} else if (account.nativeSegwit) {
psbt.addInput({
hash: frbtcWrapTxId,
index: 0,
witnessUtxo: {
script: output.script,
value: output.value,
},
})
}
addFrBtcWrapOutToPsbt({ frbtcWrapPsbt, account, psbt });
}
if (alkanesUtxos) {
for (const utxo of alkanesUtxos) {
Expand Down Expand Up @@ -1003,6 +1015,7 @@ export const actualTransactRevealFee = async ({
provider,
feeRate,
account,
frbtcWrapPsbt,
}: {
payload: AlkanesPayload
alkanesUtxos?: FormattedUtxo[]
Expand All @@ -1016,6 +1029,7 @@ export const actualTransactRevealFee = async ({
provider: Provider
feeRate?: number
account: Account
frbtcWrapPsbt?: bitcoin.Psbt
}) => {
if (!feeRate) {
feeRate = (await provider.esplora.getFeeEstimates())['1']
Expand All @@ -1034,6 +1048,7 @@ export const actualTransactRevealFee = async ({
provider,
feeRate,
account,
frbtcWrapPsbt,
})

const { fee: estimatedFee } = await getEstimatedFee({
Expand All @@ -1056,6 +1071,7 @@ export const actualTransactRevealFee = async ({
feeRate,
fee: estimatedFee,
account,
frbtcWrapPsbt,
})

const { fee: finalFee, vsize } = await getEstimatedFee({
Expand Down Expand Up @@ -1228,6 +1244,18 @@ export const executeFallbackToWitnessProxy = async ({
remainingUtxos = utxos.filter(
utxo => !spentUtxos.some(spent => spent.txId === utxo.txId && spent.outputIndex === Number(utxo.outputIndex))
);
remainingUtxos.push({
txId: getUnfinalizedPsbtTxId(frbtcWrapPsbt),
outputIndex: frbtcWrapPsbt.txOutputs.length - 1,
satoshis: (frbtcWrapPsbt.txOutputs[frbtcWrapPsbt.txOutputs.length - 1] as bitcoin.PsbtTxOutput).value,
scriptPk: (frbtcWrapPsbt.txOutputs[frbtcWrapPsbt.txOutputs.length - 1] as bitcoin.PsbtTxOutput).script.toString('hex'),
address: account.nativeSegwit.address,
inscriptions: [],
runes: {},
alkanes: {},
confirmations: 0,
indexed: true, // technically not indexed but it can be used in future txs
});
if (alkanesUtxos) {
remainingAlkanesUtxos = alkanesUtxos.filter(
utxo => !spentUtxos.some(spent => spent.txId === utxo.txId && spent.outputIndex === Number(utxo.outputIndex))
Expand Down Expand Up @@ -1536,6 +1564,7 @@ export const createTransactReveal = async ({
commitTxId,
commitPsbt,
account,
frbtcWrapPsbt,
}: {
payload: AlkanesPayload
alkanesUtxos?: FormattedUtxo[]
Expand All @@ -1550,15 +1579,17 @@ export const createTransactReveal = async ({
commitTxId: string
commitPsbt: bitcoin.Psbt
account: Account
frbtcWrapPsbt?: bitcoin.Psbt
}) => {
try {
if (!feeRate) {
feeRate = (await provider.esplora.getFeeEstimates())['1']
}

const psbt: bitcoin.Psbt = new bitcoin.Psbt({ network: provider.network })

const minFee = minimumFee({
taprootInputCount: 1,
taprootInputCount: (frbtcWrapPsbt ? 2 : 1) + (alkanesUtxos ? alkanesUtxos.length : 0),
nonTaprootInputCount: 0,
outputCount: 2,
payload
Expand All @@ -1581,6 +1612,7 @@ export const createTransactReveal = async ({
network: provider.network,
})

// this needs to be the first input
psbt.addInput({
hash: commitTxId,
index: 0,
Expand All @@ -1597,6 +1629,10 @@ export const createTransactReveal = async ({
],
})

if (frbtcWrapPsbt) {
addFrBtcWrapOutToPsbt({ frbtcWrapPsbt, account, psbt });
}

let gatheredUtxos = {
utxos: [],
totalAmount: 0,
Expand Down Expand Up @@ -1760,6 +1796,7 @@ export const inscribePayloadBulk = async ({
provider,
feeRate,
account,
frbtcWrapPsbt,
})

const { psbt: finalRevealPsbt } = await createTransactReveal({
Expand All @@ -1776,6 +1813,7 @@ export const inscribePayloadBulk = async ({
feeRate,
fee: revealFee,
account,
frbtcWrapPsbt,
})

let finalReveal = bitcoin.Psbt.fromBase64(finalRevealPsbt, {
Expand Down