Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add runes support #68

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
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
Loading