Hash win/loss record mapping on NFT.id & NFT.getType()
Closed this issue · 2 comments
Issue To Be Solved
In the current implementation of RockPaperScissorsGame, win/loss records are indexed on NFT.id. However, since the contract has been generalized to accept any NFT, this indexing scheme can lead to collisions between NFTs that assign ids arbitrarily (e.g. on total supply). This removes the uniqueness guarantees implied by win/loss records of a given NFT
Suggest A Solution
As mentioned in #33, a possible solution is to index on a combination of the NFT's id & Type. Hashing the two values would give us a single unique value on which to index. This means that the current RockPaperScissorsGame.winLossRecords
would have a Type of {String: GamingMetadataViews.BasicWinLoss}
where the String
index is the hashed value of the NFT's id & Type.
The encoding scheme would look something like:
pub fun encodeNFTIndex(nftID: UInt64, nftType: Type): String {
let nftIDToBytes = nftID.toBigEndianBytes()
let nftTypeToBytes = nftType.identifier.utf8
let hashArray = nftIDToBytes.concat(nftTypeToBytes)
return String.encodeHex(HashAlgorithm.SHA3_256.hash(hashArray))
}
Inserting & looking up the NFT's win/loss record would require the id & Type of the NFT in question, and the two arguments would be used to derive the index with the above encoding method.
@sisyphusSmiling can we close this one?
Disregarding this approach in favor of indexing win/loss on UUID