Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pubkey compression/decompression in TxIns protobuf encoding/decoding #2171

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestUTXOTransactionEncode(t *testing.T) {
in := TxIn{
PreviousOutPoint: *NewOutPoint(&common.Hash{},
MaxOutputIndex),
PubKey: []byte{0x04, 0x50, 0x49, 0x5c, 0xb2, 0xf9, 0x53, 0x5c, 0x68, 0x4e, 0xbe, 0x46, 0x87, 0xb5, 0x01, 0xc0, 0xd4, 0x1a, 0x62, 0x3d, 0x68, 0xc1, 0x18, 0xb8, 0xdc, 0xec, 0xd3, 0x93, 0x37, 0x0f, 0x1d, 0x90, 0xe6, 0x5c, 0x4c, 0x6c, 0x44, 0xcd, 0x3f, 0xe8, 0x09, 0xb4, 0x1d, 0xfa, 0xc9, 0x06, 0x0a, 0xd8, 0x4c, 0xb5, 0x7e, 0x2d, 0x57, 0x5f, 0xad, 0x24, 0xd2, 0x5a, 0x7e, 0xfa, 0x33, 0x96, 0xe7, 0x3c, 0x10},
}

newOut := TxOut{
Expand Down Expand Up @@ -776,7 +777,7 @@ func qiTxData() (*Transaction, common.Hash) {
outpoint := NewOutPoint(&txHash, 1)
inner := &QiTx{
ChainID: new(big.Int).SetUint64(1),
TxIn: TxIns{*NewTxIn(outpoint, []byte{0x01}, [][]byte{{0x01}})},
TxIn: TxIns{*NewTxIn(outpoint, []byte{0x04, 0x50, 0x49, 0x5c, 0xb2, 0xf9, 0x53, 0x5c, 0x68, 0x4e, 0xbe, 0x46, 0x87, 0xb5, 0x01, 0xc0, 0xd4, 0x1a, 0x62, 0x3d, 0x68, 0xc1, 0x18, 0xb8, 0xdc, 0xec, 0xd3, 0x93, 0x37, 0x0f, 0x1d, 0x90, 0xe6, 0x5c, 0x4c, 0x6c, 0x44, 0xcd, 0x3f, 0xe8, 0x09, 0xb4, 0x1d, 0xfa, 0xc9, 0x06, 0x0a, 0xd8, 0x4c, 0xb5, 0x7e, 0x2d, 0x57, 0x5f, 0xad, 0x24, 0xd2, 0x5a, 0x7e, 0xfa, 0x33, 0x96, 0xe7, 0x3c, 0x10}, [][]byte{{0x01}})},
TxOut: TxOuts{*NewTxOut(1, []byte{0x2}, big.NewInt(1))},
ParentHash: &parentHash,
MixHash: &mixHash,
Expand All @@ -788,7 +789,7 @@ func qiTxData() (*Transaction, common.Hash) {

func TestQiTxHash(t *testing.T) {
_, hash := qiTxData()
correctHash := common.HexToHash("0x20f420e372c75c4a43555f02ae79b9c5edaeab288af2b0b8086150b3a9a3e3f0")
correctHash := common.HexToHash("0x20b720e4860b3db006d7a19fed6be3efe5619f53f499ef561f42c46bc12b555d")
require.Equal(t, hash, correctHash, "Hash not equal to expected hash")
}

Expand Down
50 changes: 48 additions & 2 deletions core/types/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/crypto"
"github.com/dominant-strategies/go-quai/params"
)

Expand Down Expand Up @@ -84,7 +85,13 @@ func (txIn TxIn) ProtoEncode() (*ProtoTxIn, error) {
return nil, err
}

protoTxIn.PubKey = txIn.PubKey
// Compress the public key if it's uncompressed
compressedPubKey, err := compressPubKeyIfNeeded(txIn.PubKey)
if err != nil {
return nil, err
}
protoTxIn.PubKey = compressedPubKey

return protoTxIn, nil
}

Expand All @@ -93,10 +100,49 @@ func (txIn *TxIn) ProtoDecode(protoTxIn *ProtoTxIn) error {
if err != nil {
return err
}
txIn.PubKey = protoTxIn.PubKey

// Decompress the public key if it's compressed
pubKey, err := decompressPubKeyIfNeeded(protoTxIn.PubKey)
if err != nil {
return err
}
txIn.PubKey = pubKey

return nil
}

// decompressPubKeyIfNeeded decompresses the public key if it's in compressed format
func decompressPubKeyIfNeeded(pubKey []byte) ([]byte, error) {
switch len(pubKey) {
case 33: // Compressed public key
uncompressedPubKey, err := crypto.DecompressPubkey(pubKey)
if err != nil {
return nil, err
}
return crypto.FromECDSAPub(uncompressedPubKey), nil
case 65: // Uncompressed public key
return pubKey, nil
default:
return nil, errors.New("invalid public key length")
}
}

// compressPubKeyIfNeeded compresses the public key if it's in uncompressed format
func compressPubKeyIfNeeded(pubKey []byte) ([]byte, error) {
switch len(pubKey) {
case 65: // Uncompressed public key
uncompressedPubKey, err := crypto.UnmarshalPubkey(pubKey)
if err != nil {
return nil, err
}
return crypto.CompressPubkey(uncompressedPubKey), nil
case 33: // Already compressed public key
return pubKey, nil
default:
return nil, errors.New("invalid public key length")
}
}

// OutPoint defines a Qi data type that is used to track previous outputs
type OutPoint struct {
TxHash common.Hash `json:"txHash"`
Expand Down
126 changes: 126 additions & 0 deletions core/types/utxo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,129 @@ func TestModifySSig(t *testing.T) {
modifiedSigHex := hex.EncodeToString(signatureBytes)
t.Log("Modified Signature: ", modifiedSigHex)
}

func TestTxInProtoDecode(t *testing.T) {
tests := []struct {
name string
pubKeyHex string
wantPubKey string
shouldFail bool
}{
{
name: "Uncompressed public key",
pubKeyHex: "0450495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e65c4c6c44cd3fe809b41dfac9060ad84cb57e2d575fad24d25a7efa3396e73c10",
wantPubKey: "0450495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e65c4c6c44cd3fe809b41dfac9060ad84cb57e2d575fad24d25a7efa3396e73c10",
shouldFail: false,
},
{
name: "Compressed public key",
pubKeyHex: "0250495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e6",
wantPubKey: "0450495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e65c4c6c44cd3fe809b41dfac9060ad84cb57e2d575fad24d25a7efa3396e73c10",
shouldFail: false,
},
{
name: "Invalid public key (32 bytes)",
pubKeyHex: "50495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e6",
wantPubKey: "",
shouldFail: true,
},
{
name: "Invalid public key (64 bytes)",
pubKeyHex: "50495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e650495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e6",
wantPubKey: "",
shouldFail: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pubKey, _ := hex.DecodeString(tt.pubKeyHex)
protoTxIn := &ProtoTxIn{
PreviousOutPoint: &ProtoOutPoint{
Hash: &common.ProtoHash{Value: make([]byte, 32)},
Index: new(uint32),
},
PubKey: pubKey,
}

txIn := &TxIn{}
err := txIn.ProtoDecode(protoTxIn)

if tt.shouldFail {
if err == nil {
t.Errorf("Expected an error for invalid public key, but got none")
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

gotPubKey := hex.EncodeToString(txIn.PubKey)
if gotPubKey != tt.wantPubKey {
t.Errorf("Unexpected public key. Got: %s, Want: %s", gotPubKey, tt.wantPubKey)
}
}
})
}
}

func TestTxInProtoEncode(t *testing.T) {
tests := []struct {
name string
pubKeyHex string
wantPubKey string
shouldFail bool
}{
{
name: "Uncompressed public key",
pubKeyHex: "0450495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e65c4c6c44cd3fe809b41dfac9060ad84cb57e2d575fad24d25a7efa3396e73c10",
wantPubKey: "0250495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e6",
shouldFail: false,
},
{
name: "Compressed public key",
pubKeyHex: "0250495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e6",
wantPubKey: "0250495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e6",
shouldFail: false,
},
{
name: "Invalid public key (32 bytes)",
pubKeyHex: "50495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e6",
wantPubKey: "",
shouldFail: true,
},
{
name: "Invalid public key (64 bytes)",
pubKeyHex: "50495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e650495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e6",
wantPubKey: "",
shouldFail: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pubKey, _ := hex.DecodeString(tt.pubKeyHex)
txIn := &TxIn{
PubKey: pubKey,
}

protoTxIn, err := txIn.ProtoEncode()
if tt.shouldFail {
if err == nil {
t.Errorf("Expected an error for invalid public key, but got none")
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
gotPubKey := hex.EncodeToString(protoTxIn.PubKey)
if gotPubKey != tt.wantPubKey {
t.Errorf("Unexpected public key. Got: %s, Want: %s", gotPubKey, tt.wantPubKey)
}

}

})
}

}
Loading