-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recursive_Rune.ts
364 lines (322 loc) · 9.13 KB
/
Recursive_Rune.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import {
Transaction,
script,
Psbt,
initEccLib,
networks,
Signer as BTCSigner,
crypto,
payments,
opcodes,
address as Address
} from "bitcoinjs-lib";
import { Taptree } from "bitcoinjs-lib/src/types";
import { ECPairFactory, ECPairAPI } from "ecpair";
import ecc from "@bitcoinerlab/secp256k1";
import axios, { AxiosResponse } from "axios";
import networkConfig from "config/network.config";
import { WIFWallet } from 'utils/WIFWallet'
import { SeedWallet } from "utils/SeedWallet";
import cbor from 'cbor';
import {
Rune,
RuneId,
Runestone,
EtchInscription,
none,
some,
Terms,
Range,
Etching,
} from "runelib";
//test
const network = networks.testnet;
// const network = networks.bitcoin;
initEccLib(ecc as any);
const ECPair: ECPairAPI = ECPairFactory(ecc);
export const contentBuffer = (content: string) => {
return Buffer.from(content, 'utf8')
}
// const seed: string = process.env.MNEMONIC as string;
// const networkType: string = networkConfig.networkType;
// const wallet = new SeedWallet({ networkType: networkType, seed: seed });
const privateKey: string = process.env.PRIVATE_KEY as string;
const networkType: string = networkConfig.networkType;
const wallet = new WIFWallet({ networkType: networkType, privateKey: privateKey });
// input data
const txhash: string = 'ea4303aaa2c7939931a2ba129c9fc915d1905d441f2a74b6cd694c71665c7682';
const memeType: string = 'text/html;charset=utf-8';
const metaProtocol: Buffer = Buffer.concat([Buffer.from("harmonitech.team", "utf8")]);
const receiveAddress: string = 'tb1ppx220ln489s5wqu8mqgezm7twwpj0avcvle3vclpdkpqvdg3mwqsvydajn';
const metadata = {
'type': 'HarmoniTech',
'description': 'HarmoniTech team Recursive Rune'
}
const fee = 130000;
const parentInscriptionTXID: string = 'ea4303aaa2c7939931a2ba129c9fc915d1905d441f2a74b6cd694c71665c7682';
const contentBufferData: Buffer = contentBuffer(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Build Your Own Recursive Ordinal</title>
</head>
<body style="margin: 0px">
<div>
<img style="width:100%;margin:0px" src="/content/864a32c90a83835e6706c0e649f2bc9ba43281f5144efa4c86e4247264806ab3i0" />
</div>
</body>
</html>
`);
const revealtxIDBuffer = Buffer.from(parentInscriptionTXID, 'hex');
const inscriptionBuffer = revealtxIDBuffer.reverse();
const pointer1: number = 546 * 1;
const pointer2: number = 546 * 2;
const pointer3: number = 546 * 3;
const pointer4: number = 546 * 4;
let pointerBuffer : Array<Buffer> = [];
pointerBuffer.push(Buffer.from(pointer1.toString(16).padStart(4, '0'), 'hex').reverse());
pointerBuffer.push(Buffer.from(pointer2.toString(16).padStart(4, '0'), 'hex').reverse());
pointerBuffer.push(Buffer.from(pointer3.toString(16).padStart(4, '0'), 'hex').reverse());
pointerBuffer.push(Buffer.from(pointer4.toString(16).padStart(4, '0'), 'hex').reverse());
const metadataBuffer = cbor.encode(metadata);
const splitBuffer = (buffer: Buffer, chunkSize: number) => {
let chunks = [];
for (let i = 0; i < buffer.length; i += chunkSize) {
const chunk = buffer.subarray(i, i + chunkSize);
chunks.push(chunk);
}
return chunks;
};
const contentBufferArray: Array<Buffer> = splitBuffer(contentBufferData, 450)
export function createChildInscriptionTapScript(): Array<Buffer> {
const keyPair = wallet.ecPair;
let childOrdinalStacks: any = [
toXOnly(keyPair.publicKey),
opcodes.OP_CHECKSIG,
];
for (let i = 0; i < 4; i++) {
childOrdinalStacks.push(
opcodes.OP_FALSE,
opcodes.OP_IF,
Buffer.from("ord", "utf8"),
1,
1,
Buffer.concat([Buffer.from(memeType, "utf8")]),
1,
2,
pointerBuffer[i],
1,
3,
inscriptionBuffer,
1,
5,
metadataBuffer,
1,
7,
metaProtocol,
opcodes.OP_0
);
contentBufferArray.forEach((item: Buffer) => {
childOrdinalStacks.push(item)
})
childOrdinalStacks.push(opcodes.OP_ENDIF)
}
return childOrdinalStacks;
}
async function childInscribe() {
const keyPair = wallet.ecPair;
const childOrdinalStack = createChildInscriptionTapScript();
console.log(childOrdinalStack)
const ordinal_script = script.compile(childOrdinalStack);
const scriptTree: Taptree = {
output: ordinal_script,
};
const redeem = {
output: ordinal_script,
redeemVersion: 192,
};
const ordinal_p2tr = payments.p2tr({
internalPubkey: toXOnly(keyPair.publicKey),
network,
scriptTree,
redeem,
});
const address = ordinal_p2tr.address ?? "";
console.log("send coin to address", address);
const utxos = await waitUntilUTXO(address as string);
const psbt = new Psbt({ network });
const parentInscriptionUTXO = {
txid: txhash,
vout: 1,
value: 546
}
psbt.addInput({
hash: parentInscriptionUTXO.txid,
index: parentInscriptionUTXO.vout,
witnessUtxo: {
value: parentInscriptionUTXO.value,
script: wallet.output,
},
tapInternalKey: toXOnly(keyPair.publicKey),
});
psbt.addInput({
hash: utxos[0].txid,
index: utxos[0].vout,
tapInternalKey: toXOnly(keyPair.publicKey),
witnessUtxo: { value: utxos[0].value, script: ordinal_p2tr.output! },
tapLeafScript: [
{
leafVersion: redeem.redeemVersion,
script: redeem.output,
controlBlock: ordinal_p2tr.witness![ordinal_p2tr.witness!.length - 1],
},
],
});
const edicts: any = [];
edicts.push({
id: new RuneId(2817883, 2295),
amount: 10000,
output: 2,
});
edicts.push({
id: new RuneId(2817883, 2295),
amount: 10000,
output: 3,
});
edicts.push({
id: new RuneId(2817883, 2295),
amount: 10000,
output: 4,
});
edicts.push({
id: new RuneId(2817883, 2295),
amount: 10000,
output: 5,
});
const mintstone = new Runestone(
edicts,
none(),
none(),
none()
);
psbt.addOutput({
script: mintstone.encipher(),
value: 0,
});
const change = utxos[0].value - 546 * 5 - fee;
psbt.addOutput({
address: receiveAddress, //Destination Address
value: 546,
});
psbt.addOutput({
address: receiveAddress, //Destination Address
value: 546,
});
psbt.addOutput({
address: receiveAddress, //Destination Address
value: 546,
});
psbt.addOutput({
address: receiveAddress, //Destination Address
value: 546,
});
psbt.addOutput({
address: receiveAddress, //Destination Address
value: 546,
});
psbt.addOutput({
address: receiveAddress, // Change address
value: change,
});
await signAndSend(keyPair, psbt);
}
childInscribe()
export async function signAndSend(
keypair: BTCSigner,
psbt: Psbt,
) {
const signer = tweakSigner(keypair, { network })
psbt.signInput(0, signer);
psbt.signInput(1, keypair);
psbt.finalizeAllInputs()
const tx = psbt.extractTransaction();
console.log(tx.virtualSize())
console.log(tx.toHex());
// const txid = await broadcast(tx.toHex());
// console.log(`Success! Txid is ${txid}`);
}
export async function waitUntilUTXO(address: string) {
return new Promise<IUTXO[]>((resolve, reject) => {
let intervalId: any;
const checkForUtxo = async () => {
try {
const response: AxiosResponse<string> = await blockstream.get(
`/address/${address}/utxo`
);
const data: IUTXO[] = response.data
? JSON.parse(response.data)
: undefined;
console.log(data);
if (data.length > 0) {
resolve(data);
clearInterval(intervalId);
}
} catch (error) {
reject(error);
clearInterval(intervalId);
}
};
intervalId = setInterval(checkForUtxo, 4000);
});
}
export async function getTx(id: string): Promise<string> {
const response: AxiosResponse<string> = await blockstream.get(
`/tx/${id}/hex`
);
return response.data;
}
const blockstream = new axios.Axios({
baseURL: `https://mempool.space/testnet/api`,
// baseURL: `https://mempool.space/api`,
});
export async function broadcast(txHex: string) {
const response: AxiosResponse<string> = await blockstream.post("/tx", txHex);
return response.data;
}
function tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer {
return crypto.taggedHash(
"TapTweak",
Buffer.concat(h ? [pubKey, h] : [pubKey])
);
}
function toXOnly(pubkey: Buffer): Buffer {
return pubkey.subarray(1, 33);
}
function tweakSigner(signer: any, opts: any = {}) {
let privateKey = signer.privateKey;
if (!privateKey) {
throw new Error('Private key is required for tweaking signer!');
}
if (signer.publicKey[0] === 3) {
privateKey = ecc.privateNegate(privateKey);
}
const tweakedPrivateKey = ecc.privateAdd(privateKey, tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash));
if (!tweakedPrivateKey) {
throw new Error('Invalid tweaked private key!');
}
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
network: opts.network,
});
}
interface IUTXO {
txid: string;
vout: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
value: number;
}