forked from eastonqiu/brc20tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
493 lines (438 loc) · 15.4 KB
/
main_test.go
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package main
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/minchenzz/brc20tool/pkg/btcapi"
"github.com/minchenzz/brc20tool/pkg/btcapi/mempool"
"github.com/pkg/errors"
"testing"
)
func Test_01(t *testing.T) {
recoverPubkey(t)
}
func Test_02(t *testing.T) {
privKey, err := btcec.NewPrivateKey()
if err != nil {
panic(err)
}
// Generate the tweaked public key using the x value as the
// script root.
tweakedPub := txscript.ComputeTaprootKeyNoScript(privKey.PubKey())
// Now we'll generate the corresponding tweaked private key.
tweakedPriv := txscript.TweakTaprootPrivKey(*privKey, []byte{})
// The public key for this private key should be the same as
// the tweaked public key we generate above.
b := tweakedPub.IsEqual(tweakedPriv.PubKey()) &&
bytes.Equal(
schnorr.SerializePubKey(tweakedPub),
schnorr.SerializePubKey(tweakedPriv.PubKey()),
)
fmt.Println("result", b)
}
func Test_genAddress(t *testing.T) {
mainnetParams := &chaincfg.MainNetParams
privKey, err := btcec.NewPrivateKey()
if err != nil {
panic(err)
}
privstr := hex.EncodeToString(privKey.Serialize())
fmt.Println("priv1:", privstr)
privbytes1, err := hex.DecodeString(privstr)
if err != nil {
panic(err)
}
_, pub1 := btcec.PrivKeyFromBytes(privbytes1)
pubKey := privKey.PubKey()
fmt.Println("pubkey is equal", pubKey.IsEqual(pub1))
tapKey := txscript.ComputeTaprootKeyNoScript(pubKey)
address, err := btcutil.NewAddressTaproot(
schnorr.SerializePubKey(tapKey),
mainnetParams,
)
fmt.Println(address)
}
func Test_03(t *testing.T) {
mainnetParams := &chaincfg.MainNetParams
str := "87d8848481ad30367f454d83e206da93864f4cedcf8050a9a31fcb000b37724a"
privbytes1, err := hex.DecodeString(str)
if err != nil {
panic(err)
}
privKey, pub1 := btcec.PrivKeyFromBytes(privbytes1)
pubKey := privKey.PubKey()
wifkey1, err := btcutil.NewWIF(privKey, mainnetParams, false)
if err != nil {
panic(err)
}
wifKeyStr := wifkey1.String()
fmt.Println("wifkey:", wifKeyStr)
wifkey2, err := btcutil.DecodeWIF(wifKeyStr)
if err != nil {
panic(err)
}
fmt.Println("wifkey1 equal wifkey2", bytes.Equal(wifkey1.SerializePubKey(), wifkey2.SerializePubKey()))
fmt.Println("pubkey is equal", pubKey.IsEqual(pub1))
tapKey := txscript.ComputeTaprootKeyNoScript(pubKey)
address, err := btcutil.NewAddressTaproot(
schnorr.SerializePubKey(tapKey),
mainnetParams,
)
fmt.Println(address)
}
func Test_recoverTWeakPubkey(t *testing.T) {
mainnetParams := &chaincfg.MainNetParams
privKey, err := btcec.NewPrivateKey()
if err != nil {
panic(err)
}
commitTxAddress, err := btcutil.NewAddressTaproot(schnorr.SerializePubKey(txscript.ComputeTaprootKeyNoScript(privKey.PubKey())), mainnetParams)
if err != nil {
panic(err)
}
pkScript, err := txscript.PayToAddrScript(commitTxAddress)
if err != nil {
panic(err)
}
// recover the raw pubkey from pkscript
pubkey1, err := computeTWeakPkOfByScript(pkScript)
if err != nil {
panic(err)
}
b0 := bytes.Equal(pubkey1, privKey.PubKey().SerializeCompressed())
b1 := bytes.Equal(pubkey1, privKey.PubKey().SerializeUncompressed())
pubKey0, err := schnorr.ParsePubKey(pubkey1)
if err != nil {
panic(err)
}
b2 := pubKey0.IsEqual(privKey.PubKey())
// change the privkey
tweakedPriv := txscript.TweakTaprootPrivKey(*privKey, []byte{})
b3 := pubKey0.IsEqual(tweakedPriv.PubKey())
b4 := bytes.Equal(
schnorr.SerializePubKey(pubKey0),
schnorr.SerializePubKey(tweakedPriv.PubKey()),
)
fmt.Println("b0:", b0, "b1:", b1, "b2:", b2, "b3:", b3, "b4:", b4)
}
func recoverPubkey(t *testing.T) {
//netParams := &chaincfg.MainNetParams
//btcApiClient := mempool.NewClient(netParams)
client := mempool.NewClient(&chaincfg.SigNetParams)
txId, _ := chainhash.NewHashFromStr("b752d80e97196582fd02303f76b4b886c222070323fb7ccd425f6c89f5445f6c")
transaction, err := client.GetRawTransaction(txId)
if err != nil {
t.Error(err)
} else {
t.Log(transaction.TxHash().String())
}
fmt.Println("tx has witness:", transaction.HasWitness())
for _, txin := range transaction.TxIn {
txOut, err := getTxOutByOutPoint(&txin.PreviousOutPoint, client)
if err != nil {
panic(err)
}
sclass := txscript.GetScriptClass(txOut.PkScript)
fmt.Println("Script Class", sclass)
pkscript2, err := txscript.ParsePkScript(txOut.PkScript)
if err != nil {
fmt.Println("333", err)
} else {
fmt.Println("pkscript2", pkscript2.String(), "len", len(pkscript2.Script()))
}
fmt.Println("===========================================")
if txscript.IsPayToTaproot(txOut.PkScript) {
fmt.Println("is pay TR", txscript.IsPayToTaproot(txOut.PkScript))
fmt.Println("key path", len(txin.Witness) == 1)
fmt.Println("script path", len(txin.Witness) >= 2)
} else {
fmt.Println("is pubkey ", txscript.IsPayToPubKey(txOut.PkScript))
pkscript, err := txscript.ComputePkScript(txin.SignatureScript, txin.Witness)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("pkscript1", pkscript.String(), "len", len(pkscript.Script()))
addrs, err := pkscript.Address(&chaincfg.SigNetParams)
if err != nil {
fmt.Println("222", err)
} else {
fmt.Println("Addresses01 :", addrs)
}
}
}
class, addresses, reqSigs, err := txscript.ExtractPkScriptAddrs(txOut.PkScript, &chaincfg.SigNetParams)
if err != nil {
panic(err)
}
fmt.Println("Script Class:", class)
fmt.Println("Addresses:", addresses)
fmt.Println("Required Signatures:", reqSigs)
}
}
func getTxOutByOutPoint(outPoint *wire.OutPoint, btcApiClient btcapi.BTCAPIClient) (*wire.TxOut, error) {
tx, err := btcApiClient.GetRawTransaction(&outPoint.Hash)
if err != nil {
return nil, err
}
if int(outPoint.Index) >= len(tx.TxOut) {
return nil, errors.New("err out point")
}
return tx.TxOut[outPoint.Index], nil
}
func getPreTx(tx *wire.MsgTx, btcApiClient btcapi.BTCAPIClient) (*wire.MsgTx, error) {
return nil, nil
}
func maybeGetPkFromTx(tx *wire.MsgTx, btcApiClient btcapi.BTCAPIClient) (*btcec.PublicKey, error) {
firstTxIn := tx.TxIn[0]
txOut0, err := getTxOutByOutPoint(&firstTxIn.PreviousOutPoint, btcApiClient)
if err != nil {
fmt.Println("cann't get the pre-txout from the OutPoint", firstTxIn.PreviousOutPoint.String())
return nil, err
}
// not support multisign
if b, e := txscript.IsMultisigScript(txOut0.PkScript); e == nil {
if b {
fmt.Println("not support multisign, OutPoint", firstTxIn.PreviousOutPoint.String())
return nil, errors.New("not support multisign")
}
}
if txscript.IsMultisigSigScript(txOut0.PkScript) {
fmt.Println("not support multisign, OutPoint", firstTxIn.PreviousOutPoint.String())
return nil, errors.New("not support multisign")
}
if txscript.IsPayToPubKey(txOut0.PkScript) {
pubKey, err := btcec.ParsePubKey(extractPubKey(txOut0.PkScript))
if err != nil {
fmt.Println("ParsePubKey error", err, "extractPk:", extractPubKey(txOut0.PkScript))
return nil, err
}
return pubKey, nil
}
if txscript.IsPayToPubKeyHash(txOut0.PkScript) {
pubkey, err := ComputePkByScript(firstTxIn.SignatureScript, firstTxIn.Witness)
if err != nil {
fmt.Println("P2PKH: ComputePkByScript error", err, "extractPk:", extractPubKey(txOut0.PkScript))
return nil, err
}
pubKey, err := btcec.ParsePubKey(pubkey)
if err != nil {
fmt.Println("P2PKH: ParsePubKey error", err, "pubkey:", hex.EncodeToString(pubkey))
return nil, err
}
return pubKey, nil
}
if txscript.IsPayToScriptHash(txOut0.PkScript) {
pubkey, err := ComputePkByScript(firstTxIn.SignatureScript, firstTxIn.Witness)
if err != nil {
fmt.Println("P2SH:ComputePkByScript error", err, "extractPk:", extractPubKey(txOut0.PkScript))
return nil, err
}
pubKey, err := btcec.ParsePubKey(pubkey)
if err != nil {
fmt.Println("P2SH: ParsePubKey error", err, "pubkey:", hex.EncodeToString(pubkey))
return nil, err
}
return pubKey, nil
}
if txscript.IsPayToWitnessPubKeyHash(txOut0.PkScript) {
pubkey, err := ComputePkByScript(firstTxIn.SignatureScript, firstTxIn.Witness)
if err != nil {
fmt.Println("P2WPKH:ComputePkByScript error", err, "extractPk:", extractPubKey(txOut0.PkScript))
return nil, err
}
pubKey, err := btcec.ParsePubKey(pubkey)
if err != nil {
fmt.Println("P2WPKH: ParsePubKey error", err, "pubkey:", hex.EncodeToString(pubkey))
return nil, err
}
return pubKey, nil
}
if txscript.IsPayToWitnessScriptHash(txOut0.PkScript) {
pubkey, err := ComputePkByScript(firstTxIn.SignatureScript, firstTxIn.Witness)
if err != nil {
fmt.Println("P2WSH:ComputePkByScript error", err, "extractPk:", extractPubKey(txOut0.PkScript))
return nil, err
}
pubKey, err := btcec.ParsePubKey(pubkey)
if err != nil {
fmt.Println("P2WSH: ParsePubKey error", err, "pubkey:", hex.EncodeToString(pubkey))
return nil, err
}
return pubKey, nil
}
if txscript.IsPayToTaproot(txOut0.PkScript) {
}
return nil, nil
}
////////////////////////////////////////////////////////////////////////////////
// extractCompressedPubKey extracts a compressed public key from the passed
// script if it is a standard pay-to-compressed-secp256k1-pubkey script. It
// will return nil otherwise.
func extractCompressedPubKey(script []byte) []byte {
// A pay-to-compressed-pubkey script is of the form:
// OP_DATA_33 <33-byte compressed pubkey> OP_CHECKSIG
// All compressed secp256k1 public keys must start with 0x02 or 0x03.
if len(script) == 35 &&
script[34] == txscript.OP_CHECKSIG &&
script[0] == txscript.OP_DATA_33 &&
(script[1] == 0x02 || script[1] == 0x03) {
return script[1:34]
}
return nil
}
// extractUncompressedPubKey extracts an uncompressed public key from the
// passed script if it is a standard pay-to-uncompressed-secp256k1-pubkey
// script. It will return nil otherwise.
func extractUncompressedPubKey(script []byte) []byte {
// A pay-to-uncompressed-pubkey script is of the form:
// OP_DATA_65 <65-byte uncompressed pubkey> OP_CHECKSIG
//
// All non-hybrid uncompressed secp256k1 public keys must start with 0x04.
// Hybrid uncompressed secp256k1 public keys start with 0x06 or 0x07:
// - 0x06 => hybrid format for even Y coords
// - 0x07 => hybrid format for odd Y coords
if len(script) == 67 &&
script[66] == txscript.OP_CHECKSIG &&
script[0] == txscript.OP_DATA_65 &&
(script[1] == 0x04 || script[1] == 0x06 || script[1] == 0x07) {
return script[1:66]
}
return nil
}
// extractPubKey extracts either compressed or uncompressed public key from the
// passed script if it is a either a standard pay-to-compressed-secp256k1-pubkey
// or pay-to-uncompressed-secp256k1-pubkey script, respectively. It will return
// nil otherwise.
func extractPubKey(script []byte) []byte {
if pubKey := extractCompressedPubKey(script); pubKey != nil {
return pubKey
}
return extractUncompressedPubKey(script)
}
// ComputePkScript computes the PK of an output by looking at the spending
// input's signature script or witness.
//
// NOTE: Only P2PKH, P2SH, P2WSH, and P2WPKH redeem scripts are supported.
func ComputePkByScript(sigScript []byte, witness wire.TxWitness) ([]byte, error) {
switch {
case len(sigScript) > 0:
return computeNonWitnessPkScript(sigScript)
case len(witness) > 0:
return computeWitnessPkScript(witness)
default:
return nil, txscript.ErrUnsupportedScriptType
}
}
// minPubKeyHashSigScriptLen is the minimum length of a signature script
// that spends a P2PKH output. The length is composed of the following:
// Signature length (1 byte)
// Signature (min 8 bytes)
// Signature hash type (1 byte)
// Public key length (1 byte)
// Public key (33 byte)
// minPubKeyHashSigScriptLen = 1 + ecdsa.MinSigLen + 1 + 1 + 33
// maxPubKeyHashSigScriptLen is the maximum length of a signature script
// that spends a P2PKH output. The length is composed of the following:
// Signature length (1 byte)
// Signature (max 72 bytes)
// Signature hash type (1 byte)
// Public key length (1 byte)
// Public key (33 byte)
// maxPubKeyHashSigScriptLen = 1 + 72 + 1 + 1 + 33
var (
minPubKeyHashSigScriptLen = 1 + ecdsa.MinSigLen + 1 + 1 + 33
maxPubKeyHashSigScriptLen = 1 + 72 + 1 + 1 + 33
// compressedPubKeyLen is the length in bytes of a compressed public
// key.
compressedPubKeyLen = 33
// witnessV1TaprootLen is the length of a P2TR script.
witnessV1TaprootLen = 34
)
// computeNonWitnessPkScript computes the PK of an output by looking at the
// spending input's signature script.
func computeNonWitnessPkScript(sigScript []byte) ([]byte, error) {
switch {
// Since we only support P2PKH and P2SH scripts as the only non-witness
// script types, we should expect to see a push only script.
case !txscript.IsPushOnlyScript(sigScript):
return nil, txscript.ErrUnsupportedScriptType
// If a signature script is provided with a length long enough to
// represent a P2PKH script, then we'll attempt to parse the compressed
// public key from it.
case len(sigScript) >= minPubKeyHashSigScriptLen &&
len(sigScript) <= maxPubKeyHashSigScriptLen:
// The public key should be found as the last part of the
// signature script. We'll attempt to parse it to ensure this is
// a P2PKH redeem script.
pubKey := sigScript[len(sigScript)-compressedPubKeyLen:]
if btcec.IsCompressedPubKey(pubKey) {
return pubKey, nil
}
fallthrough
// If we failed to parse a compressed public key from the script in the
// case above, or if the script length is not that of a P2PKH one, we
// can assume it's a P2SH signature script.
default:
// The redeem script will always be the last data push of the
// signature script, so we'll parse the script into opcodes to
// obtain it.
const scriptVersion = 0
//err := checkScriptParses(scriptVersion, sigScript)
//if err != nil {
// return nil, err
//}
//redeemScript := finalOpcodeData(scriptVersion, sigScript)
//
//scriptHash := hash160(redeemScript)
//script, err := payToScriptHashScript(scriptHash)
//if err != nil {
// return nil, err
//}
return nil, txscript.ErrUnsupportedScriptType
}
}
// computeWitnessPkScript computes the PK of an output by looking at the
// spending input's witness.
func computeWitnessPkScript(witness wire.TxWitness) ([]byte, error) {
// We'll use the last item of the witness stack to determine the proper
// witness type.
lastWitnessItem := witness[len(witness)-1]
switch {
// If the witness stack has a size of 2 and its last item is a
// compressed public key, then this is a P2WPKH witness.
case len(witness) == 2 && len(lastWitnessItem) == compressedPubKeyLen:
pubkey := lastWitnessItem
return pubkey, nil
// For any other witnesses, we'll assume it's a P2WSH witness.
default:
//scriptHash := sha256.Sum256(lastWitnessItem)
//script, err := payToWitnessScriptHashScript(scriptHash[:])
//if err != nil {
// return nil, err
//}
return nil, txscript.ErrUnsupportedScriptType
}
}
func computePkByP2TR(witness wire.TxWitness) ([]byte, error) {
return nil, nil
}
// computeTWeakPkOfByScript extracts the raw public key bytes script if it is
// standard pay-to-witness-script-hash v1 script.
func computeTWeakPkOfByScript(script []byte) ([]byte, error) {
// A pay-to-witness-script-hash script is of the form:
// OP_1 OP_DATA_32 <32-byte-hash>
if len(script) == witnessV1TaprootLen &&
script[0] == txscript.OP_1 &&
script[1] == txscript.OP_DATA_32 {
return script[2:34], nil
}
return nil, txscript.ErrUnsupportedScriptType
}