Skip to content

Commit d5da162

Browse files
authored
Added decode runestone helper function (#51)
1 parent 3c58e33 commit d5da162

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

index.ts

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { isRunestone } from './src/artifact';
12
import { MAX_DIVISIBILITY } from './src/constants';
23
import { Etching } from './src/etching';
34
import { RuneEtchingSpec } from './src/indexer';
45
import { u128, u32, u64, u8 } from './src/integer';
56
import { None, Option, Some } from './src/monads';
7+
import { Rune } from './src/rune';
68
import { RuneId } from './src/runeid';
7-
import { Runestone } from './src/runestone';
9+
import { Runestone, RunestoneTx } from './src/runestone';
810
import { SpacedRune } from './src/spacedrune';
911
import { Terms } from './src/terms';
1012

@@ -55,6 +57,15 @@ export type RunestoneSpec = {
5557
}[];
5658
};
5759

60+
export type Cenotaph = {
61+
flaws: string[];
62+
etching?: string;
63+
mint?: {
64+
block: bigint;
65+
tx: number;
66+
};
67+
};
68+
5869
// Helper functions to ensure numbers fit the desired type correctly
5970
const u8Strict = (n: number) => {
6071
const bigN = BigInt(n);
@@ -184,3 +195,103 @@ export function encodeRunestone(runestone: RunestoneSpec): {
184195
etchingCommitment,
185196
};
186197
}
198+
199+
export function tryDecodeRunestone(tx: RunestoneTx): RunestoneSpec | Cenotaph | null {
200+
const optionArtifact = Runestone.decipher(tx);
201+
if (optionArtifact.isNone()) {
202+
return null;
203+
}
204+
205+
const artifact = optionArtifact.unwrap();
206+
if (isRunestone(artifact)) {
207+
const runestone = artifact;
208+
209+
const etching = () => runestone.etching.unwrap();
210+
const terms = () => etching().terms.unwrap();
211+
212+
return {
213+
...(runestone.etching.isSome()
214+
? {
215+
etching: {
216+
...(etching().divisibility.isSome()
217+
? { divisibility: etching().divisibility.map(Number).unwrap() }
218+
: {}),
219+
...(etching().premine.isSome() ? { premine: etching().premine.unwrap() } : {}),
220+
...(etching().rune.isSome()
221+
? {
222+
runeName: new SpacedRune(
223+
etching().rune.unwrap(),
224+
etching().spacers.map(Number).unwrapOr(0)
225+
).toString(),
226+
}
227+
: {}),
228+
...(etching().symbol.isSome() ? { symbol: etching().symbol.unwrap() } : {}),
229+
...(etching().terms.isSome()
230+
? {
231+
terms: {
232+
...(terms().amount.isSome() ? { amount: terms().amount.unwrap() } : {}),
233+
...(terms().cap.isSome() ? { cap: terms().cap.unwrap() } : {}),
234+
...(terms().height.find((option) => option.isSome())
235+
? {
236+
height: {
237+
...(terms().height[0].isSome()
238+
? { start: terms().height[0].unwrap() }
239+
: {}),
240+
...(terms().height[1].isSome()
241+
? { end: terms().height[1].unwrap() }
242+
: {}),
243+
},
244+
}
245+
: {}),
246+
...(terms().offset.find((option) => option.isSome())
247+
? {
248+
offset: {
249+
...(terms().offset[0].isSome()
250+
? { start: terms().offset[0].unwrap() }
251+
: {}),
252+
...(terms().offset[1].isSome()
253+
? { end: terms().offset[1].unwrap() }
254+
: {}),
255+
},
256+
}
257+
: {}),
258+
},
259+
}
260+
: {}),
261+
turbo: etching().turbo,
262+
},
263+
}
264+
: {}),
265+
...(runestone.mint.isSome()
266+
? {
267+
mint: {
268+
block: runestone.mint.unwrap().block,
269+
tx: Number(runestone.mint.unwrap().tx),
270+
},
271+
}
272+
: {}),
273+
...(runestone.pointer.isSome() ? { pointer: Number(runestone.pointer.unwrap()) } : {}),
274+
...(runestone.edicts.length
275+
? {
276+
edicts: runestone.edicts.map((edict) => ({
277+
id: {
278+
block: edict.id.block,
279+
tx: Number(edict.id.tx),
280+
},
281+
amount: edict.amount,
282+
output: Number(edict.output),
283+
})),
284+
}
285+
: {}),
286+
};
287+
} else {
288+
const cenotaph = artifact;
289+
return {
290+
flaws: [],
291+
...(cenotaph.etching.isSome() ? { etching: cenotaph.etching.unwrap().toString() } : {}),
292+
...(cenotaph.mint.isSome()
293+
? { mint: { block: cenotaph.mint.unwrap().block, tx: Number(cenotaph.mint.unwrap().tx) } }
294+
: {}),
295+
};
296+
}
297+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@magiceden-oss/runestone-lib",
3-
"version": "0.9.5-alpha",
3+
"version": "0.9.6-alpha",
44
"description": "",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/runestone.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Cenotaph } from './cenotaph';
1717

1818
export const MAX_SPACERS = 0b00000111_11111111_11111111_11111111;
1919

20-
type RunestoneTx = { vout: { scriptPubKey: { hex: string } }[] };
20+
export type RunestoneTx = { vout: { scriptPubKey: { hex: string } }[] };
2121

2222
type Payload = Buffer | Flaw;
2323

0 commit comments

Comments
 (0)