-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
220 lines (197 loc) · 4.91 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
import { RuneId, Runestone, SpacedRune, Symbol } from "./lib";
import { U32, U64 } from "big-varuint-js";
import {
initEccLib,
opcodes,
script,
payments,
networks,
Psbt,
} from "bitcoinjs-lib";
import * as ecc from "tiny-secp256k1";
import ECPairFactory from "ecpair";
initEccLib(ecc);
const FEE_RATE = 20
// DEFINE the network
// please note, this script only tested on regtest & testnet
// *DO NOT USE script on mainnet/production without any concern DWYOR
const network = networks.testnet;
const ECpair = ECPairFactory(ecc);
// generate wif
// console.log(ECpair.makeRandom().toWIF())
const keypair = ECpair.fromWIF(
"Kym6MbkzqMpvKTeTEreBLYk4UeHTEnwBDG5NnGd96aAm6G23Gcms",
);
const pubKeyXonly = keypair.publicKey.subarray(1, 33);
const RUNE_RECEIVE_VALUE = 600;
function createRune() {
const spacedRune = SpacedRune.fromString("WET.GEDANG.ENAKKK");
const runestone = new Runestone({
edicts: [],
pointer: new U32(0n),
// etching: {
// rune: spacedRune.rune,
// spacers: spacedRune.spacers,
// premine: new U128(1000_000n),
// symbol: Symbol.fromString("R"),
// terms: {
// amount: new U128(1000n),
// cap: new U128(100n),
// },
// },
mint: new RuneId(new U64(2587000n), new U32(1818n))
});
const buffer = runestone.enchiper();
return { buffer, commitBuffer: runestone.etching?.rune?.commitBuffer() };
}
function createMintPayment(commitBuffer: Buffer) {
// example witness + text inscription
// *commit buffer is required
const ordinalStacks = [
pubKeyXonly,
opcodes.OP_CHECKSIG,
opcodes.OP_FALSE,
opcodes.OP_IF,
Buffer.from("ord", "utf8"),
1,
1,
Buffer.concat([Buffer.from("text/plain;charset=utf-8", "utf8")]),
1,
2,
opcodes.OP_0,
1,
13,
commitBuffer,
opcodes.OP_0,
Buffer.concat([Buffer.from("Chainwave", "utf8")]),
opcodes.OP_ENDIF,
];
const ordinalScript = script.compile(ordinalStacks);
const scriptTree = {
output: ordinalScript,
};
const redeem = {
output: ordinalScript,
redeemVersion: 192,
};
const payment = payments.p2tr({
internalPubkey: pubKeyXonly,
network,
scriptTree,
redeem,
});
return {
payment,
redeem,
};
}
function createPsbt(
payment: payments.Payment,
redeem: {
output: Buffer;
redeemVersion: number;
},
hash: string,
index: number,
satValue: number,
receiverAddress: string,
runeBuffer: Buffer,
) {
const psbt = new Psbt({ network });
psbt.addInput({
hash,
index,
tapInternalKey: pubKeyXonly,
witnessUtxo: {
script: payment.output!,
value: satValue,
},
tapLeafScript: [
{
leafVersion: redeem.redeemVersion,
script: redeem.output,
controlBlock: payment.witness![payment.witness!.length - 1],
},
],
});
// this only used when then premine and pointer value is pointed to this index
// otherwise this will become dust utxo
// alternativaly you can just ignore this output if premine/pointer is not used.
psbt.addOutput({
address: receiverAddress,
value: RUNE_RECEIVE_VALUE,
});
const runeScript = script.compile([
opcodes.OP_RETURN,
opcodes.OP_13,
runeBuffer,
]);
psbt.addOutput({
script: runeScript,
value: 0,
});
return psbt;
}
function calculateFee(
payment: payments.Payment,
redeem: {
output: Buffer;
redeemVersion: number;
},
receiverAddress: string,
runeBuffer: Buffer,
) {
// create dummy tx
const tx = createPsbt(
payment,
redeem,
"e2aa2f0e1b49567e3c5e2f5985898657930e9f3ec1580b38429499e318c62b64",
0,
10 * 10 ** 8, // 10btc, just for dummy
receiverAddress,
runeBuffer,
);
tx.signAllInputs(keypair);
tx.finalizeAllInputs();
const vSize = tx.extractTransaction(true).virtualSize();
return vSize * FEE_RATE;
}
async function main() {
// STEP 1, create payment address and fund the balance
const rune = createRune();
const payment = createMintPayment(rune.commitBuffer!);
const receiverAddress =
"tb1qjc25patm0rld0wu6lfw0r5kd6sh2a4yzglrrts";
const fee = calculateFee(
payment.payment,
payment.redeem,
receiverAddress,
rune.buffer,
);
const fundValue = fee + RUNE_RECEIVE_VALUE;
console.log(
`- please fund this address ${payment.payment.address} ${fundValue} sat`,
);
// *this is required due to the ORD server checker to prevent front running transactions.
console.log(
`- wait until >=6 block confirmation and then continue to step 2`,
);
// STEP 2, create mint transaction
const txHash =
"c7632f7ad066313ef18aff7f9404774df576561ed5edf54b935fb30fb8d3c15a";
const txIndex = 0;
const psbt = createPsbt(
payment.payment,
payment.redeem,
txHash,
txIndex,
fundValue,
receiverAddress,
rune.buffer,
);
psbt.signAllInputs(keypair);
psbt.finalizeAllInputs();
const txHex = psbt.extractTransaction().toHex();
console.log({ txHex });
}
main();