forked from me-foundation/runestone-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
205 lines (184 loc) · 6.16 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
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 { SpacedRune } from './src/spacedrune';
import { Terms } from './src/terms';
export {
BlockIdentifier,
BlockInfo,
RuneBalance,
RuneBlockIndex,
RuneEtching,
RuneEtchingSpec,
RuneLocation,
RuneMintCount,
RuneOutput,
RuneUtxoBalance,
RunestoneIndexer,
RunestoneIndexerOptions,
RunestoneStorage,
} from './src/indexer';
export { Edict } from './src/edict';
export { Etching } from './src/etching';
export { Network } from './src/network';
export { Rune } from './src/rune';
export { SpacedRune } from './src/spacedrune';
export { RuneId } from './src/runeid';
export { Runestone } from './src/runestone';
export { Terms } from './src/terms';
export {
BitcoinRpcClient,
GetBlockParams,
GetBlockReturn,
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;
}[];
};
// 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);
};
const SPACERS = ['•', '.'];
// 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 encodeRunestoneUnsafe(runestone: RunestoneSpec): {
encodedRune: Buffer;
etchingCommitment: Buffer | undefined;
} {
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;
let hasSpacers = false;
for (const spacer of SPACERS) {
if (runestone.etching?.rune?.includes(spacer)) {
hasSpacers = true;
break;
}
}
let runeSpacers: number | undefined = undefined;
let parsedRawRune: Rune | undefined = undefined;
if (hasSpacers) {
const spacedRune = etchingSpec.rune ? SpacedRune.fromString(etchingSpec.rune) : undefined;
runeSpacers = spacedRune?.spacers;
parsedRawRune = spacedRune?.rune;
} else {
parsedRawRune = etchingSpec.rune ? Rune.fromString(etchingSpec.rune) : undefined;
}
const rune: Option<Rune> =
parsedRawRune !== undefined ? Some(parsedRawRune).map(() => parsedRawRune!) : 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: Option<u32> = hasSpacers && runeSpacers ? Some(u32Strict(runeSpacers)) : 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 = (parsedRawRune as Rune)?.commitment;
}
return {
encodedRune: new Runestone(mint, pointer, edicts, etching).encipher(),
etchingCommitment,
};
}