Skip to content
Draft
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
37 changes: 37 additions & 0 deletions contracts/commitReveal/CommitReveal.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

/// Basic interface to fulfil the commit reveal technique
pub contract interface CommitReveal {

/// Public resource responsible to reveal the partial secret commitment.
/// It can be used by anyone to reveal the secret commitment, given that the caller
/// posses the `salt` that was used to create secret commitment.
pub resource interface Reveal {

/// It can be used when reveal is based on a certain on-chain event
/// ex - Only reveal after a certain block or once all participants are committed.
pub fun isRevealationAllowed(identifer: AnyStruct): Bool


/// Function used to reveal the partial secret commitment.
/// @param publicCommitment It is the partial data that get committed to the chain. Used to know
/// which commitment needs to get revealed to the public.
/// @param remainingPartialCommitment Remaining part of the commitment that was secret during the commitment phase.
/// @param salt Private key or any random string used to generate the `secretCommitment`.
pub fun reveal(publicCommitment: AnyStruct, remainingPartialCommitment: AnyStruct, salt: String) {
pre {
self.isRevealationAllowed(identifer: publicCommitment) == true, "Reveal phase is not kicked in yet."
}
}
}

/// Resource responsible to commit the partial commitment on-chain.
/// User will own the resource to secretly commit the on-chain data in hash form
/// and also provide the public data that links to its secret commitment.
pub resource interface Commit: Reveal {
/// Function used to commit anything on-chain.
/// @param publicCommitment It is the partial data that get committed to the chain.
/// @param secretCommitment It is the other partial data secretly committed to the chain.
pub fun commit(publicCommitment: AnyStruct, secretCommitment: String)
}

}
71 changes: 71 additions & 0 deletions contracts/commitReveal/ExampleCommitReveal.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import CommitReveal from "./CommitReveal.cdc"

pub contract ExampleCommitReveal {

pub let ExampleCommitRevealStoragePath: StoragePath
pub let ExampleRevealPublicPath: PublicPath

pub let revealationCooldownPeriod: UInt64

event Commited(publicCommitment: String, secretCommitment: String)

event Revealed(publicCommitment: String, revealedData: String)

pub resource ExampleReveal: CommitReveal.Reveal {}


pub resource ExampleCommit: CommitReveal.Commit, ExampleReveal {

pub var commitments : {UInt64: string}

pub var commitmentTimings: {UInt64: UInt64}

init(){}

pub fun commit(publicCommitment: AnyStruct, secretCommitment: String) {
pre {
secretCommitment != "", "Secret commitment is empty string"
}
let publicValue = (publicCommitment as? UInt64) ?? panic("Incorrect type value provided to publicCommitment")
self.commitments.insert(key: publicValue, value: secretCommitment)
self.commitmentTimings.insert(key: publicValue, value: getCurrentBlock().height)
emit Commited(publicCommitment: publicValue, secretCommitment: secretCommitment)
}

pub fun isRevealationAllowed(identifer: AnyStruct) : Bool {
if let validIdentifier = identifer as? UInt64 {
if let commitmentAt = self.commitmentTimings[validIdentifier] {
return getCurrentBlock().height < commitmentAt + ExampleCommitReveal.revealationCooldownPeriod
}
}
return false
}

pub fun reveal(publicCommitment: AnyStruct, remainingPartialCommitment: AnyStruct, salt: String) {
pre {
self.isRevealationAllowed(publicCommitment) == true, "Reveal phase is not kicked in yet."
}
let publicCommitmentIdentifer = (publicCommitment as? UInt64) ?? panic("Unable to downcast the publicCommitment value")
let secretCommitment = self.commitments[publicCommitmentIdentifer] ?? panic("Commitment does not exists")
// Verify the commitment value.
// TODO: Need to create a variable to set the type of secret commitment.
let validRemainingPartialCommitment = (remainingPartialCommitment as? String) ?? panic("Unable to downcast the remainingPartialCommitment to its valid type")
let data = HashAlgorithm.SHA3_256.hash(validRemainingPartialCommitment.concat(salt).decodeHex())
assert(secretCommitment == HashAlgorithm.SHA3_256.hash(data).encodeHex(), message: "Incorrect values of salt or remainingPartialCommitment provided")

// If we want to delete the commitments then we can otherwise we can keep it.
emit Revealed(publicCommitment: publicCommitmentIdentifer, revealedData: validRemainingPartialCommitment)
}
}

pub fun createExampleCommitResource() : @ExampleCommit {
return <- create ExampleCommit()
}

init() {
self.ExampleCommitRevealStoragePath = /storage/ExampleCommitRevealPath
self.ExampleRevealPublicPath = /public/ExampleRevealPublicPath
self.revealationCooldownPeriod = 10
}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.