Skip to content

Commit

Permalink
Added decode runestone helper function (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
summraznboi authored Apr 19, 2024
1 parent 3c58e33 commit d5da162
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
113 changes: 112 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { isRunestone } from './src/artifact';
import { MAX_DIVISIBILITY } from './src/constants';
import { Etching } from './src/etching';
import { RuneEtchingSpec } from './src/indexer';
import { u128, u32, u64, u8 } from './src/integer';
import { None, Option, Some } from './src/monads';
import { Rune } from './src/rune';
import { RuneId } from './src/runeid';
import { Runestone } from './src/runestone';
import { Runestone, RunestoneTx } from './src/runestone';
import { SpacedRune } from './src/spacedrune';
import { Terms } from './src/terms';

Expand Down Expand Up @@ -55,6 +57,15 @@ export type RunestoneSpec = {
}[];
};

export type Cenotaph = {
flaws: string[];
etching?: string;
mint?: {
block: bigint;
tx: number;
};
};

// Helper functions to ensure numbers fit the desired type correctly
const u8Strict = (n: number) => {
const bigN = BigInt(n);
Expand Down Expand Up @@ -184,3 +195,103 @@ export function encodeRunestone(runestone: RunestoneSpec): {
etchingCommitment,
};
}

export function tryDecodeRunestone(tx: RunestoneTx): RunestoneSpec | Cenotaph | null {
const optionArtifact = Runestone.decipher(tx);
if (optionArtifact.isNone()) {
return null;
}

const artifact = optionArtifact.unwrap();
if (isRunestone(artifact)) {
const runestone = artifact;

const etching = () => runestone.etching.unwrap();
const terms = () => etching().terms.unwrap();

return {
...(runestone.etching.isSome()
? {
etching: {
...(etching().divisibility.isSome()
? { divisibility: etching().divisibility.map(Number).unwrap() }
: {}),
...(etching().premine.isSome() ? { premine: etching().premine.unwrap() } : {}),
...(etching().rune.isSome()
? {
runeName: new SpacedRune(
etching().rune.unwrap(),
etching().spacers.map(Number).unwrapOr(0)
).toString(),
}
: {}),
...(etching().symbol.isSome() ? { symbol: etching().symbol.unwrap() } : {}),
...(etching().terms.isSome()
? {
terms: {
...(terms().amount.isSome() ? { amount: terms().amount.unwrap() } : {}),
...(terms().cap.isSome() ? { cap: terms().cap.unwrap() } : {}),
...(terms().height.find((option) => option.isSome())
? {
height: {
...(terms().height[0].isSome()
? { start: terms().height[0].unwrap() }
: {}),
...(terms().height[1].isSome()
? { end: terms().height[1].unwrap() }
: {}),
},
}
: {}),
...(terms().offset.find((option) => option.isSome())
? {
offset: {
...(terms().offset[0].isSome()
? { start: terms().offset[0].unwrap() }
: {}),
...(terms().offset[1].isSome()
? { end: terms().offset[1].unwrap() }
: {}),
},
}
: {}),
},
}
: {}),
turbo: etching().turbo,
},
}
: {}),
...(runestone.mint.isSome()
? {
mint: {
block: runestone.mint.unwrap().block,
tx: Number(runestone.mint.unwrap().tx),
},
}
: {}),
...(runestone.pointer.isSome() ? { pointer: Number(runestone.pointer.unwrap()) } : {}),
...(runestone.edicts.length
? {
edicts: runestone.edicts.map((edict) => ({
id: {
block: edict.id.block,
tx: Number(edict.id.tx),
},
amount: edict.amount,
output: Number(edict.output),
})),
}
: {}),
};
} else {
const cenotaph = artifact;
return {
flaws: [],
...(cenotaph.etching.isSome() ? { etching: cenotaph.etching.unwrap().toString() } : {}),
...(cenotaph.mint.isSome()
? { mint: { block: cenotaph.mint.unwrap().block, tx: Number(cenotaph.mint.unwrap().tx) } }
: {}),
};
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@magiceden-oss/runestone-lib",
"version": "0.9.5-alpha",
"version": "0.9.6-alpha",
"description": "",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/runestone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Cenotaph } from './cenotaph';

export const MAX_SPACERS = 0b00000111_11111111_11111111_11111111;

type RunestoneTx = { vout: { scriptPubKey: { hex: string } }[] };
export type RunestoneTx = { vout: { scriptPubKey: { hex: string } }[] };

type Payload = Buffer | Flaw;

Expand Down

0 comments on commit d5da162

Please sign in to comment.