|
| 1 | +import { isRunestone } from './src/artifact'; |
1 | 2 | import { MAX_DIVISIBILITY } from './src/constants';
|
2 | 3 | import { Etching } from './src/etching';
|
3 | 4 | import { RuneEtchingSpec } from './src/indexer';
|
4 | 5 | import { u128, u32, u64, u8 } from './src/integer';
|
5 | 6 | import { None, Option, Some } from './src/monads';
|
| 7 | +import { Rune } from './src/rune'; |
6 | 8 | import { RuneId } from './src/runeid';
|
7 |
| -import { Runestone } from './src/runestone'; |
| 9 | +import { Runestone, RunestoneTx } from './src/runestone'; |
8 | 10 | import { SpacedRune } from './src/spacedrune';
|
9 | 11 | import { Terms } from './src/terms';
|
10 | 12 |
|
@@ -55,6 +57,15 @@ export type RunestoneSpec = {
|
55 | 57 | }[];
|
56 | 58 | };
|
57 | 59 |
|
| 60 | +export type Cenotaph = { |
| 61 | + flaws: string[]; |
| 62 | + etching?: string; |
| 63 | + mint?: { |
| 64 | + block: bigint; |
| 65 | + tx: number; |
| 66 | + }; |
| 67 | +}; |
| 68 | + |
58 | 69 | // Helper functions to ensure numbers fit the desired type correctly
|
59 | 70 | const u8Strict = (n: number) => {
|
60 | 71 | const bigN = BigInt(n);
|
@@ -184,3 +195,103 @@ export function encodeRunestone(runestone: RunestoneSpec): {
|
184 | 195 | etchingCommitment,
|
185 | 196 | };
|
186 | 197 | }
|
| 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 | +} |
0 commit comments