Skip to content

Commit

Permalink
chore: add runes support
Browse files Browse the repository at this point in the history
  • Loading branch information
topether21 committed May 20, 2024
1 parent 9a79b25 commit 3c09a9f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nosft-core",
"version": "2.5.11",
"version": "2.5.12",
"private": false,
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
40 changes: 38 additions & 2 deletions src/app/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Utxo } from './utxo';

type Metadata = {
inputs: { index: number; type: 'Ordinal' | 'Cardinal' }[];
outputs: { type: 'Ordinal' | 'Change' | 'Cardinal' }[];
outputs: { type: 'Ordinal' | 'Change' | 'Cardinal' | 'Cardinal - Runes' | 'Ordinal - Runes' }[];
};

bitcoin.initEccLib(ecc);
Expand Down Expand Up @@ -53,6 +53,35 @@ const Psbt = function (config) {
const cryptoModule = Crypto(config);
const utxoModule = Utxo(config);

const checkForRunes = async (utxos) => {
for (const utxo of utxos) {
try {
const output = await utxoModule.getOutput(`${utxo.txid}:${utxo.vout}`);
if (output.runes && output.runes.length > 0) {
return true;
}
} catch (e) {
console.log(`Error fetching output for ${utxo.txid}:${utxo.vout}`, e);
}
}
return false;
};

const filterCardinalUtxos = async (utxos) => {
const filteredUtxos = [];
for (const utxo of utxos) {
try {
const output = await utxoModule.getOutput(`${utxo.txid}:${utxo.vout}`);
if (!output.runes || output.runes.length === 0) {
filteredUtxos.push(utxo);
}
} catch (e) {
console.log(`Error fetching output for ${utxo.txid}:${utxo.vout}`, e);
}
}
return filteredUtxos;
};

const psbtModule = {
getPsbt,
getPsbtBase64,
Expand Down Expand Up @@ -356,12 +385,15 @@ const Psbt = function (config) {
}

// only used if selectedCardinalAmount is not large enough to cover fees
const cardinalUtxos = ownedUtxos.filter(utxo => !selectedUtxos.some(ownedUtxo => ownedUtxo.txid === utxo.txid))
const availableCardinalUtxos = ownedUtxos.filter(utxo => !selectedUtxos.some(ownedUtxo => ownedUtxo.txid === utxo.txid))
.filter((x) => x.status.confirmed)
.filter((x) => x.value > 10000)
.filter(utxo => !utxo.inscriptionId)
.sort((a, b) => b.value - a.value);

const hasRunes = await checkForRunes(utxosWithoutInscription);
const cardinalUtxos = await filterCardinalUtxos(availableCardinalUtxos);

const selectedCardinalAmount = utxosWithoutInscription.reduce((acc, utxo) => acc + utxo.value, 0);
const inputs = [...utxosWithInscription, ...utxosWithoutInscription];

Expand Down Expand Up @@ -445,6 +477,10 @@ const Psbt = function (config) {
metadata.outputs.push({ type: 'Cardinal' });
}

if (metadata.outputs[0] && hasRunes) {
metadata.outputs[0].type = metadata.outputs[0].type + `${hasRunes ? ' - Runes' : ''}`;
}

return {
unsignedPsbtHex: psbt.toHex(),
metadata
Expand Down

0 comments on commit 3c09a9f

Please sign in to comment.