From bffb125c691b6cb0e6f30c64923a6c7ac62c19f6 Mon Sep 17 00:00:00 2001 From: Harshal Patel Date: Thu, 13 Feb 2020 19:14:44 -0600 Subject: [PATCH 1/7] change encode func --- internal/publickey/publickey.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/internal/publickey/publickey.go b/internal/publickey/publickey.go index ae7d76e5..f96dbdde 100644 --- a/internal/publickey/publickey.go +++ b/internal/publickey/publickey.go @@ -8,6 +8,7 @@ import ( "errors" "github.com/SIGBlockchain/project_aurum/internal/hashing" + "github.com/btcsuite/btcutil/base58" ) // AurumPublicKey struct holds public key for user @@ -42,11 +43,13 @@ func Encode(key *ecdsa.PublicKey) ([]byte, error) { if key == nil { return nil, errors.New("Could not return the encoded public key - the key value is nil") } - x509EncodedPub, err := x509.MarshalPKIXPublicKey(key) - if err != nil { - return nil, err - } - return pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: x509EncodedPub}), nil + b := append(key.X.Bytes(), key.Y.Bytes()...) + + b58EncodedPub := base58.Encode(b) + + k := "0x01" + b58EncodedPub + b58PrefixEncoded := []byte(k) + return b58PrefixEncoded, nil } // Decode returns the public key from a given PEM-Encoded byte slice representation of the public key or a non-nil error if fail From 7a3face973cc6ed3b4f6c5b327adcc61c2cd93fd Mon Sep 17 00:00:00 2001 From: Harshal Patel Date: Fri, 14 Feb 2020 08:56:17 -0600 Subject: [PATCH 2/7] changed encode func --- internal/publickey/publickey.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/publickey/publickey.go b/internal/publickey/publickey.go index f96dbdde..bf7f4ee2 100644 --- a/internal/publickey/publickey.go +++ b/internal/publickey/publickey.go @@ -44,11 +44,10 @@ func Encode(key *ecdsa.PublicKey) ([]byte, error) { return nil, errors.New("Could not return the encoded public key - the key value is nil") } b := append(key.X.Bytes(), key.Y.Bytes()...) - - b58EncodedPub := base58.Encode(b) - - k := "0x01" + b58EncodedPub - b58PrefixEncoded := []byte(k) + k := "1" + string(b) + c:= []byte(k) + b58EncodedPub := base58.Encode(c) + b58PrefixEncoded := []byte(b58EncodedPub) return b58PrefixEncoded, nil } From 7c80d10bd8ed03f48c154bfb32abe1224987031e Mon Sep 17 00:00:00 2001 From: Vineet Patel Date: Mon, 17 Feb 2020 14:28:15 -0600 Subject: [PATCH 3/7] Changed unit tests to reflect new encoding. --- internal/publickey/publickey_test.go | 38 +++++++++++++--------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/internal/publickey/publickey_test.go b/internal/publickey/publickey_test.go index 551c3063..403e9607 100644 --- a/internal/publickey/publickey_test.go +++ b/internal/publickey/publickey_test.go @@ -1,12 +1,11 @@ package publickey import ( + "bytes" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" - "crypto/x509" "encoding/hex" - "encoding/pem" "reflect" "testing" @@ -54,21 +53,22 @@ func TestNew(t *testing.T) { func TestEncoding(t *testing.T) { private, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) public := private.PublicKey - x509EncodedPub, err := x509.MarshalPKIXPublicKey(nil) - // test that Encoding returns an error for bad input - if err == nil { - t.Errorf("Expected err to not be nil, but it was...") - } - encoded, err := Encode(&public) - // test that Encoding does not receive an error for valid input + encodedPublic, err := Encode(&public) + if err != nil { - t.Errorf("Received an error for valid input") + t.Errorf("Got error: %s\n", err.Error()) } - x509EncodedPub, _ = x509.MarshalPKIXPublicKey(&public) - x509EncodedPub = pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: x509EncodedPub}) - // test that Encoding results match - if !reflect.DeepEqual(x509EncodedPub, encoded) { - t.Errorf("Encoding does not match") + if encodedPublic[0] != byte(1) { + t.Errorf("Encoding is missing a byte prefix of 1, instead got: %v\n", encodedPublic[0]) + } + if bytes.Compare(encodedPublic[1:33], public.X.Bytes()) != 0 { + t.Errorf("Encoding of X from public key is mssing") + } + if bytes.Compare(encodedPublic[33:64], public.Y.Bytes()) != 0 { + t.Errorf("Encoding of Y from public key is mssing") + } + if len(encodedPublic) != 65 { + t.Errorf("Expected legnth of 65, instead got: %d\n", len(encodedPublic)) } } @@ -87,12 +87,10 @@ func TestDecoding(t *testing.T) { if err != nil { t.Errorf("Expected err to not be nil, but it was...") } - localDecoded, _ := pem.Decode(encoded) - x509EncodedPub := localDecoded.Bytes - genericPublicKey, _ := x509.ParsePKIXPublicKey(x509EncodedPub) + // test that decodings match - if !reflect.DeepEqual(genericPublicKey, decoded) { - t.Errorf("Keys do not match after decode") + if !reflect.DeepEqual(public, decoded) { + t.Errorf("Keys do not match after decode\n. %v != %v\n", public, decoded) } } From 8050ac56d5589a3b490f16b43b24412b1e1e92ee Mon Sep 17 00:00:00 2001 From: Harshal Patel Date: Thu, 5 Mar 2020 19:39:53 -0600 Subject: [PATCH 4/7] encode function tests pass, decode function test dont --- internal/publickey/publickey.go | 45 +++++++++++++++++----------- internal/publickey/publickey_test.go | 3 +- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/internal/publickey/publickey.go b/internal/publickey/publickey.go index bf7f4ee2..84cc584d 100644 --- a/internal/publickey/publickey.go +++ b/internal/publickey/publickey.go @@ -2,13 +2,13 @@ package publickey import ( "crypto/ecdsa" - "crypto/x509" + "crypto/elliptic" "encoding/hex" - "encoding/pem" "errors" + "math/big" + "github.com/SIGBlockchain/project_aurum/internal/hashing" - "github.com/btcsuite/btcutil/base58" ) // AurumPublicKey struct holds public key for user @@ -43,12 +43,12 @@ func Encode(key *ecdsa.PublicKey) ([]byte, error) { if key == nil { return nil, errors.New("Could not return the encoded public key - the key value is nil") } - b := append(key.X.Bytes(), key.Y.Bytes()...) - k := "1" + string(b) - c:= []byte(k) - b58EncodedPub := base58.Encode(c) - b58PrefixEncoded := []byte(b58EncodedPub) - return b58PrefixEncoded, nil + b := make([]byte, 1+len(key.X.Bytes())+len(key.Y.Bytes())) + b[0] = byte(1) + copy(b[1:33], key.X.Bytes()) + copy(b[33:65], key.Y.Bytes()) + + return b, nil } // Decode returns the public key from a given PEM-Encoded byte slice representation of the public key or a non-nil error if fail @@ -56,18 +56,27 @@ func Decode(key []byte) (*ecdsa.PublicKey, error) { if key == nil { return nil, errors.New("Could not return the decoded public key - the key value is nil") } - blockPub, _ := pem.Decode(key) - // pem.Decode will return nil for the first value if no PEM data is found. This would be bad - if blockPub == nil { - return nil, errors.New("Could not return the public key - failed to PEM decode in preparation x509 encode") + if(key[0]!=1){ + return nil, errors.New("Could not return the decoded public key - the key is not uncompressed public key") } - x509EncodedPub := blockPub.Bytes - genericPublicKey, err := x509.ParsePKIXPublicKey(x509EncodedPub) - if err != nil { - return nil, err + n := new(big.Int) + m := new(big.Int) + + + xByte, err := n.SetString(hex.EncodeToString(key[1:33]), 16) + + if !err { + return nil, errors.New("Could not convert string to int") + } + + yByte, ok := m.SetString(hex.EncodeToString(key[33:65]), 16) + if !ok { + return nil, errors.New("Could not convert string to int") } - return genericPublicKey.(*ecdsa.PublicKey), nil + + priv := ecdsa.PublicKey{Curve: elliptic.P256(), X: xByte, Y: yByte} + return &priv, nil } // Equals returns true if the given two *ecdsa.PublicKey are equal diff --git a/internal/publickey/publickey_test.go b/internal/publickey/publickey_test.go index 403e9607..5ea27d6a 100644 --- a/internal/publickey/publickey_test.go +++ b/internal/publickey/publickey_test.go @@ -64,7 +64,7 @@ func TestEncoding(t *testing.T) { if bytes.Compare(encodedPublic[1:33], public.X.Bytes()) != 0 { t.Errorf("Encoding of X from public key is mssing") } - if bytes.Compare(encodedPublic[33:64], public.Y.Bytes()) != 0 { + if bytes.Compare(encodedPublic[33:65], public.Y.Bytes()) != 0 { t.Errorf("Encoding of Y from public key is mssing") } if len(encodedPublic) != 65 { @@ -89,6 +89,7 @@ func TestDecoding(t *testing.T) { } // test that decodings match + if !reflect.DeepEqual(public, decoded) { t.Errorf("Keys do not match after decode\n. %v != %v\n", public, decoded) } From 76c0eeef6684b4bb89c749150763469fbea8685e Mon Sep 17 00:00:00 2001 From: Vineet77 Date: Thu, 5 Mar 2020 23:06:49 -0600 Subject: [PATCH 5/7] Fixed comparison. --- internal/publickey/publickey_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/publickey/publickey_test.go b/internal/publickey/publickey_test.go index 5ea27d6a..e4048799 100644 --- a/internal/publickey/publickey_test.go +++ b/internal/publickey/publickey_test.go @@ -89,8 +89,8 @@ func TestDecoding(t *testing.T) { } // test that decodings match - - if !reflect.DeepEqual(public, decoded) { + + if !reflect.DeepEqual(public, *decoded) { t.Errorf("Keys do not match after decode\n. %v != %v\n", public, decoded) } } From dff061d417fdfc48ba5237b8b907c74c3f28d780 Mon Sep 17 00:00:00 2001 From: Vineet77 Date: Mon, 16 Mar 2020 13:32:21 -0500 Subject: [PATCH 6/7] Progress for refactoring contract tests. --- internal/contracts/contracts.go | 37 +++++++++++----------- internal/contracts/contracts_test.go | 46 ++++++++++++++++++++++++++++ internal/publickey/info.go | 9 ++++++ internal/publickey/publickey_test.go | 1 - 4 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 internal/publickey/info.go diff --git a/internal/contracts/contracts.go b/internal/contracts/contracts.go index 179dd848..1483fc39 100644 --- a/internal/contracts/contracts.go +++ b/internal/contracts/contracts.go @@ -42,15 +42,7 @@ type JSONContract struct { StateNonce uint64 } -/* -version field comes from version parameter -sender public key comes from sender private key -signature comes from calling sign contract -signature length comes from signature -recipient pk hash comes from sha-256 hash of rpk -value is value parameter -returns contract struct -*/ +//Creates an unsigned contract func New(version uint16, sender *ecdsa.PrivateKey, recipient []byte, value uint64, nextStateNonce uint64) (*Contract, error) { if version == 0 { @@ -75,18 +67,25 @@ func New(version uint16, sender *ecdsa.PrivateKey, recipient []byte, value uint6 return &c, nil } -// // Serialize all fields of the contract -func (c *Contract) Serialize() ([]byte, error) { - /* - 0-2 version - 2-180 spubkey - 180-181 siglen - 181 - 181+c.siglen signature - 181+c.siglen - (181+c.siglen + 32) rpkh - (181+c.siglen + 32) - (181+c.siglen + 32+ 8) value +/* + Serialize will serialize the contract into an slice of bytes - */ + Byte breakdown + 0-2 version + 3: The first byte of the encoded public key will indicate the length of the encoded public key. + If this byte is 0, then there is no sender public key (usefully for mining contracts) + After the encoded public key, the next byte will be the signature length + The next bytes are the signatures + The next 32 bytes are the recipient public key hash + The next 8 bytes indicates the value of the contract +*/ +func (c *Contract) Serialize() ([]byte, error) { + spubkeyLen := 0 + if c.SenderPubKey != nil { + spubkeyLen = publickey.Info + [] + } // if contract's sender pubkey is nil, make 178 zeros in its place instead var spubkey []byte var err error diff --git a/internal/contracts/contracts_test.go b/internal/contracts/contracts_test.go index 760d1b5f..56bc5c9a 100644 --- a/internal/contracts/contracts_test.go +++ b/internal/contracts/contracts_test.go @@ -6,6 +6,7 @@ import ( "crypto/elliptic" "crypto/rand" "encoding/asn1" + "encoding/binary" "encoding/hex" "fmt" "math/big" @@ -100,6 +101,50 @@ func TestNew(t *testing.T) { } } +func TestMiningContractSerialize(t *testing.T) { + recipientPrivateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + encodedPublicKey, _ := publickey.Encode(&recipientPrivateKey.PublicKey) + recipientPubKeyHash := hashing.New(encodedPublicKey) + + expectedVersion := uint16(1) + expectedValue := uint64(10004) + expectedStateNonce := uint64(43) + + nullSenderContract, _ := New(expectedVersion, nil, recipientPubKeyHash, expectedValue, expectedStateNonce) + serializedContract, _ := nullSenderContract.Serialize() + + actualVersion := binary.LittleEndian.Uint16(serializedContract[0:2]) + sigLen := uint8(serializedContract[3]) + actualRecipPubKeyHash := serializedContract[4:36] + + if actualVersion != expectedVersion { + t.Errorf("Versions do not match. Expected: %d, actual: %d\n", expectedVersion, actualVersion) + } + if serializedContract[2] != byte(0x3) { + t.Errorf("Encoded sender public key is not 0") + } + if sigLen != 0 { + t.Errorf("Mining contract should be unsigned. Got signature length of %d, when it should be 0", sigLen) + } + if !bytes.Equal(recipientPubKeyHash, actualRecipPubKeyHash) { + t.Errorf("Recipient Public Key hashes do not match.\nExpected: %v\nActual:%v\n", recipientPubKeyHash, actualRecipPubKeyHash) + } +} + +func TestUnsignedContractSerialize(t *testing.T) { + recipientPrivateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + senderPrivateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + encodedRecipientPublicKey, _ := publickey.Encode(&recipientPrivateKey.PublicKey) + encodedSenderPublicKey, _ := publickey.Encode(&senderPrivateKey.PublicKey) + recipientPubKeyHash := hashing.New(encodedRecipientPublicKey) + + expectedVersion := uint16(3) + expectedValue := uint64(12004) + expectedStateNonce := uint64(873) + contract, _ := New(expectedVersion, recipientPrivateKey, recipientPubKeyHash, expectedValue, expectedStateNonce) + serializedContract, _ := contract.Serialize() +} + func TestContract_Serialize(t *testing.T) { senderPrivateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) recipientPrivateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) @@ -109,6 +154,7 @@ func TestContract_Serialize(t *testing.T) { unsignedContract, _ := New(1, senderPrivateKey, hashing.New(encodedRecipientKey), 1000, 0) signedContract, _ := New(1, senderPrivateKey, hashing.New(encodedRecipientKey), 1000, 0) signedContract.Sign(senderPrivateKey) + tests := []struct { name string c *Contract diff --git a/internal/publickey/info.go b/internal/publickey/info.go new file mode 100644 index 00000000..e942a8a2 --- /dev/null +++ b/internal/publickey/info.go @@ -0,0 +1,9 @@ +package publickey + +var ( + Info = map[byte]publicKeyInfo{0x01: {Length: 65}} +) + +type publicKeyInfo struct { + Length uint +} diff --git a/internal/publickey/publickey_test.go b/internal/publickey/publickey_test.go index e4048799..d8d9b6e5 100644 --- a/internal/publickey/publickey_test.go +++ b/internal/publickey/publickey_test.go @@ -89,7 +89,6 @@ func TestDecoding(t *testing.T) { } // test that decodings match - if !reflect.DeepEqual(public, *decoded) { t.Errorf("Keys do not match after decode\n. %v != %v\n", public, decoded) } From 8ae9cb4d708666de7a4bf50bcd02df416748375d Mon Sep 17 00:00:00 2001 From: Vineet77 Date: Mon, 16 Mar 2020 17:02:57 -0500 Subject: [PATCH 7/7] Refactored contract tests for new public key encoding. --- internal/contracts/contracts.go | 6 -- internal/contracts/contracts_test.go | 143 ++++++++++++++------------- 2 files changed, 77 insertions(+), 72 deletions(-) diff --git a/internal/contracts/contracts.go b/internal/contracts/contracts.go index 1483fc39..d9d0a3c2 100644 --- a/internal/contracts/contracts.go +++ b/internal/contracts/contracts.go @@ -80,12 +80,6 @@ func New(version uint16, sender *ecdsa.PrivateKey, recipient []byte, value uint6 The next 8 bytes indicates the value of the contract */ func (c *Contract) Serialize() ([]byte, error) { - - spubkeyLen := 0 - if c.SenderPubKey != nil { - spubkeyLen = publickey.Info - [] - } // if contract's sender pubkey is nil, make 178 zeros in its place instead var spubkey []byte var err error diff --git a/internal/contracts/contracts_test.go b/internal/contracts/contracts_test.go index 56bc5c9a..01b86ca5 100644 --- a/internal/contracts/contracts_test.go +++ b/internal/contracts/contracts_test.go @@ -120,7 +120,7 @@ func TestMiningContractSerialize(t *testing.T) { if actualVersion != expectedVersion { t.Errorf("Versions do not match. Expected: %d, actual: %d\n", expectedVersion, actualVersion) } - if serializedContract[2] != byte(0x3) { + if serializedContract[2] != byte(0x0) { t.Errorf("Encoded sender public key is not 0") } if sigLen != 0 { @@ -141,78 +141,89 @@ func TestUnsignedContractSerialize(t *testing.T) { expectedVersion := uint16(3) expectedValue := uint64(12004) expectedStateNonce := uint64(873) - contract, _ := New(expectedVersion, recipientPrivateKey, recipientPubKeyHash, expectedValue, expectedStateNonce) + contract, _ := New(expectedVersion, senderPrivateKey, recipientPubKeyHash, expectedValue, expectedStateNonce) serializedContract, _ := contract.Serialize() + + actualVersion := binary.LittleEndian.Uint16(serializedContract[0:2]) + + firstByte := serializedContract[2] + pubKeyLength := publickey.Info[firstByte].Length + actualPubKey := serializedContract[2 : 2+pubKeyLength] + actualSigLen := serializedContract[2+pubKeyLength] + actualRecipientHash := serializedContract[3+pubKeyLength : 35+pubKeyLength] + actualValue := binary.LittleEndian.Uint64(serializedContract[35+pubKeyLength : 43+pubKeyLength]) + actualNonce := binary.LittleEndian.Uint64(serializedContract[43+pubKeyLength : 51+pubKeyLength]) + + if actualVersion != expectedVersion { + t.Errorf("Versions do not match. Expected: %d, actual: %d\n", expectedVersion, actualVersion) + } + if serializedContract[2] == byte(0x0) { + t.Errorf("Encoded sender public key is 0\n") + } + if !bytes.Equal(actualPubKey, encodedSenderPublicKey) { + t.Errorf("Sender public key not properly encoded\nexpected: %v\nactual %v\n", encodedSenderPublicKey, actualPubKey) + } + if actualSigLen != 0 { + t.Errorf("Signature length is not 0. Expected: 0, actual: %d", actualSigLen) + } + if !bytes.Equal(actualRecipientHash, recipientPubKeyHash) { + t.Errorf("Recipeint public key hash not matching.\nExpected: %v\nActual: %v\n", recipientPubKeyHash, actualRecipientHash) + } + if actualValue != expectedValue { + t.Errorf("Values do not match\nExpected: %v\nActual: %v\n", expectedValue, actualValue) + } + if actualNonce != expectedStateNonce { + t.Errorf("State nonces do not match\nExpected: %v\nActual: %v\n", expectedStateNonce, actualNonce) + } + } -func TestContract_Serialize(t *testing.T) { - senderPrivateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) +func TestSignedContractSerialize(t *testing.T) { recipientPrivateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - encodedPublicKey, _ := publickey.Encode(&senderPrivateKey.PublicKey) - nullSenderContract, _ := New(1, nil, hashing.New(encodedPublicKey), 1000, 0) - encodedRecipientKey, _ := publickey.Encode(&recipientPrivateKey.PublicKey) - unsignedContract, _ := New(1, senderPrivateKey, hashing.New(encodedRecipientKey), 1000, 0) - signedContract, _ := New(1, senderPrivateKey, hashing.New(encodedRecipientKey), 1000, 0) - signedContract.Sign(senderPrivateKey) + senderPrivateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + encodedRecipientPublicKey, _ := publickey.Encode(&recipientPrivateKey.PublicKey) + encodedSenderPublicKey, _ := publickey.Encode(&senderPrivateKey.PublicKey) + recipientPubKeyHash := hashing.New(encodedRecipientPublicKey) - tests := []struct { - name string - c *Contract - }{ - { - name: "Minting contract", - c: nullSenderContract, - }, - { - name: "Unsigned contract", - c: unsignedContract, - }, - { - name: "Signed contract", - c: signedContract, - }, + expectedVersion := uint16(3) + expectedValue := uint64(12004) + expectedStateNonce := uint64(873) + contract, _ := New(expectedVersion, senderPrivateKey, recipientPubKeyHash, expectedValue, expectedStateNonce) + contract.Sign(senderPrivateKey) + serializedContract, _ := contract.Serialize() + + actualVersion := binary.LittleEndian.Uint16(serializedContract[0:2]) + + firstByte := serializedContract[2] + pubKeyLength := publickey.Info[firstByte].Length + actualPubKey := serializedContract[2 : 2+pubKeyLength] + actualSigLen := uint(serializedContract[2+pubKeyLength]) + actualRecipientHash := serializedContract[3+pubKeyLength+actualSigLen : 35+pubKeyLength+actualSigLen] + actualValue := binary.LittleEndian.Uint64(serializedContract[35+pubKeyLength+actualSigLen : 43+pubKeyLength+actualSigLen]) + actualNonce := binary.LittleEndian.Uint64(serializedContract[43+pubKeyLength+actualSigLen : 51+pubKeyLength+actualSigLen]) + + if actualVersion != expectedVersion { + t.Errorf("Versions do not match. Expected: %d, actual: %d\n", expectedVersion, actualVersion) } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, _ := tt.c.Serialize() - sigLen := got[180] - testSendPublicKey := tt.c.SenderPubKey - testEncodeSenderPubKey, _ := publickey.Encode(testSendPublicKey) - switch tt.name { - case "Minting contract": - if !bytes.Equal(got[2:180], make([]byte, 178)) { - t.Errorf("Non null sender public key for minting contract") - } - if sigLen != 0 { - t.Errorf("Non-zero signature length in minting contract: %v", sigLen) - } - if !bytes.Equal(got[181:213], tt.c.RecipPubKeyHash) { - t.Errorf("Invalid recipient public key hash in minting contract") - } - break - case "Unsigned contract": - if sigLen != 0 { - t.Errorf("Non-zero signature length in unsigned contract: %v", sigLen) - } - if !bytes.Equal(got[2:180], testEncodeSenderPubKey) { - t.Errorf("Invalid encoded public key for unsigned contract") - } - if !bytes.Equal(got[181:213], tt.c.RecipPubKeyHash) { - t.Errorf("Invalid recipient public key hash in unsigned contract") - } - case "Signed Contract": - if sigLen == 0 { - t.Errorf("Zero length signature in signed contract: %v", sigLen) - } - if !bytes.Equal(got[2:180], testEncodeSenderPubKey) { - t.Errorf("Invalid encoded public key for signed contract") - } - if !bytes.Equal(got[(181+int(sigLen)):(181+int(sigLen)+32)], tt.c.RecipPubKeyHash) { - t.Errorf("Invalid recipient public key hash in signed contract") - } - } - }) + if serializedContract[2] == byte(0x0) { + t.Errorf("Encoded sender public key is 0\n") + } + if !bytes.Equal(actualPubKey, encodedSenderPublicKey) { + t.Errorf("Sender public key not properly encoded\nexpected: %v\nactual %v\n", encodedSenderPublicKey, actualPubKey) + } + if actualSigLen == 0 { + t.Errorf("Signature length is 0") } + if !bytes.Equal(actualRecipientHash, recipientPubKeyHash) { + t.Errorf("Recipeint public key hash not matching.\nExpected: %v\nActual: %v\n", recipientPubKeyHash, actualRecipientHash) + } + if actualValue != expectedValue { + t.Errorf("Values do not match\nExpected: %v\nActual: %v\n", expectedValue, actualValue) + } + if actualNonce != expectedStateNonce { + t.Errorf("State nonces do not match\nExpected: %v\nActual: %v\n", expectedStateNonce, actualNonce) + } + } func TestContract_Deserialize(t *testing.T) {