-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcc-covenant-testnet.ts
321 lines (291 loc) · 11.5 KB
/
cc-covenant-testnet.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
import path from 'path';
import { BITBOX } from 'bitbox-sdk';
import { compileFile } from 'cashc';
import { stringify } from '@bitauth/libauth';
import {
Contract,
SignatureTemplate,
HashType,
SignatureAlgorithm,
} from 'cashscript';
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { createElectrumTestnetProvider, rawTxToStr } from './utils/utils';
const bitbox = new BITBOX();
const rootSeed = bitbox.Mnemonic.toSeed('cc_covenant_v2_testnet');
const hdNode = bitbox.HDNode.fromSeed(rootSeed);
// operators
const operatorKeyPairs = [...Array(10).keys()]
.map(x => bitbox.HDNode.derive(hdNode, x + 100))
.map(n => bitbox.HDNode.toKeyPair(n));
const operatorWIFs = operatorKeyPairs.map(k => bitbox.ECPair.toWIF(k));
const operatorPbks = operatorKeyPairs.map(k => bitbox.ECPair.toPublicKey(k));
const operatorPubkeysHash = bitbox.Crypto.hash160(Buffer.concat(operatorPbks))
// monitors
const monitorKeyPairs = [...Array(3).keys()]
.map(x => bitbox.HDNode.derive(hdNode, x + 200))
.map(n => bitbox.HDNode.toKeyPair(n));
const monitorWIFs = monitorKeyPairs.map(k => bitbox.ECPair.toWIF(k));
const monitorPbks = monitorKeyPairs.map(k => bitbox.ECPair.toPublicKey(k));
const monitorPubkeysHash = bitbox.Crypto.hash160(Buffer.concat(monitorPbks))
const artifact = compileFile(path.join(__dirname, 'cc-covenant-testnet.cash'));
const provider = createElectrumTestnetProvider();
yargs(hideBin(process.argv))
.command('print-covenant-info', 'show covenant info', (yargs: any) => {
return yargs
.option('verbose', {required: false, type: 'boolean', default: true})
;
}, async (argv: any) => {
printKeys(argv.verbose);
printContractInfo(argv.verbose);
})
.command('list-cc-utxo', 'show cc-UTXOs', (yargs: any) => {
return yargs;
}, async (argv: any) => {
await listUTXOs();
})
.command('redeem-by-user', 'redeem cc-UTXO by user', (yargs: any) => {
return yargs
.option('to', {required: true, type: 'string', description: 'receiver address'})
.option('utxo', {required: true, type: 'string', description: 'txid:vout'})
.option('txfee', {required: true, type: 'number', description: 'tx fee'})
.option('dryrun', {required: false, type: 'boolean', default: false})
;
}, async (argv: any) => {
await redeemByUser(argv.to, argv.utxo, argv.txfee, argv.dryrun);
})
.command('redeem-all', 'redeem all cc-UTXOs', (yargs: any) => {
return yargs
.option('to', {required: true, type: 'string', description: 'receiver address'})
.option('txfee', {required: true, type: 'number', description: 'tx fee'})
;
}, async (argv: any) => {
const contract = createContract();
let utxos = await contract.getUtxos();
console.log('addr:', contract.address);
console.log('UTXOs:', utxos.length);
for (const utxo of utxos) {
if (utxo.satoshis < argv.txfee) {
continue;
}
console.log('redeeming', utxo, '...');
await redeemByUser(argv.to, `${utxo.txid}:${utxo.vout}`, argv.txfee, false);
}
})
.command('convert-by-operators', 'convert cc-UTXO by operators', (yargs: any) => {
return yargs
.option('new-operator-pubkeys-hash', {required: true, type: 'string', description: '20-bytes hex'})
.option('new-monitor-pubkeys-hash', {required: true, type: 'string', description: '20-bytes hex'})
.option('to', {required: true, type: 'string', description: 'cc-covenant address'})
.option('utxo', {required: true, type: 'string', description: 'txid:vout'})
.option('txfee', {required: true, type: 'number', description: 'tx fee'})
.option('dryrun', {required: false, type: 'boolean', default: false})
;
}, async (argv: any) => {
await convertByOperators(argv.utxo, argv.txfee, argv.to,
argv.newOperatorPubkeysHash, argv.newMonitorPubkeysHash, argv.dryrun);
})
.command('convert-by-monitors', 'convert cc-UTXO by monitors', (yargs: any) => {
return yargs
.option('new-operator-pubkeys-hash', {required: true, type: 'string', description: '20-bytes hex'})
.option('to', {required: true, type: 'string', description: 'new cc-covenant address'})
.option('utxo', {required: true, type: 'string', description: 'txid:vout'})
.option('txfee', {required: true, type: 'number', description: 'tx fee'})
.option('fee-utxo', {required: true, type: 'string', description: 'txid:vout'})
.option('fee-wif', {required: true, type: 'string', description: 'key of fee provider in WIF'})
.option('dryrun', {required: false, type: 'boolean', default: false})
;
}, async (argv: any) => {
await convertByMonitors(argv.utxo, argv.to, argv.newOperatorPubkeysHash,
argv.feeUtxo, argv.feeWif, argv.txfee, argv.dryrun);
})
.strictCommands()
.argv;
function printKeys(verbose: boolean) {
if (verbose) {
// console.log('operatorWIFs:', operatorWIFs.map(x => x.toString('hex')));
// console.log('operatorPbks:', operatorPbks.map(x => x.toString('hex')));
const ops = [];
for (let i = 0; i < operatorWIFs.length; i++) {
ops.push({
WIF: operatorWIFs[i].toString('hex'),
PBK: operatorPbks[i].toString('hex'),
})
}
console.log('operators:');
console.table(ops);
}
if (verbose) {
// console.log('monitorWIFs:', monitorWIFs.map(x => x.toString('hex')));
// console.log('monitorPbks:', monitorPbks.map(x => x.toString('hex')));
const mos = [];
for (let i = 0; i < monitorWIFs.length; i++) {
mos.push({
WIF: monitorWIFs[i].toString('hex'),
PBK: monitorPbks[i].toString('hex'),
})
}
console.log('monitors:');
console.table(mos);
}
console.log('operatorPubkeysHash:', operatorPubkeysHash.toString('hex'));
console.log('monitorPubkeysHash :', monitorPubkeysHash.toString('hex'));
}
function createContract() {
const args = [monitorPubkeysHash, operatorPubkeysHash];
const contract = new Contract(artifact, args, provider);
return contract;
}
function printContractInfo(verbose: boolean) {
const contract = createContract();
if (verbose) {
console.log("redeemScriptHex:", contract.getRedeemScriptHex());
}
console.log('redeemScriptHash:', bitbox.Crypto.hash160(Buffer.from(contract.getRedeemScriptHex(), 'hex')).toString('hex'));
console.log('cashAddr:', contract.address);
if (verbose) {
console.log('oldAddr:', bitbox.Address.toLegacyAddress(contract.address));
}
}
async function listUTXOs() {
const contract = createContract();
const utxos = await contract.getUtxos();
const sum = utxos.reduce((partialSum, utxo) => partialSum + utxo.satoshis / 10**8, 0);
console.log('addr:', contract.address);
console.log('UTXOs:', utxos.length);
console.log('balance:', sum);
console.table(utxos);
}
async function redeemByUser(toAddr: string,
txIdVout: string,
txFee: number,
dryRun: boolean): Promise<void> {
console.log('redeemByUser...');
console.log('toAddr:', toAddr);
console.log('txIdVout:', txIdVout);
console.log('txFee:', txFee);
await redeemOrConvert(toAddr, txIdVout, txFee, '', '', dryRun);
}
async function convertByOperators(txIdVout: string,
txFee: number,
newCovenantAddr: string,
newOperatorPbukeysHash: string,
newMonitorPubkeysHash: string,
dryRun: boolean): Promise<void> {
console.log('convertByOperators...');
console.log('newCovenantAddr:', newCovenantAddr);
console.log('newOperatorPbukeysHash:', newOperatorPbukeysHash);
console.log('newMonitorPubkeysHash:', newMonitorPubkeysHash);
await redeemOrConvert(newCovenantAddr, txIdVout, txFee,
newOperatorPbukeysHash, newMonitorPubkeysHash, dryRun);
}
async function redeemOrConvert(toAddr: string,
txIdVout: string,
txFee: number,
newOperatorPbukeysHash: string,
newMonitorPubkeysHash: string,
dryRun: boolean): Promise<void> {
console.log('redeemOrConvert...');
const contract = createContract();
let utxos = await contract.getUtxos();
console.log('contract UTXOs:', utxos);
if (utxos.length == 0) {
console.log("no UTXOs !");
return;
}
const utxo = utxos.find(x => x.txid + ':' + x.vout == txIdVout);
if (!utxo) {
console.log("UTXO not found !");
return;
}
const amt = utxo.satoshis - txFee;
const operatorSigTmpls = operatorKeyPairs.slice(0, 7)
.map(p => new SignatureTemplate(p, HashType.SIGHASH_ALL, SignatureAlgorithm.ECDSA));
// console.log(operatorSigTmpls);
const txBuilder = await contract.functions
.redeemOrConvert(
...operatorSigTmpls,
...operatorPbks,
newMonitorPubkeysHash,
newOperatorPbukeysHash
)
.from([utxo])
.to(toAddr, amt)
.withHardcodedFee(txFee);
if (dryRun) {
const txHex = await txBuilder.build();
console.log('txHex:', txHex);
// const meepStr = await txBuilder.meep();
// console.log('meep:', meepStr);
} else {
const tx = await txBuilder.send();
console.log('transaction details:', stringify(tx));
}
}
async function convertByMonitors(txIdVout: string,
newCovenantAddr: string,
newOperatorPbukeysHash: string,
feeTxIdVout: string,
feeWif: string,
txFee: number,
dryRun: boolean): Promise<void> {
console.log('convertByMonitors...');
const contract = createContract();
let utxos = await contract.getUtxos();
console.log('contract UTXOs:', utxos);
if (utxos.length == 0) {
console.log("no cc-UTXOs !");
return;
}
const utxo = utxos.find(x => x.txid + ':' + x.vout == txIdVout);
if (!utxo) {
console.log("cc-UTXO not found !");
return;
}
const amt = utxo.satoshis;
const feeProviderPair = bitbox.ECPair.fromWIF(feeWif);
const feeProviderAddr = bitbox.ECPair.toCashAddress(feeProviderPair);
console.log('feeProviderAddr:', feeProviderAddr);
let feeUtxos = await provider.getUtxos(feeProviderAddr);
console.log('fee provider UTXOs:', feeUtxos)
if (feeUtxos.length == 0) {
console.log("no fee UTXOs !");
return;
}
const feeUtxo = feeUtxos.find(x => x.txid + ':' + x.vout == feeTxIdVout);
if (!feeUtxo) {
console.log("fee UTXO not found !");
return;
}
const changeAmt = feeUtxo.satoshis - txFee;
if (changeAmt < 0) {
console.log("not enough tx fee !");
return;
}
(feeUtxo as any).template = new SignatureTemplate(feeProviderPair);
const monotorSigTmpls = monitorKeyPairs.slice(0, 2)
.map(p => new SignatureTemplate(p, HashType.SIGHASH_ALL, SignatureAlgorithm.ECDSA));
// console.log(operatorSigTmpls);
const txBuilder = await contract.functions
.convertByMonitors(
...monotorSigTmpls,
...monitorPbks,
newOperatorPbukeysHash
)
.from([utxo, feeUtxo])
.to([{to: newCovenantAddr, amount: amt}])
.withHardcodedFee(txFee)
.withAge(34560);
if (changeAmt > 0) {
txBuilder.to(feeProviderAddr, changeAmt);
}
if (dryRun) {
const txHex = await txBuilder.build();
console.log('txHex:', txHex);
// const meepStr = await txBuilder.meep();
// console.log('meep:', meepStr);
} else {
const tx = await txBuilder.send();
console.log('transaction details:', stringify(tx));
}
}