Skip to content

Commit 30d61b8

Browse files
authored
Merge pull request #97 from kcalvinalvin/2023-12-26-prevent-witness-empty-panic
txscript: add check to see if witness is empty
2 parents 56725df + 4b63e17 commit 30d61b8

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

txscript/standard.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,9 @@ func ReconstructScript(sigScript []byte, witness wire.TxWitness, class ScriptCla
11311131
return nil, err
11321132
}
11331133
case WitnessV0PubKeyHashTy:
1134+
if len(witness) == 0 {
1135+
return nil, fmt.Errorf("unable to reconstruct script due to missing witness: %v", witness)
1136+
}
11341137
last := witness[len(witness)-1]
11351138
hash := hash160(last)
11361139

@@ -1139,6 +1142,9 @@ func ReconstructScript(sigScript []byte, witness wire.TxWitness, class ScriptCla
11391142
return nil, err
11401143
}
11411144
case WitnessV0ScriptHashTy:
1145+
if len(witness) == 0 {
1146+
return nil, fmt.Errorf("unable to reconstruct script due to missing witness: %v", witness)
1147+
}
11421148
last := witness[len(witness)-1]
11431149
hash := chainhash.HashB(last)
11441150

txscript/standard_test.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,7 @@ func TestReconstructScript(t *testing.T) {
12571257
pkScript string
12581258
witness []string
12591259
class ScriptClass
1260+
expectErr bool
12601261
}{
12611262
{
12621263
// 8ea059a12212dd6d2ca3a87b9550bbaa290211679a6be990e9ed8152094c987d:0 on testnet3
@@ -1271,7 +1272,8 @@ func TestReconstructScript(t *testing.T) {
12711272
"DATA_33 0x037b80492279732766f5960bd3" +
12721273
"8b18a2ae7089a1efdaf188d2a536bffd3430c574 " +
12731274
"OP_DUP OP_DROP",
1274-
class: PubKeyHashTy,
1275+
class: PubKeyHashTy,
1276+
expectErr: false,
12751277
},
12761278
{
12771279
// 37096812a90af1a8a3267d3f9d75111a25e2a8c5411770998e0ee5f75599c9cc:1 on mainnet
@@ -1287,7 +1289,8 @@ func TestReconstructScript(t *testing.T) {
12871289
"03f7fda7194f762c315362487d2f65f3807d0cd34e" +
12881290
"285bab7c2a5db2155911ece3",
12891291
},
1290-
class: ScriptHashTy,
1292+
class: ScriptHashTy,
1293+
expectErr: false,
12911294
},
12921295
{
12931296
// 8127acdb257b70c74857be882e869e80f081f460f70dc8b6490cd32ecc14c55f:1 on mainnet
@@ -1301,7 +1304,8 @@ func TestReconstructScript(t *testing.T) {
13011304
"c1aed639fdfd818ac7028259"},
13021305
pkScript: "0 DATA_20 " +
13031306
"0x8d6046312c5c9d4bf42dc53e1b5a668a23d573eb",
1304-
class: WitnessV0PubKeyHashTy,
1307+
class: WitnessV0PubKeyHashTy,
1308+
expectErr: false,
13051309
},
13061310
{
13071311
// b714a52258e3034e532a188433a4506ca806cf1885d21034fb26906cc4a1c3c3:1 on mainnet
@@ -1324,7 +1328,18 @@ func TestReconstructScript(t *testing.T) {
13241328
"6d495bfdd5ba4145e3e046fee45e84a8a4" +
13251329
"8ad05bd8dbb395c011a32cf9f88053ae",
13261330
},
1327-
class: WitnessV0ScriptHashTy,
1331+
class: WitnessV0ScriptHashTy,
1332+
expectErr: false,
1333+
},
1334+
{
1335+
// b714a52258e3034e532a188433a4506ca806cf1885d21034fb26906cc4a1c3c3:1 on mainnet
1336+
// but missing witness.
1337+
name: "p2wsh",
1338+
pkScript: "0 DATA_32 0x701a8d401c84fb13e6baf169d5968" +
1339+
"4e17abd9fa216c8cc5b9fc63d622ff8c58d",
1340+
witness: []string{},
1341+
class: WitnessV0ScriptHashTy,
1342+
expectErr: true,
13281343
},
13291344
{
13301345
// 4cd658ba8b5f10f2544becf4f1756d5e929d72f0a7372a120cc2affc16679e8d:0 on mainnet
@@ -1337,7 +1352,8 @@ func TestReconstructScript(t *testing.T) {
13371352
"f28ed45df7fb4d9f71764c9aaad3e46f4c36921934" +
13381353
"cc6022100ef76bcd57a871129836d7fe3b898e97bf" +
13391354
"6a241e7d81c2ae3b026567b9246c4dc01",
1340-
class: PubKeyTy,
1355+
class: PubKeyTy,
1356+
expectErr: false,
13411357
},
13421358
}
13431359

@@ -1347,20 +1363,25 @@ func TestReconstructScript(t *testing.T) {
13471363

13481364
var witness wire.TxWitness
13491365

1350-
for _, witElement := range test.witness {
1351-
wit, err := hex.DecodeString(witElement)
1352-
if err != nil {
1353-
t.Fatalf("%s: unable to decode witness "+
1354-
"element: %v", test.name, err)
1355-
}
1366+
if !test.expectErr {
1367+
for _, witElement := range test.witness {
1368+
wit, err := hex.DecodeString(witElement)
1369+
if err != nil {
1370+
t.Fatalf("%s: unable to decode witness "+
1371+
"element: %v", test.name, err)
1372+
}
13561373

1357-
witness = append(witness, wit)
1374+
witness = append(witness, wit)
1375+
}
13581376
}
13591377

13601378
reconScript, err := ReconstructScript(sigScript, witness, test.class)
13611379
if err != nil {
1362-
t.Errorf("%s: got unexpected error - %v",
1363-
test.name, err)
1380+
if !test.expectErr {
1381+
t.Errorf("%s: got unexpected error - %v",
1382+
test.name, err)
1383+
}
1384+
return
13641385
}
13651386

13661387
if (test.class == PubKeyHashTy) ||

0 commit comments

Comments
 (0)