Skip to content

Commit

Permalink
change: differentiate ECDSA and schnoor Pubkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
0xB10C committed Jul 5, 2021
1 parent ab4d700 commit b145281
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
12 changes: 6 additions & 6 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (in *Input) SpendsP2WPKH() bool {
}

secondWitnessElement := in.Witness[1]
if !secondWitnessElement.IsPubKey() {
if !secondWitnessElement.IsECDSAPubKey() {
return false
}

Expand Down Expand Up @@ -341,7 +341,7 @@ func (in *Input) SpendsNestedP2WSH() bool {
func (in *Input) SpendsP2PKH() (spendsP2PKH bool) {
pbs := in.ScriptSig.Parse()
if !in.HasWitness() && len(pbs) == 2 {
return pbs[0].IsECDSASignature(false) && pbs[1].IsPubKey()
return pbs[0].IsECDSASignature(false) && pbs[1].IsECDSAPubKey()
}
return false
}
Expand All @@ -352,7 +352,7 @@ func (in *Input) SpendsP2PKH() (spendsP2PKH bool) {
func (in *Input) SpendsP2PKHWithIsCompressed() (spendsP2PKH bool, isCompressedPubKey bool) {
pbs := in.ScriptSig.Parse()
if !in.HasWitness() && len(pbs) == 2 {
isPubKey, isCompressedPubKey := pbs[1].IsPubKeyWithIsCompressed()
isPubKey, isCompressedPubKey := pbs[1].IsECDSAPubKeyWithIsCompressed()
return pbs[0].IsECDSASignature(false) && isPubKey, isCompressedPubKey
}
return false, false
Expand Down Expand Up @@ -380,7 +380,7 @@ func (in *Input) SpendsP2SH() (spendsP2SH bool) {
pbs := in.ScriptSig.Parse()
if !in.HasWitness() && len(pbs) > 0 {
redeemScript := pbs[len(pbs)-1]
return !redeemScript.IsECDSASignature(false) && !redeemScript.IsPubKey() // is not a signature and is not a pubkey
return !redeemScript.IsECDSASignature(false) && !redeemScript.IsECDSAPubKey() // is not a signature and is not a pubkey
}
return false
}
Expand Down Expand Up @@ -461,8 +461,8 @@ func (in *Input) IsLNUniliteralClosing() bool {
redeemScript := in.GetP2WSHRedeemScript()
pbs := redeemScript.Parse()
if len(pbs) == 9 {
if pbs[0].OpCode == OpIF && pbs[1].IsPubKey() &&
pbs[2].OpCode == OpELSE && pbs[4].OpCode == OpCHECKSEQUENCEVERIFY && pbs[5].OpCode == OpDROP && pbs[6].IsPubKey() &&
if pbs[0].OpCode == OpIF && pbs[1].IsECDSAPubKey() &&
pbs[2].OpCode == OpELSE && pbs[4].OpCode == OpCHECKSEQUENCEVERIFY && pbs[5].OpCode == OpDROP && pbs[6].IsECDSAPubKey() &&
pbs[7].OpCode == OpENDIF &&
pbs[8].OpCode == OpCHECKSIG {
return true
Expand Down
2 changes: 1 addition & 1 deletion output.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (out *Output) IsP2MSOutput() (is bool, m int, n int) {
func (out *Output) IsP2PKOutput() bool {
pbs := out.ScriptPubKey.Parse()
if len(pbs) == 2 {
if pbs[0].IsPubKey() && pbs[1].OpCode == OpCHECKSIG {
if pbs[0].IsECDSAPubKey() && pbs[1].OpCode == OpCHECKSIG {
return true
}
}
Expand Down
26 changes: 13 additions & 13 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,35 +220,35 @@ func (poc ParsedOpCode) GetSigHash() (sighash byte) {
return 0x00
}

// IsPubKeyWithIsCompressed checks a two byte slices if they could represent a pubkey and if that pubkey is compressed.
func (poc ParsedOpCode) IsPubKeyWithIsCompressed() (isPubKey bool, isCompressed bool) {
compressed := poc.IsCompressedPubKey()
uncompressed := poc.IsUncompressedPubKey()
// IsECDSAPubKeyWithIsCompressed checks a two byte slices if they could represent a pubkey and if that pubkey is compressed.
func (poc ParsedOpCode) IsECDSAPubKeyWithIsCompressed() (isPubKey bool, isCompressed bool) {
compressed := poc.IsCompressedECDSAPubKey()
uncompressed := poc.IsUncompressedECDSAPubKey()
return (compressed || uncompressed), compressed
}

// IsPubKey checks a two byte slices if they could represent a public key.
func (poc ParsedOpCode) IsPubKey() bool {
if poc.IsCompressedPubKey() { // check compressed pubKeys first because they are more common
// IsECDSAPubKey checks a two byte slices if they could represent a public key.
func (poc ParsedOpCode) IsECDSAPubKey() bool {
if poc.IsCompressedECDSAPubKey() { // check compressed pubKeys first because they are more common
return true
} else if poc.IsUncompressedPubKey() {
} else if poc.IsUncompressedECDSAPubKey() {
return true
}
return false
}

// IsCompressedPubKey checks a two byte slices if they could represent a
// IsCompressedECDSAPubKey checks a two byte slices if they could represent a
// compressed public key. <pubkey length> <|pubkey|>
func (poc ParsedOpCode) IsCompressedPubKey() bool {
func (poc ParsedOpCode) IsCompressedECDSAPubKey() bool {
if poc.OpCode == OpDATA33 && len(poc.PushedData) == 33 {
return (poc.PushedData[0] == 0x02 || poc.PushedData[0] == 0x03)
}
return false
}

// IsUncompressedPubKey checks a two byte slices if they could represent a
// IsUncompressedECDSAPubKey checks a two byte slices if they could represent a
// uncompressed public key. <pubkey length> <|pubkey|>
func (poc ParsedOpCode) IsUncompressedPubKey() bool {
func (poc ParsedOpCode) IsUncompressedECDSAPubKey() bool {
if poc.OpCode == OpDATA65 && len(poc.PushedData) == 65 {
return poc.PushedData[0] == 0x04
}
Expand All @@ -275,7 +275,7 @@ func (s BitcoinScript) IsMultisigScript() (isMultisig bool, numRequiredSigs int,

// check that the next `numPossiblePubKeys` are pubKeys
for i := 1; i < numPossiblePubKeys; i++ {
if !parsed[pLength-2-i].IsPubKey() {
if !parsed[pLength-2-i].IsECDSAPubKey() {
return false, 0, 0
}
}
Expand Down
16 changes: 8 additions & 8 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ func TestIsMultisigScript(t *testing.T) {
func TestStrictlyDEREncodedECDSASig(t *testing.T) {
ECDSASigStrictlyDER := ParsedOpCode{OpCode: OpDATA71, PushedData: []byte{0x30, 0x44, 0x02, 0x20, 0x3c, 0x02, 0xbd, 0x6f, 0x63, 0xe4, 0x79, 0xc7, 0xc6, 0xd1, 0xdc, 0x7d, 0x94, 0x3b, 0x58, 0x4b, 0xa2, 0x03, 0xc6, 0xf1, 0x50, 0x37, 0x9c, 0x78, 0x9f, 0x84, 0xf8, 0xa5, 0xf3, 0x3c, 0x95, 0x12, 0x02, 0x20, 0x1c, 0xcf, 0x01, 0xbb, 0xeb, 0x2c, 0x5f, 0x68, 0xb1, 0x78, 0xab, 0x96, 0xa1, 0xa5, 0x64, 0xaf, 0x66, 0x09, 0xf7, 0x33, 0x87, 0xb9, 0x35, 0x5a, 0x62, 0x65, 0x54, 0x48, 0xb6, 0xa2, 0x7a, 0x43, 0x01}}

if ECDSASigStrictlyDER.IsCompressedPubKey() {
if ECDSASigStrictlyDER.IsCompressedECDSAPubKey() {
t.Errorf("A DER encoded signature should not be recognized as compressed pubkey")
}

if ECDSASigStrictlyDER.IsUncompressedPubKey() {
if ECDSASigStrictlyDER.IsUncompressedECDSAPubKey() {
t.Errorf("A DER encoded signature should not be recognized as uncompressed pubkey")
}

Expand All @@ -139,10 +139,10 @@ func TestGarbageSignature(t *testing.T) {
tooSmallOpCodeSig := ParsedOpCode{OpCode: OpDATA6, PushedData: []byte{0x10}}
emptyPushDataSig := ParsedOpCode{OpCode: OpDATA33, PushedData: []byte{}}

if tooBigOpCodeSig.IsCompressedPubKey() {
if tooBigOpCodeSig.IsCompressedECDSAPubKey() {
t.Errorf("A garbage signature should not be recognized as compressed pubkey")
}
if tooBigOpCodeSig.IsUncompressedPubKey() {
if tooBigOpCodeSig.IsUncompressedECDSAPubKey() {
t.Errorf("A garbage signature should not be recognized as uncompressed pubkey")
}
if tooBigOpCodeSig.IsSignature() {
Expand All @@ -158,10 +158,10 @@ func TestGarbageSignature(t *testing.T) {
t.Errorf("The SigHash of the garbage signature should be invalid (0x00)")
}

if tooSmallOpCodeSig.IsCompressedPubKey() {
if tooSmallOpCodeSig.IsCompressedECDSAPubKey() {
t.Errorf("A garbage signature should not be recognized as compressed pubkey")
}
if tooSmallOpCodeSig.IsUncompressedPubKey() {
if tooSmallOpCodeSig.IsUncompressedECDSAPubKey() {
t.Errorf("A garbage signature should not be recognized as uncompressed pubkey")
}
if tooSmallOpCodeSig.IsSignature() {
Expand All @@ -177,10 +177,10 @@ func TestGarbageSignature(t *testing.T) {
t.Errorf("The SigHash of the garbage signature should be invalid (0x00)")
}

if emptyPushDataSig.IsCompressedPubKey() {
if emptyPushDataSig.IsCompressedECDSAPubKey() {
t.Errorf("A garbage signature should not be recognized as compressed pubkey")
}
if emptyPushDataSig.IsUncompressedPubKey() {
if emptyPushDataSig.IsUncompressedECDSAPubKey() {
t.Errorf("A garbage signature should not be recognized as uncompressed pubkey")
}
if emptyPushDataSig.IsSignature() {
Expand Down
12 changes: 6 additions & 6 deletions txstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (input *Input) InputStats() *InputStats {
if opCode.IsSignature() {
ss := opCode.SignatureStats()
inputStats.SigStats = append(inputStats.SigStats, ss)
} else if opCode.IsPubKey() {
} else if opCode.IsECDSAPubKey() {
pks := opCode.PubKeyStats()
inputStats.PubKeyStats = append(inputStats.PubKeyStats, pks)
} else if opCode.OpCode.IsDataPushOpCode() {
Expand All @@ -136,7 +136,7 @@ func (input *Input) InputStats() *InputStats {
if inScriptOpcode.IsSignature() {
ss := inScriptOpcode.SignatureStats()
inputStats.SigStats = append(inputStats.SigStats, ss)
} else if inScriptOpcode.IsPubKey() {
} else if inScriptOpcode.IsECDSAPubKey() {
pks := inScriptOpcode.PubKeyStats()
inputStats.PubKeyStats = append(inputStats.PubKeyStats, pks)
}
Expand All @@ -151,7 +151,7 @@ func (input *Input) InputStats() *InputStats {
if witnessElement.IsSignature() {
ss := witnessElement.SignatureStats()
inputStats.SigStats = append(inputStats.SigStats, ss)
} else if witnessElement.IsPubKey() {
} else if witnessElement.IsECDSAPubKey() {
pks := witnessElement.PubKeyStats()
inputStats.PubKeyStats = append(inputStats.PubKeyStats, pks)
} else if witnessElement.OpCode.IsDataPushOpCode() {
Expand All @@ -160,7 +160,7 @@ func (input *Input) InputStats() *InputStats {
if witnessOpCode.IsSignature() {
ss := witnessOpCode.SignatureStats()
inputStats.SigStats = append(inputStats.SigStats, ss)
} else if witnessOpCode.IsPubKey() {
} else if witnessOpCode.IsECDSAPubKey() {
pks := witnessOpCode.PubKeyStats()
inputStats.PubKeyStats = append(inputStats.PubKeyStats, pks)
}
Expand Down Expand Up @@ -211,7 +211,7 @@ type PubKeyStats struct {
// The caller must make sure that the *ParsedOpCode is a pubkey.
func (sigOpCode *ParsedOpCode) PubKeyStats() *PubKeyStats {
pkStats := &PubKeyStats{}
pkStats.IsCompressed = sigOpCode.IsCompressedPubKey()
pkStats.IsCompressed = sigOpCode.IsCompressedECDSAPubKey()
return pkStats
}

Expand Down Expand Up @@ -240,7 +240,7 @@ func (out *Output) OutputStats() *OutputStats {
outStats.OpCodes = make([]OpCode, 0)
parsedScriptPubKey := out.ScriptPubKey.Parse()
for _, opCode := range parsedScriptPubKey {
if opCode.IsPubKey() {
if opCode.IsECDSAPubKey() {
pks := opCode.PubKeyStats()
outStats.PubKeyStats = append(outStats.PubKeyStats, pks)
}
Expand Down

0 comments on commit b145281

Please sign in to comment.