-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
337 lines (309 loc) · 10.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { MAX_DIVISIBILITY } from './src/constants';
import { Etching } from './src/etching';
import { Flaw as FlawEnum } from './src/flaw';
import { RuneEtchingSpec } from './src/indexer';
import { u128, u32, u64, u8 } from './src/integer';
import { None, Option, Some } from './src/monads';
import { RuneId } from './src/runeid';
import { Runestone, RunestoneTx } from './src/runestone';
import { SpacedRune } from './src/spacedrune';
import { Terms } from './src/terms';
export {
BlockIdentifier,
BlockInfo,
RuneBalance,
RuneBlockIndex,
RuneEtching,
RuneEtchingSpec,
RuneLocation,
RuneMintCount,
RuneSpentUtxoBalance,
RuneUpdater,
RuneUtxoBalance,
RunestoneIndexer,
RunestoneIndexerOptions,
RunestoneStorage,
} from './src/indexer';
export { Network } from './src/network';
export {
BitcoinRpcClient,
GetBlockParams,
GetBlockReturn,
GetBlockhashParams,
GetRawTransactionParams,
GetRawTransactionReturn,
RpcResponse,
Tx,
} from './src/rpcclient';
export type RunestoneSpec = {
mint?: {
block: bigint;
tx: number;
};
pointer?: number;
etching?: RuneEtchingSpec;
edicts?: {
id: {
block: bigint;
tx: number;
};
amount: bigint;
output: number;
}[];
};
export type Flaw =
| 'edict_output'
| 'edict_rune_id'
| 'invalid_script'
| 'opcode'
| 'supply_overflow'
| 'trailing_integers'
| 'truncated_field'
| 'unrecognized_even_tag'
| 'unrecognized_flag'
| 'varint';
export type Cenotaph = {
flaws: Flaw[];
etching?: string;
mint?: {
block: bigint;
tx: number;
};
};
function getFlawString(flaw: FlawEnum): Flaw {
switch (flaw) {
case FlawEnum.EDICT_OUTPUT:
return 'edict_output';
case FlawEnum.EDICT_RUNE_ID:
return 'edict_rune_id';
case FlawEnum.INVALID_SCRIPT:
return 'invalid_script';
case FlawEnum.OPCODE:
return 'opcode';
case FlawEnum.SUPPLY_OVERFLOW:
return 'supply_overflow';
case FlawEnum.TRAILING_INTEGERS:
return 'trailing_integers';
case FlawEnum.TRUNCATED_FIELD:
return 'truncated_field';
case FlawEnum.UNRECOGNIZED_EVEN_TAG:
return 'unrecognized_even_tag';
case FlawEnum.UNRECOGNIZED_FLAG:
return 'unrecognized_flag';
case FlawEnum.VARINT:
return 'varint';
}
}
// Helper functions to ensure numbers fit the desired type correctly
const u8Strict = (n: number) => {
const bigN = BigInt(n);
if (bigN < 0n || bigN > u8.MAX) {
throw Error('u8 overflow');
}
return u8(bigN);
};
const u32Strict = (n: number) => {
const bigN = BigInt(n);
if (bigN < 0n || bigN > u32.MAX) {
throw Error('u32 overflow');
}
return u32(bigN);
};
const u64Strict = (n: bigint) => {
const bigN = BigInt(n);
if (bigN < 0n || bigN > u64.MAX) {
throw Error('u64 overflow');
}
return u64(bigN);
};
const u128Strict = (n: bigint) => {
const bigN = BigInt(n);
if (bigN < 0n || bigN > u128.MAX) {
throw Error('u128 overflow');
}
return u128(bigN);
};
// TODO: Add unit tests
/**
* Low level function to allow for encoding runestones without any indexer and transaction checks.
*
* @param runestone runestone spec to encode as runestone
* @returns encoded runestone bytes
* @throws Error if encoding is detected to be considered a cenotaph
*/
export function encodeRunestone(runestone: RunestoneSpec): {
encodedRunestone: Buffer;
etchingCommitment?: Buffer;
} {
const mint = runestone.mint
? Some(new RuneId(u64Strict(runestone.mint.block), u32Strict(runestone.mint.tx)))
: None;
const pointer = runestone.pointer !== undefined ? Some(runestone.pointer).map(u32Strict) : None;
const edicts = (runestone.edicts ?? []).map((edict) => ({
id: new RuneId(u64Strict(edict.id.block), u32Strict(edict.id.tx)),
amount: u128Strict(edict.amount),
output: u32Strict(edict.output),
}));
let etching: Option<Etching> = None;
let etchingCommitment: Buffer | undefined = undefined;
if (runestone.etching) {
const etchingSpec = runestone.etching;
const spacedRune = etchingSpec.runeName
? SpacedRune.fromString(etchingSpec.runeName)
: undefined;
const rune = spacedRune?.rune !== undefined ? Some(spacedRune.rune) : None;
if (
etchingSpec.symbol &&
!(
etchingSpec.symbol.length === 1 ||
(etchingSpec.symbol.length === 2 && etchingSpec.symbol.codePointAt(0)! >= 0x10000)
)
) {
throw Error('Symbol must be one code point');
}
const divisibility =
etchingSpec.divisibility !== undefined ? Some(etchingSpec.divisibility).map(u8Strict) : None;
const premine =
etchingSpec.premine !== undefined ? Some(etchingSpec.premine).map(u128Strict) : None;
const spacers =
spacedRune?.spacers !== undefined && spacedRune.spacers !== 0
? Some(u32Strict(spacedRune.spacers))
: None;
const symbol = etchingSpec.symbol ? Some(etchingSpec.symbol) : None;
if (divisibility.isSome() && divisibility.unwrap() > MAX_DIVISIBILITY) {
throw Error(`Divisibility is greater than protocol max ${MAX_DIVISIBILITY}`);
}
let terms: Option<Terms> = None;
if (etchingSpec.terms) {
const termsSpec = etchingSpec.terms;
const amount = termsSpec.amount !== undefined ? Some(termsSpec.amount).map(u128Strict) : None;
const cap = termsSpec.cap !== undefined ? Some(termsSpec.cap).map(u128Strict) : None;
const height: [Option<u64>, Option<u64>] = termsSpec.height
? [
termsSpec.height.start !== undefined
? Some(termsSpec.height.start).map(u64Strict)
: None,
termsSpec.height.end !== undefined ? Some(termsSpec.height.end).map(u64Strict) : None,
]
: [None, None];
const offset: [Option<u64>, Option<u64>] = termsSpec.offset
? [
termsSpec.offset.start !== undefined
? Some(termsSpec.offset.start).map(u64Strict)
: None,
termsSpec.offset.end !== undefined ? Some(termsSpec.offset.end).map(u64Strict) : None,
]
: [None, None];
if (amount.isSome() && cap.isSome() && amount.unwrap() * cap.unwrap() > u128.MAX) {
throw Error('Terms overflow with amount times cap');
}
terms = Some({ amount, cap, height, offset });
}
const turbo = etchingSpec.turbo ?? false;
etching = Some(new Etching(divisibility, rune, spacers, symbol, terms, premine, turbo));
etchingCommitment = rune.isSome() ? rune.unwrap().commitment : undefined;
}
return {
encodedRunestone: new Runestone(mint, pointer, edicts, etching).encipher(),
etchingCommitment,
};
}
export function isRunestone(artifact: RunestoneSpec | Cenotaph): artifact is RunestoneSpec {
return !('flaws' in artifact);
}
export function tryDecodeRunestone(tx: RunestoneTx): RunestoneSpec | Cenotaph | null {
const optionArtifact = Runestone.decipher(tx);
if (optionArtifact.isNone()) {
return null;
}
const artifact = optionArtifact.unwrap();
if (artifact.type === 'runestone') {
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.flaws.map(getFlawString),
...(cenotaph.etching.isSome() ? { etching: cenotaph.etching.unwrap().toString() } : {}),
...(cenotaph.mint.isSome()
? { mint: { block: cenotaph.mint.unwrap().block, tx: Number(cenotaph.mint.unwrap().tx) } }
: {}),
};
}
}