-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_elements.js
More file actions
293 lines (262 loc) · 8.8 KB
/
example_elements.js
File metadata and controls
293 lines (262 loc) · 8.8 KB
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
const cfdjs = require('cfd-js-wasm');
cfdjs.addInitializedListener(async () => {
const cfdjsObj = cfdjs.getCfd();
const utxo = {txid: '0000000000000000000000000000000000000000000000000000000000001111', vout: 0};
const utxo2 = {txid: '0000000000000000000000000000000000000000000000000000000000002222', vout: 0};
const txdata = await cfdjsObj.ElementsCreateRawTransaction({
'version': 2,
'locktime': 0,
'txins': [{
'txid': utxo.txid,
'vout': utxo.vout,
'sequence': 4294967295,
}, {
'txid': utxo2.txid,
'vout': utxo2.vout,
'sequence': 4294967295,
}],
'txouts': [{
'address': 'AzptfproXqXSz52kFUDAN4WJe3nVkxyapTkVkmN67Jmw4isdLWXrXF4jcF3354LcaQ5ZsBrvRmvWUz73',
'asset': '6f1a4b6bd5571b5f08ab79c314dc6483f9b952af2f5ef206cd6f8e68eb1186f3',
'amount': 10000000,
}, {
'address': 'el1qqtl9a3n6878ex25u0wv8u5qlzpfkycc0cftk65t52pkauk55jqka0fajk8d80lafn4t9kqxe77cu9ez2dyr6sq54lwy009uex',
'asset': '6f1a4b6bd5571b5f08ab79c314dc6483f9b952af2f5ef206cd6f8e68eb1186f3',
'amount': 10000000,
}],
'fee': {
'amount': 500000,
'asset': '6f1a4b6bd5571b5f08ab79c314dc6483f9b952af2f5ef206cd6f8e68eb1186f3',
},
});
const blindTx = await cfdjsObj.BlindRawTransaction({
tx: txdata.hex,
txins: [{
txid: utxo.txid,
vout: utxo.vout,
asset: '6f1a4b6bd5571b5f08ab79c314dc6483f9b952af2f5ef206cd6f8e68eb1186f3',
amount: 10500000,
blindFactor: '0000000000000000000000000000000000000000000000000000000000000000',
assetBlindFactor: '0000000000000000000000000000000000000000000000000000000000000000',
}, {
txid: utxo2.txid,
vout: utxo2.vout,
asset: '6f1a4b6bd5571b5f08ab79c314dc6483f9b952af2f5ef206cd6f8e68eb1186f3',
amount: 10000000,
blindFactor: 'f87734c279533d8beba96c5369e169e6caf5f307a34d72d4a0f9c9a7b8f8f269',
assetBlindFactor: '28093061ab2e407c6015f8cb33f337ffb118eaf3beb2d254de64203aa27ecbf7',
},
],
txoutConfidentialAddresses: [
'AzptfproXqXSz52kFUDAN4WJe3nVkxyapTkVkmN67Jmw4isdLWXrXF4jcF3354LcaQ5ZsBrvRmvWUz73',
'el1qqtl9a3n6878ex25u0wv8u5qlzpfkycc0cftk65t52pkauk55jqka0fajk8d80lafn4t9kqxe77cu9ez2dyr6sq54lwy009uex',
],
});
// privkey sign (calc sighash + get ecSig + add Signature)
const signTx = await cfdjsObj.SignWithPrivkey({
tx: blindTx.hex,
isElements: true,
txin: {
txid: utxo.txid,
vout: utxo.vout,
privkey: 'cU4KjNUT7GjHm7CkjRjG46SzLrXHXoH3ekXmqa2jTCFPMkQ64sw1',
hashType: 'p2wpkh',
sighashType: 'all',
confidentialValueCommitment: '096c711e318b1b540f683aa3ed570c571889417483979e24403e47d8be7c2ac664',
amount: 10500000,
},
});
// multisig sign start
const multisigScript = '5221021266577a886f59271acc99d78a3371c04fbd0e1991cb52db6d74b090c96ed47d2103700c35bbe646ac4e80a02f5688d7fe36f944d7eeb7abe0490c272e9fd3e093ac52ae';
// ert1q7w0kyu46ddterr4sglzac38mgaf4dv8jfsf0egumry5yaqqq3fpqe89kmh
const sighash = await cfdjsObj.CreateElementsSignatureHash({
tx: blindTx.hex,
txin: {
'txid': utxo2.txid,
'vout': utxo2.vout,
'keyData': {
'hex': multisigScript,
'type': 'redeem_script',
},
'amount': 10000000,
'hashType': 'p2wsh',
},
});
const privkey1 = 'cQfvmeis4EWJ4EtLoz5dAs8XY44SCdZazyTnyPVgibMWcYZCSEne';
const privkey2 = 'cN3qiuWdpPWKsEjFxnkiNdex3nRYENzuieSH6MsYBCk6oyKdfdeo';
const sig1 = await cfdjsObj.CalculateEcSignature({
'sighash': sighash.sighash,
'privkeyData': {
'privkey': privkey1,
'network': 'regtest',
},
});
const sig2 = await cfdjsObj.CalculateEcSignature({
'sighash': sighash.sighash,
'privkeyData': {
'privkey': privkey2,
'network': 'regtest',
},
});
const multisigSign = await cfdjsObj.AddMultisigSign({
tx: signTx.hex,
isElements: true,
txin: {
txid: utxo2.txid,
vout: utxo2.vout,
signParams: [
{
hex: sig1.signature,
type: 'sign',
derEncode: true,
relatedPubkey: '021266577a886f59271acc99d78a3371c04fbd0e1991cb52db6d74b090c96ed47d',
},
{
hex: sig2.signature,
type: 'sign',
derEncode: true,
relatedPubkey: '03700c35bbe646ac4e80a02f5688d7fe36f944d7eeb7abe0490c272e9fd3e093ac',
},
],
witnessScript: multisigScript,
hashType: 'p2wsh',
},
});
// multisig sign end
console.log(multisigSign.hex);
console.log('-------------------------------------------------------------------');
console.log('-- large output tx --');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const networkType = 'liquidv1';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const mainchainNetworkType = 'mainnet';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const masterBlindingKey = '28054244faf0d4a04fc9dd3012443fc126c4a353f48d0277d3c57f69164adf87';
// dust laptop safe error tent soon fragile skill pear alley awkward vague stomach duck future
// xprv9s21ZrQH143K39sCCERa3w6NuVmYLMxHKH1PjEnuiaq2RB9iHhEwncTGpbx1WANWJZzFFbFdBi7BKECLg3HnFgajeRi5Go6YxD1K2nZtpDB/44h/1776h/1h
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const baseXpubkey = 'xpub6CADKiKYZrFrmFbPAQPSrzKMRohBHNmYM7GHNngAaaVHqNhC3apR1aNKJkigUjBDU7HwciQSRjeBK42vZUMZGNEjZkPjDWDawKVxTLGhNVE';
const minimumBits = 36;
// ----
const utxoTxHex = '';
const utxoTxid = '';
const utxoVout = 0;
const ctAddrList = [
];
const feeOutputNum = 49;
const txFeeAmount = 4476; // feerate: 0.148
const feeRate = 0.100;
const unblindData = await cfdjsObj.UnblindRawTransaction({
tx: utxoTxHex,
txouts: [{
index: 0,
blindingKey: '',
}],
});
console.log(unblindData);
const utxoData = unblindData.outputs[0];
const feeUtxo = {
txid: utxoTxid,
vout: utxoVout,
amount: utxoData.amount,
asset: utxoData.asset,
blindFactor: utxoData.blindFactor,
assetBlindFactor: utxoData.assetBlindFactor,
privkey: '',
};
// const feeAmount = parseInt((feeUtxo.amount - txFeeAmount) / (feeOutputNum + 1));
const feeAmount = 100000;
const changeAmount =
feeUtxo.amount - txFeeAmount - (feeAmount * feeOutputNum);
const isBlind = true;
const txoutList = [];
for (let i = 0; i <= feeOutputNum; ++i) {
const ctAddr = ctAddrList[i];
const amount = (i === feeOutputNum) ? changeAmount : feeAmount;
txoutList.push({
// address: ctAddr.confidentialAddress,
address: ctAddr,
asset: feeUtxo.asset,
amount: amount,
});
// ctAddrList.push(ctAddr.confidentialAddress);
}
const feeTxdata = await cfdjsObj.ElementsCreateRawTransaction({
version: 2,
locktime: 0,
txins: [{
txid: feeUtxo.txid,
vout: feeUtxo.vout,
sequence: 4294967295,
}],
txouts: txoutList,
fee: {
amount: txFeeAmount,
asset: feeUtxo.asset,
},
});
let blindTxHex = feeTxdata.hex;
if (isBlind) {
const basetx = await cfdjsObj.ElementsDecodeRawTransaction({
hex: feeTxdata.hex,
});
console.log(basetx);
const feeBlindTx = await cfdjsObj.BlindRawTransaction({
tx: feeTxdata.hex,
txins: [{
txid: feeUtxo.txid,
vout: feeUtxo.vout,
asset: feeUtxo.asset,
amount: feeUtxo.amount,
blindFactor: feeUtxo.blindFactor,
assetBlindFactor: feeUtxo.assetBlindFactor,
},
],
txoutConfidentialAddresses: ctAddrList,
minimumBits: minimumBits,
});
blindTxHex = feeBlindTx.hex;
}
const commitment = await cfdjsObj.GetCommitment({
amount: feeUtxo.amount,
asset: feeUtxo.asset,
assetBlindFactor: feeUtxo.assetBlindFactor,
blindFactor: feeUtxo.blindFactor,
});
// privkey sign (calc sighash + get ecSig + add Signature)
const feeSignTx = await cfdjsObj.SignWithPrivkey({
tx: blindTxHex,
isElements: true,
txin: {
txid: feeUtxo.txid,
vout: feeUtxo.vout,
privkey: feeUtxo.privkey,
hashType: 'p2wpkh',
sighashType: 'all',
confidentialValueCommitment: commitment.amountCommitment,
},
});
const dectx = await cfdjsObj.ElementsDecodeRawTransaction({
hex: feeSignTx.hex,
});
console.log(feeSignTx.hex);
console.log(`Amount:${feeAmount}, changeAmount:${changeAmount}`);
console.log(`vsize: ${dectx.vsize}`);
console.log(`dectx:`, dectx);
const estimateFeeResult = await cfdjsObj.EstimateFee({
selectUtxos: [{
txid: feeUtxo.txid,
vout: feeUtxo.vout,
amount: feeUtxo.amount,
asset: feeUtxo.asset,
descriptor: 'wpkh([e3c39d64/0\'/1\'/14\']02c7822c824258258d8d16b6fd25317b60b4374ca4f5ce1a69b810615e0c5497a8)',
}],
feeRate: feeRate,
tx: feeSignTx.hex,
isElements: true,
feeAsset: feeUtxo.asset,
isBlind: true,
minimumBits: minimumBits,
});
console.log(`EstimateFee:`, estimateFeeResult);
});