Skip to content

Commit

Permalink
allowing to add user keys in secp256k1 with 0x04 prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Zemtsov <a.zemtsov@gmail.com>
  • Loading branch information
zemtsov committed Jun 15, 2024
1 parent b3a8768 commit a373234
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
28 changes: 23 additions & 5 deletions cc/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,37 @@ func publicKeyFromBase58String(base58Encoded string) (PublicKey, error) {
}, nil
}

func (key *PublicKey) isSecp256k1() bool {
if len(key.Bytes) == 0 {
return false
}
if key.Bytes[0] == helpers.PrefixUncompressedSecp259k1Key {
return len(key.Bytes) == helpers.KeyLengthSecp256k1+1
}
return len(key.Bytes) == helpers.KeyLengthSecp256k1
}

func (key *PublicKey) isEd25519() bool {
return len(key.Bytes) == helpers.KeyLengthEd25519
}

func (key *PublicKey) isGost() bool {
return len(key.Bytes) == helpers.KeyLengthEd25519
}

func (key *PublicKey) validateLength() error {
var expectedLength int
valid := false

switch key.Type {
case pb.KeyType_secp256k1.String():
expectedLength = helpers.KeyLengthSecp256k1
valid = key.isSecp256k1()
case pb.KeyType_gost.String():
expectedLength = helpers.KeyLengthGOST
valid = key.isGost()
default:
expectedLength = helpers.KeyLengthEd25519
valid = key.isEd25519()
}

if len(key.Bytes) != expectedLength {
if !valid {
return fmt.Errorf("unexpected key length %d", len(key.Bytes))
}

Expand Down
2 changes: 1 addition & 1 deletion cc/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func verifySecp256k1Signature(
}

func secp256k1PublicKeyFromBytes(bytes []byte) *ecdsa.PublicKey {
if bytes[0] == 0x04 && len(bytes) == helpers.KeyLengthSecp256k1+1 {
if len(bytes) == helpers.KeyLengthSecp256k1+1 && bytes[0] == helpers.PrefixUncompressedSecp259k1Key {
bytes = bytes[1:]
}
if len(bytes) != helpers.KeyLengthSecp256k1 {
Expand Down
5 changes: 5 additions & 0 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
KeyLengthGOST = 64
)

const PrefixUncompressedSecp259k1Key = 0x04

// DecodeBase58PublicKey decode public key from base58 to a byte array
func DecodeBase58PublicKey(encodedBase58PublicKey string) ([]byte, error) {
if len(encodedBase58PublicKey) == 0 {
Expand Down Expand Up @@ -216,6 +218,9 @@ func ValidateKeyLength(key []byte) bool {
if len(key) == KeyLengthEd25519 {
return true
}
if len(key) == KeyLengthSecp256k1+1 && key[0] == PrefixUncompressedSecp259k1Key {
return true
}
if len(key) == KeyLengthSecp256k1 {
return true
}
Expand Down
10 changes: 6 additions & 4 deletions tests/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ var (
KeyType: KeyTypeEd25519,
},
{
PublicKey: "5Tevazf8xxwyyKGku4VCCSVMDN56mU3mm2WsnENk1zv5",
PrivateKey: "5D2BpuHZwik9zPFuaqba4zbvNP8TB7PQ6usZke5bufPbKf8xG6ZMHReBqwKw9aDfpTaNfaRsg1j2zVZWrX8hg18D",
KeyType: KeyTypeEd25519,
// secp256k1 key with 0x04 prefix
PublicKey: "RtR8wrDuNvVXHNraBkNyeR6YVCdfUL6dWGXk1GAz2wPkp41BUYApzjVcJ9DutTmTZCSPQdKf3UgiuWrGCuL4C7fg",
PrivateKey: "CPjbqe7PzmgimpTdvvAuHsF8KcCw8ac3Sj8phUp2duuS",
KeyType: KeyTypeSecp256k1,
},
{
// secp256k1 key without 0x04 prefix
PublicKey: "4DorLT9cRqaUeiDsBtDmm2Gwz18CqGsLn3f4eNLPi8LfzaS3h29aGZXp8aSFMEb8K3BEDA3Z9kFnTqD2TuAud15V",
PrivateKey: "8XfQpgs3iBeJ1tSKzsdCU9t7Jd8vbxcLsrDgGHq78C4x",
KeyType: KeyTypeSecp256k1,
Expand Down Expand Up @@ -324,7 +326,7 @@ func verifySecp256k1Signature(
}

func secp256k1PublicKeyFromBytes(bytes []byte) *ecdsa.PublicKey {
if bytes[0] == 0x04 && len(bytes) == helpers.KeyLengthSecp256k1+1 {
if len(bytes) == helpers.KeyLengthSecp256k1+1 && bytes[0] == helpers.PrefixUncompressedSecp259k1Key {
bytes = bytes[1:]
}
if len(bytes) != helpers.KeyLengthSecp256k1 {
Expand Down
47 changes: 44 additions & 3 deletions tests/unit/ecdsa_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/hyperledger/fabric-chaincode-go/shim"
)

func TestAddUserECDSAPublicKey(t *testing.T) {
func TestAddUserSecp256k1PublicKey(t *testing.T) {
t.Parallel()

const (
Expand All @@ -17,7 +17,7 @@ func TestAddUserECDSAPublicKey(t *testing.T) {

stub := common.StubCreateAndInit(t)

t.Run("[negative] add user with wrong key length", func(t *testing.T) {
t.Run("[negative] add user with wrong key type", func(t *testing.T) {
s := &seriesAddUser{
testPubKey: testKeyECDSA,
testAddress: testAddress,
Expand All @@ -32,7 +32,22 @@ func TestAddUserECDSAPublicKey(t *testing.T) {
validationResultAddUser(t, stub, resp, s)
})

t.Run("add user with ecdsa key", func(t *testing.T) {
t.Run("[negative] add user with wrong key length", func(t *testing.T) {
s := &seriesAddUser{
testPubKey: testKeyECDSA[0 : len(testKeyECDSA)-1],
testAddress: testAddress,
kycHash: kycHash,
testUserID: testUserID,
testPubKeyType: common.KeyTypeSecp256k1,
respStatus: int32(shim.ERROR),
errorMsg: "unexpected key length",
}

resp := addUser(stub, s)
validationResultAddUser(t, stub, resp, s)
})

t.Run("add user with secp256k1 key", func(t *testing.T) {
s := &seriesAddUser{
testPubKey: testKeyECDSA,
testAddress: testAddress,
Expand Down Expand Up @@ -62,3 +77,29 @@ func TestAddUserECDSAPublicKey(t *testing.T) {
validationResultAddUser(t, stub, resp, s)
})
}

func TestAddUserSecp256k1WithPrefixPublicKey(t *testing.T) {
t.Parallel()

const (
testKeyECDSA = "RtR8wrDuNvVXHNraBkNyeR6YVCdfUL6dWGXk1GAz2wPkp41BUYApzjVcJ9DutTmTZCSPQdKf3UgiuWrGCuL4C7fg"
testAddress = "HfqBDFi6uQFGENqLVLfmR1LKmo8Ghzpd9NhjMZbVqmLknyBTg"
)

stub := common.StubCreateAndInit(t)

t.Run("add user with secp256k1 key with 0x04 prefix", func(t *testing.T) {
s := &seriesAddUser{
testPubKey: testKeyECDSA,
testAddress: testAddress,
kycHash: kycHash,
testUserID: testUserID,
testPubKeyType: common.KeyTypeSecp256k1,
respStatus: int32(shim.OK),
errorMsg: "",
}

resp := addUserWithPublicKeyType(stub, s)
validationResultAddUser(t, stub, resp, s)
})
}

0 comments on commit a373234

Please sign in to comment.