Fixed claim signing after changes in 90bef98bc3

This commit is contained in:
Mark Beamer Jr 2019-05-04 00:00:23 -04:00
parent c54836bca0
commit d701bab7f6
No known key found for this signature in database
GPG key ID: 1C314FB89AD76973
5 changed files with 65 additions and 19 deletions

View file

@ -31,6 +31,7 @@ type ClaimHelper struct {
ClaimID []byte ClaimID []byte
Version version Version version
Signature []byte Signature []byte
Payload []byte
} }
const migrationErrorMessage = "migration from v1 to v2 protobuf failed with: " const migrationErrorMessage = "migration from v1 to v2 protobuf failed with: "
@ -143,6 +144,7 @@ func (c *ClaimHelper) LoadFromBytes(raw_claim []byte, blockchainName string) err
ClaimID: claimID, ClaimID: claimID,
Version: version, Version: version,
Signature: signature, Signature: signature,
Payload: pbPayload,
} }
err = c.ValidateAddresses(blockchainName) err = c.ValidateAddresses(blockchainName)
@ -166,7 +168,7 @@ func (c *ClaimHelper) LoadFromHexString(claim_hex string, blockchainName string)
} }
func DecodeClaimProtoBytes(serialized []byte, blockchainName string) (*ClaimHelper, error) { func DecodeClaimProtoBytes(serialized []byte, blockchainName string) (*ClaimHelper, error) {
claim := &ClaimHelper{&pb.Claim{}, nil, nil, NoSig, nil} claim := &ClaimHelper{&pb.Claim{}, nil, nil, NoSig, nil, nil}
err := claim.LoadFromBytes(serialized, blockchainName) err := claim.LoadFromBytes(serialized, blockchainName)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -25,14 +25,13 @@ func (c *ClaimHelper) serialized() ([]byte, error) {
func (c *ClaimHelper) getProtobuf() *pb.Claim { func (c *ClaimHelper) getProtobuf() *pb.Claim {
claim := &pb.Claim{ claim := &pb.Claim{
Title: c.Title, Title: c.GetTitle(),
Description: c.Description, Description: c.GetDescription(),
Thumbnail: c.Thumbnail, Thumbnail: c.GetThumbnail(),
Tags: c.Tags, Tags: c.GetTags(),
Languages: c.Languages, Languages: c.GetLanguages(),
Locations: c.Locations, Locations: c.GetLocations(),
} }
return c.Claim
if c.GetChannel() != nil { if c.GetChannel() != nil {
claim.Type = &pb.Claim_Channel{Channel: c.GetChannel()} claim.Type = &pb.Claim_Channel{Channel: c.GetChannel()}
} else if c.GetStream() != nil { } else if c.GetStream() != nil {

View file

@ -14,7 +14,7 @@ func TestSign(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
channel := &ClaimHelper{newChannelClaim(), nil, nil, NoSig, nil} channel := &ClaimHelper{newChannelClaim(), nil, nil, NoSig, nil, nil}
pubkeyBytes, err := PublicKeyToDER(privateKey.PubKey()) pubkeyBytes, err := PublicKeyToDER(privateKey.PubKey())
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -29,7 +29,7 @@ func TestSign(t *testing.T) {
return return
} }
claim := &ClaimHelper{newStreamClaim(), nil, reverseBytes(claimIDHexBytes), WithSig, nil} claim := &ClaimHelper{newStreamClaim(), nil, reverseBytes(claimIDHexBytes), WithSig, nil, nil}
claim.Claim.Title = "Test title" claim.Claim.Title = "Test title"
claim.Claim.Description = "Test description" claim.Claim.Description = "Test description"
sig, err := Sign(*privateKey, *channel, *claim, txid) sig, err := Sign(*privateKey, *channel, *claim, txid)
@ -104,7 +104,7 @@ func TestSignWithV1Channel(t *testing.T) {
return return
} }
claim := &ClaimHelper{newStreamClaim(), nil, reverseBytes(claimIDHexBytes), WithSig, nil} claim := &ClaimHelper{newStreamClaim(), nil, reverseBytes(claimIDHexBytes), WithSig, nil, nil}
claim.Claim.Title = "Test title" claim.Claim.Title = "Test title"
claim.Claim.Description = "Test description" claim.Claim.Description = "Test description"
sig, err := Sign(*privateKey, *channel, *claim, txid) sig, err := Sign(*privateKey, *channel, *claim, txid)

View file

@ -5,6 +5,7 @@ import (
"crypto/sha256" "crypto/sha256"
"crypto/x509/pkix" "crypto/x509/pkix"
"encoding/asn1" "encoding/asn1"
"encoding/binary"
"encoding/hex" "encoding/hex"
"math/big" "math/big"
@ -57,13 +58,13 @@ func (c *ClaimHelper) ValidateClaimSignature(certificate *ClaimHelper, k string,
return c.validateClaimSignature(certificate, k, certificateId, blockchainName) return c.validateClaimSignature(certificate, k, certificateId, blockchainName)
} }
func (c *ClaimHelper) validateClaimSignature(certificate *ClaimHelper, firstInputTxID, certificateId string, blockchainName string) (bool, error) { func (c *ClaimHelper) validateClaimSignature(certificate *ClaimHelper, firstInputTxHash, certificateId string, blockchainName string) (bool, error) {
certificateIdSlice, err := hex.DecodeString(certificateId) certificateIdSlice, err := hex.DecodeString(certificateId)
if err != nil { if err != nil {
return false, errors.Err(err) return false, errors.Err(err)
} }
certificateIdSlice = reverseBytes(certificateIdSlice) certificateIdSlice = reverseBytes(certificateIdSlice)
firstInputTxIDBytes, err := hex.DecodeString(firstInputTxID) firstInputTxIDBytes, err := hex.DecodeString(firstInputTxHash)
if err != nil { if err != nil {
return false, errors.Err(err) return false, errors.Err(err)
} }
@ -77,12 +78,7 @@ func (c *ClaimHelper) validateClaimSignature(certificate *ClaimHelper, firstInpu
signatureBytes[i] = b signatureBytes[i] = b
} }
serialized, err := c.serialized() claimDigest := getClaimSignatureDigest(firstInputTxIDBytes, certificateIdSlice, c.Payload)
if err != nil {
return false, errors.Err("serialization error")
}
claimDigest := getClaimSignatureDigest(firstInputTxIDBytes, certificateIdSlice, serialized)
return c.VerifyDigest(certificate, signatureBytes, claimDigest), nil return c.VerifyDigest(certificate, signatureBytes, claimDigest), nil
} }
@ -120,3 +116,13 @@ func (c *ClaimHelper) validateV1ClaimSignature(certificate *ClaimHelper, claimAd
claimDigest := getClaimSignatureDigest(claimAddress[:], serializedNoSig, certificateIdSlice) claimDigest := getClaimSignatureDigest(claimAddress[:], serializedNoSig, certificateIdSlice)
return c.VerifyDigest(certificate, signatureBytes, claimDigest), nil return c.VerifyDigest(certificate, signatureBytes, claimDigest), nil
} }
func GetOutpointHash(txid string, vout uint32) (string, error) {
txidBytes, err := hex.DecodeString(txid)
if err != nil {
return "", errors.Err(err)
}
var voutBytes = make([]byte, 4)
binary.LittleEndian.PutUint32(voutBytes, vout)
return hex.EncodeToString(append(reverseBytes(txidBytes), voutBytes...)), nil
}

View file

@ -2,6 +2,8 @@ package claim
import ( import (
"testing" "testing"
"gotest.tools/assert"
) )
func TestV1ValidateClaimSignature(t *testing.T) { func TestV1ValidateClaimSignature(t *testing.T) {
@ -53,3 +55,40 @@ func TestV1FailToValidateClaimSignature(t *testing.T) {
t.Error("failed to validate signature:", result) t.Error("failed to validate signature:", result)
} }
} }
func TestV2ValidateClaimSignature(t *testing.T) {
cert_claim_hex := "00125a0a583056301006072a8648ce3d020106052b8104000a034200045a0343c155302280da01ae0001b7295241eb03c42a837acf92ccb9680892f7db50fd1d3c14b28bb594e304f05fc4ae7c1f222a85d1d1a3461b3cfb9906f66cb5"
signed_claim_hex := "015cb78e424a34fbf79b67f9107430427aa62373e69b4998a29ecec8f14a9e0a213a043ced8064c069d7e464b5fd3ccb92b45bd59b15c0e1bb27e3c366d43f86a9a6b5ad42647a1aad69a73ac50b19ae3ec978c2c70aa2010a99010a301c662f19abc461e7eddecf165adfa7fca569e209773f3db31241c1e297f0a8d5b3e4768828b065fbeb1d6776f61073f6121b3031202d20556e6d6173746572656420496d70756c7365732e377a187a22146170706c69636174696f6e2f782d6578742d377a32302eb61ea475017e28c013616a56c1219ba90dc35fffff453d9675146f648f66634e0d1516528d37aba9f5801229d9f2181a044e6f6e6542087465737420707562520062020801"
signed_claim, err := DecodeClaimHex(signed_claim_hex, "lbrycrd_main")
if err != nil {
t.Error(err)
}
cert_claim, err := DecodeClaimHex(cert_claim_hex, "lbrycrd_main")
if err != nil {
t.Error(err)
}
firstInputTxHash, err := GetOutpointHash("becb96a4a2e66bd24f083772fe9da904654ea9b5f07cc5bfbee233355911ddb1", uint32(0))
if err != nil {
t.Error(err)
}
cert_id := "e67323a67a42307410f9679bf7fb344a428eb75c"
result, err := signed_claim.ValidateClaimSignature(cert_claim, firstInputTxHash, cert_id, "lbrycrd_main")
if err != nil {
t.Error(err)
}
if result != true {
t.Error("failed to validate signature:", result)
}
}
func TestGetOutpointHash(t *testing.T) {
hash, err := GetOutpointHash("dc3dcf2f94d3c91e454ac2474802e20f26b30705372dda43890c811d918aef64", 1)
if err != nil {
t.Error(err)
}
assert.Assert(t, hash == "64ef8a911d810c8943da2d370507b3260fe2024847c24a451ec9d3942fcf3ddc01000000", uint(1))
}