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

fix verify ontid public key bug #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions cred.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/ontio/ontology-go-sdk/utils"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -147,7 +148,7 @@ func (this *Credential) VerifySignReq(request *Request) error {
return fmt.Errorf("VerifySignReq, this.GetPublicKeyList error: %s", err)
}
for _, v := range publicKeyList {
data, err := hex.DecodeString(v.PublicKeyHex)
data, err := utils.PubKeyEncodeString(v.PublicKeyHex, v.Type)
if err != nil {
return fmt.Errorf("VerifySignReq, hex.DecodeString public key error: %s", err)
}
Expand Down Expand Up @@ -228,18 +229,18 @@ func (this *Credential) GetPublicKeyId(ontId string, publicKeyHex string) (uint3
return 0, nil, fmt.Errorf("GetPublicKeyId, record not found")
}

func (this *Credential) GetPublicKey(ontId string, Id string) (string, error) {
func (this *Credential) GetPublicKey(ontId string, Id string) (*PublicKey, error) {
publicKeyList, err := this.GetPublicKeyList(ontId)
if err != nil {
return "", fmt.Errorf("GetPublicKeyId, this.GetPublicKeyList error: %s", err)
return nil, fmt.Errorf("GetPublicKeyId, this.GetPublicKeyList error: %s", err)
}

for _, v := range publicKeyList {
if v.Id == Id {
return v.PublicKeyHex, nil
return v, nil
}
}
return "", fmt.Errorf("GetPublicKeyId, record not found")
return nil, fmt.Errorf("GetPublicKeyId, record not found")
}

func (this *Credential) GetPublicKeyList(ontId string) (PublicKeyList, error) {
Expand Down Expand Up @@ -541,11 +542,11 @@ func (this *Credential) verifyProof(ontId string, proof *Proof, msg []byte) erro
return fmt.Errorf("VerifyProof, hex.DecodeString signature error: %s", err)
}

publicKeyHex, err := this.GetPublicKey(ontId, proof.VerificationMethod)
publicKey, err := this.GetPublicKey(ontId, proof.VerificationMethod)
if err != nil {
return fmt.Errorf("VerifyProof, this.GetPublicKey error: %s", err)
}
data, err := hex.DecodeString(publicKeyHex)
data, err := utils.PubKeyEncodeString(publicKey.PublicKeyHex, publicKey.Type)
if err != nil {
return fmt.Errorf("VerifyProof, hex.DecodeString public key error: %s", err)
}
Expand Down
5 changes: 3 additions & 2 deletions cred_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/ontio/ontology-go-sdk/utils"
"strings"
"time"

Expand Down Expand Up @@ -243,11 +244,11 @@ func (this *Credential) verifyJWSProof(ontId, kid string, msg []byte, jws string
return fmt.Errorf("VerifyJWSProof, base64.StdEncoding.DecodeString jws error: %s", err)
}

publicKeyHex, err := this.GetPublicKey(ontId, kid)
publicKey, err := this.GetPublicKey(ontId, kid)
if err != nil {
return fmt.Errorf("VerifyJWSProof, this.GetPublicKey error: %s", err)
}
data, err := hex.DecodeString(publicKeyHex)
data, err := utils.PubKeyEncodeString(publicKey.PublicKeyHex, publicKey.Type)
if err != nil {
return fmt.Errorf("VerifyJWSProof, hex.DecodeString public key error: %s", err)
}
Expand Down
48 changes: 48 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package utils

import (
"bytes"
"encoding/hex"
"fmt"
"os"
Expand Down Expand Up @@ -122,3 +123,50 @@ func PubKeysEqual(pks1, pks2 []keypair.PublicKey) bool {
}
return true
}

func PubKeyEncodeString(pubKeyHex, SignType string) ([]byte, error) {
publicKey, err := hex.DecodeString(pubKeyHex)
if err != nil {
return nil, err
}
var buf bytes.Buffer
switch SignType {
case "EcdsaSecp224r1VerificationKey2019":
buf.WriteByte(byte(keypair.PK_ECDSA))
buf.WriteByte(keypair.P224)
buf.Write(publicKey)
return buf.Bytes(), nil
case "EcdsaSecp256r1VerificationKey2019":
buf.WriteByte(byte(keypair.PK_ECDSA))
buf.WriteByte(keypair.P256)
buf.Write(publicKey)
return buf.Bytes(), nil
case "EcdsaSecp384r1VerificationKey2019":
buf.WriteByte(byte(keypair.PK_ECDSA))
buf.WriteByte(keypair.P384)
buf.Write(publicKey)
return buf.Bytes(), nil
case "EcdsaSecp521r1VerificationKey2019":
buf.WriteByte(byte(keypair.PK_ECDSA))
buf.WriteByte(keypair.P521)
buf.Write(publicKey)
return buf.Bytes(), nil
case "EcdsaSecp256k1VerificationKey2019":
buf.WriteByte(byte(keypair.PK_ECDSA))
buf.WriteByte(keypair.SECP256K1)
buf.Write(publicKey)
return buf.Bytes(), nil
case "Ed25519VerificationKey2018":
buf.WriteByte(byte(keypair.PK_EDDSA))
buf.WriteByte(keypair.ED25519)
buf.Write(publicKey)
return buf.Bytes(), nil
case "SM2VerificationKey2019":
buf.WriteByte(byte(keypair.PK_SM2))
buf.WriteByte(keypair.SM2P256V1)
buf.Write(publicKey)
return buf.Bytes(), nil
default:
return nil, fmt.Errorf("unsupported type 2")
}
}