From 95fa6280c5e3d21b7b4fc00e3a741ce90ed845fe Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Thu, 13 Jun 2019 18:33:22 -0500 Subject: [PATCH] Fix mappings util - Remove hex prefix so that the function can accept keys with or without a hex prefix and return the same result --- libraries/shared/storage/mappings.go | 2 +- libraries/shared/storage/mappings_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/libraries/shared/storage/mappings.go b/libraries/shared/storage/mappings.go index dcb2e3ea..18878683 100644 --- a/libraries/shared/storage/mappings.go +++ b/libraries/shared/storage/mappings.go @@ -47,7 +47,7 @@ const ( ) func GetMapping(indexOnContract, key string) common.Hash { - keyBytes := common.FromHex("0x" + key + indexOnContract) + keyBytes := common.FromHex(key + indexOnContract) encoded := crypto.Keccak256(keyBytes) return common.BytesToHash(encoded) } diff --git a/libraries/shared/storage/mappings_test.go b/libraries/shared/storage/mappings_test.go index f696add6..7114a39b 100644 --- a/libraries/shared/storage/mappings_test.go +++ b/libraries/shared/storage/mappings_test.go @@ -21,6 +21,16 @@ var _ = Describe("Mappings", func() { expectedStorageKey := common.HexToHash("0xee0c1b59a3856bafbfb8730e7694c4badc271eb5f01ce4a8d7a53d8a6499676f") Expect(storageKey).To(Equal(expectedStorageKey)) }) + + It("returns same result if value includes hex prefix", func() { + indexOfMappingOnContract := storage.IndexZero + keyForDesiredValueInMapping := "0x1234567890abcdef" + + storageKey := storage.GetMapping(indexOfMappingOnContract, keyForDesiredValueInMapping) + + expectedStorageKey := common.HexToHash("0xee0c1b59a3856bafbfb8730e7694c4badc271eb5f01ce4a8d7a53d8a6499676f") + Expect(storageKey).To(Equal(expectedStorageKey)) + }) }) Describe("GetNestedMapping", func() {